Update app.py
Browse files
app.py
CHANGED
@@ -1,10 +1,19 @@
|
|
1 |
import streamlit as st
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
# function part
|
10 |
# img2text
|
@@ -14,42 +23,30 @@ def img2text(url):
|
|
14 |
text = image_to_text_model(url)[0]["generated_text"]
|
15 |
return text
|
16 |
except Exception as e:
|
17 |
-
st.error(f"
|
18 |
return None
|
19 |
|
20 |
-
#
|
21 |
def text2story(text):
|
22 |
try:
|
23 |
-
|
24 |
-
|
|
|
25 |
return story
|
26 |
except Exception as e:
|
27 |
-
st.error(f"
|
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"
|
53 |
return None
|
54 |
|
55 |
st.set_page_config(page_title="Your Image to Audio Story",
|
@@ -65,21 +62,21 @@ if uploaded_file is not None:
|
|
65 |
temp_file_path = temp_file.name
|
66 |
|
67 |
st.image(uploaded_file, caption="Uploaded Image",
|
68 |
-
use_container_width=True)
|
69 |
|
70 |
-
#
|
71 |
st.text('Processing img2text...')
|
72 |
scenario = img2text(temp_file_path)
|
73 |
if scenario:
|
74 |
st.write(scenario)
|
75 |
|
76 |
-
#
|
77 |
st.text('Generating a story...')
|
78 |
story = text2story(scenario)
|
79 |
if story:
|
80 |
st.write(story)
|
81 |
|
82 |
-
#
|
83 |
st.text('Generating audio data...')
|
84 |
audio_data = text2audio(story)
|
85 |
if audio_data:
|
@@ -89,8 +86,5 @@ if uploaded_file is not None:
|
|
89 |
format="audio/mpeg",
|
90 |
start_time=0)
|
91 |
|
92 |
-
#
|
93 |
-
|
94 |
-
os.remove(temp_file_path)
|
95 |
-
except Exception as e:
|
96 |
-
st.error(f"删除临时文件出错: {e}")
|
|
|
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 |
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 |
+
|
33 |
+
story_generator = pipeline("text-generation", model="perplexity-ai/r1-1776", trust_remote_code=True)
|
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",
|
|
|
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 |
format="audio/mpeg",
|
87 |
start_time=0)
|
88 |
|
89 |
+
# 删除临时文件
|
90 |
+
os.remove(temp_file_path)
|
|
|
|
|
|