mayf commited on
Commit
c2c4e19
·
verified ·
1 Parent(s): 5e41bcc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -27
app.py CHANGED
@@ -44,7 +44,7 @@ else:
44
  img.save(buf, format="PNG")
45
  cap_out = caption_client(data=buf.getvalue())
46
 
47
- # Unwrap list/dict properly
48
  if isinstance(cap_out, list) and cap_out:
49
  cap_text = cap_out[0].get("generated_text", "").strip()
50
  elif isinstance(cap_out, dict):
@@ -58,7 +58,7 @@ else:
58
 
59
  st.markdown(f"**Caption:** {cap_text}")
60
 
61
- # 3) Build prompt
62
  prompt = (
63
  f"Here’s an image description: “{cap_text}”.\n\n"
64
  "Write an 80–100 word playful story for 3–10 year-old children that:\n"
@@ -68,26 +68,31 @@ else:
68
  "Story:"
69
  )
70
 
71
- # 4) Generate story via HF Inference API (use `params`)
72
  with st.spinner("✍️ Generating story..."):
73
- story_out = story_client(
74
- inputs=prompt,
75
- parameters={ # ← must be `params`, not `parameters`
76
- "max_new_tokens": 120,
77
- "do_sample": True,
78
- "temperature": 0.7,
79
- "top_p": 0.9,
80
- "top_k": 50,
81
- "repetition_penalty": 1.2,
82
- "no_repeat_ngram_size": 3
83
- }
84
- )
85
- if isinstance(story_out, list) and story_out:
86
- story = story_out[0].get("generated_text", "").strip()
87
- elif isinstance(story_out, dict):
88
- story = story_out.get("generated_text", "").strip()
89
- else:
90
- story = str(story_out).strip()
 
 
 
 
 
91
 
92
  if not story:
93
  st.error("😕 Couldn’t generate a story. Please try again!")
@@ -96,11 +101,14 @@ else:
96
  st.markdown("**Story:**")
97
  st.write(story)
98
 
99
- # 5) Text-to-Speech via gTTS
100
  with st.spinner("🔊 Converting to speech..."):
101
- tts = gTTS(text=story, lang="en")
102
- tmp = tempfile.NamedTemporaryFile(suffix=".mp3", delete=False)
103
- tts.write_to_fp(tmp)
104
- tmp.flush()
105
- st.audio(tmp.name, format="audio/mp3")
 
 
 
106
 
 
44
  img.save(buf, format="PNG")
45
  cap_out = caption_client(data=buf.getvalue())
46
 
47
+ # Handle caption response
48
  if isinstance(cap_out, list) and cap_out:
49
  cap_text = cap_out[0].get("generated_text", "").strip()
50
  elif isinstance(cap_out, dict):
 
58
 
59
  st.markdown(f"**Caption:** {cap_text}")
60
 
61
+ # 3) Build story prompt
62
  prompt = (
63
  f"Here’s an image description: “{cap_text}”.\n\n"
64
  "Write an 80–100 word playful story for 3–10 year-old children that:\n"
 
68
  "Story:"
69
  )
70
 
71
+ # 4) Generate story with CORRECT parameters
72
  with st.spinner("✍️ Generating story..."):
73
+ try:
74
+ story_out = story_client(
75
+ inputs=prompt,
76
+ max_new_tokens=150,
77
+ do_sample=True,
78
+ temperature=0.8,
79
+ top_p=0.9,
80
+ repetition_penalty=1.1,
81
+ no_repeat_ngram_size=2
82
+ )
83
+
84
+ # Handle different response formats
85
+ if isinstance(story_out, list):
86
+ story_text = story_out[0].get("generated_text", "")
87
+ else:
88
+ story_text = story_out.get("generated_text", "")
89
+
90
+ # Clean up the output
91
+ story = story_text.split("Story:")[-1].strip()
92
+
93
+ except Exception as e:
94
+ st.error(f"🚨 Story generation failed: {str(e)}")
95
+ st.stop()
96
 
97
  if not story:
98
  st.error("😕 Couldn’t generate a story. Please try again!")
 
101
  st.markdown("**Story:**")
102
  st.write(story)
103
 
104
+ # 5) Text-to-Speech
105
  with st.spinner("🔊 Converting to speech..."):
106
+ try:
107
+ tts = gTTS(text=story, lang="en")
108
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp:
109
+ tts.write_to_fp(tmp)
110
+ tmp.seek(0)
111
+ st.audio(tmp.name, format="audio/mp3")
112
+ except Exception as e:
113
+ st.error(f"🔇 Audio conversion failed: {str(e)}")
114