codelion commited on
Commit
a214565
·
verified ·
1 Parent(s): 591125d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +119 -85
app.py CHANGED
@@ -74,7 +74,7 @@ def generate_ideas(user_input):
74
  user_input (str): The user's input concept or idea.
75
 
76
  Yields:
77
- tuple: (progress_percentage, message) for UI updates.
78
  list: Final list of ideas as strings.
79
  """
80
  yield (10, f"Brainstorming epic ideas for {user_input}... 🌟")
@@ -138,7 +138,7 @@ def generate_item(user_input, ideas, generate_video=False, max_retries=3):
138
  max_retries (int): Maximum number of retries for image generation per cycle.
139
 
140
  Yields:
141
- tuple: (progress_percentage, message) for UI updates.
142
  dict: Final dictionary with 'text', 'image_base64', 'video_base64', and 'ideas'.
143
  """
144
  video_base64 = None
@@ -480,6 +480,77 @@ def generate_item(user_input, ideas, generate_video=False, max_retries=3):
480
  'ideas': ideas
481
  }
482
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
483
  def start_feed(user_input, generate_video, current_index, feed_items):
484
  """
485
  Start or update the feed based on the user input.
@@ -492,8 +563,8 @@ def start_feed(user_input, generate_video, current_index, feed_items):
492
  feed_items (list): The current list of feed items.
493
 
494
  Yields:
495
- tuple: (progress_percentage, message, html_content, is_loading) for UI updates.
496
- tuple: (current_user_input, current_index, feed_items, html_content, share_links, is_loading) for final result.
497
  """
498
  if not user_input.strip():
499
  user_input = "trending"
@@ -503,8 +574,8 @@ def start_feed(user_input, generate_video, current_index, feed_items):
503
 
504
  # Set initial loading state
505
  is_loading = True
506
- html_content = generate_html([], False, 0, user_input, is_loading, 0, f"Getting started with {user_input}... 🚀")
507
- yield (0, f"Getting started with {user_input}... 🚀", html_content, is_loading)
508
  share_links = ""
509
 
510
  try:
@@ -514,8 +585,8 @@ def start_feed(user_input, generate_video, current_index, feed_items):
514
  for update in ideas_gen:
515
  if isinstance(update, tuple):
516
  progress, message = update
517
- html_content = generate_html([], False, 0, user_input, is_loading, progress, message)
518
- yield (progress, message, html_content, is_loading)
519
  else:
520
  ideas = update
521
 
@@ -525,13 +596,14 @@ def start_feed(user_input, generate_video, current_index, feed_items):
525
  for update in item_gen:
526
  if isinstance(update, tuple):
527
  progress, message = update
528
- html_content = generate_html([], False, 0, user_input, is_loading, progress, message)
529
- yield (progress, message, html_content, is_loading)
530
  else:
531
  item = update
532
 
533
  feed_items = [item]
534
  current_index = 0
 
