Update app.py
Browse files
app.py
CHANGED
|
@@ -33,6 +33,7 @@ def text2audio(story_text):
|
|
| 33 |
tts.save(audio_file.name)
|
| 34 |
return audio_file.name
|
| 35 |
|
|
|
|
| 36 |
def main():
|
| 37 |
st.set_page_config(page_title="Image to Story")
|
| 38 |
st.header("Upload Your Image")
|
|
@@ -40,29 +41,44 @@ def main():
|
|
| 40 |
uploaded_file = st.file_uploader("Choose image", type=["jpg", "png", "jpeg"])
|
| 41 |
|
| 42 |
if uploaded_file:
|
| 43 |
-
# 注意:从这里开始统一缩进(4个空格)
|
| 44 |
temp_img = os.path.join(tempfile.gettempdir(), uploaded_file.name)
|
| 45 |
with open(temp_img, "wb") as f:
|
| 46 |
f.write(uploaded_file.getvalue())
|
| 47 |
|
| 48 |
st.image(uploaded_file)
|
| 49 |
|
| 50 |
-
#
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
| 52 |
st.write("Image Caption:", scenario)
|
| 53 |
|
| 54 |
-
#
|
| 55 |
-
story =
|
|
|
|
|
|
|
|
|
|
| 56 |
st.subheader("Story")
|
| 57 |
st.write(story)
|
| 58 |
|
| 59 |
-
#
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
| 61 |
st.audio(audio_path)
|
| 62 |
|
| 63 |
-
# 清理文件
|
| 64 |
os.unlink(temp_img)
|
| 65 |
os.unlink(audio_path)
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
if __name__ == "__main__":
|
| 68 |
main()
|
|
|
|
| 33 |
tts.save(audio_file.name)
|
| 34 |
return audio_file.name
|
| 35 |
|
| 36 |
+
|
| 37 |
def main():
|
| 38 |
st.set_page_config(page_title="Image to Story")
|
| 39 |
st.header("Upload Your Image")
|
|
|
|
| 41 |
uploaded_file = st.file_uploader("Choose image", type=["jpg", "png", "jpeg"])
|
| 42 |
|
| 43 |
if uploaded_file:
|
|
|
|
| 44 |
temp_img = os.path.join(tempfile.gettempdir(), uploaded_file.name)
|
| 45 |
with open(temp_img, "wb") as f:
|
| 46 |
f.write(uploaded_file.getvalue())
|
| 47 |
|
| 48 |
st.image(uploaded_file)
|
| 49 |
|
| 50 |
+
# 图片转文字处理状态
|
| 51 |
+
with st.status("🖼️ Processing img2text...", expanded=True) as status:
|
| 52 |
+
scenario = img2text(temp_img)
|
| 53 |
+
status.update(label="✅ Image analysis completed!", state="complete")
|
| 54 |
+
|
| 55 |
st.write("Image Caption:", scenario)
|
| 56 |
|
| 57 |
+
# 故事生成处理状态
|
| 58 |
+
with st.status("📝 Generating a story...", expanded=True) as status:
|
| 59 |
+
story = text2story(scenario)
|
| 60 |
+
status.update(label="📖 Story generation completed!", state="complete")
|
| 61 |
+
|
| 62 |
st.subheader("Story")
|
| 63 |
st.write(story)
|
| 64 |
|
| 65 |
+
# 音频生成处理状态
|
| 66 |
+
with st.status("🔊 Converting to audio...", expanded=True) as status:
|
| 67 |
+
audio_path = text2audio(story)
|
| 68 |
+
status.update(label="🎧 Audio conversion completed!", state="complete")
|
| 69 |
+
|
| 70 |
st.audio(audio_path)
|
| 71 |
|
|
|
|
| 72 |
os.unlink(temp_img)
|
| 73 |
os.unlink(audio_path)
|
| 74 |
|
| 75 |
+
|
| 76 |
+
if st.button("🎵 Play Audio Story"):
|
| 77 |
+
st.audio(
|
| 78 |
+
audio_data["audio"],
|
| 79 |
+
format="audio/mp3",
|
| 80 |
+
start_time=0,
|
| 81 |
+
sample_rate=audio_data["sampling_rate"]
|
| 82 |
+
)
|
| 83 |
if __name__ == "__main__":
|
| 84 |
main()
|