File size: 2,993 Bytes
9dd5dc1
4fddba4
 
3603f06
8de2446
4fddba4
9dd5dc1
4fddba4
3a25fa2
 
3603f06
 
 
 
 
 
4fddba4
9dd5dc1
4fddba4
3603f06
 
 
 
 
 
9dd5dc1
3603f06
 
 
 
9dd5dc1
4fddba4
3603f06
4fddba4
3603f06
 
 
8de2446
4fddba4
9dd5dc1
 
 
 
3a25fa2
9dd5dc1
4fddba4
 
9dd5dc1
4fddba4
 
 
 
3603f06
4e17b21
 
9dd5dc1
4fddba4
9dd5dc1
3a25fa2
 
9dd5dc1
3a25fa2
9dd5dc1
9323a68
9dd5dc1
 
8de2446
9323a68
9dd5dc1
 
3603f06
8de2446
9dd5dc1
 
 
 
 
 
 
 
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
68
69
70
71
72
73
74
75
76
77
78
79
80
# Import necessary libraries
import streamlit as st
from transformers import pipeline
from gtts import gTTS
import os

# Function to convert image to text
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"]
    
    # Remove unwanted words like "illustration"
    unwanted_words = ["illustration", "painting", "drawing", "artwork"]
    for word in unwanted_words:
        text = text.replace(word, "")
    return text.strip()

# Function to generate a story from text using GPT-2
def text2story(text):
    # Add a strong prompt to guide the model
    prompt = f"Write a short, happy, and fun story for kids aged 3-10 based on the following description: {text}. " \
             "The story should be cheerful, imaginative, and suitable for young children. " \
             "Avoid any scary or sad elements. Keep the story under 95 words and make sure it has a clear beginning, middle, and end."
    
    # Use the GPT-2 model to generate the story
    text_generator = pipeline("text-generation", model="gpt2")
    story = text_generator(prompt, max_length=95, num_return_sequences=1)[0]["generated_text"]
    
    # Clean up the output to remove the prompt (if necessary)
    story = story.replace(prompt, "").strip()
    return story

# Function to convert text to audio using gTTS
def text2audio(story_text):
    audio_file = "story_audio.mp3"
    tts = gTTS(story_text, lang="en")
    tts.save(audio_file)
    return audio_file

# Main application
st.set_page_config(page_title="Image to Story", page_icon="πŸ“–")
st.header("πŸ“– Image to Story")
st.markdown("### Turn your image into a fun story!")

uploaded_file = st.file_uploader("Select an Image...", type=["jpg", "jpeg", "png"])

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

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

    # Stage 1: Image to Text
    st.text('πŸ–ΌοΈ Processing image...')
    scenario = img2text(uploaded_file.name)
    st.write("**What I see:**", scenario)

    # Stage 2: Text to Story
    st.text('πŸ“ Creating a story...')
    story = text2story(scenario)
    st.write("**Your Story:**", story)

    # Stage 3: Story to Audio
    st.text('πŸŽ™οΈ Turning your story into audio...')
    audio_file = text2audio(story)

    # Play button for audio
    if st.button("🎧 Listen to the Story"):
        st.audio(audio_file, format="audio/mp3")

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

# Add some fun prompts for kids
st.markdown("### 🎨 Tips for a Great Story!")
st.write("1. Upload a picture of your favorite animal, place, or toy!")
st.write("2. Imagine what's happening in the picture and let the story begin!")
st.write("3. Listen to your story and share it with your friends!")