Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from PIL import Image
|
3 |
+
from io import BytesIO
|
4 |
+
from transformers import pipeline
|
5 |
+
from gtts import gTTS
|
6 |
+
import tempfile
|
7 |
+
|
8 |
+
# ββββββββ Page config and title
|
9 |
+
st.set_page_config(page_title="Storyteller for Kids", layout="centered")
|
10 |
+
st.title("πΌοΈ β‘οΈ π Interactive Storyteller")
|
11 |
+
|
12 |
+
# ββββββββ Load pipelines (cached)
|
13 |
+
@st.experimental_singleton
|
14 |
+
def load_pipelines():
|
15 |
+
# 1. Image captioning
|
16 |
+
captioner = pipeline(
|
17 |
+
"image-captioning",
|
18 |
+
model="Salesforce/blip-image-captioning-base",
|
19 |
+
device=0 if not st.session_state.get("CPU_ONLY", False) else -1
|
20 |
+
)
|
21 |
+
# 2. Story generation (you can swap to a kid-friendly fine-tuned model)
|
22 |
+
storyteller = pipeline(
|
23 |
+
"text-generation",
|
24 |
+
model="gpt2",
|
25 |
+
device=0 if not st.session_state.get("CPU_ONLY", False) else -1
|
26 |
+
)
|
27 |
+
return captioner, storyteller
|
28 |
+
|
29 |
+
captioner, storyteller = load_pipelines()
|
30 |
+
|
31 |
+
# ββββββββ Sidebar: CPU/GPU toggle (optional)
|
32 |
+
st.sidebar.write("### Settings")
|
33 |
+
st.sidebar.checkbox("Force CPU only", key="CPU_ONLY")
|
34 |
+
|
35 |
+
# ββββββββ Main UI: image upload
|
36 |
+
uploaded = st.file_uploader("Upload an image:", type=["jpg","jpeg","png"])
|
37 |
+
if uploaded:
|
38 |
+
image = Image.open(uploaded).convert("RGB")
|
39 |
+
st.image(image, caption="Your picture", use_column_width=True)
|
40 |
+
|
41 |
+
# ββββββββ 1. Caption
|
42 |
+
with st.spinner("π Looking at the image..."):
|
43 |
+
caption = captioner(image)[0]["generated_text"]
|
44 |
+
st.markdown(f"**Caption:** {caption}")
|
45 |
+
|
46 |
+
# ββββββββ 2. Story generation
|
47 |
+
prompt = (
|
48 |
+
f"Use the following description to write a playful story (50β100 words) "
|
49 |
+
f"for 3β10 year-old children:\n\nβ{caption}β\n\nStory:"
|
50 |
+
)
|
51 |
+
with st.spinner("βοΈ Writing a story..."):
|
52 |
+
output = storyteller(
|
53 |
+
prompt,
|
54 |
+
max_length= prompt.count(" ") + 100, # approx ~100 words
|
55 |
+
num_return_sequences=1,
|
56 |
+
do_sample=True,
|
57 |
+
top_p=0.9,
|
58 |
+
temperature=0.8
|
59 |
+
)
|
60 |
+
story = output[0]["generated_text"].split("Story:")[-1].strip()
|
61 |
+
st.markdown("**Story:**")
|
62 |
+
st.write(story)
|
63 |
+
|
64 |
+
# ββββββββ 3. Text-to-Speech
|
65 |
+
with st.spinner("π Converting to speech..."):
|
66 |
+
tts = gTTS(story, lang="en")
|
67 |
+
tmp = tempfile.NamedTemporaryFile(suffix=".mp3", delete=False)
|
68 |
+
tts.write_to_fp(tmp)
|
69 |
+
tmp.flush()
|
70 |
+
st.audio(tmp.name, format="audio/mp3")
|