Update app.py
Browse files
app.py
CHANGED
@@ -17,58 +17,58 @@ def load_pipelines():
|
|
17 |
captioner = pipeline(
|
18 |
"image-to-text",
|
19 |
model="Salesforce/blip-image-captioning-base",
|
20 |
-
device
|
21 |
)
|
22 |
-
# 2) Flan-T5-Large for
|
23 |
storyteller = pipeline(
|
24 |
"text2text-generation",
|
25 |
model="google/flan-t5-large",
|
26 |
device=0
|
27 |
)
|
28 |
|
29 |
-
# Warm-up
|
30 |
dummy = Image.new("RGB", (384, 384), color=(128, 128, 128))
|
31 |
captioner(dummy)
|
32 |
-
storyteller("
|
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",
|
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)
|
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"
|
54 |
"Write an 80–100 word playful story for 3–10 year-old children that:\n"
|
55 |
-
"1) Sets the scene
|
56 |
-
"2) Describes what
|
57 |
-
"3)
|
58 |
"Story:"
|
59 |
)
|
60 |
with st.spinner("✍️ Generating story..."):
|
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 |
-
return_full_text=False # only the story text, not the prompt
|
71 |
)
|
|
|
72 |
story = out[0]["generated_text"].strip()
|
73 |
|
74 |
st.markdown("**Story:**")
|
@@ -81,4 +81,3 @@ if uploaded:
|
|
81 |
tts.write_to_fp(tmp)
|
82 |
tmp.flush()
|
83 |
st.audio(tmp.name, format="audio/mp3")
|
84 |
-
|
|
|
17 |
captioner = pipeline(
|
18 |
"image-to-text",
|
19 |
model="Salesforce/blip-image-captioning-base",
|
20 |
+
device=-1 # or -1 if you only have CPU
|
21 |
)
|
22 |
+
# 2) Flan-T5-Large for instruction-driven stories
|
23 |
storyteller = pipeline(
|
24 |
"text2text-generation",
|
25 |
model="google/flan-t5-large",
|
26 |
device=0
|
27 |
)
|
28 |
|
29 |
+
# Warm-up to avoid first-call lag
|
30 |
dummy = Image.new("RGB", (384, 384), color=(128, 128, 128))
|
31 |
captioner(dummy)
|
32 |
+
storyteller("Warm up", 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 & 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) Caption
|
47 |
with st.spinner("🔍 Generating caption..."):
|
48 |
cap = captioner(image)[0]["generated_text"].strip()
|
49 |
st.markdown(f"**Caption:** {cap}")
|
50 |
|
51 |
+
# 3) Story
|
52 |
prompt = (
|
53 |
+
f"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 around the panda.\n"
|
56 |
+
"2) Describes what it’s doing and how it feels.\n"
|
57 |
+
"3) Ends with a fun conclusion.\n\n"
|
58 |
"Story:"
|
59 |
)
|
60 |
with st.spinner("✍️ Generating story..."):
|
61 |
out = storyteller(
|
62 |
prompt,
|
63 |
+
max_new_tokens=130,
|
64 |
do_sample=True,
|
65 |
temperature=0.7,
|
66 |
top_p=0.9,
|
67 |
top_k=50,
|
68 |
+
repetition_penalty=1.3,
|
69 |
+
no_repeat_ngram_size=3
|
|
|
70 |
)
|
71 |
+
# text2text pipeline returns only the generated part
|
72 |
story = out[0]["generated_text"].strip()
|
73 |
|
74 |
st.markdown("**Story:**")
|
|
|
81 |
tts.write_to_fp(tmp)
|
82 |
tmp.flush()
|
83 |
st.audio(tmp.name, format="audio/mp3")
|
|