Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -28,16 +28,24 @@ def text2story(text):
|
|
28 |
text (str): Text caption generated from the image.
|
29 |
|
30 |
Returns:
|
31 |
-
str: Generated story suitable for kids aged 3-10.
|
32 |
"""
|
33 |
# Add a prompt to ensure the story is kid-friendly and happy
|
34 |
-
prompt = f"Write a happy and
|
35 |
|
36 |
# Load the text generation model
|
37 |
story_generator = pipeline("text-generation", model="aspis/gpt2-genre-story-generation")
|
38 |
|
39 |
# Generate the story
|
40 |
-
story = story_generator(prompt, max_length=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
return story
|
43 |
|
@@ -60,12 +68,21 @@ def text2audio(story_text):
|
|
60 |
return audio_file
|
61 |
|
62 |
# Main application
|
63 |
-
st.set_page_config(page_title="
|
64 |
-
|
65 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
# Upload image
|
68 |
-
uploaded_file = st.file_uploader("
|
69 |
|
70 |
if uploaded_file is not None:
|
71 |
# Save the uploaded file
|
@@ -74,24 +91,24 @@ if uploaded_file is not None:
|
|
74 |
file.write(bytes_data)
|
75 |
|
76 |
# Display the uploaded image
|
77 |
-
st.image(uploaded_file, caption="
|
78 |
|
79 |
# Stage 1: Image to Text
|
80 |
-
st.text('
|
81 |
scenario = img2text(uploaded_file.name)
|
82 |
-
st.write("**
|
83 |
|
84 |
# Stage 2: Text to Story
|
85 |
-
st.text('
|
86 |
story = text2story(scenario)
|
87 |
-
st.write("**
|
88 |
|
89 |
# Stage 3: Story to Audio
|
90 |
-
st.text('
|
91 |
audio_file = text2audio(story)
|
92 |
|
93 |
# Play button for the generated audio
|
94 |
-
if st.button("Play Audio"):
|
95 |
st.audio(audio_file, format="audio/mp3")
|
96 |
|
97 |
# Clean up the generated audio file
|
|
|
28 |
text (str): Text caption generated from the image.
|
29 |
|
30 |
Returns:
|
31 |
+
str: Generated story suitable for kids aged 3-10, within 95 words.
|
32 |
"""
|
33 |
# Add a prompt to ensure the story is kid-friendly and happy
|
34 |
+
prompt = f"Write a happy, fun and complete story for kids aged 3-10 based on the following scenario: {text}. Keep the story under 120 words."
|
35 |
|
36 |
# Load the text generation model
|
37 |
story_generator = pipeline("text-generation", model="aspis/gpt2-genre-story-generation")
|
38 |
|
39 |
# Generate the story
|
40 |
+
story = story_generator(prompt, max_length=150, num_return_sequences=1)[0]["generated_text"]
|
41 |
+
|
42 |
+
# Remove the prompt from the generated story
|
43 |
+
story = story.replace(prompt, "").strip()
|
44 |
+
|
45 |
+
# Ensure the story is within 95 words
|
46 |
+
story_words = story.split()
|
47 |
+
if len(story_words) > 98:
|
48 |
+
story = " ".join(story_words[:98])
|
49 |
|
50 |
return story
|
51 |
|
|
|
68 |
return audio_file
|
69 |
|
70 |
# Main application
|
71 |
+
st.set_page_config(page_title="Picture Stories π¨π", page_icon="π¦")
|
72 |
+
st.title("Picture Stories π¨π")
|
73 |
+
st.markdown("### Turn your pictures into fun stories and listen to them! π")
|
74 |
+
|
75 |
+
# Instructions for kids
|
76 |
+
st.markdown("""
|
77 |
+
**How to use this app:**
|
78 |
+
1. **Upload a picture** of something fun, like your favorite toy, a park, or your pet.
|
79 |
+
2. Wait for the app to **create a story** from your picture.
|
80 |
+
3. **Listen to the story** by clicking the "Play Audio" button.
|
81 |
+
4. Enjoy your fun story! π§
|
82 |
+
""")
|
83 |
|
84 |
# Upload image
|
85 |
+
uploaded_file = st.file_uploader("π· **Upload your picture here!**", type=["jpg", "jpeg", "png"])
|
86 |
|
87 |
if uploaded_file is not None:
|
88 |
# Save the uploaded file
|
|
|
91 |
file.write(bytes_data)
|
92 |
|
93 |
# Display the uploaded image
|
94 |
+
st.image(uploaded_file, caption="Your awesome picture!", use_container_width=True)
|
95 |
|
96 |
# Stage 1: Image to Text
|
97 |
+
st.text('β¨ Turning your picture into words...')
|
98 |
scenario = img2text(uploaded_file.name)
|
99 |
+
st.write("**What we see:**", scenario)
|
100 |
|
101 |
# Stage 2: Text to Story
|
102 |
+
st.text('π Creating a fun story for you...')
|
103 |
story = text2story(scenario)
|
104 |
+
st.write("**Your story:**", story)
|
105 |
|
106 |
# Stage 3: Story to Audio
|
107 |
+
st.text('π§ Turning your story into audio...')
|
108 |
audio_file = text2audio(story)
|
109 |
|
110 |
# Play button for the generated audio
|
111 |
+
if st.button("π΅ **Play Audio**"):
|
112 |
st.audio(audio_file, format="audio/mp3")
|
113 |
|
114 |
# Clean up the generated audio file
|