IrisDeng commited on
Commit
a4a0cba
·
verified ·
1 Parent(s): 0898a51

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -0
app.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ 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
+
10
+ def extract_image_caption(image_data):
11
+ """
12
+ 利用预训练模型从图像中提取描述性文字。
13
+ """
14
+ img_obj = Image.open(image_data)
15
+ caption_pipeline = pipeline(
16
+ "image-to-text",
17
+ model="Salesforce/blip-image-captioning-base",
18
+ )
19
+ caption_results = caption_pipeline(img_obj)
20
+ caption_text = caption_results[0]['generated_text']
21
+ return caption_text
22
+
23
+
24
+ def compose_story_from_caption(caption_detail):
25
+ """
26
+ 根据图像描述创作一篇充满创意的儿童故事。
27
+ """
28
+ story_pipeline = pipeline(
29
+ "text-generation",
30
+ model="Qwen/Qwen2-1.5B",
31
+ )
32
+ prompt_text = (
33
+ "You are a creative children's story writer. Based on the following image details, "
34
+ "please write an imaginative story for children aged 3-10. Do not simply rephrase the image details; "
35
+ "instead, expand creatively by adding fun characters, adventures, and unexpected twists. "
36
+ "The story must be at least 100 words long.\n\n"
37
+ f"Image Details: {caption_detail}\n\nStory:"
38
+ )
39
+ story_results = story_pipeline(prompt_text, max_length=300, num_return_sequences=1)
40
+ story_text = story_results[0]['generated_text']
41
+ return story_text
42
+
43
+
44
+ def convert_text_to_audio(text_content, audio_path="output.mp3"):
45
+ """
46
+ 将文本转换为音频文件。
47
+ """
48
+ tts_engine = gTTS(text=text_content, lang="en")
49
+ tts_engine.save(audio_path)
50
+ return audio_path
51
+
52
+
53
+ def run_app():
54
+ st.markdown("<h1 style='text-align: center;'>Your Image to Audio Story 🦜</h1>", unsafe_allow_html=True)
55
+ st.write("Upload an image below and we will generate an engaging story from the picture, then convert the story into an audio playback!")
56
+
57
+ uploaded_image = st.file_uploader("Select an Image", type=["png", "jpg", "jpeg"])
58
+
59
+ if uploaded_image is not None:
60
+ image_display = Image.open(uploaded_image)
61
+ st.image(image_display, caption="Uploaded Image", use_container_width=True)
62
+
63
+ with st.spinner("Generating caption for the image..."):
64
+ caption_text = extract_image_caption(uploaded_image)
65
+ st.write("**Generated Caption:**", caption_text)
66
+
67
+ with st.spinner("Composing story..."):
68
+ story_text = compose_story_from_caption(caption_text)
69
+ st.write("**Story:**")
70
+ st.write(story_text)
71
+
72
+ with st.spinner("Converting text to audio..."):
73
+ audio_file = convert_text_to_audio(story_text)
74
+ st.audio(audio_file, format="audio/mp3")
75
+
76
+
77
+ if __name__ == "__main__":
78
+ run_app()