Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,54 +2,57 @@ 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 model to
|
| 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
|
| 20 |
def generate_audio(story):
|
| 21 |
-
# Uses a speech model to read the story
|
| 22 |
pipe = pipeline("text-to-speech", model="facebook/mms-tts-eng")
|
| 23 |
audio = pipe(story)
|
| 24 |
return audio
|
| 25 |
|
| 26 |
-
# Streamlit UI:
|
| 27 |
|
| 28 |
-
#
|
| 29 |
-
st.title("
|
| 30 |
|
| 31 |
-
#
|
| 32 |
-
st.write("
|
| 33 |
|
| 34 |
-
#
|
| 35 |
-
uploaded_file = st.file_uploader("
|
| 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
|
| 41 |
|
| 42 |
# Gets the picture’s description
|
| 43 |
image_caption = generate_caption(image)
|
| 44 |
-
st.subheader("
|
| 45 |
st.write(image_caption)
|
| 46 |
|
| 47 |
-
#
|
| 48 |
story_telling = generate_story(image_caption)
|
| 49 |
-
st.subheader("Your
|
| 50 |
st.write(story_telling)
|
| 51 |
|
| 52 |
-
#
|
| 53 |
audio = generate_audio(story_telling)
|
| 54 |
-
if st.button("
|
| 55 |
-
st.audio(audio['audio'],
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from transformers import pipeline
|
| 3 |
from PIL import Image
|
| 4 |
|
| 5 |
+
# Creates a brief description of a picture using a smart model
|
| 6 |
def generate_caption(image):
|
| 7 |
+
# Loads the BLIP model to examine and describe the picture
|
| 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 |
+
# Builds a story from the picture’s description
|
| 13 |
def generate_story(caption):
|
| 14 |
+
# Uses a story model to craft a tale from 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 spoken audio
|
| 20 |
def generate_audio(story):
|
| 21 |
+
# Uses a speech model to read the story aloud
|
| 22 |
pipe = pipeline("text-to-speech", model="facebook/mms-tts-eng")
|
| 23 |
audio = pipe(story)
|
| 24 |
return audio
|
| 25 |
|
| 26 |
+
# Streamlit UI: Makes a simple interface for kids to enjoy stories
|
| 27 |
|
| 28 |
+
# Displays a fun title
|
| 29 |
+
st.title("Picture to Story Fun! 🌈")
|
| 30 |
|
| 31 |
+
# Describes the app for young users
|
| 32 |
+
st.write("Hi, kids! Share a picture to get a fun story! Great for ages 3-10.")
|
| 33 |
|
| 34 |
+
# Allows picture uploads
|
| 35 |
+
uploaded_file = st.file_uploader("Pick a 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 Picture!", use_container_width=True)
|
| 41 |
|
| 42 |
# Gets the picture’s description
|
| 43 |
image_caption = generate_caption(image)
|
| 44 |
+
st.subheader("Picture Description:")
|
| 45 |
st.write(image_caption)
|
| 46 |
|
| 47 |
+
# Creates a story
|
| 48 |
story_telling = generate_story(image_caption)
|
| 49 |
+
st.subheader("Your Story:")
|
| 50 |
st.write(story_telling)
|
| 51 |
|
| 52 |
+
# Generates audio
|
| 53 |
audio = generate_audio(story_telling)
|
| 54 |
+
if st.button("Hear Story!"):
|
| 55 |
+
st.audio(audio['audio'],
|
| 56 |
+
format="audio/wav",
|
| 57 |
+
start_time=0,
|
| 58 |
+
sample_rate=audio['sampling_rate'])
|