codelion commited on
Commit
a2efbc9
·
verified ·
1 Parent(s): 915daef

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -80
app.py CHANGED
@@ -388,48 +388,43 @@ def start_feed(user_input, generate_video, current_index, feed_items):
388
  ideas = generate_ideas(user_input)
389
 
390
  item_generator = generate_item(user_input, ideas, generate_video=generate_video)
391
- for progress in item_generator:
392
- if time.time() - start_time > timeout_seconds:
393
- logging.error("Generation timed out")
394
- raise TimeoutError("Feed generation timed out")
395
- logging.debug(f"Progress update: {progress}")
396
- if isinstance(progress, dict) and "stage" in progress:
397
- html_content = generate_html([], False, 0, user_input, is_loading, progress["stage"], progress["message"], progress["progress"])
398
- yield current_user_input, current_index, feed_items, html_content, share_links, is_loading
399
- else:
400
- logging.debug("Received final item from generator")
401
- item = progress
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
402
  feed_items = [item]
403
  current_index = 0
404
  share_links = generate_share_links(item['image_base64'], item['video_base64'], item['text'])
405
  is_loading = False
406
  html_content = generate_html(feed_items, False, current_index, user_input, is_loading)
407
- logging.debug("Feed generation complete")
408
  yield current_user_input, current_index, feed_items, html_content, share_links, is_loading
409
  return
410
- logging.warning("Generator exhausted without returning final item")
411
- # Fallback if generator doesn't return
412
- html_content = """
413
- <div style="
414
- display: flex;
415
- flex-direction: column;
416
- align-items: center;
417
- justify-content: center;
418
- max-width: 360px;
419
- margin: 0 auto;
420
- background-color: #000;
421
- height: 640px;
422
- border: 1px solid #333;
423
- border-radius: 10px;
424
- color: white;
425
- font-family: Arial, sans-serif;
426
- ">
427
- <p>Generation incomplete. Please try again!</p>
428
- </div>
429
- """
430
- is_loading = False
431
- yield current_user_input, current_index, feed_items, html_content, share_links, is_loading
432
- return
433
  except Exception as e:
434
  logging.error(f"Error in start_feed: {e}")
435
  html_content = """
@@ -488,17 +483,36 @@ def load_next(user_input, generate_video, current_index, feed_items):
488
  yield current_user_input, current_index, feed_items, html_content, share_links, is_loading
489
 
490
  item_generator = generate_item(user_input, ideas, generate_video=generate_video)
491
- for progress in item_generator:
492
- if time.time() - start_time > timeout_seconds:
493
- logging.error("Generation timed out")
494
- raise TimeoutError("Feed generation timed out")
495
- logging.debug(f"Progress update: {progress}")
496
- if isinstance(progress, dict) and "stage" in progress:
497
- html_content = generate_html(feed_items, False, current_index, user_input, is_loading, progress["stage"], progress["message"], progress["progress"])
498
- yield current_user_input, current_index, feed_items, html_content, share_links, is_loading
499
- else:
500
- logging.debug("Received final item from generator")
501
- new_item = progress
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
502
  feed_items.append(new_item)
503
  current_index = len(feed_items) - 1
504
  is_loading = False
@@ -508,32 +522,12 @@ def load_next(user_input, generate_video, current_index, feed_items):
508
  feed_items[current_index]['video_base64'],
509
  feed_items[current_index]['text']
510
  )
511
- logging.debug("New feed item generated")
512
  yield current_user_input, current_index, feed_items, html_content, share_links, is_loading
513
  return
514
- logging.warning("Generator exhausted without returning final item")
515
- # Fallback if generator doesn't return
516
- html_content = """
517
- <div style="
518
- display: flex;
519
- flex-direction: column;
520
- align-items: center;
521
- justify-content: center;
522
- max-width: 360px;
523
- margin: 0 auto;
524
- background-color: #000;
525
- height: 640px;
526
- border: 1px solid #333;
527
- border-radius: 10px;
528
- color: white;
529
- font-family: Arial, sans-serif;
530
- ">
531
- <p>Generation incomplete. Please try again!</p>
532
- </div>
533
- """
534
- is_loading = False
535
- yield current_user_input, current_index, feed_items, html_content, share_links, is_loading
536
- return
537
  except Exception as e:
538
  logging.error(f"Error in load_next: {e}")
539
  html_content = """
@@ -602,15 +596,15 @@ def generate_share_links(image_base64, video_base64, caption):
602
  video_data_url = f"data:video/mp4;base64,{video_base64}"
603
  download_links += f"""
604
  <a href="{video_data_url}" download="feed_video.mp4" style="
605
- background-color: #4CAF50;
606
- color: white;
607
- padding: 8px 16px;
608
- border-radius: 5px;
609
- text-decoration: none;
610
- font-size: 14px;
611
- font-weight: bold;
612
- transition: background-color 0.3s;
613
- " onmouseover="this.style.backgroundColor='#45a049'" onmouseout="this.style.backgroundColor='#4CAF50'">Download Video</a>
614
  """
615
  download_links += "</div>"
616
 
@@ -924,7 +918,7 @@ with gr.Blocks(
924
  label="Generate Video (may take longer)",
925
  value=False
926
  )
927
- magic_button = gr.Button("✨Create✨", elem_classes="gr-button")
928
 
