IrisDeng commited on
Commit
16f5ed7
·
verified ·
1 Parent(s): f1d429a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -9
app.py CHANGED
@@ -2,21 +2,32 @@ import streamlit as st
2
  from PIL import Image
3
  from transformers import pipeline
4
  from gtts import gTTS
5
- import torch
6
 
7
  st.set_page_config(page_title="Image to Audio Story", page_icon="🦜")
8
 
9
- # Load models once
10
- caption_pipeline = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
11
- story_pipeline = pipeline("text-generation", model="Qwen/Qwen2-1.5B")
12
-
13
 
14
  def extract_image_caption(image_data):
 
 
 
15
  img_obj = Image.open(image_data)
 
 
 
 
16
  caption_results = caption_pipeline(img_obj)
17
- return caption_results[0]['generated_text']
 
 
18
 
19
  def compose_story_from_caption(caption_detail):
 
 
 
 
 
 
 
20
  prompt_text = (
21
  "You are a talented and imaginative storyteller for children aged 3 to 10. "
22
  "Using the details derived from the image below, craft a captivating tale that goes beyond merely describing the scene. "
@@ -26,19 +37,30 @@ def compose_story_from_caption(caption_detail):
26
  )
27
  story_results = story_pipeline(prompt_text, num_return_sequences=1)
28
  story_text = story_results[0]['generated_text']
29
- return story_text.split("Story:", 1)[1].strip() if "Story:" in story_text else story_text.strip()
 
 
 
 
 
 
 
30
 
31
  def convert_text_to_audio(text_content, audio_path="output.mp3"):
 
 
 
32
  tts_engine = gTTS(text=text_content, lang="en")
33
  tts_engine.save(audio_path)
34
  return audio_path
35
 
 
36
  def run_app():
37
  st.markdown("<h1 style='text-align: center;'>Your Image to Audio Story 🦜</h1>", unsafe_allow_html=True)
38
  st.write("Upload an image below and we will generate an engaging story from the picture, then convert the story into an audio playback!")
39
-
40
  uploaded_image = st.file_uploader("Select an Image", type=["png", "jpg", "jpeg"])
41
-
42
  if uploaded_image is not None:
43
  image_display = Image.open(uploaded_image)
44
  st.image(image_display, caption="Uploaded Image", use_container_width=True)
@@ -56,5 +78,6 @@ def run_app():
56
  audio_file = convert_text_to_audio(story_text)
57
  st.audio(audio_file, format="audio/mp3")
58
 
 
59
  if __name__ == "__main__":
60
  run_app()
 
2
  from PIL import Image
3
  from transformers import pipeline
4
  from gtts import gTTS
 
5
 
6
  st.set_page_config(page_title="Image to Audio Story", page_icon="🦜")
7
 
 
 
 
 
8
 
9
  def extract_image_caption(image_data):
10
+ """
11
+ 利用预训练模型从图像中提取描述性文字。
12
+ """
13
  img_obj = Image.open(image_data)
14
+ caption_pipeline = pipeline(
15
+ "image-to-text",
16
+ model="Salesforce/blip-image-captioning-base",
17
+ )
18
  caption_results = caption_pipeline(img_obj)
19
+ caption_text = caption_results[0]['generated_text']
20
+ return caption_text
21
+
22
 
23
  def compose_story_from_caption(caption_detail):
24
+ """
25
+ 根据图像描述创作一篇充满创意的儿童故事。
26
+ """
27
+ story_pipeline = pipeline(
28
+ "text-generation",
29
+ model="Qwen/Qwen2-1.5B",
30
+ )
31
  prompt_text = (
32
  "You are a talented and imaginative storyteller for children aged 3 to 10. "
33
  "Using the details derived from the image below, craft a captivating tale that goes beyond merely describing the scene. "
 
37
  )
38
  story_results = story_pipeline(prompt_text, num_return_sequences=1)
39
  story_text = story_results[0]['generated_text']
40
+
41
+ if "Story:" in story_text:
42
+ story = story_text.split("Story:", 1)[1].strip()
43
+ else:
44
+ story = story_text.strip()
45
+
46
+ return story
47
+
48
 
49
  def convert_text_to_audio(text_content, audio_path="output.mp3"):
50
+ """
51
+ 将文本转换为音频文件。
52
+ """
53
  tts_engine = gTTS(text=text_content, lang="en")
54
  tts_engine.save(audio_path)
55
  return audio_path
56
 
57
+
58
  def run_app():
59
  st.markdown("<h1 style='text-align: center;'>Your Image to Audio Story 🦜</h1>", unsafe_allow_html=True)
60
  st.write("Upload an image below and we will generate an engaging story from the picture, then convert the story into an audio playback!")
61
+
62
  uploaded_image = st.file_uploader("Select an Image", type=["png", "jpg", "jpeg"])
63
+
64
  if uploaded_image is not None:
65
  image_display = Image.open(uploaded_image)
66
  st.image(image_display, caption="Uploaded Image", use_container_width=True)
 
78
  audio_file = convert_text_to_audio(story_text)
79
  st.audio(audio_file, format="audio/mp3")
80
 
81
+
82
  if __name__ == "__main__":
83
  run_app()