Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,8 @@
|
|
1 |
# Import necessary libraries
|
2 |
-
import streamlit as st
|
3 |
-
from transformers import pipeline
|
4 |
-
from gtts import gTTS
|
5 |
-
import os
|
6 |
|
7 |
# Function to convert image to text using Hugging Face's BLIP model
|
8 |
def img2text(url):
|
@@ -13,17 +13,24 @@ def img2text(url):
|
|
13 |
url (str): Path to the image file.
|
14 |
|
15 |
Returns:
|
16 |
-
str: Generated text caption from the image, without words like "illustration".
|
17 |
"""
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
# Function to generate a kid-friendly superhero story from the text caption
|
29 |
def text2story(text):
|
@@ -36,21 +43,28 @@ def text2story(text):
|
|
36 |
Returns:
|
37 |
str: Generated superhero story suitable for kids aged 3-10, within 100 words.
|
38 |
"""
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
# Function to convert text to audio using gTTS
|
56 |
def text2audio(story_text):
|
@@ -63,63 +77,78 @@ def text2audio(story_text):
|
|
63 |
Returns:
|
64 |
str: Path to the generated audio file.
|
65 |
"""
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
|
73 |
-
#
|
74 |
-
st.
|
75 |
-
|
76 |
-
|
|
|
|
|
|
|
|
|
77 |
|
78 |
-
#
|
79 |
-
st.
|
80 |
-
**How to use this app:**
|
81 |
-
1. **Upload a picture** of something fun, like your favorite toy, a park, or your pet.
|
82 |
-
2. Wait for the app to **create a superhero story** from your picture.
|
83 |
-
3. **Listen to the story** by clicking the "Play Audio" button.
|
84 |
-
4. Enjoy your fun superhero story! π§
|
85 |
-
""")
|
86 |
|
87 |
-
|
88 |
-
|
|
|
|
|
|
|
89 |
|
90 |
-
|
91 |
-
|
92 |
-
bytes_data = uploaded_file.getvalue()
|
93 |
-
with open(uploaded_file.name, "wb") as file:
|
94 |
-
file.write(bytes_data)
|
95 |
|
96 |
-
|
97 |
-
|
|
|
|
|
|
|
98 |
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
|
|
103 |
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
st.session_state.audio_file = text2audio(story)
|
115 |
-
|
116 |
-
# Play button for the generated audio
|
117 |
-
if st.button("π΅ **Play Audio**"):
|
118 |
-
if os.path.exists(st.session_state.audio_file):
|
119 |
-
st.audio(st.session_state.audio_file, format="audio/mp3")
|
120 |
-
else:
|
121 |
-
st.error("Audio file not found. Please try again.")
|
122 |
|
123 |
-
|
124 |
-
|
125 |
-
|
|
|
1 |
# Import necessary libraries
|
2 |
+
import streamlit as st # For building the web application
|
3 |
+
from transformers import pipeline # For using pre-trained models (image-to-text and text-generation)
|
4 |
+
from gtts import gTTS # For converting text to speech
|
5 |
+
import os # For file handling (saving and deleting temporary files)
|
6 |
|
7 |
# Function to convert image to text using Hugging Face's BLIP model
|
8 |
def img2text(url):
|
|
|
13 |
url (str): Path to the image file.
|
14 |
|
15 |
Returns:
|
16 |
+
str: Generated text caption from the image, without unwanted words like "illustration".
|
17 |
"""
|
18 |
+
try:
|
19 |
+
# Load the image-to-text model
|
20 |
+
image_to_text_model = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
|
21 |
+
|
22 |
+
# Generate text caption from the image
|
23 |
+
text = image_to_text_model(url)[0]["generated_text"]
|
24 |
+
|
25 |
+
# Remove unwanted words like "illustration" to make the caption cleaner
|
26 |
+
unwanted_words = ["illustration", "drawing", "sketch", "picture", "dream", "imagination"]
|
27 |
+
for word in unwanted_words:
|
28 |
+
text = text.replace(word, "")
|
29 |
+
|
30 |
+
return text.strip() # Return the cleaned caption
|
31 |
+
except Exception as e:
|
32 |
+
st.error(f"Error processing image: {e}") # Display error message if something goes wrong
|
33 |
+
return None
|
34 |
|
35 |
# Function to generate a kid-friendly superhero story from the text caption
|
36 |
def text2story(text):
|
|
|
43 |
Returns:
|
44 |
str: Generated superhero story suitable for kids aged 3-10, within 100 words.
|
45 |
"""
|
46 |
+
try:
|
47 |
+
# Load the text-generation model
|
48 |
+
story_generator = pipeline("text-generation", model="pranavpsv/gpt2-genre-story-generator")
|
49 |
+
|
50 |
+
# Generate the story with the superhero genre
|
51 |
+
prompt = f"<BOS> <superhero> {text}" # Add genre tags to the prompt
|
52 |
+
story = story_generator(prompt, max_length=100, num_return_sequences=1)[0]['generated_text']
|
53 |
+
|
54 |
+
# Remove <BOS> and <superhero> tags from the generated story
|
55 |
+
story = story.replace("<BOS>", "").replace("<superhero>", "").strip()
|
56 |
+
|
57 |
+
# Remove the input text (scenario) from the generated story to avoid redundancy
|
58 |
+
if text in story:
|
59 |
+
story = story.replace(text, "").strip()
|
60 |
+
|
61 |
+
# Ensure the story is within 100 words by truncating if necessary
|
62 |
+
story = " ".join(story.split()[:100])
|
63 |
+
|
64 |
+
return story
|
65 |
+
except Exception as e:
|
66 |
+
st.error(f"Error generating story: {e}") # Display error message if something goes wrong
|
67 |
+
return None
|
68 |
|
69 |
# Function to convert text to audio using gTTS
|
70 |
def text2audio(story_text):
|
|
|
77 |
Returns:
|
78 |
str: Path to the generated audio file.
|
79 |
"""
|
80 |
+
try:
|
81 |
+
# Convert text to speech using gTTS
|
82 |
+
tts = gTTS(text=story_text, lang='en')
|
83 |
+
audio_file = "story_audio.mp3" # Define the output audio file name
|
84 |
+
tts.save(audio_file) # Save the audio file
|
85 |
+
return audio_file
|
86 |
+
except Exception as e:
|
87 |
+
st.error(f"Error generating audio: {e}") # Display error message if something goes wrong
|
88 |
+
return None
|
89 |
+
|
90 |
+
# Main application function
|
91 |
+
def main():
|
92 |
+
"""
|
93 |
+
Main function to run the Streamlit application.
|
94 |
+
"""
|
95 |
+
# Configure the Streamlit app page
|
96 |
+
st.set_page_config(page_title="Picture Stories π¨π", page_icon="π¦")
|
97 |
+
st.title("Picture Stories π¨π")
|
98 |
+
st.markdown("### Turn your pictures into fun superhero stories and listen to them! π")
|
99 |
|
100 |
+
# Instructions for kids
|
101 |
+
st.markdown("""
|
102 |
+
**How to use this app:**
|
103 |
+
1. **Upload a picture** of something fun, like your favorite toy, a park, or your pet.
|
104 |
+
2. Wait for the app to **create a superhero story** from your picture.
|
105 |
+
3. **Listen to the story** by clicking the "Play Audio" button.
|
106 |
+
4. Enjoy your fun superhero story! π§
|
107 |
+
""")
|
108 |
|
109 |
+
# Upload image
|
110 |
+
uploaded_file = st.file_uploader("π· **Upload your picture here!**", type=["jpg", "jpeg", "png"])
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
|
112 |
+
if uploaded_file is not None:
|
113 |
+
# Save the uploaded file to disk
|
114 |
+
image_bytes = uploaded_file.getvalue()
|
115 |
+
with open(uploaded_file.name, "wb") as file:
|
116 |
+
file.write(image_bytes)
|
117 |
|
118 |
+
# Display the uploaded image in the app
|
119 |
+
st.image(uploaded_file, caption="Your awesome picture!", use_container_width=True)
|
|
|
|
|
|
|
120 |
|
121 |
+
# Stage 1: Image to Text
|
122 |
+
with st.spinner('β¨ Turning your picture into words...'):
|
123 |
+
scenario = img2text(uploaded_file.name) # Generate text caption from the image
|
124 |
+
if scenario:
|
125 |
+
st.write("**What we see:**", scenario) # Display the generated caption
|
126 |
|
127 |
+
# Stage 2: Text to Story
|
128 |
+
with st.spinner('π Creating a fun superhero story for you...'):
|
129 |
+
story = text2story(scenario) # Generate a superhero story from the caption
|
130 |
+
if story:
|
131 |
+
st.write("**Your superhero story:**", story) # Display the generated story
|
132 |
|
133 |
+
# Stage 3: Story to Audio
|
134 |
+
with st.spinner('π§ Turning your story into audio...'):
|
135 |
+
# Generate audio file if it doesn't already exist in the session state
|
136 |
+
if 'audio_file' not in st.session_state:
|
137 |
+
st.session_state.audio_file = text2audio(story)
|
138 |
+
|
139 |
+
# Play button for the generated audio
|
140 |
+
if st.button("π΅ **Play Audio**"):
|
141 |
+
if os.path.exists(st.session_state.audio_file):
|
142 |
+
st.audio(st.session_state.audio_file, format="audio/mp3") # Play the audio
|
143 |
+
else:
|
144 |
+
st.error("Audio file not found. Please try again.") # Display error if audio file is missing
|
145 |
|
146 |
+
# Clean up temporary files (uploaded image and generated audio)
|
147 |
+
if os.path.exists(uploaded_file.name):
|
148 |
+
os.remove(uploaded_file.name) # Delete the uploaded image file
|
149 |
+
if 'audio_file' in st.session_state and os.path.exists(st.session_state.audio_file):
|
150 |
+
os.remove(st.session_state.audio_file) # Delete the generated audio file
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
151 |
|
152 |
+
# Run the application
|
153 |
+
if __name__ == "__main__":
|
154 |
+
main()
|