929
  feed_html = gr.HTML()
930
  share_html = gr.HTML(label="Share this item:")
 
388
  ideas = generate_ideas(user_input)
389
 
390
  item_generator = generate_item(user_input, ideas, generate_video=generate_video)
391
+ try:
392
+ while True:
393
+ if time.time() - start_time > timeout_seconds:
394
+ logging.error("Generation timed out")
395
+ raise TimeoutError("Feed generation timed out")
396
+ progress = next(item_generator)
397
+ logging.debug(f"Progress update: {progress}")
398
+ if isinstance(progress, dict) and "stage" in progress:
399
+ html_content = generate_html([], False, 0, user_input, is_loading, progress["stage"], progress["message"], progress["progress"])
400
+ yield current_user_input, current_index, feed_items, html_content, share_links, is_loading
401
+ else:
402
+ logging.debug(f"Received final item: {progress}")
403
+ item = progress
404
+ feed_items = [item]
405
+ current_index = 0
406
+ share_links = generate_share_links(item['image_base64'], item['video_base64'], item['text'])
407
+ is_loading = False
408
+ html_content = generate_html(feed_items, False, current_index, user_input, is_loading)
409
+ logging.debug("Feed generation complete")
410
+ yield current_user_input, current_index, feed_items, html_content, share_links, is_loading
411
+ return
412
+ except StopIteration as e:
413
+ # Handle the generator's return value
414
+ if e.value is not None:
415
+ logging.debug(f"Generator returned final item: {e.value}")
416
+ item = e.value
417
  feed_items = [item]
418
  current_index = 0
419
  share_links = generate_share_links(item['image_base64'], item['video_base64'], item['text'])
420
  is_loading = False
421
  html_content = generate_html(feed_items, False, current_index, user_input, is_loading)
422
+ logging.debug("Feed generation complete via StopIteration")
423
  yield current_user_input, current_index, feed_items, html_content, share_links, is_loading
424
  return
425
+ else:
426
+ logging.warning("Generator returned without a final item")
427
+ raise ValueError("Generator did not return a valid item")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
428
  except Exception as e:
429
  logging.error(f"Error in start_feed: {e}")
430
  html_content = """
 
483
  yield current_user_input, current_index, feed_items, html_content, share_links, is_loading
484
 
485
  item_generator = generate_item(user_input, ideas, generate_video=generate_video)
486
+ try:
487
+ while True:
488
+ if time.time() - start_time > timeout_seconds:
489
+ logging.error("Generation timed out")
490
+ raise TimeoutError("Feed generation timed out")
491
+ progress = next(item_generator)
492
+ logging.debug(f"Progress update: {progress}")
493
+ if isinstance(progress, dict) and "stage" in progress:
494
+ html_content = generate_html(feed_items, False, current_index, user_input, is_loading, progress["stage"], progress["message"], progress["progress"])
495
+ yield current_user_input, current_index, feed_items, html_content, share_links, is_loading
496
+ else:
497
+ logging.debug(f"Received final item: {progress}")
498
+ new_item = progress
499
+ feed_items.append(new_item)
500
+ current_index = len(feed_items) - 1
501
+ is_loading = False
502
+ html_content = generate_html(feed_items, False, current_index, user_input, is_loading)
503
+ share_links = generate_share_links(
504
+ feed_items[current_index]['image_base64'],
505
+ feed_items[current_index]['video_base64'],
506
+ feed_items[current_index]['text']
507
+ )
508
+ logging.debug("New feed item generated")
509
+ yield current_user_input, current_index, feed_items, html_content, share_links, is_loading
510
+ return
511
+ except StopIteration as e:
512
+ # Handle the generator's return value
513
+ if e.value is not None:
514
+ logging.debug(f"Generator returned final item: {e.value}")
515
+ new_item = e.value
516
  feed_items.append(new_item)
517
  current_index = len(feed_items) - 1
518
  is_loading = False
 
522
  feed_items[current_index]['video_base64'],
523
  feed_items[current_index]['text']
524
  )
525
+ logging.debug("New feed item generated via StopIteration")
526
  yield current_user_input, current_index, feed_items, html_content, share_links, is_loading
527
  return
528
+ else:
529
+ logging.warning("Generator returned without a final item")
530
+ raise ValueError("Generator did not return a valid item")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
531
  except Exception as e:
532
  logging.error(f"Error in load_next: {e}")
533
  html_content = """
 
596
  video_data_url = f"data:video/mp4;base64,{video_base64}"
597
  download_links += f"""
598
  <a href="{video_data_url}" download="feed_video.mp4" style="
599
+ background-color: #4CAF50;
600
+ color: white;
601
+ padding: 8px 16px;
602
+ border-radius: 5px;
603
+ text-decoration: none;
604
+ font-size: 14px;
605
+ font-weight: bold;
606
+ transition: background-color 0.3s;
607
+ " onmouseover="this.style.backgroundColor='#45a049'" onmouseout="this.style.backgroundColor='#4CAF50'">Download Video</a>
608
  """
609
  download_links += "</div>"
610
 
 
918
  label="Generate Video (may take longer)",
919
  value=False
920
  )
921
+ magic_button = gr.Button("✨ Generate Next Item", elem_classes="gr-button")
922
 
923
  feed_html = gr.HTML()
924
  share_html = gr.HTML(label="Share this item:")