pranavinani commited on
Commit
c354f04
·
verified ·
1 Parent(s): 5522414

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. gradio_app.py +60 -35
gradio_app.py CHANGED
@@ -384,14 +384,31 @@ def create_interface():
384
  gr.Markdown("# SyncDub - Video Translation and Dubbing")
385
  gr.Markdown("Translate and dub videos to different languages with speaker diarization")
386
 
387
- session_id = create_session_id() # Create a session ID for tracking progress
 
388
 
389
  with gr.Tab("Process Video"):
390
  with gr.Row():
391
  with gr.Column(scale=2):
392
- # Change Textbox to Video component
393
- media_input = gr.Video(label="Video URL or Upload", sources=["upload", "clipboard"])
394
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
395
  with gr.Row():
396
  # Enhanced language dropdown with full language names
397
  target_language = gr.Dropdown(
@@ -487,9 +504,22 @@ def create_interface():
487
  )
488
 
489
  # Function to actually pass the gender values to the process_video function
490
- def process_with_genders(media_source, target_language, tts_choice, max_speakers, translation_method, *gender_values):
 
 
 
 
 
 
 
 
 
 
 
491
  # Convert the gender values into a dictionary to pass to process_video
492
  speaker_genders_dict = {str(i): gender for i, gender in enumerate(gender_values) if gender}
 
 
493
  result = process_video(media_source, target_language, tts_choice, max_speakers,
494
  speaker_genders_dict, session_id, translation_method=translation_method)
495
 
@@ -503,62 +533,55 @@ def create_interface():
503
  process_btn.click(
504
  fn=process_with_genders,
505
  inputs=[
506
- media_input,
 
 
507
  target_language,
508
  tts_choice,
509
  max_speakers,
510
- translation_method, # Add translation method to inputs
511
- # Pass individual radio components, not a Group
 
512
  *[speaker_genders[str(i)] for i in range(8)]
513
  ],
514
  outputs=[output, subtitle_output, output_message]
515
  )
516
 
517
- # Update status periodically
518
- status_timer = gr.Timer(2, lambda: get_processing_status(session_id), None, status_text)
519
 
520
  # Create a more compatible approach for status updates
521
- def start_status_updates(session_id):
522
- def update_status_thread():
523
- import time
524
- while session_id in processing_status and processing_status[session_id]["progress"] < 1.0:
525
- try:
526
- time.sleep(1) # Update status every second
527
- # This is a workaround since we can't use JavaScript directly
528
- except:
529
- break
530
-
531
- thread = threading.Thread(target=update_status_thread)
532
- thread.daemon = True # Thread will exit when main program exits
533
- thread.start()
534
- return "Processing started"
535
 
536
  # Status checking function
537
- def check_status(session_id):
538
- status = get_processing_status(session_id)
539
  return status
540
 
541
  # Define the handle_reset function here
542
  def handle_reset():
543
  """Handle the reset button click by calling reset_application()"""
544
  try:
545
- # Assuming reset_application is defined elsewhere in your code
546
  result = reset_application()
547
- # Inform user that reset is in progress
548
  gr.Info("Resetting application...")
549
  # Return values in the order expected by the outputs list
 
 
550
  return (
551
- "Application reset successful. Ready for new video processing.",
552
- None, # media_input
 
553
  None, # output
554
  None, # subtitle_output
555
- "" # output_message
 
556
  )
557
  except Exception as e:
558
  logger.exception("Error in reset handler")
 
559
  return (
560
  f"Reset failed: {str(e)}",
561
- None, None, None, ""
562
  )
563
 
564
  # Replace multiple button rows with a single row containing both buttons
@@ -569,7 +592,7 @@ def create_interface():
569
  # Connect the refresh button to check status
570
  refresh_btn.click(
571
  fn=check_status,
572
- inputs=[gr.State(session_id)],
573
  outputs=[status_text]
574
  )
575
 
@@ -579,14 +602,16 @@ def create_interface():
579
  inputs=[],
580
  outputs=[
581
  status_text,
582
- media_input,
 
583
  output,
584
  subtitle_output,
585
- output_message
 
586
  ]
587
  )
588
 
