File size: 2,317 Bytes
4fddba4
 
 
 
 
 
 
3a25fa2
 
 
 
 
4fddba4
 
 
3a25fa2
 
 
 
 
 
 
 
4fddba4
 
 
3a25fa2
 
 
 
4fddba4
 
3a25fa2
 
 
 
4fddba4
 
 
 
 
 
3a25fa2
4e17b21
 
3a25fa2
4fddba4
3a25fa2
 
 
 
 
 
9323a68
3a25fa2
 
 
9323a68
3a25fa2
 
 
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 part
import streamlit as st
from transformers import pipeline

# function part
# img2text
def img2text(url):
    image_to_text_model = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
    text = image_to_text_model(url)[0]["generated_text"]
    # Make the caption more fun and descriptive
    fun_caption = f"Wow! {text.capitalize()}! 🌟"
    return fun_caption

# text2story
def text2story(text):
    # Use a text-generation model to create a fun and realistic story
    story_generator = pipeline("text-generation", model="gpt2")
    # Add a prompt to guide the story generation (avoid magical elements)
    prompt = f"Write a fun and realistic story for kids based on this: {text}. Keep it simple and under 95 words."
    story = story_generator(prompt, max_length=95, num_return_sequences=1)[0]["generated_text"]
    # Ensure the story is simple and fun for kids
    simple_story = story[:95]  # Limit to 95 words
    return simple_story

# text2audio
def text2audio(story_text):
    # Use a TTS model to convert text to audio
    tts = pipeline("text-to-speech", model="facebook/fastspeech2-en-ljspeech")
    audio_data = tts(story_text)
    return audio_data

# main part
st.set_page_config(page_title="Story Explorer", page_icon="🦜")
st.header("Story Explorer: Turn Your Picture into a Fun Story! 🎨📖")

uploaded_file = st.file_uploader("Choose a picture...", type=["jpg", "png", "jpeg"])

if uploaded_file is not None:
    bytes_data = uploaded_file.getvalue()
    with open(uploaded_file.name, "wb") as file:
        file.write(bytes_data)

    st.image(uploaded_file, caption="Your Picture", use_container_width=True)

    # Stage 1: Image to Text
    st.text('Let’s explore your picture! 🧐✨')
    scenario = img2text(uploaded_file.name)
    st.write(f"**Here’s what I see in your picture:** {scenario}")

    # Stage 2: Text to Story
    st.text('Creating a fun story for you! 📖')
    story = text2story(scenario)
    st.write(f"**Here’s your story:** {story}")

    # Stage 3: Story to Audio data
    st.text('Turning your story into audio... 🎧')
    audio_data = text2audio(story)

    # Play button
    if st.button("Play Audio"):
        st.audio(audio_data['audio'], format="audio/wav", start_time=0, sample_rate=audio_data['sampling_rate'])