Update app.py
Browse files
app.py
CHANGED
@@ -18,10 +18,11 @@ def load_pipelines():
|
|
18 |
"image-to-text",
|
19 |
model="Salesforce/blip-image-captioning-base"
|
20 |
)
|
21 |
-
# 2) Story generation with
|
22 |
storyteller = pipeline(
|
23 |
-
"
|
24 |
-
model="
|
|
|
25 |
)
|
26 |
return captioner, storyteller
|
27 |
|
@@ -36,25 +37,24 @@ if uploaded:
|
|
36 |
# —––––––– 1. Caption
|
37 |
with st.spinner("🔍 Looking at the image..."):
|
38 |
cap_outputs = captioner(image)
|
39 |
-
# BLIP returns a list of dicts with key "generated_text"
|
40 |
cap = cap_outputs[0].get("generated_text", "").strip()
|
41 |
st.markdown(f"**Caption:** {cap}")
|
42 |
|
43 |
# —––––––– 2. Story generation
|
44 |
prompt = (
|
45 |
-
"Write a 80–100
|
46 |
f"based on this description:\n\n“{cap}”\n\nStory:"
|
47 |
)
|
48 |
with st.spinner("✍️ Writing a story..."):
|
49 |
out = storyteller(
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
)
|
57 |
-
|
58 |
st.markdown("**Story:**")
|
59 |
st.write(story)
|
60 |
|
|
|
18 |
"image-to-text",
|
19 |
model="Salesforce/blip-image-captioning-base"
|
20 |
)
|
21 |
+
# 2) Story generation with GPT-Neo 2.7B
|
22 |
storyteller = pipeline(
|
23 |
+
"text-generation",
|
24 |
+
model="EleutherAI/gpt-neo-2.7B",
|
25 |
+
device=-1 # set to -1 if you only have CPU
|
26 |
)
|
27 |
return captioner, storyteller
|
28 |
|
|
|
37 |
# —––––––– 1. Caption
|
38 |
with st.spinner("🔍 Looking at the image..."):
|
39 |
cap_outputs = captioner(image)
|
|
|
40 |
cap = cap_outputs[0].get("generated_text", "").strip()
|
41 |
st.markdown(f"**Caption:** {cap}")
|
42 |
|
43 |
# —––––––– 2. Story generation
|
44 |
prompt = (
|
45 |
+
"Write a playful, 80–100 word story for 3–10 year-old children "
|
46 |
f"based on this description:\n\n“{cap}”\n\nStory:"
|
47 |
)
|
48 |
with st.spinner("✍️ Writing a story..."):
|
49 |
out = storyteller(
|
50 |
+
prompt,
|
51 |
+
max_new_tokens=120, # allow space for ~100 words
|
52 |
+
do_sample=True,
|
53 |
+
top_p=0.9,
|
54 |
+
temperature=0.8,
|
55 |
+
num_return_sequences=1
|
56 |
+
)
|
57 |
+
story = out[0]["generated_text"].strip()
|
58 |
st.markdown("**Story:**")
|
59 |
st.write(story)
|
60 |
|