Update app.py
Browse files
app.py
CHANGED
@@ -10,75 +10,75 @@ import tempfile
|
|
10 |
st.set_page_config(page_title="Storyteller for Kids", layout="centered")
|
11 |
st.title("🖼️ ➡️ 📖 Interactive Storyteller")
|
12 |
|
13 |
-
# —––––––– Load & warm
|
14 |
@st.cache_resource
|
15 |
def load_pipelines():
|
16 |
-
# 1) BLIP-base for
|
17 |
captioner = pipeline(
|
18 |
"image-to-text",
|
19 |
model="Salesforce/blip-image-captioning-base",
|
20 |
-
device=0
|
21 |
)
|
22 |
-
# 2) Flan-T5-Large for instruction
|
23 |
storyteller = pipeline(
|
24 |
"text2text-generation",
|
25 |
model="google/flan-t5-large",
|
26 |
device=0
|
27 |
)
|
28 |
|
29 |
-
# Warm
|
30 |
-
dummy = Image.new("RGB", (384, 384), color=(128,128,128))
|
31 |
captioner(dummy)
|
32 |
-
storyteller("Hello", max_new_tokens=1)
|
33 |
|
34 |
return captioner, storyteller
|
35 |
|
36 |
captioner, storyteller = load_pipelines()
|
37 |
|
38 |
# —––––––– Main UI
|
39 |
-
uploaded = st.file_uploader("Upload an image:", type=["jpg","jpeg","png"])
|
40 |
if uploaded:
|
41 |
-
# 1) Preprocess
|
42 |
image = Image.open(uploaded).convert("RGB")
|
43 |
-
image = image.resize((384,384), Image.LANCZOS)
|
44 |
st.image(image, caption="Your image", use_container_width=True)
|
45 |
|
46 |
-
# 2)
|
47 |
with st.spinner("🔍 Generating caption..."):
|
48 |
cap = captioner(image)[0]["generated_text"].strip()
|
49 |
st.markdown(f"**Caption:** {cap}")
|
50 |
|
51 |
-
# 3)
|
52 |
prompt = (
|
53 |
-
f"Here
|
54 |
-
"Write
|
55 |
-
"
|
56 |
-
"
|
57 |
-
"
|
58 |
"Story:"
|
59 |
)
|
60 |
-
with st.spinner("✍️
|
61 |
out = storyteller(
|
62 |
prompt,
|
63 |
-
max_new_tokens=
|
64 |
do_sample=True,
|
65 |
temperature=0.7,
|
66 |
top_p=0.9,
|
67 |
top_k=50,
|
68 |
-
repetition_penalty=1.
|
69 |
-
no_repeat_ngram_size=3
|
|
|
70 |
)
|
71 |
-
|
72 |
-
raw = out[0]["generated_text"]
|
73 |
-
story = raw.split("Story:")[-1].strip()
|
74 |
|
75 |
st.markdown("**Story:**")
|
76 |
st.write(story)
|
77 |
|
78 |
-
# 4) Text-to-Speech
|
79 |
with st.spinner("🔊 Converting to speech..."):
|
80 |
tts = gTTS(text=story, lang="en")
|
81 |
tmp = tempfile.NamedTemporaryFile(suffix=".mp3", delete=False)
|
82 |
tts.write_to_fp(tmp)
|
83 |
tmp.flush()
|
84 |
st.audio(tmp.name, format="audio/mp3")
|
|
|
|
10 |
st.set_page_config(page_title="Storyteller for Kids", layout="centered")
|
11 |
st.title("🖼️ ➡️ 📖 Interactive Storyteller")
|
12 |
|
13 |
+
# —––––––– Load & warm pipelines
|
14 |
@st.cache_resource
|
15 |
def load_pipelines():
|
16 |
+
# 1) BLIP-base for captions
|
17 |
captioner = pipeline(
|
18 |
"image-to-text",
|
19 |
model="Salesforce/blip-image-captioning-base",
|
20 |
+
device=0 # GPU if available, else set -1 for CPU
|
21 |
)
|
22 |
+
# 2) Flan-T5-Large for coherent, instruction-driven stories
|
23 |
storyteller = pipeline(
|
24 |
"text2text-generation",
|
25 |
model="google/flan-t5-large",
|
26 |
device=0
|
27 |
)
|
28 |
|
29 |
+
# Warm-up so first real inference is faster
|
30 |
+
dummy = Image.new("RGB", (384, 384), color=(128, 128, 128))
|
31 |
captioner(dummy)
|
32 |
+
storyteller("Hello", max_new_tokens=1, return_full_text=False)
|
33 |
|
34 |
return captioner, storyteller
|
35 |
|
36 |
captioner, storyteller = load_pipelines()
|
37 |
|
38 |
# —––––––– Main UI
|
39 |
+
uploaded = st.file_uploader("Upload an image:", type=["jpg", "jpeg", "png"])
|
40 |
if uploaded:
|
41 |
+
# 1) Preprocess & display
|
42 |
image = Image.open(uploaded).convert("RGB")
|
43 |
+
image = image.resize((384, 384), Image.LANCZOS)
|
44 |
st.image(image, caption="Your image", use_container_width=True)
|
45 |
|
46 |
+
# 2) Generate caption
|
47 |
with st.spinner("🔍 Generating caption..."):
|
48 |
cap = captioner(image)[0]["generated_text"].strip()
|
49 |
st.markdown(f"**Caption:** {cap}")
|
50 |
|
51 |
+
# 3) Generate story with stronger prompt & no carry-over
|
52 |
prompt = (
|
53 |
+
f"Here is an image description: “{cap}”.\n"
|
54 |
+
"Write an 80–100 word playful story for 3–10 year-old children that:\n"
|
55 |
+
"1) Sets the scene with the panda and its surroundings.\n"
|
56 |
+
"2) Describes what the panda is doing and how it feels.\n"
|
57 |
+
"3) Wraps up with a fun conclusion.\n\n"
|
58 |
"Story:"
|
59 |
)
|
60 |
+
with st.spinner("✍️ Generating story..."):
|
61 |
out = storyteller(
|
62 |
prompt,
|
63 |
+
max_new_tokens=120,
|
64 |
do_sample=True,
|
65 |
temperature=0.7,
|
66 |
top_p=0.9,
|
67 |
top_k=50,
|
68 |
+
repetition_penalty=1.2,
|
69 |
+
no_repeat_ngram_size=3,
|
70 |
+
return_full_text=False # only the story text, not the prompt
|
71 |
)
|
72 |
+
story = out[0]["generated_text"].strip()
|
|
|
|
|
73 |
|
74 |
st.markdown("**Story:**")
|
75 |
st.write(story)
|
76 |
|
77 |
+
# 4) Text-to-Speech via gTTS
|
78 |
with st.spinner("🔊 Converting to speech..."):
|
79 |
tts = gTTS(text=story, lang="en")
|
80 |
tmp = tempfile.NamedTemporaryFile(suffix=".mp3", delete=False)
|
81 |
tts.write_to_fp(tmp)
|
82 |
tmp.flush()
|
83 |
st.audio(tmp.name, format="audio/mp3")
|
84 |
+
|