Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,89 +2,54 @@ import streamlit as st
|
|
| 2 |
from transformers import pipeline
|
| 3 |
from PIL import Image
|
| 4 |
|
| 5 |
-
#
|
| 6 |
def generate_caption(image):
|
| 7 |
-
#
|
| 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 |
-
#
|
| 13 |
def generate_story(caption):
|
| 14 |
-
# Uses a story
|
| 15 |
pipe = pipeline("text-generation", model="pranavpsv/genre-story-generator-v2")
|
| 16 |
story = pipe(caption)[0]['generated_text']
|
| 17 |
return story
|
| 18 |
|
| 19 |
-
#
|
| 20 |
def generate_audio(story):
|
| 21 |
-
#
|
| 22 |
pipe = pipeline("text-to-speech", model="facebook/mms-tts-eng")
|
| 23 |
audio = pipe(story)
|
| 24 |
return audio
|
| 25 |
|
| 26 |
-
# Streamlit UI:
|
| 27 |
|
| 28 |
-
# Shows a
|
| 29 |
-
st.
|
| 30 |
|
| 31 |
-
#
|
| 32 |
-
st.
|
| 33 |
-
"""
|
| 34 |
-
<p style='text-align: center; font-size: 16px;'>
|
| 35 |
-
🦄 Hey there, story explorers! Share a picture, and I’ll weave a super cool story for you!
|
| 36 |
-
This app is made for kids aged 3-10 who love adventures and imagination.
|
| 37 |
-
Ready to dive in? Let’s go! 🚀
|
| 38 |
-
</p>
|
| 39 |
-
""",
|
| 40 |
-
unsafe_allow_html=True
|
| 41 |
-
)
|
| 42 |
|
| 43 |
-
# Lets kids upload a picture
|
| 44 |
-
uploaded_file = st.file_uploader("
|
| 45 |
|
| 46 |
-
# Checks if a picture has been uploaded
|
| 47 |
if uploaded_file is not None:
|
| 48 |
-
#
|
| 49 |
image = Image.open(uploaded_file)
|
| 50 |
-
st.image(image, caption="Your
|
| 51 |
-
|
| 52 |
-
# Creates a description of the picture
|
| 53 |
-
with st.spinner("🕵️♂️ Exploring your picture..."):
|
| 54 |
-
image_caption = generate_caption(image)
|
| 55 |
-
|
| 56 |
-
st.subheader("🌈 What’s Your Picture About?")
|
| 57 |
-
st.write(f"{image_caption}")
|
| 58 |
-
st.markdown("<p style='font-size: 14px;'>This is the start of your adventure! Let’s build a story! 🏰</p>", unsafe_allow_html=True)
|
| 59 |
-
|
| 60 |
-
# Makes a story from the picture’s description
|
| 61 |
-
with st.spinner("✍️ Crafting a fantastic tale..."):
|
| 62 |
-
story_telling = generate_story(image_caption)
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
st.
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
)
|
| 78 |
-
st.markdown("<p style='font-size: 14px;'>Wow, your story sounds amazing! Listen again anytime! 🌟</p>", unsafe_allow_html=True)
|
| 79 |
-
|
| 80 |
-
# Adds a cheerful note to encourage more storytelling
|
| 81 |
-
st.markdown(
|
| 82 |
-
"""
|
| 83 |
-
<hr>
|
| 84 |
-
<p style='text-align: center; font-size: 14px;'>
|
| 85 |
-
📸 Ready for another tale? Upload a new picture for a fresh adventure!
|
| 86 |
-
Created with sparkles for awesome kids! 💖
|
| 87 |
-
</p>
|
| 88 |
-
""",
|
| 89 |
-
unsafe_allow_html=True
|
| 90 |
-
)
|
|
|
|
| 2 |
from transformers import pipeline
|
| 3 |
from PIL import Image
|
| 4 |
|
| 5 |
+
# Makes a short description of a picture using a clever model
|
| 6 |
def generate_caption(image):
|
| 7 |
+
# Sets up the BLIP model to look at the picture and describe it
|
| 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 |
+
# Creates a fun story from the picture’s description
|
| 13 |
def generate_story(caption):
|
| 14 |
+
# Uses a story model to write a tale based on the description
|
| 15 |
pipe = pipeline("text-generation", model="pranavpsv/genre-story-generator-v2")
|
| 16 |
story = pipe(caption)[0]['generated_text']
|
| 17 |
return story
|
| 18 |
|
| 19 |
+
# Turns the story into a spoken audio clip
|
| 20 |
def generate_audio(story):
|
| 21 |
+
# Uses a speech model to read the story out loud
|
| 22 |
pipe = pipeline("text-to-speech", model="facebook/mms-tts-eng")
|
| 23 |
audio = pipe(story)
|
| 24 |
return audio
|
| 25 |
|
| 26 |
+
# Streamlit UI: Sets up a simple, fun interface for kids
|
| 27 |
|
| 28 |
+
# Shows a bright title to excite young users
|
| 29 |
+
st.title("🌟 Picture Story Magic! 🌟")
|
| 30 |
|
| 31 |
+
# Explains the app in a fun way
|
| 32 |
+
st.write("Hey, kids! Upload a picture and get a cool story! Perfect for ages 3-10. Let’s make magic! 🪄")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
# Lets kids upload a picture
|
| 35 |
+
uploaded_file = st.file_uploader("📸 Pick a fun picture!", type=["png", "jpg", "jpeg"])
|
| 36 |
|
|
|
|
| 37 |
if uploaded_file is not None:
|
| 38 |
+
# Shows the uploaded picture
|
| 39 |
image = Image.open(uploaded_file)
|
| 40 |
+
st.image(image, caption="Your Awesome Picture!", use_container_width=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
+
# Gets the picture’s description
|
| 43 |
+
image_caption = generate_caption(image)
|
| 44 |
+
st.subheader("What’s in Your Picture?")
|
| 45 |
+
st.write(image_caption)
|
| 46 |
+
|
| 47 |
+
# Makes a story from the description
|
| 48 |
+
story_telling = generate_story(image_caption)
|
| 49 |
+
st.subheader("Your Super Story!")
|
| 50 |
+
st.write(story_telling)
|
| 51 |
+
|
| 52 |
+
# Creates audio for the story
|
| 53 |
+
audio = generate_audio(story_telling)
|
| 54 |
+
if st.button("🎵 Listen to Your Story!"):
|
| 55 |
+
st.audio(audio['audio'], format="audio/wav", start_time=0, sample_rate=audio['sampling_rate'])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|