535
  share_links = generate_share_links(
536
  item['image_base64'],
537
  item['video_base64'],
@@ -541,7 +613,7 @@ def start_feed(user_input, generate_video, current_index, feed_items):
541
  print(f"Error in start_feed: {e}")
542
  feed_items = []
543
  current_index = 0
544
- html_content = """
545
  <div style="
546
  display: flex;
547
  flex-direction: column;
@@ -559,15 +631,15 @@ def start_feed(user_input, generate_video, current_index, feed_items):
559
  <p>Error generating content. Please try again!</p>
560
  </div>
561
  """
 
 
562
  is_loading = False
563
- yield (100, "Oops, something went wrong! 😅", html_content, is_loading)
564
- return current_user_input, current_index, feed_items, html_content, share_links, is_loading
565
 
566
  # Set loading state to False and update UI
567
  is_loading = False
568
- html_content = generate_html(feed_items, False, current_index, user_input, is_loading, 100, "")
569
- yield (100, f"Ready to roll with {user_input}! 🚀", html_content, is_loading)
570
- return current_user_input, current_index, feed_items, html_content, share_links, is_loading
571
 
572
  def load_next(user_input, generate_video, current_index, feed_items):
573
  """
@@ -578,11 +650,11 @@ def load_next(user_input, generate_video, current_index, feed_items):
578
  user_input (str): The user's input concept or idea (updated from the textbox).
579
  generate_video (bool): Whether to generate a video.
580
  current_index (int): The current item index.
581
- feed_items (list): The current list of feed items.
582
 
583
  Yields:
584
- tuple: (progress_percentage, message, html_content, is_loading) for UI updates.
585
- tuple: (current_user_input, current_index, feed_items, html_content, share_links, is_loading) for final result.
586
  """
587
  # Update current_user_input with the latest user_input from the textbox
588
  current_user_input = user_input if user_input.strip() else "trending"
@@ -591,14 +663,15 @@ def load_next(user_input, generate_video, current_index, feed_items):
591
  user_input = current_user_input
592
 
593
  is_loading = True
594
- html_content = generate_html(feed_items, False, current_index, user_input, is_loading, 0, f"Loading next {user_input} vibe... 🚀")
595
- yield (0, f"Loading next {user_input} vibe... 🚀", html_content, is_loading)
596
  share_links = ""
597
 
598
  try:
599
  if current_index + 1 < len(feed_items):
600
  current_index += 1
601
- yield (50, f"Switching to the next {user_input} moment... 🔄", html_content, is_loading)
 
602
  else:
603
  ideas = feed_items[-1]['ideas'] if feed_items else None
604
  if not ideas:
@@ -607,8 +680,8 @@ def load_next(user_input, generate_video, current_index, feed_items):
607
  for update in ideas_gen:
608
  if isinstance(update, tuple):
609
  progress, message = update
610
- html_content = generate_html(feed_items, False, current_index, user_input, is_loading, progress, message)
611
- yield (progress, message, html_content, is_loading)
612
  else:
613
  ideas = update
614
 
@@ -618,14 +691,15 @@ def load_next(user_input, generate_video, current_index, feed_items):
618
  for update in new_item_gen:
619
  if isinstance(update, tuple):
620
  progress, message = update
621
- html_content = generate_html(feed_items, False, current_index, user_input, is_loading, progress, message)
622
- yield (progress, message, html_content, is_loading)
623
  else:
624
  new_item = update
625
 
626
  feed_items.append(new_item)
627
  current_index = len(feed_items) - 1
628
 
 
629
  share_links = generate_share_links(
630
  feed_items[current_index]['image_base64'],
631
  feed_items[current_index]['video_base64'],
@@ -633,7 +707,7 @@ def load_next(user_input, generate_video, current_index, feed_items):
633
  )
634
  except Exception as e:
635
  print(f"Error in load_next: {e}")
636
- html_content = """
637
  <div style="
638
  display: flex;
639
  flex-direction: column;
@@ -651,14 +725,14 @@ def load_next(user_input, generate_video, current_index, feed_items):
651
  <p>Error generating content. Please try again!</p>
652
  </div>
653
  """
 
 
654
  is_loading = False
655
- yield (100, "Oops, something went wrong! 😅", html_content, is_loading)
656
- return current_user_input, current_index, feed_items, html_content, share_links, is_loading
657
 
658
  is_loading = False
659
- html_content = generate_html(feed_items, False, current_index, user_input, is_loading, 100, "")
660
- yield (100, f"Next {user_input} vibe is live! 🚀", html_content, is_loading)
661
- return current_user_input, current_index, feed_items, html_content, share_links, is_loading
662
 
663
  def load_previous(user_input, generate_video, current_index, feed_items):
664
  """
@@ -671,20 +745,20 @@ def load_previous(user_input, generate_video, current_index, feed_items):
671
  feed_items (list): The current list of feed items.
672
 
673
  Returns:
674
- tuple: (current_user_input, current_index, feed_items, html_content, share_links, is_loading)
675
  """
676
  # Update current_user_input with the latest user_input
677
  current_user_input = user_input if user_input.strip() else "trending"
678
 
679
  if current_index > 0:
680
  current_index -= 1
681
- html_content = generate_html(feed_items, False, current_index, user_input, False, 100, "")
682
  share_links = generate_share_links(
683
  feed_items[current_index]['image_base64'],
684
  feed_items[current_index]['video_base64'],
685
  feed_items[current_index]['text']
686
  )
687
- return current_user_input, current_index, feed_items, html_content, share_links, False
688
 
689
  def generate_share_links(image_base64, video_base64, caption):
690
  """
@@ -693,7 +767,7 @@ def generate_share_links(image_base64, video_base64, caption):
693
 
694
  Args:
695
  image_base64 (str): The base64-encoded image data.
696
- video_base64 (str or None): The base64-encoded video data (single video).
697
  caption (str): The caption to share.
698
 
699
  Returns:
@@ -843,13 +917,13 @@ def generate_share_links(image_base64, video_base64, caption):
843
 
844
  def generate_html(feed_items, scroll_to_latest=False, current_index=0, user_input="", is_loading=False, progress=0, message=""):
845
  """
846
- Generate an HTML string to display the current feed item or loading state with progress bar.
847
 
848
  Args:
849
  feed_items (list): List of dictionaries containing 'text', 'image_base64', and 'video_base64'.
850
  scroll_to_latest (bool): Whether to auto-scroll to the latest item (not used here).
851
  current_index (int): The index of the item to display.
852
- user_input (str): The user's input concept or idea for loading messages.
853
  is_loading (bool): Whether the feed is currently loading.
854
  progress (float): Current progress percentage (0–100).
855
  message (str): Current loading message to display.
@@ -858,12 +932,12 @@ def generate_html(feed_items, scroll_to_latest=False, current_index=0, user_inpu
858
  str: HTML string representing the feed or loading state.
859
  """
860
  if is_loading:
861
- return f"""
 
862
  <div id="feed-container" style="
863
  display: flex;
864
  flex-direction: column;
865
  align-items: center;
866
- justify-content: center;
867
  max-width: 360px;
868
  margin: 0 auto;
869
  background-color: #000;
@@ -872,54 +946,13 @@ def generate_html(feed_items, scroll_to_latest=False, current_index=0, user_inpu
872
  border-radius: 10px;
873
  color: white;
874
  font-family: Arial, sans-serif;
875
- position: relative;
876
  ">
877
- <div id="loading-message" style="
878
- font-size: 18px;
879
- font-weight: bold;
880
- text-align: center;
881
- margin-bottom: 20px;
882
- text-shadow: 1px 1px 2px rgba(0,0,0,0.5);
883
- ">
884
- {message}
885
- </div>
886
- <div style="
887
- width: 80%;
888
- height: 10px;
889
- background-color: #333;
890
- border-radius: 5px;
891
- overflow: hidden;
892
- ">
893
- <div id="progress-bar" style="
894
- width: {progress}%;
895
- height: 100%;
896
- background: linear-gradient(to right, #ff2d55, #ff5e78);
897
- transition: width 0.5s ease-in-out;
898
- "></div>
899
- </div>
900
- <div style="
901
- margin-top: 10px;
902
- font-size: 14px;
903
- color: #ccc;
904
- ">
905
- {int(progress)}% Complete
906
- </div>
907
- <style>
908
- @keyframes pulse {{
909
- 0% {{ opacity: 1; }}
910
- 50% {{ opacity: 0.5; }}
911
- 100% {{ opacity: 1; }}
912
- }}
913
- #loading-message {{
914
- animation: pulse 2s infinite;
915
- }}
916
- </style>
917
  </div>
918
  """
919
 
920
  if not feed_items or current_index >= len(feed_items):
921
  return """
922
- <div style="
923
  display: flex;
924
  flex-direction: column;
925
  align-items: center;
@@ -1058,6 +1091,7 @@ with gr.Blocks(
1058
  magic_button = gr.Button("✨ Generate Next Item", elem_classes="gr-button")
1059
 
1060
  # Output display
 
1061
  feed_html = gr.HTML()
1062
  share_html = gr.HTML(label="Share this item:")
1063
 
@@ -1066,14 +1100,14 @@ with gr.Blocks(
1066
  user_input.submit(
1067
  fn=start_feed,
1068
  inputs=[user_input, generate_video_checkbox, current_index, feed_items],
1069
- outputs=[current_user_input, current_index, feed_items, feed_html, share_html, is_loading]
1070
  )
1071
 
1072
  # Handle magic button click to generate next item
1073
  magic_button.click(
1074
  fn=load_next,
1075
  inputs=[user_input, generate_video_checkbox, current_index, feed_items],
1076
- outputs=[current_user_input, current_index, feed_items, feed_html, share_html, is_loading]
1077
  )
1078
 
1079
  # Hidden button for previous item navigation
@@ -1083,7 +1117,7 @@ with gr.Blocks(
1083
  previous_button.click(
1084
  fn=load_previous,
1085
  inputs=[user_input, generate_video_checkbox, current_index, feed_items],
1086
- outputs=[current_user_input, current_index, feed_items, feed_html, share_html, is_loading]
1087
  )
1088
 
1089
  # Launch the app
 
74
  user_input (str): The user's input concept or idea.
75
 
76
  Yields:
77
+ tuple: (progress_percentage, message) for progress_html updates.
78
  list: Final list of ideas as strings.
79
  """
80
  yield (10, f"Brainstorming epic ideas for {user_input}... 🌟")
 
138
  max_retries (int): Maximum number of retries for image generation per cycle.
139
 
140
  Yields:
141
+ tuple: (progress_percentage, message) for progress_html updates.
142
  dict: Final dictionary with 'text', 'image_base64', 'video_base64', and 'ideas'.
143
  """
144
  video_base64 = None
 
480
  'ideas': ideas
481
  }
482
 
483
+ def generate_progress_html(progress, message, user_input):
484
+ """
485
+ Generate HTML for the progress bar and witty text.
486
+
487
+ Args:
488
+ progress (float): Current progress percentage (0–100).
489
+ message (str): Current loading message to display.
490
+ user_input (str): The user's input concept or idea for context.
491
+
492
+ Returns:
493
+ str: HTML string for the progress bar.
494
+ """
495
+ return f"""
496
+ <div id="progress-container" style="
497
+ display: flex;
498
+ flex-direction: column;
499
+ align-items: center;
500
+ justify-content: center;
501
+ max-width: 360px;
502
+ margin: 0 auto;
503
+ background-color: #000;
504
+ height: 200px;
505
+ border: 1px solid #333;
506
+ border-radius: 10px;
507
+ color: white;
508
+ font-family: Arial, sans-serif;
509
+ position: relative;
510
+ ">
511
+ <div id="loading-message" style="
512
+ font-size: 18px;
513
+ font-weight: bold;
514
+ text-align: center;
515
+ margin-bottom: 20px;
516
+ text-shadow: 1px 1px 2px rgba(0,0,0,0.5);
517
+ ">
518
+ {message}
519
+ </div>
520
+ <div style="
521
+ width: 80%;
522
+ height: 10px;
523
+ background-color: #333;
524
+ border-radius: 5px;
525
+ overflow: hidden;
526
+ ">
527
+ <div id="progress-bar" style="
528
+ width: {progress}%;
529
+ height: 100%;
530
+ background: linear-gradient(to right, #ff2d55, #ff5e78);
531
+ transition: width 0.5s ease-in-out;
532
+ "></div>
533
+ </div>
534
+ <div style="
535
+ margin-top: 10px;
536
+ font-size: 14px;
537
+ color: #ccc;
538
+ ">
539
+ {int(progress)}% Complete
540
+ </div>
541
+ <style>
542
+ @keyframes pulse {{
543
+ 0% {{ opacity: 1; }}
544
+ 50% {{ opacity: 0.5; }}
545
+ 100% {{ opacity: 1; }}
546
+ }}
547
+ #loading-message {{
548
+ animation: pulse 2s infinite;
549
+ }}
550
+ </style>
551
+ </div>
552
+ """
553
+
554
  def start_feed(user_input, generate_video, current_index, feed_items):
555
  """
556
  Start or update the feed based on the user input.
 
563
  feed_items (list): The current list of feed items.
564
 
565
  Yields:
566
+ tuple: (progress_html) for progress updates.
567
+ tuple: (current_user_input, current_index, feed_items, feed_html, share_html, is_loading) for final result.
568
  """
569
  if not user_input.strip():
570
  user_input = "trending"
 
574
 
575
  # Set initial loading state
576
  is_loading = True
577
+ progress_html = generate_progress_html(0, f"Getting started with {user_input}... 🚀", user_input)
578
+ yield (progress_html,)
579
  share_links = ""
580
 
581
  try:
 
585
  for update in ideas_gen:
586
  if isinstance(update, tuple):
587
  progress, message = update
588
+ progress_html = generate_progress_html(progress, message, user_input)
589
+ yield (progress_html,)
590
  else:
591
  ideas = update
592
 
 
596
  for update in item_gen:
597
  if isinstance(update, tuple):
598
  progress, message = update
599
+ progress_html = generate_progress_html(progress, message, user_input)
600
+ yield (progress_html,)
601
  else:
602
  item = update
603
 
604
  feed_items = [item]
605
  current_index = 0
606
+ feed_html = generate_html(feed_items, False, current_index, user_input, is_loading=False)
607
  share_links = generate_share_links(
608
  item['image_base64'],
609
  item['video_base64'],
 
613
  print(f"Error in start_feed: {e}")
614
  feed_items = []
615
  current_index = 0
616
+ feed_html = """
617
  <div style="
618
  display: flex;
619
  flex-direction: column;
 
631
  <p>Error generating content. Please try again!</p>
632
  </div>
633
  """
634
+ progress_html = generate_progress_html(100, "Oops, something went wrong! 😅", user_input)
635
+ yield (progress_html,)
636
  is_loading = False
637
+ return current_user_input, current_index, feed_items, feed_html, share_links, is_loading
 
638
 
639
  # Set loading state to False and update UI
640
  is_loading = False
641
+ progress_html = ""
642
+ return current_user_input, current_index, feed_items, feed_html, share_links, is_loading
 
643
 
644
  def load_next(user_input, generate_video, current_index, feed_items):
645
  """
 
650
  user_input (str): The user's input concept or idea (updated from the textbox).
651
  generate_video (bool): Whether to generate a video.
652
  current_index (int): The current item index.
653
+ feed_items (list): The current list of feed_items.
654
 
655
  Yields:
656
+ tuple: (progress_html) for progress updates.
657
+ tuple: (current_user_input, current_index, feed_items, feed_html, share_html, is_loading) for final result.
658
  """
659
  # Update current_user_input with the latest user_input from the textbox
660
  current_user_input = user_input if user_input.strip() else "trending"
 
663
  user_input = current_user_input
664
 
665
  is_loading = True
666
+ progress_html = generate_progress_html(0, f"Loading next {user_input} vibe... 🚀", user_input)
667
+ yield (progress_html,)
668
  share_links = ""
669
 
670
  try:
671
  if current_index + 1 < len(feed_items):
672
  current_index += 1
673
+ progress_html = generate_progress_html(50, f"Switching to the next {user_input} moment... 🔄", user_input)
674
+ yield (progress_html,)
675
  else:
676
  ideas = feed_items[-1]['ideas'] if feed_items else None
677
  if not ideas:
 
680
  for update in ideas_gen:
681
  if isinstance(update, tuple):
682
  progress, message = update
683
+ progress_html = generate_progress_html(progress, message, user_input)
684
+ yield (progress_html,)
685
  else:
686
  ideas = update
687
 
 
691
  for update in new_item_gen:
692
  if isinstance(update, tuple):
693
  progress, message = update
694
+ progress_html = generate_progress_html(progress, message, user_input)
695
+ yield (progress_html,)
696
  else:
697
  new_item = update
698
 
699
  feed_items.append(new_item)
700
  current_index = len(feed_items) - 1
701
 
702
+ feed_html = generate_html(feed_items, False, current_index, user_input, is_loading=False)
703
  share_links = generate_share_links(
704
  feed_items[current_index]['image_base64'],
705
  feed_items[current_index]['video_base64'],
 
707
  )
708
  except Exception as e:
709
  print(f"Error in load_next: {e}")
710
+ feed_html = """
711
  <div style="
712
  display: flex;
713
  flex-direction: column;
 
725
  <p>Error generating content. Please try again!</p>
726
  </div>
727
  """
728
+ progress_html = generate_progress_html(100, "Oops, something went wrong! 😅", user_input)
729
+ yield (progress_html,)
730
  is_loading = False
731
+ return current_user_input, current_index, feed_items, feed_html, share_links, is_loading
 
732
 
733
  is_loading = False
734
+ progress_html = ""
735
+ return current_user_input, current_index, feed_items, feed_html, share_links, is_loading
 
736
 
737
  def load_previous(user_input, generate_video, current_index, feed_items):
738
  """
 
745
  feed_items (list): The current list of feed items.
746
 
747
  Returns:
748
+ tuple: (current_user_input, current_index, feed_items, feed_html, share_html, is_loading)
749
  """
750
  # Update current_user_input with the latest user_input
751
  current_user_input = user_input if user_input.strip() else "trending"
752
 
753
  if current_index > 0:
754
  current_index -= 1
755
+ feed_html = generate_html(feed_items, False, current_index, user_input, is_loading=False)
756
  share_links = generate_share_links(
757
  feed_items[current_index]['image_base64'],
758
  feed_items[current_index]['video_base64'],
759
  feed_items[current_index]['text']
760
  )
761
+ return current_user_input, current_index, feed_items, feed_html, share_links, False
762
 
763
  def generate_share_links(image_base64, video_base64, caption):
764
  """
 
767
 
768
  Args:
769
  image_base64 (str): The base64-encoded image data.
770
+ video_base64 (str or None): The base64-enoded video data (single video).
771
  caption (str): The caption to share.
772
 
773
  Returns:
 
917
 
918
  def generate_html(feed_items, scroll_to_latest=False, current_index=0, user_input="", is_loading=False, progress=0, message=""):
919
  """
920
+ Generate an HTML string to display the current feed item or loading state.
921
 
922
  Args:
923
  feed_items (list): List of dictionaries containing 'text', 'image_base64', and 'video_base64'.
924
  scroll_to_latest (bool): Whether to auto-scroll to the latest item (not used here).
925
  current_index (int): The index of the item to display.
926
+ user_input (str): The user's input concept or idea for context.
927
  is_loading (bool): Whether the feed is currently loading.
928
  progress (float): Current progress percentage (0–100).
929
  message (str): Current loading message to display.
 
932
  str: HTML string representing the feed or loading state.
933
  """
934
  if is_loading:
935
+ # When loading, return an empty feed container (progress is handled by progress_html)
936
+ return """
937
  <div id="feed-container" style="
938
  display: flex;
939
  flex-direction: column;
940
  align-items: center;
 
941
  max-width: 360px;
942
  margin: 0 auto;
943
  background-color: #000;
 
946
  border-radius: 10px;
947
  color: white;
948
  font-family: Arial, sans-serif;
 
949
  ">
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
950
  </div>
951
  """
952
 
953
  if not feed_items or current_index >= len(feed_items):
954
  return """
955
+ <div id="feed-container" style="
956
  display: flex;
957
  flex-direction: column;
958
  align-items: center;
 
1091
  magic_button = gr.Button("✨ Generate Next Item", elem_classes="gr-button")
1092
 
1093
  # Output display
1094
+ progress_html = gr.HTML(label="Progress", visible=True)
1095
  feed_html = gr.HTML()
1096
  share_html = gr.HTML(label="Share this item:")
1097
 
 
1100
  user_input.submit(
1101
  fn=start_feed,
1102
  inputs=[user_input, generate_video_checkbox, current_index, feed_items],
1103
+ outputs=[current_user_input, current_index, feed_items, feed_html, share_html, is_loading, progress_html]
1104
  )
1105
 
1106
  # Handle magic button click to generate next item
1107
  magic_button.click(
1108
  fn=load_next,
1109
  inputs=[user_input, generate_video_checkbox, current_index, feed_items],
1110
+ outputs=[current_user_input, current_index, feed_items, feed_html, share_html, is_loading, progress_html]
1111
  )
1112
 
1113
  # Hidden button for previous item navigation
 
1117
  previous_button.click(
1118
  fn=load_previous,
1119
  inputs=[user_input, generate_video_checkbox, current_index, feed_items],
1120
+ outputs=[current_user_input, current_index, feed_items, feed_html, share_html, is_loading, progress_html]
1121
  )
1122
 
1123
  # Launch the app