Update app.py
Browse files
app.py
CHANGED
@@ -78,6 +78,8 @@ if uploaded_image:
|
|
78 |
story_prompt = (
|
79 |
f"<|im_start|>system\n"
|
80 |
f"You're a children's author. Create a short story (100-150 words) based on: {image_caption}\n"
|
|
|
|
|
81 |
)
|
82 |
|
83 |
# Generate story with progress
|
@@ -111,4 +113,40 @@ if uploaded_image:
|
|
111 |
|
112 |
# Format story text
|
113 |
sentences = []
|
114 |
-
for sent in re
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
story_prompt = (
|
79 |
f"<|im_start|>system\n"
|
80 |
f"You're a children's author. Create a short story (100-150 words) based on: {image_caption}\n"
|
81 |
+
f"Use simple language and include a moral lesson.<|im_end|>\n"
|
82 |
+
f"<|im_start|>assistant\n"
|
83 |
)
|
84 |
|
85 |
# Generate story with progress
|
|
|
113 |
|
114 |
# Format story text
|
115 |
sentences = []
|
116 |
+
for sent in re.split(r'(?<=[.!?]) +', clean_story):
|
117 |
+
sent = sent.strip()
|
118 |
+
if sent:
|
119 |
+
if len(sent) > 1 and not sent.endswith(('.','!','?')):
|
120 |
+
sent += '.'
|
121 |
+
sentences.append(sent[0].upper() + sent[1:])
|
122 |
+
|
123 |
+
final_story = ' '.join(sentences)[:600] # Limit length
|
124 |
+
|
125 |
+
update_progress(5)
|
126 |
+
time.sleep(0.5) # Final progress pause
|
127 |
+
|
128 |
+
except Exception as e:
|
129 |
+
st.error(f"❌ Story generation failed: {str(e)}")
|
130 |
+
st.stop()
|
131 |
+
|
132 |
+
finally:
|
133 |
+
progress_bar.empty()
|
134 |
+
status_text.empty()
|
135 |
+
|
136 |
+
# Display story
|
137 |
+
st.subheader("✨ Your Magical Story")
|
138 |
+
st.write(final_story)
|
139 |
+
|
140 |
+
# Audio conversion
|
141 |
+
with st.spinner("🔊 Creating audio version..."):
|
142 |
+
try:
|
143 |
+
audio = gTTS(text=final_story, lang="en", slow=False)
|
144 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
|
145 |
+
audio.save(tmp_file.name)
|
146 |
+
st.audio(tmp_file.name, format="audio/mp3")
|
147 |
+
except Exception as e:
|
148 |
+
st.error(f"❌ Audio conversion failed: {str(e)}")
|
149 |
+
|
150 |
+
# Footer
|
151 |
+
st.markdown("---")
|
152 |
+
st.markdown("📚 Made with ♥ by The Story Wizard • [Report Issues](https://example.com)")
|