r3gm commited on
Commit
1045868
·
verified ·
1 Parent(s): 643b678

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +92 -38
app.py CHANGED
@@ -1,4 +1,5 @@
1
  import os
 
2
  import gradio as gr
3
  import spaces
4
  from infer_rvc_python import BaseLoader
@@ -26,6 +27,57 @@ import sys
26
  import torch
27
  import fairseq
28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  parser = argparse.ArgumentParser(description="Run the app with optional sharing")
30
  parser.add_argument(
31
  '--share',
@@ -154,10 +206,10 @@ def find_my_model(a_, b_):
154
 
155
  for ff in end_files:
156
  if ff.endswith(".pth"):
157
- model = os.path.join(directory, ff)
158
  gr.Info(f"Model found: {ff}")
159
  if ff.endswith(".index"):
160
- index = os.path.join(directory, ff)
161
  gr.Info(f"Index found: {ff}")
162
 
163
  if not model:
@@ -368,6 +420,7 @@ def run(
368
  audio_effects,
369
  type_output,
370
  steps,
 
371
  ):
372
  if not audio_files:
373
  raise ValueError("The audio pls")
@@ -381,7 +434,7 @@ def run(
381
  except Exception as e:
382
  print(e)
383
 
384
- if file_m is not None and file_m.endswith(".txt"):
385
  file_m, file_index = find_my_model(file_m, file_index)
386
  print(file_m, file_index)
387
 
@@ -409,7 +462,9 @@ def run(
409
  if audio_effects:
410
  result = add_audio_effects(result, type_output)
411
 
412
- return result
 
 
413
 
414
 
415
  def audio_conf():
@@ -418,6 +473,7 @@ def audio_conf():
418
  file_count="multiple",
419
  type="filepath",
420
  container=True,
 
421
  )
422
 
423
 
@@ -426,6 +482,7 @@ def model_conf():
426
  label="Model file",
427
  type="filepath",
428
  height=130,
 
429
  )
430
 
431
 
@@ -456,6 +513,7 @@ def index_conf():
456
  label="Index file",
457
  type="filepath",
458
  height=130,
 
459
  )
460
 
461
 
@@ -550,13 +608,12 @@ def tts_button_conf():
550
  )
551
 
552
 
553
- def tts_play_conf():
554
  return gr.Checkbox(
555
  False,
556
- label="Play",
557
- # info="",
558
  container=False,
559
- visible=False,
560
  )
561
 
562
 
@@ -564,11 +621,10 @@ def sound_gui():
564
  return gr.Audio(
565
  value=None,
566
  type="filepath",
567
- # format="mp3",
568
  autoplay=True,
569
- visible=True,
570
  interactive=False,
571
- elem_id="audio_tts",
572
  )
573
 
574
 
@@ -610,7 +666,7 @@ def effects_conf():
610
  )
611
 
612
 
613
- def infer_tts_audio(tts_voice, tts_text, play_tts):
614
  out_dir = "output"
615
  folder_tts = "USER_"+str(random.randint(10000, 99999))
616
 
@@ -619,9 +675,8 @@ def infer_tts_audio(tts_voice, tts_text, play_tts):
619
  out_path = os.path.join(out_dir, folder_tts, "tts.mp3")
620
 
621
  asyncio.run(edge_tts.Communicate(tts_text, "-".join(tts_voice.split('-')[:-1])).save(out_path))
622
- if play_tts:
623
- return [out_path], out_path
624
- return [out_path], None
625
 
626
 
627
  def show_components_tts(value_active):
@@ -631,8 +686,6 @@ def show_components_tts(value_active):
631
  visible=value_active
632
  ), gr.update(
633
  visible=value_active
634
- ), gr.update(
635
- visible=value_active
636
  )
637
 
638
 
@@ -672,15 +725,7 @@ def show_components_down(value_active):
672
  visible=value_active
673
  )
674
 
675
- CSS = """
676
- #audio_tts {
677
- visibility: hidden; /* invisible but still takes space */
678
- height: 0px;
679
- width: 0px;
680
- max-width: 0px;
681
- max-height: 0px;
682
- }
683
- """
684
 
685
  def get_gui(theme):
686
  with gr.Blocks(theme=theme, css=CSS, fill_width=True, fill_height=False, delete_cache=delete_cache_time) as app:
@@ -696,26 +741,18 @@ def get_gui(theme):
696
  with gr.Column():
697
  with gr.Row():
698
  tts_voice = tts_voice_conf()
699
- tts_active_play = tts_play_conf()
700
 
701
  tts_button = tts_button_conf()
702
- tts_play = sound_gui()
703
 
