File size: 3,705 Bytes
1fa4382
 
5225da1
a0338c0
36726bd
6a488e2
1fa4382
3ae2d5d
a0338c0
 
 
 
 
 
59e88fb
1fa4382
a0338c0
546773b
6a488e2
0d189e5
f05e929
0d189e5
 
6a488e2
a0338c0
7c3c59f
a0338c0
1fa4382
6a488e2
a0338c0
 
 
 
1fa4382
de2e7c7
a0338c0
 
 
0f38e84
 
 
 
3ae2d5d
0f38e84
 
 
 
3ae2d5d
0f38e84
3ae2d5d
0f38e84
3ae2d5d
 
 
 
 
 
 
0f38e84
 
3ae2d5d
0f38e84
6adb3a5
 
3ae2d5d
a0338c0
6adb3a5
 
a0338c0
5225da1
a0338c0
 
 
 
 
 
 
3ae2d5d
a0338c0
6a488e2
a0338c0
 
 
 
3ae2d5d
6a488e2
a0338c0
 
 
 
 
3ae2d5d
6a488e2
a0338c0
 
 
 
 
 
 
 
1fa4382
5225da1
 
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# import part
import streamlit as st
from transformers import pipeline
from gtts import gTTS
import io
import re


# 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"]
    return text


# text2story
def text2story(text):
    pipe = pipeline("text-generation", model="pranavpsv/genre-story-generator-v2", max_new_tokens=160, min_new_tokens=130, num_return_sequences=1)
    story_text = pipe(text)[0]['generated_text']
    last_punctuation = max(story_text.rfind("."), story_text.rfind("!"), story_text.rfind("?"))
    if last_punctuation != -1:
        story_text = story_text[:last_punctuation+1]
    return story_text


# text2audio
def text2audio(story_text):
    tts = gTTS(text = story_text, lang='en')
    audio_bytes = io.BytesIO()
    tts.write_to_fp(audio_bytes)
    audio_bytes.seek(0)
    return audio_bytes


def main():
    # Optimize title area to attract children's attention
    st.set_page_config(page_title="Magic Storyteller", page_icon="๐Ÿงš")
    st.markdown("""
    <style>
    @import url('https://fonts.googleapis.com/css2?family=Comic+Neue:wght@700&display=swap');
    .header {
        background: linear-gradient(45deg, #FF9A6C, #FF6B6B);
        border-radius: 15px;
        padding: 2rem;
        text-align: center;
        box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        margin-bottom: 2rem;
    }
    .subtitle {
        font-family: 'Comic Neue', cursive;
        color: #4B4B4B;
        font-size: 1.2rem;
        margin: 1rem 0;
        padding: 1rem;
        background: rgba(255,255,255,0.9);
        border-radius: 10px;
        border-left: 5px solid #FF6B6B;
    }
    </style>
    """, unsafe_allow_html=True)

    st.markdown("""
    <div class="header">
        <h1 style='margin:0;'>๐Ÿช„ Magic Storyteller</h1>
        <p style='color: white; font-size: 1.2rem;'>Turn your pictures into stories!</p>
    </div>
    """, unsafe_allow_html=True)
    uploaded_file = st.file_uploader("๐Ÿ‘‰๐Ÿป Upload your magic picture here...", type=["jpg", "png"])

    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 Magic Picture โœจ", use_container_width=True)
        status_container = st.empty()
        progress_bar = st.progress(0)

        # Stage 1: Image to Text
        with status_container.status("๐Ÿ”ฎ **Step 1/3**: Decoding picture magic...", expanded=True) as status:  # Add progress bar components to improve experience
            progress_bar.progress(33)
            scenario = img2text(uploaded_file.name)
            status.update(label="โœ… Picture decoded!", state="complete")
            st.write(f"**What I see:** {scenario}")

        #Stage 2: Text to Story
        with status_container.status("๐Ÿ“š **Step 2/3**: Writing your fairy tale...", expanded=True) as status:
            progress_bar.progress(66)
            story = text2story(scenario)
            status.update(label="โœ… Story created!", state="complete")
            st.write(f"**Your Story:**\n{story}")

        #Stage 3: Story to Audio data
        with status_container.status("๐ŸŽต **Step 3/3**: Adding magic audio...", expanded=True) as status:
            progress_bar.progress(100)
            audio_data = text2audio(story)
            status.update(label="โœ… Start playing the story!", state="complete")
        
            st.audio(audio_data, 
                    format="audio/mp3",
                    autoplay=True)

if __name__ == "__main__":
    main()