Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,6 @@ import streamlit as st
|
|
| 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,7 +14,8 @@ def 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,6 +25,7 @@ if uploaded_file:
|
|
| 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,6 +33,7 @@ if uploaded_file:
|
|
| 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(
|
|
@@ -46,12 +48,15 @@ if uploaded_file:
|
|
| 46 |
pad_token_id=50256,
|
| 47 |
eos_token_id=50256,
|
| 48 |
)[0]["generated_text"]
|
| 49 |
-
|
| 50 |
-
#
|
| 51 |
-
story =
|
|
|
|
|
|
|
| 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')
|
|
|
|
| 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 |
|
| 15 |
processor, blip_model, gpt2 = load_models()
|
| 16 |
|
| 17 |
+
# Streamlit UI
|
| 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 |
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 |
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(
|
|
|
|
| 48 |
pad_token_id=50256,
|
| 49 |
eos_token_id=50256,
|
| 50 |
)[0]["generated_text"]
|
| 51 |
+
|
| 52 |
+
# Strip prompt from generated output
|
| 53 |
+
story = story_output[len(prompt):].strip() if story_output.startswith(prompt) else story_output.strip()
|
| 54 |
+
story = " ".join(story.split()[:100]) # Trim to ~100 words
|
| 55 |
+
|
| 56 |
st.success("Story created!")
|
| 57 |
st.write(f"**Story:**\n\n{story}")
|
| 58 |
|
| 59 |
+
# Step 3: Convert to Audio
|
| 60 |
with st.spinner("Converting story to audio..."):
|
| 61 |
try:
|
| 62 |
tts = gTTS(text=story, lang='en')
|