704
  active_tts.change(
705
  fn=show_components_tts,
706
  inputs=[active_tts],
707
- outputs=[tts_voice, tts_text, tts_button, tts_active_play],
708
  )
709
 
710
  aud = audio_conf()
711
  # gr.HTML("<hr>")
712
 
713
- tts_button.click(
714
- fn=infer_tts_audio,
715
- inputs=[tts_voice, tts_text, tts_active_play],
716
- outputs=[aud, tts_play],
717
- )
718
-
719
  down_active_gui = down_active_conf()
720
  down_info = gr.Markdown(
721
  f"Provide a link to a zip file, like this one: `https://huggingface.co/MrDawg/ToothBrushing/resolve/main/ToothBrushing.zip?download=true`, or separate links with a comma for the .pth and .index files, like this: `{test_model}`",
@@ -758,6 +795,22 @@ def get_gui(theme):
758
  with gr.Row():
759
  denoise_gui = denoise_conf()
760
  effects_gui = effects_conf()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
761
  button_base = button_conf()
762
  output_base = output_conf()
763
 
@@ -777,8 +830,9 @@ def get_gui(theme):
777
  effects_gui,
778
  format_out,
779
  steps_gui,
 
780
  ],
781
- outputs=[output_base],
782
  )
783
 
784
  gr.Examples(
@@ -830,7 +884,7 @@ def get_gui(theme):
830
  envel_r,
831
  const,
832
  ],
833
- outputs=[output_base],
834
  cache_examples=False,
835
  )
836
  gr.Markdown(RESOURCES)
@@ -856,4 +910,4 @@ if __name__ == "__main__":
856
  quiet=False,
857
  debug=IS_COLAB,
858
  ssr_mode=False,
859
- )
 
1
  import os
2
+ import subprocess
3
  import gradio as gr
4
  import spaces
5
  from infer_rvc_python import BaseLoader
 
27
  import torch
28
  import fairseq
29
 
30
+
31
+ def get_supported_audio_video_extensions():
32
+ try:
33
+ subtitle_codecs = set()
34
+ decoders_res = subprocess.run(
35
+ ["ffmpeg", "-decoders"], capture_output=True, text=True, check=True
36
+ )
37
+ for line in decoders_res.stdout.splitlines():
38
+ line_str = line.strip()
39
+ if len(line_str) > 7 and line_str[0] in ("V", "A", "S"):
40
+ parts = line_str.split()
41
+ if len(parts) >= 2:
42
+ media_type = line_str[0]
43
+ codec_name = parts[1].lower()
44
+ if media_type == "S":
45
+ subtitle_codecs.add(codec_name)
46
+
47
+ demuxers_res = subprocess.run(
48
+ ["ffmpeg", "-demuxers"], capture_output=True, text=True, check=True
49
+ )
50
+ extensions = set()
51
+ for line in demuxers_res.stdout.splitlines():
52
+ line_str = line.strip()
53
+ if line_str.startswith("D"):
54
+ parts = line_str.split(maxsplit=2)
55
+ if len(parts) >= 2:
56
+ demux_names = parts[1].split(",")
57
+ description = parts[2].lower() if len(parts) >= 3 else ""
58
+
59
+ if any(
60
+ sub_kw in description
61
+ for sub_kw in ["subtitle", "caption", "teletext", "lyrics"]
62
+ ):
63
+ continue
64
+
65
+ for ext in demux_names:
66
+ clean_ext = ext.strip().lower()
67
+ if clean_ext in subtitle_codecs:
68
+ continue
69
+ if clean_ext and clean_ext.isalnum():
70
+ extensions.add(f".{clean_ext}")
71
+
72
+ return sorted(list(extensions))
73
+ except Exception as e:
74
+ print(f"Error querying ffmpeg: {e}")
75
+ return [".mp3", ".wav", ".flac", ".m4a", ".aac", ".ogg", ".opus", ".wma", ".aiff", ".aif", ".alac", ".caf", ".amr"]
76
+
77
+
78
+ supported_extensions = get_supported_audio_video_extensions()
79
+ print("Supported extensions found:", supported_extensions)
80
+
81
  parser = argparse.ArgumentParser(description="Run the app with optional sharing")
82
  parser.add_argument(
83
  '--share',
 
206
 
207
  for ff in end_files:
208
  if ff.endswith(".pth"):
209
+ model = ff
210
  gr.Info(f"Model found: {ff}")
211
  if ff.endswith(".index"):
212
+ index = ff
213
  gr.Info(f"Index found: {ff}")
214
 
215
  if not model:
 
420
  audio_effects,
421
  type_output,
422
  steps,
423
+ play_audio=False,
424
  ):
