Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import streamlit as st
|
|
| 2 |
from PIL import Image
|
| 3 |
from transformers import BlipProcessor, BlipForConditionalGeneration, pipeline
|
| 4 |
from gtts import gTTS
|
|
|
|
| 5 |
import tempfile
|
| 6 |
|
| 7 |
# Load models
|
|
@@ -14,8 +15,7 @@ def load_models():
|
|
| 14 |
|
| 15 |
processor, blip_model, gpt2 = load_models()
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
st.set_page_config(page_title="Kids Storyteller", page_icon="๐", layout="centered")
|
| 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 |
|
|
@@ -25,7 +25,6 @@ if uploaded_file:
|
|
| 25 |
image = Image.open(uploaded_file).convert("RGB")
|
| 26 |
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 27 |
|
| 28 |
-
# Step 1: Generate Caption
|
| 29 |
with st.spinner("Generating image caption..."):
|
| 30 |
inputs = processor(images=image, return_tensors="pt")
|
| 31 |
out = blip_model.generate(**inputs)
|
|
@@ -33,10 +32,10 @@ if uploaded_file:
|
|
| 33 |
st.success("Caption generated!")
|
| 34 |
st.write(f"**Caption:** {caption}")
|
| 35 |
|
| 36 |
-
# Step 2: Generate Story (remove prompt from output)
|
| 37 |
with st.spinner("Writing a children's story..."):
|
| 38 |
prompt = f"Write a short, imaginative story for children aged 3-10 about this: {caption}"
|
| 39 |
story_output = gpt2(
|
|
|
|
| 40 |
max_length=100,
|
| 41 |
num_return_sequences=1,
|
| 42 |
do_sample=True,
|
|
@@ -47,15 +46,12 @@ if uploaded_file:
|
|
| 47 |
pad_token_id=50256,
|
| 48 |
eos_token_id=50256,
|
| 49 |
)[0]["generated_text"]
|
| 50 |
-
|
| 51 |
-
#
|
| 52 |
-
story =
|
| 53 |
-
story = " ".join(story.split()[:100]) # Trim to ~100 words
|
| 54 |
-
|
| 55 |
st.success("Story created!")
|
| 56 |
st.write(f"**Story:**\n\n{story}")
|
| 57 |
|
| 58 |
-
# Step 3: Convert to Audio
|
| 59 |
with st.spinner("Converting story to audio..."):
|
| 60 |
try:
|
| 61 |
tts = gTTS(text=story, lang='en')
|
|
|
|
| 2 |
from PIL import Image
|
| 3 |
from transformers import BlipProcessor, BlipForConditionalGeneration, pipeline
|
| 4 |
from gtts import gTTS
|
| 5 |
+
import os
|
| 6 |
import tempfile
|
| 7 |
|
| 8 |
# Load models
|
|
|
|
| 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 |
|
|
|
|
| 25 |
image = Image.open(uploaded_file).convert("RGB")
|
| 26 |
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 27 |
|
|
|
|
| 28 |
with st.spinner("Generating image caption..."):
|
| 29 |
inputs = processor(images=image, return_tensors="pt")
|
| 30 |
out = blip_model.generate(**inputs)
|
|
|
|
| 32 |
st.success("Caption generated!")
|
| 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,
|
|
|
|
| 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')
|