File size: 2,170 Bytes
d55766c
5e5ea3c
d55766c
fb5ea01
a7bd32c
fb5ea01
 
d55766c
fb5ea01
d55766c
fb5ea01
 
 
 
 
a7bd32c
fb5ea01
 
 
d55766c
fb5ea01
 
 
d55766c
fb5ea01
a7bd32c
fb5ea01
d55766c
fb5ea01
 
 
5e5ea3c
fb5ea01
 
 
5e5ea3c
fb5ea01
 
a7bd32c
fb5ea01
 
 
 
5e5ea3c
fb5ea01
 
 
d55766c
fb5ea01
d55766c
a7bd32c
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
import streamlit as st
from transformers import pipeline
from PIL import Image
import requests
import os
from io import BytesIO
import tempfile

# Load Hugging Face pipelines
@st.cache_resource
def load_pipelines():
    image_captioner = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
    story_generator = pipeline("text-generation", model="Tevatron/tiny-stories-generator", max_length=200)
    text_to_speech = pipeline("text-to-speech", model="espnet/kan-bayashi_ljspeech_vits", framework="pt")
    return image_captioner, story_generator, text_to_speech

# Define main interface
def main():
    st.set_page_config(page_title="Kids Story Maker πŸ§’πŸ“–", page_icon="πŸ“Έ", layout="centered")

    # Child-friendly header
    st.markdown("<h1 style='color:#FF69B4; font-family:Comic Sans MS;'>🎨 Welcome to Kids Story Maker!</h1>", unsafe_allow_html=True)
    st.markdown("<h3 style='color:#4CAF50;'>Upload a picture, and we'll turn it into a magical story and voice! 🐻✨</h3>", unsafe_allow_html=True)

    image_captioner, story_generator, text_to_speech = load_pipelines()

    uploaded_file = st.file_uploader("πŸ–ΌοΈ Upload an image:", type=["png", "jpg", "jpeg"])

    if uploaded_file:
        image = Image.open(uploaded_file)
        st.image(image, caption="Your Image", use_column_width=True)

        with st.spinner("πŸ” Generating a description..."):
            caption = image_captioner(image)[0]['generated_text']
            st.success(f"πŸ“ Description: {caption}")

        # Prompt template for storytelling
        story_prompt = f"Write a short story for children aged 3 to 10 based on this description: {caption}. The story should be creative, friendly, and use simple words."

        with st.spinner("✍️ Creating your story..."):
            story = story_generator(story_prompt)[0]['generated_text']
            st.success("πŸ“– Here's your story:")
            st.write(story)

        with st.spinner("πŸ”Š Turning your story into voice..."):
            speech = text_to_speech(story)[0]['audio']
            st.audio(speech, format="audio/wav")

# Run the app
if __name__ == "__main__":
    main()