Jaward commited on
Commit
a85ca37
·
verified ·
1 Parent(s): 35bf16f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -24
app.py CHANGED
@@ -431,9 +431,10 @@ Example: 'Received {total_slides} slides and {total_slides} scripts. Lecture is
431
 
432
  progress = 0
433
  label = "Research: in progress..."
 
434
  yield (
435
  html_with_progress(label, progress),
436
- [], 0, [], None
437
  )
438
  await asyncio.sleep(0.1)
439
 
@@ -479,7 +480,7 @@ Example: 'Received {total_slides} slides and {total_slides} scripts. Lecture is
479
  label = "Slides: generating..."
480
  yield (
481
  html_with_progress(label, progress),
482
- [], 0, [], None
483
  )
484
  await asyncio.sleep(0.1)
485
  elif source == "slide_agent" and message.target == "script_agent":
@@ -504,7 +505,7 @@ Example: 'Received {total_slides} slides and {total_slides} scripts. Lecture is
504
  label = "Scripts: generating..."
505
  yield (
506
  html_with_progress(label, progress),
507
- [], 0, [], None
508
  )
509
  await asyncio.sleep(0.1)
510
  elif source == "script_agent" and message.target == "feynman_agent":
@@ -518,7 +519,7 @@ Example: 'Received {total_slides} slides and {total_slides} scripts. Lecture is
518
  label = "Review: in progress..."
519
  yield (
520
  html_with_progress(label, progress),
521
- [], 0, [], None
522
  )
523
  await asyncio.sleep(0.1)
524
 
@@ -528,7 +529,7 @@ Example: 'Received {total_slides} slides and {total_slides} scripts. Lecture is
528
  label = "Slides: generating..."
529
  yield (
530
  html_with_progress(label, progress),
531
- [], 0, [], None
532
  )
533
  await asyncio.sleep(0.1)
534
 
@@ -561,7 +562,7 @@ Example: 'Received {total_slides} slides and {total_slides} scripts. Lecture is
561
  label = "Scripts: generating..."
562
  yield (
563
  html_with_progress(label, progress),
564
- [], 0, [], None
565
  )
566
  await asyncio.sleep(0.1)
567
  else:
@@ -595,7 +596,7 @@ Example: 'Received {total_slides} slides and {total_slides} scripts. Lecture is
595
  label = "Scripts generated and saved. Reviewing..."
596
  yield (
597
  html_with_progress(label, progress),
598
- [], 0, [], None
599
  )
600
  await asyncio.sleep(0.1)
601
  else:
@@ -624,7 +625,7 @@ Example: 'Received {total_slides} slides and {total_slides} scripts. Lecture is
624
  html_with_progress(label, progress),
625
  txt_file_paths,
626
  0,
627
- [None] * total_slides,
628
  zip_file_path
629
  )
630
  await asyncio.sleep(0.1)
@@ -641,7 +642,7 @@ Example: 'Received {total_slides} slides and {total_slides} scripts. Lecture is
641
  logger.debug("Message from %s, type: %s, content: %s", source, type(msg), msg.to_text() if hasattr(msg, 'to_text') else str(msg))
642
  yield (
643
  error_html,
644
- [], 0, [], None
645
  )
646
  return
647
 
@@ -654,7 +655,7 @@ Example: 'Received {total_slides} slides and {total_slides} scripts. Lecture is
654
  <p style="margin-top: 20px;">Expected {total_slides} slides, but generated {len(slides)}. Please try again.</p>
655
  </div>
656
  """,
657
- [], 0, [], None
658
  )
659
  return
660
 
@@ -667,7 +668,7 @@ Example: 'Received {total_slides} slides and {total_slides} scripts. Lecture is
667
  <p style="margin-top: 20px;">Scripts must be a list of strings. Please try again.</p>
668
  </div>
669
  """,
670
- [], 0, [], None
671
  )
672
  return
673
 
@@ -680,7 +681,7 @@ Example: 'Received {total_slides} slides and {total_slides} scripts. Lecture is
680
  <p style="margin-top: 20px;">Generated {len(slides)} slides but {len(scripts)} scripts. Please try again.</p>
681
  </div>
682
  """,
683
- [], 0, [], None
684
  )
685
  return
686
 
@@ -694,7 +695,7 @@ Example: 'Received {total_slides} slides and {total_slides} scripts. Lecture is
694
  <p style="margin-top: 20px;">Please try again.</p>
695
  </div>
696
  """,
697
- [], 0, [], None
698
  )
699
  return
700
 
@@ -713,7 +714,7 @@ Example: 'Received {total_slides} slides and {total_slides} scripts. Lecture is
713
  </div>
