Update app.py
Browse files
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
|
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 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
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 |
"""
|