File size: 2,192 Bytes
45be966
 
 
 
f28c5f0
45be966
1d39cce
 
 
 
5db9b0c
45be966
f28c5f0
45be966
1d39cce
 
 
 
5db9b0c
45be966
f28c5f0
45be966
1d39cce
 
 
 
5db9b0c
45be966
f28c5f0
45be966
f28c5f0
 
45be966
f28c5f0
 
5db9b0c
f28c5f0
 
5db9b0c
45be966
cdda5a8
45be966
f28c5f0
5db9b0c
cdda5a8
 
f28c5f0
cdda5a8
 
f28c5f0
cdda5a8
f28c5f0
cdda5a8
 
f28c5f0
cdda5a8
f28c5f0
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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'])