Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +45 -0
- requirements.txt +5 -0
app.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from transformers import BlipProcessor, BlipForConditionalGeneration, pipeline
|
| 5 |
+
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")
|
| 12 |
+
blip_model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 13 |
+
gpt2_pipeline = pipeline("text-generation", model="gpt2")
|
| 14 |
+
return processor, blip_model, gpt2_pipeline
|
| 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 |
+
|
| 21 |
+
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
| 22 |
+
|
| 23 |
+
if uploaded_file:
|
| 24 |
+
image = Image.open(uploaded_file).convert("RGB")
|
| 25 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 26 |
+
|
| 27 |
+
with st.spinner("Generating image caption..."):
|
| 28 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 29 |
+
out = blip_model.generate(**inputs)
|
| 30 |
+
caption = processor.decode(out[0], skip_special_tokens=True)
|
| 31 |
+
st.success("Caption generated!")
|
| 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(prompt, max_length=120, do_sample=True)[0]["generated_text"]
|
| 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 |
+
tts = gTTS(text=story, lang='en')
|
| 43 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as fp:
|
| 44 |
+
tts.save(fp.name)
|
| 45 |
+
st.audio(fp.name, format="audio/mp3")
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
transformers
|
| 3 |
+
torch
|
| 4 |
+
Pillow
|
| 5 |
+
gtts
|