425
  if not audio_files:
426
  raise ValueError("The audio pls")
 
434
  except Exception as e:
435
  print(e)
436
 
437
+ if file_m is not None and (file_m.endswith(".txt") or file_m.endswith(".zip")):
438
  file_m, file_index = find_my_model(file_m, file_index)
439
  print(file_m, file_index)
440
 
 
462
  if audio_effects:
463
  result = add_audio_effects(result, type_output)
464
 
465
+ player_audio = result[0] if (play_audio and result and len(result) > 0) else None
466
+
467
+ return result, player_audio
468
 
469
 
470
  def audio_conf():
 
473
  file_count="multiple",
474
  type="filepath",
475
  container=True,
476
+ file_types=supported_extensions,
477
  )
478
 
479
 
 
482
  label="Model file",
483
  type="filepath",
484
  height=130,
485
+ file_types=[".txt", ".pth", ".zip"],
486
  )
487
 
488
 
 
513
  label="Index file",
514
  type="filepath",
515
  height=130,
516
+ file_types=[".index", ".txt"],
517
  )
518
 
519
 
 
608
  )
609
 
610
 
611
+ def player_conf():
612
  return gr.Checkbox(
613
  False,
614
+ label="Audio Player",
 
615
  container=False,
616
+ visible=True,
617
  )
618
 
619
 
 
621
  return gr.Audio(
622
  value=None,
623
  type="filepath",
 
624
  autoplay=True,
625
+ visible=False,
626
  interactive=False,
627
+ label="Audio Player",
628
  )
629
 
630
 
 
666
  )
667
 
668
 
669
+ def infer_tts_audio(tts_voice, tts_text, play_audio):
670
  out_dir = "output"
671
  folder_tts = "USER_"+str(random.randint(10000, 99999))
672
 
 
675
  out_path = os.path.join(out_dir, folder_tts, "tts.mp3")
676
 
677
  asyncio.run(edge_tts.Communicate(tts_text, "-".join(tts_voice.split('-')[:-1])).save(out_path))
678
+ player_audio = out_path if play_audio else None
679
+ return [out_path], player_audio
 
680
 
681
 
682
  def show_components_tts(value_active):
 
686
  visible=value_active
687
  ), gr.update(
688
  visible=value_active
 
 
689
  )
690
 
691
 
 
725
  visible=value_active
726
  )
727
 
728
+ CSS = ""
 
 
 
 
 
 
 
 
729
 
730
  def get_gui(theme):
731
  with gr.Blocks(theme=theme, css=CSS, fill_width=True, fill_height=False, delete_cache=delete_cache_time) as app:
 
741
  with gr.Column():
742
  with gr.Row():
743
  tts_voice = tts_voice_conf()
 
744
 
745
  tts_button = tts_button_conf()
 
746
 
747
  active_tts.change(
748
  fn=show_components_tts,
749
  inputs=[active_tts],
750
+ outputs=[tts_voice, tts_text, tts_button],
751
  )
752
 
753
  aud = audio_conf()
754
  # gr.HTML("<hr>")
755
 
 
 
 
 
 
 
756
  down_active_gui = down_active_conf()
757
  down_info = gr.Markdown(
758
  f"Provide a link to a zip file, like this one: `https://huggingface.co/MrDawg/ToothBrushing/resolve/main/ToothBrushing.zip?download=true`, or separate links with a comma for the .pth and .index files, like this: `{test_model}`",
 
795
  with gr.Row():
796
  denoise_gui = denoise_conf()
797
  effects_gui = effects_conf()
798
+ player_gui = player_conf()
799
+
800
+ player_audio = sound_gui()
801
+
802
+ player_gui.change(
803
+ fn=lambda val: gr.update(visible=val),
804
+ inputs=[player_gui],
805
+ outputs=[player_audio],
806
+ )
807
+
808
+ tts_button.click(
809
+ fn=infer_tts_audio,
810
+ inputs=[tts_voice, tts_text, player_gui],
811
+ outputs=[aud, player_audio],
812
+ )
813
+
814
  button_base = button_conf()
815
  output_base = output_conf()
816
 
 
830
  effects_gui,
831
  format_out,
832
  steps_gui,
833
+ player_gui,
834
  ],
835
+ outputs=[output_base, player_audio],
836
  )
837
 
838
  gr.Examples(
 
884
  envel_r,
885
  const,
886
  ],
887
+ outputs=[output_base, player_audio],
888
  cache_examples=False,
889
  )
890
  gr.Markdown(RESOURCES)
 
910
  quiet=False,
911
  debug=IS_COLAB,
912
  ssr_mode=False,
913
+ )