IrisDeng commited on
Commit
2e3e400
·
verified ·
1 Parent(s): 8498664

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -17
app.py CHANGED
@@ -5,7 +5,7 @@ from gtts import gTTS
5
 
6
  st.set_page_config(page_title="Image to Audio Story", page_icon="🦜")
7
  caption_pipeline = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
8
- story_pipeline = pipeline("text-generation", model="Qwen/Qwen2.5-0.5B-Instruct")
9
 
10
 
11
  def extract_image_caption(image_data):
@@ -15,22 +15,27 @@ def extract_image_caption(image_data):
15
 
16
 
17
  def compose_story_from_caption(caption_detail):
18
- prompt_text = (
19
- "You are a talented and imaginative storyteller for children aged 3 to 10. "
20
- "Using the details derived from the image below, craft a captivating tale that includes three main characters, "
21
- "an adventurous journey, and delightful surprises. "
22
- "Your story should be vivid, original, and between 90 and 100 words in length.\n\n"
23
- f"Image Details: {caption_detail}\n\nStory:"
24
- )
25
-
26
- # 生成故事,设置最大长度为150,以确保有足够空间生成90-100词的内容
27
- story_results = story_pipeline(prompt_text, num_return_sequences=1, max_length=150)
28
- story_text = story_results[0]['generated_text']
29
-
30
- # 提取故事文本
31
- if "Story:" in story_text:
32
- return story_text.split("Story:", 1)[1].strip()
33
- return story_text.strip()
 
 
 
 
 
34
 
35
  def convert_text_to_audio(text_content, audio_path="output.mp3"):
36
  """
 
5
 
6
  st.set_page_config(page_title="Image to Audio Story", page_icon="🦜")
7
  caption_pipeline = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
8
+ story_pipeline = pipeline("text-generation", model="Qwen/Qwen2-1.5B")
9
 
10
 
11
  def extract_image_caption(image_data):
 
15
 
16
 
17
  def compose_story_from_caption(caption_detail):
18
+ while True:
19
+ prompt_text = (
20
+ "You are a talented and imaginative storyteller for children aged 3 to 10. "
21
+ "Using the details derived from the image below, craft a complete and captivating tale that includes three main characters, "
22
+ "an adventurous journey, and delightful surprises. "
23
+ "Your story should have a clear beginning, middle, and end, and be between 80 and 100 words in length.\n\n"
24
+ f"Image Details: {caption_detail}\n\nStory:"
25
+ )
26
+
27
+ story_results = story_pipeline(prompt_text, num_return_sequences=1, max_length=150)
28
+ story_text = story_results[0]['generated_text']
29
+
30
+ # 提取故事
31
+ if "Story:" in story_text:
32
+ story = story_text.split("Story:", 1)[1].strip()
33
+ else:
34
+ story = story_text.strip()
35
+
36
+ # 检查长度
37
+ if 80 <= len(story.split()) <= 100:
38
+ return story
39
 
40
  def convert_text_to_audio(text_content, audio_path="output.mp3"):
41
  """