Spaces:
Build error
Build error
| import streamlit as st | |
| from transformers import pipeline | |
| from PIL import Image | |
| # Creates a brief description of a picture using a smart model | |
| def generate_caption(image): | |
| with st.spinner("🔍 Looking at your picture..."): | |
| # Loads the BLIP model to examine and describe the picture | |
| image_to_text = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base") | |
| caption = image_to_text(image)[0]["generated_text"] | |
| return caption | |
| # Builds a story from the picture’s description | |
| def generate_story(caption): | |
| with st.spinner("✍️ Writing a fun story..."): | |
| # Uses a story model to craft a tale from the description | |
| pipe = pipeline("text-generation", model="pranavpsv/genre-story-generator-v2") | |
| story = pipe(caption)[0]['generated_text'] | |
| return story | |
| # Turns the story into spoken audio | |
| def generate_audio(story): | |
| with st.spinner("🎙️ Turning story into audio..."): | |
| # Uses a speech model to read the story aloud | |
| pipe = pipeline("text-to-speech", model="facebook/mms-tts-eng") | |
| audio = pipe(story) | |
| return audio | |
| # Streamlit UI: Makes a simple interface for kids to enjoy stories | |
| # Displays a fun title | |
| st.title("Picture to Story Fun! 🌈") | |
| # Describes the app for young users | |
| st.write("Hi, kids! Share a picture to get a fun story! Great for ages 3-10.") | |
| # Allows picture uploads | |
| uploaded_file = st.file_uploader("Pick a picture!", type=["png", "jpg", "jpeg"]) | |
| if uploaded_file is not None: | |
| # Shows the uploaded picture | |
| image = Image.open(uploaded_file) | |
| st.image(image, caption="Your Picture!", use_container_width=True) | |
| # Gets the picture’s description | |
| image_caption = generate_caption(image) | |
| st.subheader("Picture Description:") | |
| st.write(image_caption) | |
| # Creates a story | |
| story_telling = generate_story(image_caption) | |
| st.subheader("Your Story:") | |
| st.write(story_telling) | |
| # Generates audio | |
| audio = generate_audio(story_telling) | |
| if st.button("Hear Story!"): | |
| st.audio(audio['audio'], | |
| format="audio/wav", | |
| start_time=0, | |
| sample_rate=audio['sampling_rate']) |