Update app.py
Browse files
app.py
CHANGED
@@ -40,17 +40,22 @@ else:
|
|
40 |
|
41 |
# 2) Generate caption
|
42 |
with st.spinner("🔍 Generating caption..."):
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
if not cap_text:
|
56 |
st.error("😕 Couldn’t generate a caption. Try another image.")
|
@@ -68,36 +73,40 @@ else:
|
|
68 |
"Story:"
|
69 |
)
|
70 |
|
71 |
-
|
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 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
|
|
99 |
st.stop()
|
100 |
|
|
|
|
|
|
|
101 |
# 5) Text-to-Speech
|
102 |
with st.spinner("🔊 Converting to speech..."):
|
103 |
try:
|
|
|
40 |
|
41 |
# 2) Generate caption
|
42 |
with st.spinner("🔍 Generating caption..."):
|
43 |
+
try:
|
44 |
+
buf = BytesIO()
|
45 |
+
img.save(buf, format="PNG")
|
46 |
+
cap_out = caption_client(data=buf.getvalue())
|
47 |
+
|
48 |
+
# Handle caption response
|
49 |
+
if isinstance(cap_out, list) and cap_out:
|
50 |
+
cap_text = cap_out[0].get("generated_text", "").strip()
|
51 |
+
elif isinstance(cap_out, dict):
|
52 |
+
cap_text = cap_out.get("generated_text", "").strip()
|
53 |
+
else:
|
54 |
+
cap_text = str(cap_out).strip()
|
55 |
+
|
56 |
+
except Exception as e:
|
57 |
+
st.error(f"🚨 Caption generation failed: {str(e)}")
|
58 |
+
st.stop()
|
59 |
|
60 |
if not cap_text:
|
61 |
st.error("😕 Couldn’t generate a caption. Try another image.")
|
|
|
73 |
"Story:"
|
74 |
)
|
75 |
|
76 |
+
# 4) Generate story
|
77 |
+
with st.spinner("✍️ Generating story..."):
|
78 |
+
try:
|
79 |
+
story_out = story_client(
|
80 |
+
inputs=prompt,
|
81 |
+
parameters={
|
82 |
+
"max_new_tokens": 250,
|
83 |
+
"temperature": 0.7,
|
84 |
+
"top_p": 0.9,
|
85 |
+
"repetition_penalty": 1.1,
|
86 |
+
"do_sample": True
|
87 |
+
}
|
88 |
+
)
|
89 |
+
|
90 |
+
# Parse response
|
91 |
+
if isinstance(story_out, list):
|
92 |
+
story_text = story_out[0].get("generated_text", "")
|
93 |
+
else:
|
94 |
+
story_text = story_out.get("generated_text", "")
|
95 |
+
|
96 |
+
# Clean up output
|
97 |
+
story = story_text.split("Story:")[-1].strip()
|
98 |
+
|
99 |
+
except Exception as e:
|
100 |
+
st.error(f"🚨 Story generation failed: {str(e)}")
|
101 |
+
st.stop()
|
102 |
+
|
103 |
+
if not story:
|
104 |
+
st.error("😕 Couldn’t generate a story. Please try again!")
|
105 |
st.stop()
|
106 |
|
107 |
+
st.markdown("**Story:**")
|
108 |
+
st.write(story)
|
109 |
+
|
110 |
# 5) Text-to-Speech
|
111 |
with st.spinner("🔊 Converting to speech..."):
|
112 |
try:
|