589
- # Create a simple auto-refresh component using a Textbox with a timer
590
  gr.HTML("""
591
  <script>
592
  // Simple poller to update status
 
384
  gr.Markdown("# SyncDub - Video Translation and Dubbing")
385
  gr.Markdown("Translate and dub videos to different languages with speaker diarization")
386
 
387
+ # Use a unique session ID per interface instance
388
+ session_id_state = gr.State(create_session_id())
389
 
390
  with gr.Tab("Process Video"):
391
  with gr.Row():
392
  with gr.Column(scale=2):
393
+ # Input type selection
394
+ input_type = gr.Radio(["URL", "Upload"], label="Input Type", value="URL")
395
 
396
+ # URL Input (visible by default)
397
+ url_input = gr.Textbox(label="Video URL", placeholder="Enter a YouTube URL", visible=True)
398
+
399
+ # Upload Input (hidden by default)
400
+ upload_input = gr.Video(label="Upload Video", sources=["upload"], visible=False)
401
+
402
+ # Function to toggle input visibility
403
+ def update_input_visibility(choice):
404
+ if choice == "URL":
405
+ return {url_input: gr.Textbox(visible=True), upload_input: gr.Video(visible=False)}
406
+ else: # Upload
407
+ return {url_input: gr.Textbox(visible=False), upload_input: gr.Video(visible=True)}
408
+
409
+ # Connect radio button change to visibility function
410
+ input_type.change(fn=update_input_visibility, inputs=input_type, outputs=[url_input, upload_input])
411
+
412
  with gr.Row():
413
  # Enhanced language dropdown with full language names
414
  target_language = gr.Dropdown(
 
504
  )
505
 
506
  # Function to actually pass the gender values to the process_video function
507
+ def process_with_genders(input_type_val, url_val, upload_val, target_language, tts_choice, max_speakers, translation_method, session_id, *gender_values):
508
+ # Determine the actual media source based on the input type
509
+ if input_type_val == "URL":
510
+ media_source = url_val
511
+ elif input_type_val == "Upload":
512
+ media_source = upload_val # This will be the file path from gr.Video
513
+ else:
514
+ return None, None, "Invalid input type selected."
515
+
516
+ if not media_source:
517
+ return None, None, "Please provide a video URL or upload a file."
518
+
519
  # Convert the gender values into a dictionary to pass to process_video
520
  speaker_genders_dict = {str(i): gender for i, gender in enumerate(gender_values) if gender}
521
+
522
+ # Call the main processing function
523
  result = process_video(media_source, target_language, tts_choice, max_speakers,
524
  speaker_genders_dict, session_id, translation_method=translation_method)
525
 
 
533
  process_btn.click(
534
  fn=process_with_genders,
535
  inputs=[
536
+ input_type, # Pass the radio button value
537
+ url_input, # Pass the URL input component
538
+ upload_input, # Pass the Upload input component
539
  target_language,
540
  tts_choice,
541
  max_speakers,
542
+ translation_method,
543
+ session_id_state, # Pass the session ID state
544
+ # Pass individual radio components for genders
545
  *[speaker_genders[str(i)] for i in range(8)]
546
  ],
547
  outputs=[output, subtitle_output, output_message]
548
  )
549
 
550
+ # Update status periodically using the session ID from state
551
+ status_timer = gr.Timer(2, lambda s_id: get_processing_status(s_id), session_id_state, status_text)
552
 
553
  # Create a more compatible approach for status updates
554
+ # ... (start_status_updates function remains the same) ...
 
 
 
 
 
 
 
 
 
 
 
 
 
555
 
556
  # Status checking function
557
+ def check_status(s_id):
558
+ status = get_processing_status(s_id)
559
  return status
560
 
561
  # Define the handle_reset function here
562
  def handle_reset():
563
  """Handle the reset button click by calling reset_application()"""
564
  try:
 
565
  result = reset_application()
 
566
  gr.Info("Resetting application...")
567
  # Return values in the order expected by the outputs list
568
+ # Also reset the input fields and generate a new session ID
569
+ new_session_id = result.get("session_id", create_session_id()) # Get new ID from reset or generate
570
  return (
571
+ result.get("new_status", "Reset complete."), # status_text
572
+ None, # url_input
573
+ None, # upload_input
574
  None, # output
575
  None, # subtitle_output
576
+ "", # output_message
577
+ new_session_id # Update session_id_state
578
  )
579
  except Exception as e:
580
  logger.exception("Error in reset handler")
581
+ # Keep the old session ID on failure? Or generate new? Let's keep old for now.
582
  return (
583
  f"Reset failed: {str(e)}",
584
+ None, None, None, None, "", gr.State() # No change to session ID state
585
  )
586
 
587
  # Replace multiple button rows with a single row containing both buttons
 
592
  # Connect the refresh button to check status
593
  refresh_btn.click(
594
  fn=check_status,
595
+ inputs=[session_id_state], # Use session ID from state
596
  outputs=[status_text]
597
  )
598
 
 
602
  inputs=[],
603
  outputs=[
604
  status_text,
605
+ url_input, # Clear URL input
606
+ upload_input, # Clear Upload input
607
  output,
608
  subtitle_output,
609
+ output_message,
610
+ session_id_state # Update the session ID state
611
  ]
612
  )
613
 
614
+ # Create a simple auto-refresh component using JavaScript
615
  gr.HTML("""
616
  <script>
617
  // Simple poller to update status