714
  <div style="padding: 20px; text-align: center;">
715
  <div id="audio-timeline" style="display: flex; justify-content: center; margin-bottom: 10px;">
716
- <!-- Audio components will be rendered here by Gradio -->
717
  </div>
718
  <div style="display: flex; justify-content: center; margin-bottom: 10px;">
719
  <button id="prev-btn" style="border-radius: 50%; width: 40px; height: 40px; margin: 0 5px; font-size: 1.2em; cursor: pointer;">⏮</button>
@@ -730,15 +731,20 @@ Example: 'Received {total_slides} slides and {total_slides} scripts. Lecture is
730
  let audioElements = [];
731
  let isPlaying = false;
732
 
733
- // Function to populate audio elements (will be updated by Gradio)
734
  function updateAudioElements() {{
735
  audioElements = [];
736
  for (let i = 0; i < totalSlides; i++) {{
737
- const audio = document.getElementById(`audio-${{i+1}}`);
738
- if (audio) {{
739
- audioElements.push(audio);
 
 
 
 
740
  }}
741
  }}
 
742
  }}
743
 
744
  function renderSlide() {{
@@ -841,8 +847,8 @@ Example: 'Received {total_slides} slides and {total_slides} scripts. Lecture is
841
  document.getElementById('next-btn').addEventListener('click', nextSlide);
842
  document.getElementById('fullscreen-btn').addEventListener('click', toggleFullScreen);
843
 
844
- // Initialize
845
- updateAudioElements();
846
  </script>
847
  """
848
  logger.info("Yielding lecture materials before audio generation")
@@ -865,7 +871,7 @@ Example: 'Received {total_slides} slides and {total_slides} scripts. Lecture is
865
  <p style="margin-top: 20px;">Please upload a valid MP3 or WAV audio file and try again.</p>
866
  </div>
867
  """,
868
- [], 0, [], None
869
  )
870
  return
871
 
@@ -952,7 +958,7 @@ Example: 'Received {total_slides} slides and {total_slides} scripts. Lecture is
952
  <p style="margin-top: 20px;">Please try again or adjust your inputs.</p>
953
  </div>
954
  """,
955
- [], 0, [], None
956
  )
957
  return
958
 
@@ -989,7 +995,8 @@ with gr.Blocks(title="Agent Feynman") as demo:
989
  """
990
  slide_display = gr.HTML(label="Lecture Slides", value=default_slide_html)
991
  file_output = gr.File(label="Download Generated Files")
992
- audio_outputs = gr.Audio(label="Slide Audio", visible=False)
 
993
  slide_index = gr.State(value=0)
994
  zip_output = gr.File(label="Download All Files as ZIP")
995
 
@@ -1002,7 +1009,7 @@ with gr.Blocks(title="Agent Feynman") as demo:
1002
  generate_btn.click(
1003
  fn=on_generate,
1004
  inputs=[api_service, api_key, serpapi_key, title, lecture_content_description, lecture_type, speaker_audio, num_slides],
1005
- outputs=[slide_display, file_output, slide_index, audio_outputs, zip_output]
1006
  )
1007
 
1008
  if __name__ == "__main__":
 
431
 
432
  progress = 0
433
  label = "Research: in progress..."
434
+ audio_outputs = [None] * total_slides # Initialize for total_slides
435
  yield (
436
  html_with_progress(label, progress),
437
+ [], 0, audio_outputs, None
438
  )
439
  await asyncio.sleep(0.1)
440
 
 
480
  label = "Slides: generating..."
481
  yield (
482
  html_with_progress(label, progress),
483
+ [], 0, audio_outputs, None
484
  )
485
  await asyncio.sleep(0.1)
486
  elif source == "slide_agent" and message.target == "script_agent":
 
505
  label = "Scripts: generating..."
506
  yield (
507
  html_with_progress(label, progress),
508
+ [], 0, audio_outputs, None
509
  )
510
  await asyncio.sleep(0.1)
511
  elif source == "script_agent" and message.target == "feynman_agent":
 
519
  label = "Review: in progress..."
520
  yield (
521
  html_with_progress(label, progress),
522
+ [], 0, audio_outputs, None
523
  )
524
  await asyncio.sleep(0.1)
525
 
 
529
  label = "Slides: generating..."
530
  yield (
531
  html_with_progress(label, progress),
532
+ [], 0, audio_outputs, None
533
  )
534
  await asyncio.sleep(0.1)
535
 
 
562
  label = "Scripts: generating..."
563
  yield (
564
  html_with_progress(label, progress),
565
+ [], 0, audio_outputs, None
566
  )
567
  await asyncio.sleep(0.1)
568
  else:
 
596
  label = "Scripts generated and saved. Reviewing..."
597
  yield (
598
  html_with_progress(label, progress),
599
+ [], 0, audio_outputs, None
600
  )
601
  await asyncio.sleep(0.1)
602
  else:
 
625
  html_with_progress(label, progress),
626
  txt_file_paths,
627
  0,
628
+ audio_outputs,
629
  zip_file_path
630
  )
