ccclllwww commited on
Commit
5227f85
ยท
verified ยท
1 Parent(s): ac70fac

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -39
app.py CHANGED
@@ -214,7 +214,7 @@ def generate_audio_from_story(story_text: str, output_path: str = "output.wav")
214
  st.set_page_config(
215
  page_title="Magic Story Generator",
216
  page_icon="๐Ÿงš",
217
- layout="wide",
218
  initial_sidebar_state="collapsed"
219
  )
220
 
@@ -224,7 +224,7 @@ st.markdown("""
224
  /* Primary title styling */
225
  .main-title {
226
  color: #E91E63;
227
- font-size: 2.8rem;
228
  text-align: center;
229
  padding: 20px;
230
  text-shadow: 2px 2px #FFC107;
@@ -264,7 +264,13 @@ st.markdown('<p class="main-title">๐Ÿงš Welcome to Magic Story Maker!</p>', unsa
264
  # File upload section
265
  with st.container():
266
  st.subheader("Step 1: Upload Your Picture")
267
- uploaded_image = st.file_uploader("Choose an image...", type=["png", "jpg", "jpeg"],label_visibility="collapsed")
 
 
 
 
 
 
268
 
269
  # Main processing flow
270
  if uploaded_image is not None:
@@ -284,55 +290,74 @@ if uploaded_image is not None:
284
  help="Generate educational story with life lessons",
285
  key="edu_btn"):
286
  st.session_state.selected_prompt = "educational"
 
287
  with col2:
288
  if st.button("๐ŸŒ  Fantasy Adventure",
289
  help="Create magical adventure story",
290
  key="fantasy_btn"):
291
  st.session_state.selected_prompt = "adventure"
 
292
  with col3:
293
  if st.button("๐Ÿป Animal Friends",
294
  help="Make story about friendly animals",
295
  key="animal_btn"):
296
  st.session_state.selected_prompt = "animal"
 
 
 
 
 
 
 
 
 
297
 
298
- # Define prompt templates
299
- PROMPT_TEMPLATES = {
300
- "educational": {
301
- "system": "You are a children's educator. Create a simple 150-word story that teaches basic life skills or moral lessons.",
302
- "icon": "๐Ÿ“š"
303
- },
304
- "adventure": {
305
- "system": "You are a fantasy writer. Create a 150-word magical adventure story suitable for children.",
306
- "icon": "๐ŸŒ "
307
- },
308
- "animal": {
309
- "system": "You are an animal expert. Create a 150-word story about friendly animals learning together.",
310
- "icon": "๐Ÿป"
 
 
 
311
  }
312
- }
313
 
314
- # Story generation section
315
- with st.spinner(f"{PROMPT_TEMPLATES[st.session_state.selected_prompt]['icon']} Creating your story..."):
316
- # Generate image caption
317
- image_caption = generate_image_caption(image)
318
-
319
- # Generate story content
320
- selected_template = PROMPT_TEMPLATES[st.session_state.selected_prompt]
321
- story_text = generate_story_content(
322
- system_prompt=selected_template["system"],
323
- user_prompt=image_caption
324
- )
325
 
326
- # Display formatted story
327
- st.subheader("Step 3: Your Magical Story")
328
- st.markdown(f'<div class="story-container">{story_text}</div>',
329
- unsafe_allow_html=True)
 
 
 
 
 
 
 
 
 
 
 
 
330
 
331
- # Audio generation section
332
- with st.spinner("๐Ÿ”ฎ Preparing story narration..."):
333
- audio_file = generate_audio_from_story(story_text, "story_audio.wav")
334
- st.subheader("๐ŸŽง Listen to Your Story")
335
- st.audio(audio_file)
 
 
 
336
 
337
  # Help section
338
  st.markdown("---")
@@ -340,6 +365,7 @@ st.subheader("๐ŸŒŸ How to Use:")
340
  st.info("""
341
  1. Upload any picture (animals, nature, or people work best!)
342
  2. Choose your favorite story style
343
- 3. Wait for magic to happen!
344
- 4. Listen to your personalized story
 
345
  """)
 
214
  st.set_page_config(
215
  page_title="Magic Story Generator",
216
  page_icon="๐Ÿงš",
217
+ layout="centered",
218
  initial_sidebar_state="collapsed"
219
  )
220
 
 
224
  /* Primary title styling */
225
  .main-title {
226
  color: #E91E63;
227
+ font-size: 6rem;
228
  text-align: center;
229
  padding: 20px;
230
  text-shadow: 2px 2px #FFC107;
 
264
  # File upload section
265
  with st.container():
266
  st.subheader("Step 1: Upload Your Picture")
267
+ uploaded_image = st.file_uploader("Choose an image...",
268
+ type=["png", "jpg", "jpeg"],
269
+ label_visibility="collapsed")
270
+
271
+ # Initialize session state for confirmation status
272
+ if 'confirmed' not in st.session_state:
273
+ st.session_state.confirmed = False
274
 
275
  # Main processing flow
276
  if uploaded_image is not None:
 
290
  help="Generate educational story with life lessons",
291
  key="edu_btn"):
292
  st.session_state.selected_prompt = "educational"
293
+ st.session_state.confirmed = False # Reset confirmation
294
  with col2:
295
  if st.button("๐ŸŒ  Fantasy Adventure",
296
  help="Create magical adventure story",
297
  key="fantasy_btn"):
298
  st.session_state.selected_prompt = "adventure"
299
+ st.session_state.confirmed = False
300
  with col3:
301
  if st.button("๐Ÿป Animal Friends",
302
  help="Make story about friendly animals",
303
  key="animal_btn"):
304
  st.session_state.selected_prompt = "animal"
305
+ st.session_state.confirmed = False
306
+
307
+ # Add confirmation button
308
+ with st.container():
309
+ st.subheader("Step 3: Confirm Selection")
310
+ if st.button("๐Ÿ”ฎ Start Magic Creation!",
311
+ help="Click to generate story after choosing style",
312
+ type="primary"):
313
+ st.session_state.confirmed = True
314
 
315
+ # Only show generation when confirmed
316
+ if st.session_state.get('confirmed', False):
317
+ # Define prompt templates
318
+ PROMPT_TEMPLATES = {
319
+ "educational": {
320
+ "system": "You are a children's educator. Create a simple 150-word story that teaches basic life skills or moral lessons.",
321
+ "icon": "๐Ÿ“š"
322
+ },
323
+ "adventure": {
324
+ "system": "You are a fantasy writer. Create a 150-word magical adventure story suitable for children.",
325
+ "icon": "๐ŸŒ "
326
+ },
327
+ "animal": {
328
+ "system": "You are an animal expert. Create a 150-word story about friendly animals learning together.",
329
+ "icon": "๐Ÿป"
330
+ }
331
  }
 
332
 
333
+ # Safe access with default fallback
334
+ selected_prompt = st.session_state.get("selected_prompt", "educational")
 
 
 
 
 
 
 
 
 
335
 
336
+ # Story generation section
337
+ with st.spinner(f"{PROMPT_TEMPLATES[selected_prompt]['icon']} Creating your story..."):
338
+ # Generate image caption
339
+ image_caption = generate_image_caption(image)
340
+
341
+ # Generate story content
342
+ selected_template = PROMPT_TEMPLATES[selected_prompt]
343
+ story_text = generate_story_content(
344
+ system_prompt=selected_template["system"],
345
+ user_prompt=image_caption
346
+ )
347
+
348
+ # Display formatted story
349
+ st.subheader("โœจ Your Magical Story")
350
+ st.markdown(f'<div class="story-container">{story_text}</div>',
351
+ unsafe_allow_html=True)
352
 
353
+ # Audio generation section
354
+ with st.spinner("๐Ÿ”ฎ Preparing story narration..."):
355
+ audio_file = generate_audio_from_story(story_text, "story_audio.wav")
356
+ st.subheader("๐ŸŽง Listen to Your Story")
357
+ st.audio(audio_file)
358
+ else:
359
+ # Show waiting message
360
+ st.info("โ„น๏ธ Please select a story style and click the confirmation button to continue")
361
 
362
  # Help section
363
  st.markdown("---")
 
365
  st.info("""
366
  1. Upload any picture (animals, nature, or people work best!)
367
  2. Choose your favorite story style
368
+ 3. Click the confirmation button
369
+ 4. Wait for magic to happen!
370
+ 5. Listen to your personalized story
371
  """)