Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,42 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
from transformers import pipeline
|
| 3 |
from PIL import Image
|
| 4 |
-
import
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
@st.cache_resource
|
| 8 |
-
def load_image_caption_model():
|
| 9 |
-
return pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
|
| 10 |
-
|
| 11 |
-
def generate_caption(image):
|
| 12 |
-
caption_model = load_image_caption_model()
|
| 13 |
-
result = caption_model(image)
|
| 14 |
-
return result[0]['generated_text']
|
| 15 |
-
|
| 16 |
-
# Stage 2: Text to Story (Children-friendly)
|
| 17 |
-
@st.cache_resource
|
| 18 |
-
def load_story_generator():
|
| 19 |
-
return pipeline("text2text-generation", model="google/flan-t5-base", max_length=100)
|
| 20 |
-
|
| 21 |
-
def text2story(description):
|
| 22 |
-
story_gen = load_story_generator()
|
| 23 |
-
prompt = f"Generate a short and imaginative children's story about: {description}"
|
| 24 |
-
story = story_gen(prompt)[0]['generated_text']
|
| 25 |
-
return story
|
| 26 |
-
|
| 27 |
-
# Stage 3: Story to Speech (Lightweight & Compatible)
|
| 28 |
-
@st.cache_resource
|
| 29 |
-
def load_tts():
|
| 30 |
-
return pipeline("text-to-speech", model="suno/bark-small")
|
| 31 |
-
|
| 32 |
-
def story_to_audio(story_text):
|
| 33 |
-
tts = load_tts()
|
| 34 |
-
output = tts(story_text)
|
| 35 |
-
audio = output["audio"]
|
| 36 |
-
sample_rate = output["sampling_rate"]
|
| 37 |
-
return audio, sample_rate
|
| 38 |
-
|
| 39 |
-
# Streamlit App UI
|
| 40 |
-
def main():
|
| 41 |
-
st.set_page_config(page_title="Kids Story Creator", layout="centered")
|
| 42 |
-
st.title("🧒 Kids Story Creator 📖")
|
| 43 |
-
|
| 44 |
-
uploaded_image = st.file_uploader("Upload an image (jpg/jpeg/png):", type=["jpg", "jpeg", "png"])
|
| 45 |
-
|
| 46 |
-
if uploaded_image is not None:
|
| 47 |
-
image = Image.open(uploaded_image).convert("RGB")
|
| 48 |
-
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 49 |
-
|
| 50 |
-
with st.spinner("Generating description..."):
|
| 51 |
-
caption = generate_caption(image)
|
| 52 |
-
st.success(f"Caption: {caption}")
|
| 53 |
-
|
| 54 |
-
with st.spinner("Generating story from caption..."):
|
| 55 |
-
story = text2story(caption)
|
| 56 |
-
st.success("Here's your story:")
|
| 57 |
-
st.write(story)
|
| 58 |
-
|
| 59 |
-
with st.spinner("Converting story to audio..."):
|
| 60 |
-
audio, sample_rate = story_to_audio(story)
|
| 61 |
-
st.audio(audio, format="audio/wav", sample_rate=sample_rate)
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
from PIL import Image
|
| 3 |
+
import tempfile
|
| 4 |
+
import pyttsx3
|
| 5 |
+
from transformers import pipeline
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
st.set_page_config(page_title="Image Storytelling App", layout="centered")
|
| 8 |
+
|
| 9 |
+
# Title
|
| 10 |
+
st.title("🧒 Children's Image Storytelling App")
|
| 11 |
+
|
| 12 |
+
# Stage 1: Image Upload & Caption
|
| 13 |
+
st.header("Step 1: Upload an Image")
|
| 14 |
+
uploaded_file = st.file_uploader("Upload an illustration", type=["jpg", "jpeg", "png"])
|
| 15 |
+
|
| 16 |
+
if uploaded_file:
|
| 17 |
+
image = Image.open(uploaded_file)
|
| 18 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 19 |
+
|
| 20 |
+
# Generate image description (simulated for this example)
|
| 21 |
+
st.subheader("Image Description")
|
| 22 |
+
description = "Children playing in the park" # You can use BLIP or similar models here
|
| 23 |
+
st.success(f"Caption: {description}")
|
| 24 |
+
|
| 25 |
+
# Stage 2: Description to Story
|
| 26 |
+
st.header("Step 2: Generate a Short Story")
|
| 27 |
+
story_generator = pipeline("text-generation", model="tiiuae/falcon-7b-instruct", max_new_tokens=150)
|
| 28 |
+
|
| 29 |
+
prompt = (
|
| 30 |
+
f"Write a creative short story (50-100 words) for children (ages 3–10) "
|
| 31 |
+
f"based on this image description: '{description}'. Make it imaginative, safe, and fun."
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
if st.button("Generate Story"):
|
| 35 |
+
with st.spinner("Generating story..."):
|
| 36 |
+
try:
|
| 37 |
+
result = story_generator(prompt)
|
| 38 |
+
story = result[0]['generated_text']
|
| 39 |
+
st.subheader("Generated Story")
|
| 40 |
+
st.write(story)
|
| 41 |
+
except Exception as e:
|
| 42 |
+
st.error(f"Story generation
|