631
  await asyncio.sleep(0.1)
 
642
  logger.debug("Message from %s, type: %s, content: %s", source, type(msg), msg.to_text() if hasattr(msg, 'to_text') else str(msg))
643
  yield (
644
  error_html,
645
+ [], 0, audio_outputs, None
646
  )
647
  return
648
 
 
655
  <p style="margin-top: 20px;">Expected {total_slides} slides, but generated {len(slides)}. Please try again.</p>
656
  </div>
657
  """,
658
+ [], 0, audio_outputs, None
659
  )
660
  return
661
 
 
668
  <p style="margin-top: 20px;">Scripts must be a list of strings. Please try again.</p>
669
  </div>
670
  """,
671
+ [], 0, audio_outputs, None
672
  )
673
  return
674
 
 
681
  <p style="margin-top: 20px;">Generated {len(slides)} slides but {len(scripts)} scripts. Please try again.</p>
682
  </div>
683
  """,
684
+ [], 0, audio_outputs, None
685
  )
686
  return
687
 
 
695
  <p style="margin-top: 20px;">Please try again.</p>
696
  </div>
697
  """,
698
+ [], 0, audio_outputs, None
699
  )
700
  return
701
 
 
714
  </div>
715
  <div style="padding: 20px; text-align: center;">
716
  <div id="audio-timeline" style="display: flex; justify-content: center; margin-bottom: 10px;">
717
+ {"".join([f'<div id="audio-container-{i+1}" style="display: inline-block; margin: 0 10px; width: 200px;"></div>' for i in range(total_slides)])}
718
  </div>
719
  <div style="display: flex; justify-content: center; margin-bottom: 10px;">
720
  <button id="prev-btn" style="border-radius: 50%; width: 40px; height: 40px; margin: 0 5px; font-size: 1.2em; cursor: pointer;">⏮</button>
 
731
  let audioElements = [];
732
  let isPlaying = false;
733
 
734
+ // Function to populate audio elements
735
  function updateAudioElements() {{
736
  audioElements = [];
737
  for (let i = 0; i < totalSlides; i++) {{
738
+ // Gradio assigns IDs like "audio-<component_id>-<index>"
739
+ const container = document.getElementById(`audio-container-${{i+1}}`);
740
+ if (container) {{
741
+ const audio = container.querySelector('audio');
742
+ if (audio) {{
743
+ audioElements.push(audio);
744
+ }}
745
  }}
746
  }}
747
+ console.log("Updated audio elements:", audioElements);
748
  }}
749
 
750
  function renderSlide() {{
 
847
  document.getElementById('next-btn').addEventListener('click', nextSlide);
848
  document.getElementById('fullscreen-btn').addEventListener('click', toggleFullScreen);
849
 
850
+ // Initialize audio elements after DOM updates
851
+ setTimeout(updateAudioElements, 1000); // Delay to ensure Gradio renders the audio components
852
  </script>
853
  """
854
  logger.info("Yielding lecture materials before audio generation")
 
871
  <p style="margin-top: 20px;">Please upload a valid MP3 or WAV audio file and try again.</p>
872
  </div>
873
  """,
874
+ [], 0, audio_files, None
875
  )
876
  return
877
 
 
958
  <p style="margin-top: 20px;">Please try again or adjust your inputs.</p>
959
  </div>
960
  """,
961
+ [], 0, audio_outputs, None
962
  )
963
  return
964
 
 
995
  """
996
  slide_display = gr.HTML(label="Lecture Slides", value=default_slide_html)
997
  file_output = gr.File(label="Download Generated Files")
998
+ # Create a list of audio components for each slide
999
+ audio_outputs = [gr.Audio(label=f"Slide {i+1} Audio", visible=True) for i in range(20)] # Max 20 slides
1000
  slide_index = gr.State(value=0)
1001
  zip_output = gr.File(label="Download All Files as ZIP")
1002
 
 
1009
  generate_btn.click(
1010
  fn=on_generate,
1011
  inputs=[api_service, api_key, serpapi_key, title, lecture_content_description, lecture_type, speaker_audio, num_slides],
1012
+ outputs=[slide_display, file_output, slide_index] + audio_outputs[:20] + [zip_output]
1013
  )
1014
 
1015
  if __name__ == "__main__":