Eason918 commited on
Commit
e83bae4
·
verified ·
1 Parent(s): 6d4a6a3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +2 -15
app.py CHANGED
@@ -21,18 +21,12 @@ def text2story(text):
21
 
22
  # 故事 → 语音(TTS)
23
  def text2audio_gtts(story_text, filename="story.mp3"):
24
- # 避免文件冲突
25
  if os.path.exists(filename):
26
  os.remove(filename)
27
 
28
- # 限制 TTS 文本长度
29
- max_chars = 500 # gTTS 可能不支持过长文本
30
- story_text = story_text[:max_chars]
31
-
32
- # 生成语音
33
  tts = gTTS(text=story_text, lang="en")
34
  tts.save(filename)
35
-
36
  return filename
37
 
38
  # Streamlit Web UI
@@ -42,33 +36,26 @@ st.header("📖 AI Storyteller: Turn Your Image into a Story with Audio")
42
  uploaded_file = st.file_uploader("Upload an Image...", type=["jpg", "png"])
43
 
44
  if uploaded_file:
45
- # 保存图片到本地
46
  image_path = "uploaded_image.jpg"
47
  with open(image_path, "wb") as f:
48
  f.write(uploaded_file.getbuffer())
49
 
50
- # 读取并显示图片
51
  image = Image.open(image_path)
52
  st.image(image, caption="Uploaded Image", use_column_width=True)
53
 
54
- # 生成图片描述
55
  st.text("🔍 Generating image caption...")
56
- caption = img2text(image_path) # 这里改成文件路径
57
  st.write("**Image Description:**", caption)
58
 
59
- # 生成故事
60
  st.text("📝 Generating story...")
61
  story = text2story(caption)
62
  st.write("**Generated Story:**")
63
  st.write(story)
64
 
65
- # 生成语音
66
  st.text("🔊 Generating audio...")
67
  audio_file = text2audio_gtts(story)
68
 
69
- # 播放音频
70
  st.audio(audio_file, format="audio/mp3")
71
 
72
- # 下载按钮
73
  with open(audio_file, "rb") as file:
74
  st.download_button("📥 Download Audio", file, file_name="story.mp3")
 
21
 
22
  # 故事 → 语音(TTS)
23
  def text2audio_gtts(story_text, filename="story.mp3"):
 
24
  if os.path.exists(filename):
25
  os.remove(filename)
26
 
27
+ story_text = story_text[:500] # 限制 TTS 文本长度
 
 
 
 
28
  tts = gTTS(text=story_text, lang="en")
29
  tts.save(filename)
 
30
  return filename
31
 
32
  # Streamlit Web UI
 
36
  uploaded_file = st.file_uploader("Upload an Image...", type=["jpg", "png"])
37
 
38
  if uploaded_file:
 
39
  image_path = "uploaded_image.jpg"
40
  with open(image_path, "wb") as f:
41
  f.write(uploaded_file.getbuffer())
42
 
 
43
  image = Image.open(image_path)
44
  st.image(image, caption="Uploaded Image", use_column_width=True)
45
 
 
46
  st.text("🔍 Generating image caption...")
47
+ caption = img2text(image_path)
48
  st.write("**Image Description:**", caption)
49
 
 
50
  st.text("📝 Generating story...")
51
  story = text2story(caption)
52
  st.write("**Generated Story:**")
53
  st.write(story)
54
 
 
55
  st.text("🔊 Generating audio...")
56
  audio_file = text2audio_gtts(story)
57
 
 
58
  st.audio(audio_file, format="audio/mp3")
59
 
 
60
  with open(audio_file, "rb") as file:
61
  st.download_button("📥 Download Audio", file, file_name="story.mp3")