Files changed (1) hide show
  1. app.py +34 -29
app.py CHANGED
@@ -1,19 +1,10 @@
1
  import streamlit as st
2
- import importlib.util
3
- try:
4
- # 检查 accelerate 库是否安装
5
- spec = importlib.util.find_spec("accelerate")
6
- if spec is None:
7
- st.error("缺少 'accelerate' 库,请安装该库以加载 FP8 量化模型。可以使用 'pip install accelerate' 进行安装。")
8
- st.stop()
9
- from transformers import pipeline
10
- from gtts import gTTS
11
- import io
12
- import tempfile
13
- import os
14
- except ImportError as e:
15
- st.error(f"导入库时出错: {e}")
16
- st.stop()
17
 
18
  # function part
19
  # img2text
@@ -23,30 +14,42 @@ def img2text(url):
23
  text = image_to_text_model(url)[0]["generated_text"]
24
  return text
25
  except Exception as e:
26
- st.error(f"图像描述生成出错: {e}")
27
  return None
28
 
29
- # text2story
30
  def text2story(text):
31
  try:
32
- # 使用 gpt2 模型
33
- story_generator = pipeline("text-generation", model="gpt2")
34
- story = story_generator(text, max_length=200, num_return_sequences=1)[0]['generated_text']
35
  return story
36
  except Exception as e:
37
- st.error(f"故事生成出错: {e}")
38
  return None
39
 
40
  # text2audio
41
  def text2audio(story_text):
42
  try:
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  tts = gTTS(text=story_text, lang='en')
44
  audio_file = io.BytesIO()
45
  tts.write_to_fp(audio_file)
46
  audio_file.seek(0)
47
  return audio_file
48
  except Exception as e:
49
- st.error(f"文本转语音出错: {e}")
50
  return None
51
 
52
  st.set_page_config(page_title="Your Image to Audio Story",
@@ -61,22 +64,21 @@ if uploaded_file is not None:
61
  temp_file.write(uploaded_file.getvalue())
62
  temp_file_path = temp_file.name
63
 
64
- st.image(uploaded_file, caption="Uploaded Image",
65
- use_container_width=True) # 修改为 use_container_width
66
 
67
- #Stage 1: Image to Text
68
  st.text('Processing img2text...')
69
  scenario = img2text(temp_file_path)
70
  if scenario:
71
  st.write(scenario)
72
 
73
- #Stage 2: Text to Story
74
  st.text('Generating a story...')
75
  story = text2story(scenario)
76
  if story:
77
  st.write(story)
78
 
79
- #Stage 3: Story to Audio data
80
  st.text('Generating audio data...')
81
  audio_data = text2audio(story)
82
  if audio_data:
@@ -86,5 +88,8 @@ if uploaded_file is not None:
86
  format="audio/mpeg",
87
  start_time=0)
88
 
89
- # 删除临时文件
90
- os.remove(temp_file_path)
 
 
 
 
1
  import streamlit as st
2
+ from transformers import pipeline
3
+ from gtts import gTTS
4
+ import io
5
+ import os
6
+ import langdetect
7
+ import tempfile
 
 
 
 
 
 
 
 
 
8
 
9
  # function part
10
  # img2text
 
14
  text = image_to_text_model(url)[0]["generated_text"]
15
  return text
16
  except Exception as e:
17
+ st.error(f"图像转文本出错: {e}")
18
  return None
19
 
20
+ # text2story
21
  def text2story(text):
22
  try:
23
+ story_generator = pipeline("text-generation", model="Qwen/QwQ-32B")
24
+ story = story_generator(text, max_length=200, num_return_sequences=1)[0]["generated_text"]
 
25
  return story
26
  except Exception as e:
27
+ st.error(f"文本生成故事出错: {e}")
28
  return None
29
 
30
  # text2audio
31
  def text2audio(story_text):
32
  try:
33
+ # 检测故事的语言
34
+ detected_lang = langdetect.detect(story_text)
35
+ # 根据检测到的语言创建 gTTS 对象
36
+ tts = gTTS(text=story_text, lang=detected_lang)
37
+ # 创建一个字节流对象用于存储音频数据
38
+ audio_file = io.BytesIO()
39
+ # 将音频数据写入字节流
40
+ tts.write_to_fp(audio_file)
41
+ # 将文件指针移到开头
42
+ audio_file.seek(0)
43
+ return audio_file
44
+ except langdetect.LangDetectException:
45
+ st.error("无法检测故事的语言,默认使用英语进行语音合成。")
46
  tts = gTTS(text=story_text, lang='en')
47
  audio_file = io.BytesIO()
48
  tts.write_to_fp(audio_file)
49
  audio_file.seek(0)
50
  return audio_file
51
  except Exception as e:
52
+ st.error(f"文本转音频出错: {e}")
53
  return None
54
 
55
  st.set_page_config(page_title="Your Image to Audio Story",
 
64
  temp_file.write(uploaded_file.getvalue())
65
  temp_file_path = temp_file.name
66
 
67
+ st.image(uploaded_file, caption="Uploaded Image", use_container_width=True)
 
68
 
69
+ # Stage 1: Image to Text
70
  st.text('Processing img2text...')
71
  scenario = img2text(temp_file_path)
72
  if scenario:
73
  st.write(scenario)
74
 
75
+ # Stage 2: Text to Story
76
  st.text('Generating a story...')
77
  story = text2story(scenario)
78
  if story:
79
  st.write(story)
80
 
81
+ # Stage 3: Story to Audio data
82
  st.text('Generating audio data...')
83
  audio_data = text2audio(story)
84
  if audio_data:
 
88
  format="audio/mpeg",
89
  start_time=0)
90
 
91
+ # 删除临时文件并进行异常处理
92
+ try:
93
+ os.remove(temp_file_path)
94
+ except Exception as e:
95
+ st.error(f"删除临时文件出错: {e}")