Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,83 +2,94 @@ import streamlit as st
|
|
| 2 |
from transformers import pipeline
|
| 3 |
from PIL import Image
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
def load_image_to_text():
|
| 8 |
-
return pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
|
| 9 |
-
|
| 10 |
-
@st.cache_resource
|
| 11 |
-
def load_story_generator():
|
| 12 |
-
return pipeline("text-generation", model="pranavpsv/genre-story-generator-v2")
|
| 13 |
-
|
| 14 |
-
@st.cache_resource
|
| 15 |
-
def load_tts():
|
| 16 |
-
return pipeline("text-to-speech", model="facebook/mms-tts-eng")
|
| 17 |
-
|
| 18 |
-
# Generation functions with error handling
|
| 19 |
def generate_caption(image):
|
| 20 |
-
image_to_text =
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
return caption
|
| 24 |
-
except Exception as e:
|
| 25 |
-
st.error(f"Oops! Something went wrong while generating the caption: {e}")
|
| 26 |
-
return None
|
| 27 |
|
|
|
|
|
|
|
| 28 |
def generate_story(caption):
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
story = story_generator(prompt, max_length=200, do_sample=True, temperature=0.7)[0]['generated_text']
|
| 33 |
-
return story
|
| 34 |
-
except Exception as e:
|
| 35 |
-
st.error(f"Oops! Something went wrong while generating the story: {e}")
|
| 36 |
-
return None
|
| 37 |
|
|
|
|
|
|
|
| 38 |
def generate_audio(story):
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
return audio
|
| 43 |
-
except Exception as e:
|
| 44 |
-
st.error(f"Oops! Something went wrong while generating the audio: {e}")
|
| 45 |
-
return None
|
| 46 |
|
| 47 |
-
# Streamlit UI
|
| 48 |
-
|
| 49 |
-
st.markdown("<p style='text-align: center;'>Upload a fun picture and I’ll tell you a magical story about it!</p>", unsafe_allow_html=True)
|
| 50 |
|
| 51 |
-
#
|
| 52 |
-
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
if uploaded_file is not None:
|
| 55 |
-
#
|
|
|
|
| 56 |
image = Image.open(uploaded_file)
|
| 57 |
-
st.image(image, caption="Your
|
| 58 |
-
|
| 59 |
-
# Generate and display caption with spinner
|
| 60 |
-
with st.spinner("Creating a caption..."):
|
| 61 |
-
generated_caption = generate_caption(image)
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from transformers import pipeline
|
| 3 |
from PIL import Image
|
| 4 |
|
| 5 |
+
# Function to generate a caption from an uploaded image
|
| 6 |
+
# Uses the Hugging Face pipeline with the Salesforce BLIP model to analyze the image and produce a descriptive caption
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
def generate_caption(image):
|
| 8 |
+
image_to_text = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
|
| 9 |
+
caption = image_to_text(image)[0]["generated_text"]
|
| 10 |
+
return caption
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
# Function to generate a story based on the image caption
|
| 13 |
+
# Uses the pranavpsv/genre-story-generator-v2 model to create a creative story inspired by the caption
|
| 14 |
def generate_story(caption):
|
| 15 |
+
pipe = pipeline("text-generation", model="pranavpsv/genre-story-generator-v2")
|
| 16 |
+
story = pipe(caption, max_length=150, do_sample=True)[0]['generated_text'] # Added max_length for shorter, kid-friendly stories
|
| 17 |
+
return story
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
# Function to convert the story into audio
|
| 20 |
+
# Uses the facebook/mms-tts-eng model to generate a spoken version of the story
|
| 21 |
def generate_audio(story):
|
| 22 |
+
pipe = pipeline("text-to-speech", model="facebook/mms-tts-eng")
|
| 23 |
+
audio = pipe(story)
|
| 24 |
+
return audio
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
# Streamlit UI: Creating a fun and interactive interface for kids
|
| 27 |
+
# The app is designed to be simple, colorful, and engaging for young users
|
|
|
|
| 28 |
|
| 29 |
+
# Display a vibrant, centered title with emojis to grab attention
|
| 30 |
+
st.markdown("<h1 style='text-align: center; color: purple;'>🌟 Picture to Story Magic! 🌟</h1>", unsafe_allow_html=True)
|
| 31 |
|
| 32 |
+
# Add a welcoming description to explain the app’s purpose in kid-friendly language
|
| 33 |
+
st.markdown(
|
| 34 |
+
"""
|
| 35 |
+
<p style='text-align: center; font-size: 16px;'>
|
| 36 |
+
🎉 Hello, young adventurers! Upload a picture, and I’ll create a magical story just for you!
|
| 37 |
+
This app is perfect for kids aged 3-10 who love fun and creative storytelling.
|
| 38 |
+
Let’s make some story magic together! 🧙♂️
|
| 39 |
+
</p>
|
| 40 |
+
""",
|
| 41 |
+
unsafe_allow_html=True
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
# File uploader for images, with a playful prompt
|
| 45 |
+
uploaded_file = st.file_uploader("📸 Pick a cool picture!", type=["png", "jpg", "jpeg"], help="Choose a fun image to start your story adventure!")
|
| 46 |
+
|
| 47 |
+
# Check if an image has been uploaded
|
| 48 |
if uploaded_file is not None:
|
| 49 |
+
# Load and display the uploaded image
|
| 50 |
+
# The image is opened using Pillow and displayed with a caption
|
| 51 |
image = Image.open(uploaded_file)
|
| 52 |
+
st.image(image, caption="Your Awesome Picture! 😊", use_column_width=True) # Changed to use_column_width for better scaling
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
+
# Generate and display the image caption
|
| 55 |
+
# The caption describes what’s in the image, serving as the story’s starting point
|
| 56 |
+
with st.spinner("🔍 Looking at your picture..."):
|
| 57 |
+
image_caption = generate_caption(image)
|
| 58 |
+
|
| 59 |
+
st.subheader("✨ What’s in Your Picture?")
|
| 60 |
+
st.write(f"{image_caption}")
|
| 61 |
+
st.markdown("<p style='font-size: 14px;'>This is what I see in your picture! It’s the start of our story! 📖</p>", unsafe_allow_html=True)
|
| 62 |
|
| 63 |
+
# Generate and display the story
|
| 64 |
+
# The story is created based on the caption, designed to be short and engaging for kids
|
| 65 |
+
with st.spinner("🖌️ Writing a magical story..."):
|
| 66 |
+
story_telling = generate_story(image_caption)
|
| 67 |
+
|
| 68 |
+
st.subheader("📚 Your Magical Story!")
|
| 69 |
+
st.write(f"{story_telling}")
|
| 70 |
+
st.markdown("<p style='font-size: 14px;'>Wow, what a fun story! Want to hear it out loud? Click below! 🎶</p>", unsafe_allow_html=True)
|
| 71 |
|
| 72 |
+
# Generate and play audio for the story
|
| 73 |
+
# Audio is generated only when the user clicks the button, keeping the app responsive
|
| 74 |
+
if st.button("🎙️ Listen to Your Story!"):
|
| 75 |
+
with st.spinner("🎵 Turning your story into sound..."):
|
| 76 |
+
audio = generate_audio(story_telling)
|
| 77 |
+
st.audio(
|
| 78 |
+
audio['audio'],
|
| 79 |
+
format="audio/wav",
|
| 80 |
+
start_time=0,
|
| 81 |
+
sample_rate=audio['sampling_rate']
|
| 82 |
+
)
|
| 83 |
+
st.markdown("<p style='font-size: 14px;'>Amazing! You can listen to your story anytime! 😄</p>", unsafe_allow_html=True)
|
| 84 |
|
| 85 |
+
# Add a footer to encourage users to try again
|
| 86 |
+
st.markdown(
|
| 87 |
+
"""
|
| 88 |
+
<hr>
|
| 89 |
+
<p style='text-align: center; font-size: 14px;'>
|
| 90 |
+
🖼️ Want another adventure? Upload a new picture and create a brand-new story!
|
| 91 |
+
Made with love for curious kids! ❤️
|
| 92 |
+
</p>
|
| 93 |
+
""",
|
| 94 |
+
unsafe_allow_html=True
|
| 95 |
+
)
|