File size: 2,410 Bytes
4fddba4
 
 
8de2446
 
4fddba4
 
 
 
3a25fa2
 
 
 
 
4fddba4
 
 
3a25fa2
 
 
 
 
8de2446
 
 
4fddba4
 
 
8de2446
 
 
 
 
4fddba4
 
3a25fa2
 
 
 
4fddba4
 
 
 
 
 
3a25fa2
4e17b21
 
3a25fa2
4fddba4
3a25fa2
 
 
 
 
 
9323a68
3a25fa2
 
8de2446
9323a68
3a25fa2
 
8de2446
 
 
 
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
62
63
64
65
66
67
# import part
import streamlit as st
from transformers import pipeline
from gtts import gTTS  # Using gTTS for text-to-speech
import os

# 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"]
    # Remove the prompt from the generated story
    story = story.replace(prompt, "").strip()  # Clean up the output
    return story[:95]  # Limit to 95 words

# text2audio
def text2audio(story_text):
    # Use gTTS for text-to-speech conversion
    tts = gTTS(text=story_text, lang="en")
    audio_file = "story_audio.mp3"
    tts.save(audio_file)
    return audio_file

# 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_file = text2audio(story)

    # Play button
    if st.button("Play Audio"):
        st.audio(audio_file, format="audio/mp3")

    # Clean up the audio file after use
    os.remove(audio_file)