mayf commited on
Commit
eb25a05
·
verified ·
1 Parent(s): dd489ad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -25
app.py CHANGED
@@ -17,62 +17,62 @@ def load_pipelines():
17
  captioner = pipeline(
18
  "image-to-text",
19
  model="Salesforce/blip-image-captioning-base",
20
- device=0 # change to -1 if you only have CPU
21
  )
22
- # 2) Small GPT-Neo for quick stories
23
  storyteller = pipeline(
24
- "text-generation",
25
- model="EleutherAI/gpt-neo-125M",
26
  device=0
27
  )
28
 
29
- # Warm up both so the first real call is faster
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) Load + resize for faster encoding
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 step
47
  with st.spinner("🔍 Generating caption..."):
48
  cap = captioner(image)[0]["generated_text"].strip()
49
  st.markdown(f"**Caption:** {cap}")
50
 
51
- # 3) Story generation (sampling + repetition control)
52
  prompt = (
53
- f"Write an 80–100 word fun story for 3–10 year-old children "
54
- f"based on this description:\n\n“{cap}”\n\nStory: "
 
55
  )
56
  with st.spinner("✍️ Generating story..."):
57
  out = storyteller(
58
  prompt,
59
- max_new_tokens=120, # room for ~100 words
60
- do_sample=True, # enable sampling
61
- temperature=0.8, # creativity
62
- top_p=0.9, # nucleus sampling
63
- top_k=50, # limit to top 50 tokens
64
- repetition_penalty=1.2, # discourage exact repeats
65
- no_repeat_ngram_size=3 # prevent 3-gram repeats
66
  )
67
- # strip off the prompt so only the story remains
68
- story = out[0]["generated_text"][len(prompt):].strip()
 
69
  st.markdown("**Story:**")
70
  st.write(story)
71
 
72
- # 4) Text-to-Speech via gTTS
73
  with st.spinner("🔊 Converting to speech..."):
74
  tts = gTTS(text=story, lang="en")
75
  tmp = tempfile.NamedTemporaryFile(suffix=".mp3", delete=False)
76
  tts.write_to_fp(tmp)
77
  tmp.flush()
78
- st.audio(tmp.name, format="audio/mp3")
 
17
  captioner = pipeline(
18
  "image-to-text",
19
  model="Salesforce/blip-image-captioning-base",
20
+ device=0
21
  )
22
+ # 2) Instruction-tuned Flan-T5 small for stories
23
  storyteller = pipeline(
24
+ "text2text-generation",
25
+ model="google/flan-t5-small",
26
  device=0
27
  )
28
 
29
+ # Warm up so first real request is faster
30
  dummy = Image.new("RGB", (384, 384), color=(128, 128, 128))
31
  captioner(dummy)
32
+ storyteller("Tell me something", 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) Load + downsize
42
+ image = Image.open(uploaded).convert("RGB").resize((384, 384), Image.LANCZOS)
 
43
  st.image(image, caption="Your image", use_container_width=True)
44
 
45
+ # 2) Caption
46
  with st.spinner("🔍 Generating caption..."):
47
  cap = captioner(image)[0]["generated_text"].strip()
48
  st.markdown(f"**Caption:** {cap}")
49
 
50
+ # 3) Story generation
51
  prompt = (
52
+ f"Here is an image description: “{cap}”.\n"
53
+ "Write a playful, 80–100 word story for 3–10 year-olds\n\n"
54
+ "Story:"
55
  )
56
  with st.spinner("✍️ Generating story..."):
57
  out = storyteller(
58
  prompt,
59
+ max_new_tokens=150,
60
+ do_sample=True,
61
+ temperature=0.7,
62
+ top_p=0.9,
63
+ repetition_penalty=1.2,
64
+ no_repeat_ngram_size=3
 
65
  )
66
+ # strip off the prompt so you only get the story
67
+ story = out[0]["generated_text"].split("Story:")[-1].strip()
68
+
69
  st.markdown("**Story:**")
70
  st.write(story)
71
 
72
+ # 4) Text-to-Speech
73
  with st.spinner("🔊 Converting to speech..."):
74
  tts = gTTS(text=story, lang="en")
75
  tmp = tempfile.NamedTemporaryFile(suffix=".mp3", delete=False)
76
  tts.write_to_fp(tmp)
77
  tmp.flush()
78
+ st.audio(tmp.name, format="audio/mp3")