Update app.py
Browse files
app.py
CHANGED
@@ -44,7 +44,7 @@ else:
|
|
44 |
img.save(buf, format="PNG")
|
45 |
cap_out = caption_client(data=buf.getvalue())
|
46 |
|
47 |
-
#
|
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
|
72 |
with st.spinner("✍️ Generating story..."):
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
|
|
|
|
|
|
|
|
|
|
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
|
100 |
with st.spinner("🔊 Converting to speech..."):
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
|
|
|
|
|
|
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 |
|