hivecorp commited on
Commit
fa07cc9
·
verified ·
1 Parent(s): 341cd2f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -19
app.py CHANGED
@@ -20,7 +20,7 @@ class TimingManager:
20
  def get_timing(self, duration):
21
  start_time = self.current_time
22
  end_time = start_time + duration
23
- self.current_time = end_time + self.segment_gap
24
  return start_time, end_time
25
 
26
  def get_audio_length(audio_file):
@@ -430,7 +430,14 @@ async def process_text_with_progress(
430
  ):
431
  # Input validation
432
  if not text or text.strip() == "":
433
- return None, None, None, gr.update(value="", visible=True), gr.update(value="", visible=False), "Please enter some text to convert to speech."
 
 
 
 
 
 
 
434
 
435
  # Format pitch and rate strings
436
  pitch_str = f"{pitch:+d}Hz" if pitch != 0 else "+0Hz"
@@ -455,24 +462,32 @@ async def process_text_with_progress(
455
  )
456
 
457
  # Generate Markdown links for download that open in a new tab
458
- srt_download_link = f'<a href="file={srt_path}" download="subtitles.srt" target="_blank">Download SRT</a>'
459
- audio_download_link = f'<a href="file={audio_path}" download="audio.mp3" target="_blank">Download Audio</a>'
460
 
461
- # Return the paths for gr.Audio and Markdown for download links
462
  return (
463
- audio_path,
464
- gr.update(value=srt_download_link, visible=True), # Use gr.Markdown for SRT download
465
- gr.update(value=audio_download_link, visible=True), # Use gr.Markdown for Audio download
466
- gr.update(value="", visible=False), # Hide error message
467
- "" # Clear error message
468
  )
469
  except TTSError as e:
470
  # Return specific TTS error
471
- return None, gr.update(value="", visible=False), gr.update(value="", visible=False), gr.update(value=f"TTS Error: {str(e)}", visible=True), f"TTS Error: {str(e)}"
 
 
 
 
 
472
  except Exception as e:
473
  # Return any other error
474
- return None, gr.update(value="", visible=False), gr.update(value="", visible=False), gr.update(value=f"Unexpected error: {str(e)}", visible=True), f"Unexpected error: {str(e)}"
475
-
 
 
 
 
476
 
477
  # Voice options dictionary
478
  voice_options = {
@@ -579,14 +594,17 @@ with gr.Blocks(title="Advanced TTS with Configurable SRT Generation") as app:
579
 
580
  submit_btn = gr.Button("Generate Audio & Subtitles")
581
 
582
- # Add error message component
583
- error_output = gr.Textbox(label="Status", visible=False)
 
 
584
 
585
  with gr.Row():
586
  with gr.Column():
587
  audio_output = gr.Audio(label="Preview Audio")
588
  with gr.Column():
589
  # Change gr.File to gr.Markdown for download links
 
590
  srt_download_link = gr.Markdown(value="", visible=False, label="Download SRT")
591
  audio_download_link = gr.Markdown(value="", visible=False, label="Download Audio")
592
 
@@ -604,10 +622,9 @@ with gr.Blocks(title="Advanced TTS with Configurable SRT Generation") as app:
604
  ],
605
  outputs=[
606
  audio_output,
607
- srt_download_link, # Output to Markdown component
608
- audio_download_link, # Output to Markdown component
609
- error_output,
610
- error_output
611
  ],
612
  api_name="generate"
613
  )
 
20
  def get_timing(self, duration):
21
  start_time = self.current_time
22
  end_time = start_time + duration
23
+ self.current_time = end_time + duration + self.segment_gap
24
  return start_time, end_time
25
 
26
  def get_audio_length(audio_file):
 
430
  ):
431
  # Input validation
432
  if not text or text.strip() == "":
433
+ # For error case, return empty strings for markdown links and None for audio,
434
+ # along with visibility updates for error_output
435
+ return (
436
+ None, # audio_output
437
+ gr.update(value="", visible=False), # srt_download_link
438
+ gr.update(value="", visible=False), # audio_download_link
439
+ gr.update(value="Please enter some text to convert to speech.", visible=True) # error_output
440
+ )
441
 
442
  # Format pitch and rate strings
443
  pitch_str = f"{pitch:+d}Hz" if pitch != 0 else "+0Hz"
 
462
  )
463
 
464
  # Generate Markdown links for download that open in a new tab
465
+ srt_download_link_html = f'<a href="file={srt_path}" download="subtitles.srt" target="_blank">Download SRT</a>'
466
+ audio_download_link_html = f'<a href="file={audio_path}" download="audio.mp3" target="_blank">Download Audio</a>'
467
 
468
+ # If successful, return results and hide error
469
  return (
470
+ audio_path, # gr.Audio expects a file path
471
+ gr.update(value=srt_download_link_html, visible=True), # gr.Markdown for SRT download
472
+ gr.update(value=audio_download_link_html, visible=True), # gr.Markdown for Audio download
473
+ gr.update(value="", visible=False) # Hide error message
 
474
  )
475
  except TTSError as e:
476
  # Return specific TTS error
477
+ return (
478
+ None, # audio_output should be None on error
479
+ gr.update(value="", visible=False), # Hide SRT download link
480
+ gr.update(value="", visible=False), # Hide Audio download link
481
+ gr.update(value=f"TTS Error: {str(e)}", visible=True) # Show error message
482
+ )
483
  except Exception as e:
484
  # Return any other error
485
+ return (
486
+ None, # audio_output should be None on error
487
+ gr.update(value="", visible=False), # Hide SRT download link
488
+ gr.update(value="", visible=False), # Hide Audio download link
489
+ gr.update(value=f"Unexpected error: {str(e)}", visible=True) # Show error message
490
+ )
491
 
492
  # Voice options dictionary
493
  voice_options = {
 
594
 
595
  submit_btn = gr.Button("Generate Audio & Subtitles")
596
 
597
+ # Add error message component - it should be visible initially
598
+ # or its visibility handled in the return of the function
599
+ error_output = gr.Textbox(label="Status", visible=True) # Changed to visible=True for easier debugging.
600
+ # You can set it to False if you want it hidden by default.
601
 
602
  with gr.Row():
603
  with gr.Column():
604
  audio_output = gr.Audio(label="Preview Audio")
605
  with gr.Column():
606
  # Change gr.File to gr.Markdown for download links
607
+ # Set initial values to empty string and visible=False to hide them on load
608
  srt_download_link = gr.Markdown(value="", visible=False, label="Download SRT")
609
  audio_download_link = gr.Markdown(value="", visible=False, label="Download Audio")
610
 
 
622
  ],
623
  outputs=[
624
  audio_output,
625
+ srt_download_link,
626
+ audio_download_link,
627
+ error_output # Only one error_output here, as it's a single component
 
628
  ],
629
  api_name="generate"
630
  )