File size: 2,349 Bytes
29e42d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from transformers import pipeline
import textwrap
import numpy as np
import soundfile as sf
import tempfile
import os

# Initialize pipelines
@st.cache_resource
def load_pipelines():
    captioner = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
    storyer = pipeline("text-generation", model="aspis/gpt2-genre-story-generation")
    tts = pipeline("text-to-speech", model="facebook/mms-tts-eng")
    return captioner, storyer, tts

captioner, storyer, tts = load_pipelines()

# Main logic
def generate_content(image):
    # Generate caption
    caption = captioner(image)[0]["generated_text"]
    st.write("**Caption:**", caption)

    # Generate story
    prompt = (
        f"Write a funny, warm children's story for ages 3-10, 50–100 words, "
        f"in third-person narrative, that describes this scene exactly: {caption} "
        f"mention the exact place or venue within {caption}"
    )
    raw = storyer(
        prompt,
        max_new_tokens=150,
        temperature=0.7,
        top_p=0.9,
        no_repeat_ngram_size=2,
        return_full_text=False
    )[0]["generated_text"].strip()

    # Trim to max 100 words
    words = raw.split()
    story = " ".join(words[:100])
    st.write("**Story:**", story)

    # Convert story to speech
    chunks = textwrap.wrap(story, width=200)
    audio = np.concatenate([tts(chunk)["audio"].squeeze() for chunk in chunks])

    # Save audio to temporary file
    with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_file:
        sf.write(temp_file.name, audio, tts.model.config.sampling_rate)
        temp_file_path = temp_file.name

    return caption, story, temp_file_path

# Streamlit UI
st.title("Image to Children's Story and Audio")
st.write("Upload an image to generate a caption, a children's story, and an audio narration.")

uploaded_image = st.file_uploader("Choose an image", type=["jpg", "jpeg", "png"])

if uploaded_image is not None:
    st.image(uploaded_image, caption="Uploaded Image", use_column_width=True)
    if st.button("Generate Story and Audio"):
        with st.spinner("Generating content..."):
            caption, story, audio_path = generate_content(uploaded_image)
            st.audio(audio_path, format="audio/wav")
            # Clean up temporary file
            os.remove(audio_path)