mayf commited on
Commit
60c225b
·
verified ·
1 Parent(s): 0fdc556

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -16
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=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:**")
@@ -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")