Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
|
| 2 |
import streamlit as st
|
| 3 |
from PIL import Image
|
| 4 |
from transformers import BlipProcessor, BlipForConditionalGeneration, pipeline
|
|
@@ -6,6 +5,7 @@ from gtts import gTTS
|
|
| 6 |
import os
|
| 7 |
import tempfile
|
| 8 |
|
|
|
|
| 9 |
@st.cache_resource
|
| 10 |
def load_models():
|
| 11 |
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
|
@@ -15,6 +15,7 @@ def load_models():
|
|
| 15 |
|
| 16 |
processor, blip_model, gpt2 = load_models()
|
| 17 |
|
|
|
|
| 18 |
st.title("🖼️📖 Storyteller for Kids")
|
| 19 |
st.write("Upload an image and let the app create and read a magical story just for kids!")
|
| 20 |
|
|
@@ -32,14 +33,31 @@ if uploaded_file:
|
|
| 32 |
st.write(f"**Caption:** {caption}")
|
| 33 |
|
| 34 |
with st.spinner("Writing a children's story..."):
|
| 35 |
-
prompt = f"Write a story for children aged 3-10 about this: {caption}"
|
| 36 |
-
story_output = gpt2(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
story = story_output.strip().replace('\n', ' ')
|
|
|
|
|
|
|
| 38 |
st.success("Story created!")
|
| 39 |
st.write(f"**Story:**\n\n{story}")
|
| 40 |
|
| 41 |
with st.spinner("Converting story to audio..."):
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from PIL import Image
|
| 3 |
from transformers import BlipProcessor, BlipForConditionalGeneration, pipeline
|
|
|
|
| 5 |
import os
|
| 6 |
import tempfile
|
| 7 |
|
| 8 |
+
# Load models
|
| 9 |
@st.cache_resource
|
| 10 |
def load_models():
|
| 11 |
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
|
|
|
| 15 |
|
| 16 |
processor, blip_model, gpt2 = load_models()
|
| 17 |
|
| 18 |
+
# UI
|
| 19 |
st.title("🖼️📖 Storyteller for Kids")
|
| 20 |
st.write("Upload an image and let the app create and read a magical story just for kids!")
|
| 21 |
|
|
|
|
| 33 |
st.write(f"**Caption:** {caption}")
|
| 34 |
|
| 35 |
with st.spinner("Writing a children's story..."):
|
| 36 |
+
prompt = f"Write a short, imaginative story for children aged 3-10 about this: {caption}"
|
| 37 |
+
story_output = gpt2(
|
| 38 |
+
prompt,
|
| 39 |
+
max_length=100,
|
| 40 |
+
num_return_sequences=1,
|
| 41 |
+
do_sample=True,
|
| 42 |
+
temperature=0.9,
|
| 43 |
+
top_p=0.95,
|
| 44 |
+
top_k=50,
|
| 45 |
+
repetition_penalty=1.2,
|
| 46 |
+
pad_token_id=50256,
|
| 47 |
+
eos_token_id=50256,
|
| 48 |
+
)[0]["generated_text"]
|
| 49 |
story = story_output.strip().replace('\n', ' ')
|
| 50 |
+
# Truncate to ~100 words for safety
|
| 51 |
+
story = " ".join(story.split()[:100])
|
| 52 |
st.success("Story created!")
|
| 53 |
st.write(f"**Story:**\n\n{story}")
|
| 54 |
|
| 55 |
with st.spinner("Converting story to audio..."):
|
| 56 |
+
try:
|
| 57 |
+
tts = gTTS(text=story, lang='en')
|
| 58 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as fp:
|
| 59 |
+
tts.save(fp.name)
|
| 60 |
+
st.audio(fp.name, format="audio/mp3")
|
| 61 |
+
st.success("Audio playback ready!")
|
| 62 |
+
except Exception as e:
|
| 63 |
+
st.error(f"Text-to-speech failed: {e}")
|