Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import textwrap
|
| 4 |
+
import numpy as np
|
| 5 |
+
import soundfile as sf
|
| 6 |
+
import tempfile
|
| 7 |
+
import os
|
| 8 |
+
|
| 9 |
+
# Initialize pipelines
|
| 10 |
+
@st.cache_resource
|
| 11 |
+
def load_pipelines():
|
| 12 |
+
captioner = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
|
| 13 |
+
storyer = pipeline("text-generation", model="aspis/gpt2-genre-story-generation")
|
| 14 |
+
tts = pipeline("text-to-speech", model="facebook/mms-tts-eng")
|
| 15 |
+
return captioner, storyer, tts
|
| 16 |
+
|
| 17 |
+
captioner, storyer, tts = load_pipelines()
|
| 18 |
+
|
| 19 |
+
# Main logic
|
| 20 |
+
def generate_content(image):
|
| 21 |
+
# Generate caption
|
| 22 |
+
caption = captioner(image)[0]["generated_text"]
|
| 23 |
+
st.write("**Caption:**", caption)
|
| 24 |
+
|
| 25 |
+
# Generate story
|
| 26 |
+
prompt = (
|
| 27 |
+
f"Write a funny, warm children's story for ages 3-10, 50–100 words, "
|
| 28 |
+
f"in third-person narrative, that describes this scene exactly: {caption} "
|
| 29 |
+
f"mention the exact place or venue within {caption}"
|
| 30 |
+
)
|
| 31 |
+
raw = storyer(
|
| 32 |
+
prompt,
|
| 33 |
+
max_new_tokens=150,
|
| 34 |
+
temperature=0.7,
|
| 35 |
+
top_p=0.9,
|
| 36 |
+
no_repeat_ngram_size=2,
|
| 37 |
+
return_full_text=False
|
| 38 |
+
)[0]["generated_text"].strip()
|
| 39 |
+
|
| 40 |
+
# Trim to max 100 words
|
| 41 |
+
words = raw.split()
|
| 42 |
+
story = " ".join(words[:100])
|
| 43 |
+
st.write("**Story:**", story)
|
| 44 |
+
|
| 45 |
+
# Convert story to speech
|
| 46 |
+
chunks = textwrap.wrap(story, width=200)
|
| 47 |
+
audio = np.concatenate([tts(chunk)["audio"].squeeze() for chunk in chunks])
|
| 48 |
+
|
| 49 |
+
# Save audio to temporary file
|
| 50 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_file:
|
| 51 |
+
sf.write(temp_file.name, audio, tts.model.config.sampling_rate)
|
| 52 |
+
temp_file_path = temp_file.name
|
| 53 |
+
|
| 54 |
+
return caption, story, temp_file_path
|
| 55 |
+
|
| 56 |
+
# Streamlit UI
|
| 57 |
+
st.title("Image to Children's Story and Audio")
|
| 58 |
+
st.write("Upload an image to generate a caption, a children's story, and an audio narration.")
|
| 59 |
+
|
| 60 |
+
uploaded_image = st.file_uploader("Choose an image", type=["jpg", "jpeg", "png"])
|
| 61 |
+
|
| 62 |
+
if uploaded_image is not None:
|
| 63 |
+
st.image(uploaded_image, caption="Uploaded Image", use_column_width=True)
|
| 64 |
+
if st.button("Generate Story and Audio"):
|
| 65 |
+
with st.spinner("Generating content..."):
|
| 66 |
+
caption, story, audio_path = generate_content(uploaded_image)
|
| 67 |
+
st.audio(audio_path, format="audio/wav")
|
| 68 |
+
# Clean up temporary file
|
| 69 |
+
os.remove(audio_path)
|