Singhp08 commited on
Commit
0ef5b3e
·
verified ·
1 Parent(s): 135441a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -180
app.py CHANGED
@@ -102,11 +102,8 @@ async def get_voices_list(proxy=None):
102
  def find_files(directory):
103
  file_paths = []
104
  for filename in os.listdir(directory):
105
- # Check if the file has the desired extension
106
  if filename.endswith('.pth') or filename.endswith('.zip') or filename.endswith('.index'):
107
- # If yes, add the file path to the list
108
  file_paths.append(os.path.join(directory, filename))
109
-
110
  return file_paths
111
 
112
 
@@ -120,7 +117,6 @@ def unzip_in_folder(my_zip, my_dir):
120
 
121
 
122
  def find_my_model(a_, b_):
123
-
124
  if a_ is None or a_.endswith(".pth"):
125
  return a_, b_
126
 
@@ -179,7 +175,6 @@ def ensure_valid_file(url):
179
  raise ValueError("No Content-Length header found")
180
 
181
  file_size = int(content_length)
182
- # print("debug", url, file_size)
183
  if file_size > 900000000 and IS_ZERO_GPU:
184
  raise ValueError("The file is too large. Max allowed is 900 MB.")
185
 
@@ -196,7 +191,6 @@ def clear_files(directory):
196
 
197
 
198
  def get_my_model(url_data, progress=gr.Progress(track_tqdm=True)):
199
-
200
  if not url_data:
201
  return None, None
202
 
@@ -250,12 +244,50 @@ def get_my_model(url_data, progress=gr.Progress(track_tqdm=True)):
250
  except Exception as e:
251
  raise e
252
  finally:
253
- # time.sleep(10)
254
- # shutil.rmtree(directory)
255
  t = threading.Thread(target=clear_files, args=(directory,))
256
  t.start()
257
 
258
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
259
  def add_audio_effects(audio_list, type_output):
260
  print("Audio effects")
261
 
@@ -264,7 +296,6 @@ def add_audio_effects(audio_list, type_output):
264
  try:
265
  output_path = f'{os.path.splitext(audio_path)[0]}_effects.{type_output}'
266
 
267
- # Initialize audio effects plugins
268
  board = Pedalboard(
269
  [
270
  HighpassFilter(),
@@ -273,7 +304,6 @@ def add_audio_effects(audio_list, type_output):
273
  ]
274
  )
275
 
276
- # Temporary WAV to hold processed data before exporting
277
  temp_wav = f'{os.path.splitext(audio_path)[0]}_temp.wav'
278
 
279
  with AudioFile(audio_path) as f:
@@ -283,11 +313,9 @@ def add_audio_effects(audio_list, type_output):
283
  effected = board(chunk, f.samplerate, reset=False)
284
  o.write(effected)
285
 
286
- # Convert with pydub to desired output type
287
  audio_seg = AudioSegment.from_file(temp_wav, format=type_output)
288
  audio_seg.export(output_path, format=type_output, bitrate=("320k" if type_output == "mp3" else None))
289
 
290
- # Clean up temp file
291
  os.remove(temp_wav)
292
 
293
  result.append(output_path)
@@ -300,7 +328,6 @@ def add_audio_effects(audio_list, type_output):
300
 
301
 
302
  def apply_noisereduce(audio_list, type_output):
303
- # https://github.com/sa-if/Audio-Denoiser
304
  print("Noice reduce")
305
 
306
  result = []
@@ -308,16 +335,12 @@ def apply_noisereduce(audio_list, type_output):
308
  out_path = f"{os.path.splitext(audio_path)[0]}_noisereduce.{type_output}"
309
 
310
  try:
311
- # Load audio file
312
  audio = AudioSegment.from_file(audio_path)
313
 
314
- # Convert audio to numpy array
315
  samples = np.array(audio.get_array_of_samples())
316
 
317
- # Reduce noise
318
  reduced_noise = nr.reduce_noise(samples, sr=audio.frame_rate, prop_decrease=0.6)
319
 
320
- # Convert reduced noise signal back to audio
321
  reduced_audio = AudioSegment(
322
  reduced_noise.tobytes(),
323
  frame_rate=audio.frame_rate,
@@ -325,7 +348,6 @@ def apply_noisereduce(audio_list, type_output):
325
  channels=audio.channels
326
  )
327
 
328
- # Save reduced audio to file
329
  reduced_audio.export(out_path, format=type_output, bitrate=("320k" if type_output == "mp3" else None))
330
  result.append(out_path)
331
 
@@ -353,10 +375,10 @@ def convert_now(audio_files, random_tag, converter, type_output, steps):
353
 
354
  def run(
355
  audio_files,
356
- file_m,
357
  pitch_alg,
358
  pitch_lvl,
359
- file_index,
360
  index_inf,
361
  r_m_f,
362
  e_r,
@@ -378,6 +400,7 @@ def run(
378
  except Exception as e:
379
  print(e)
380
 
 
381
  if file_m is not None and file_m.endswith(".txt"):
382
  file_m, file_index = find_my_model(file_m, file_index)
383
  print(file_m, file_index)
@@ -409,6 +432,8 @@ def run(
409
  return result
410
 
411
 
 
 
412
  def audio_conf():
413
  return gr.File(
414
  label="Audio files",
@@ -418,14 +443,26 @@ def audio_conf():
418
  )
419
 
420
 
421
- def model_conf():
422
- return gr.File(
423
- label="Model file",
424
- type="filepath",
425
- height=130,
 
 
 
 
426
  )
427
 
428
 
 
 
 
 
 
 
 
 
429
  def pitch_algo_conf():
430
  return gr.Dropdown(
431
  PITCH_ALGO_OPT,
@@ -448,14 +485,6 @@ def pitch_lvl_conf():
448
  )
449
 
450
 
451
- def index_conf():
452
- return gr.File(
453
- label="Index file",
454
- type="filepath",
455
- height=130,
456
- )
457
-
458
-
459
  def index_inf_conf():
460
  return gr.Slider(
461
  minimum=0,
@@ -515,7 +544,6 @@ def active_tts_conf():
515
  return gr.Checkbox(
516
  False,
517
  label="TTS",
518
- # info="",
519
  container=False,
520
  )
521
 
@@ -551,7 +579,6 @@ def tts_play_conf():
551
  return gr.Checkbox(
552
  False,
553
  label="Play",
554
- # info="",
555
  container=False,
556
  visible=False,
557
  )
@@ -561,7 +588,6 @@ def sound_gui():
561
  return gr.Audio(
562
  value=None,
563
  type="filepath",
564
- # format="mp3",
565
  autoplay=True,
566
  visible=True,
567
  interactive=False,
@@ -591,7 +617,6 @@ def denoise_conf():
591
  return gr.Checkbox(
592
  False,
593
  label="Denoise",
594
- # info="",
595
  container=False,
596
  visible=True,
597
  )
@@ -601,7 +626,6 @@ def effects_conf():
601
  return gr.Checkbox(
602
  False,
603
  label="Reverb",
604
- # info="",
605
  container=False,
606
  visible=True,
607
  )
@@ -637,7 +661,6 @@ def down_active_conf():
637
  return gr.Checkbox(
638
  False,
639
  label="URL-to-Model",
640
- # info="",
641
  container=False,
642
  )
643
 
@@ -669,9 +692,10 @@ def show_components_down(value_active):
669
  visible=value_active
670
  )
671
 
 
672
  CSS = """
673
  #audio_tts {
674
- visibility: hidden; /* invisible but still takes space */
675
  height: 0px;
676
  width: 0px;
677
  max-width: 0px;
@@ -684,6 +708,7 @@ def get_gui(theme):
684
  gr.Markdown(title)
685
  gr.Markdown(description)
686
 
 
687
  active_tts = active_tts_conf()
688
  with gr.Row():
689
  with gr.Column(scale=1):
@@ -705,7 +730,6 @@ def get_gui(theme):
705
  )
706
 
707
  aud = audio_conf()
708
- # gr.HTML("<hr>")
709
 
710
  tts_button.click(
711
  fn=infer_tts_audio,
@@ -713,144 +737,7 @@ def get_gui(theme):
713
  outputs=[aud, tts_play],
714
  )
715
 
 
716
  down_active_gui = down_active_conf()
717
  down_info = gr.Markdown(
718
- 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}`",
719
- visible=False
720
- )
721
- with gr.Row():
722
- with gr.Column(scale=3):
723
- down_url_gui = down_url_conf()
724
- with gr.Column(scale=1):
725
- down_button_gui = down_button_conf()
726
-
727
- with gr.Column():
728
- with gr.Row():
729
- model = model_conf()
730
- indx = index_conf()
731
-
732
- down_active_gui.change(
733
- show_components_down,
734
- [down_active_gui],
735
- [down_info, down_url_gui, down_button_gui]
736
- )
737
-
738
- down_button_gui.click(
739
- get_my_model,
740
- [down_url_gui],
741
- [model, indx]
742
- )
743
-
744
- with gr.Accordion(label="Advanced settings", open=False):
745
- algo = pitch_algo_conf()
746
- algo_lvl = pitch_lvl_conf()
747
- indx_inf = index_inf_conf()
748
- res_fc = respiration_filter_conf()
749
- envel_r = envelope_ratio_conf()
750
- const = consonant_protec_conf()
751
- steps_gui = steps_conf()
752
- format_out = format_output_gui()
753
- with gr.Row():
754
- with gr.Column():
755
- with gr.Row():
756
- denoise_gui = denoise_conf()
757
- effects_gui = effects_conf()
758
- button_base = button_conf()
759
- output_base = output_conf()
760
-
761
- button_base.click(
762
- run,
763
- inputs=[
764
- aud,
765
- model,
766
- algo,
767
- algo_lvl,
768
- indx,
769
- indx_inf,
770
- res_fc,
771
- envel_r,
772
- const,
773
- denoise_gui,
774
- effects_gui,
775
- format_out,
776
- steps_gui,
777
- ],
778
- outputs=[output_base],
779
- )
780
-
781
- gr.Examples(
782
- examples=[
783
- [
784
- ["./test.ogg"],
785
- "./model.pth",
786
- "rmvpe+",
787
- 0,
788
- "./model.index",
789
- 0.75,
790
- 3,
791
- 0.25,
792
- 0.50,
793
- ],
794
- [
795
- ["./example2/test2.ogg"],
796
- "./example2/model_link.txt",
797
- "rmvpe+",
798
- 0,
799
- "./example2/index_link.txt",
800
- 0.75,
801
- 3,
802
- 0.25,
803
- 0.50,
804
- ],
805
- [
806
- ["./example3/test3.wav"],
807
- "./example3/zip_link.txt",
808
- "rmvpe+",
809
- 0,
810
- None,
811
- 0.75,
812
- 3,
813
- 0.25,
814
- 0.50,
815
- ],
816
-
817
- ],
818
- fn=run,
819
- inputs=[
820
- aud,
821
- model,
822
- algo,
823
- algo_lvl,
824
- indx,
825
- indx_inf,
826
- res_fc,
827
- envel_r,
828
- const,
829
- ],
830
- outputs=[output_base],
831
- cache_examples=False,
832
- )
833
- gr.Markdown(RESOURCES)
834
-
835
- return app
836
-
837
-
838
- if __name__ == "__main__":
839
- tts_voice_list = asyncio.new_event_loop().run_until_complete(get_voices_list(proxy=None))
840
- voices = sorted([
841
- (" - ".join(reversed(v["FriendlyName"].split("-"))).replace("Microsoft ", "").replace("Online (Natural)", f"({v['Gender']})").strip(), f"{v['ShortName']}-{v['Gender']}")
842
- for v in tts_voice_list
843
- ])
844
-
845
- app = get_gui(theme)
846
-
847
- app.queue(default_concurrency_limit=40)
848
-
849
- app.launch(
850
- max_threads=40,
851
- share=IS_COLAB,
852
- show_error=True,
853
- quiet=False,
854
- debug=IS_COLAB,
855
- ssr_mode=False,
856
- )
 
102
  def find_files(directory):
103
  file_paths = []
104
  for filename in os.listdir(directory):
 
105
  if filename.endswith('.pth') or filename.endswith('.zip') or filename.endswith('.index'):
 
106
  file_paths.append(os.path.join(directory, filename))
 
107
  return file_paths
108
 
109
 
 
117
 
118
 
119
  def find_my_model(a_, b_):
 
120
  if a_ is None or a_.endswith(".pth"):
121
  return a_, b_
122
 
 
175
  raise ValueError("No Content-Length header found")
176
 
177
  file_size = int(content_length)
 
178
  if file_size > 900000000 and IS_ZERO_GPU:
179
  raise ValueError("The file is too large. Max allowed is 900 MB.")
180
 
 
191
 
192
 
193
  def get_my_model(url_data, progress=gr.Progress(track_tqdm=True)):
 
194
  if not url_data:
195
  return None, None
196
 
 
244
  except Exception as e:
245
  raise e
246
  finally:
 
 
247
  t = threading.Thread(target=clear_files, args=(directory,))
248
  t.start()
249
 
250
 
251
+ # ========== नया फ़ंक्शन: logs फोल्डर से मॉडल्स की सूची बनाना ==========
252
+ def scan_models():
253
+ """logs/ फोल्डर के अंदर मौजूद हर सबफोल्डर को एक मॉडल मानकर उसकी .pth और .index फाइल ढूंढें"""
254
+ logs_dir = "logs"
255
+ if not os.path.isdir(logs_dir):
256
+ return []
257
+
258
+ models = []
259
+ for model_name in os.listdir(logs_dir):
260
+ model_path = os.path.join(logs_dir, model_name)
261
+ if not os.path.isdir(model_path):
262
+ continue
263
+ # सबफोल्डर में .pth और .index फाइल देखें (नाम फोल्डर जैसा होना चाहिए)
264
+ pth_file = os.path.join(model_path, f"{model_name}.pth")
265
+ index_file = os.path.join(model_path, f"{model_name}.index")
266
+ # अगर exact नाम न मिले तो कोई भी .pth और .index उठा लें (fallback)
267
+ if not os.path.isfile(pth_file) or not os.path.isfile(index_file):
268
+ # Fallback: फोल्डर में मौजूद पहली .pth और .index फाइल लें
269
+ pth_candidates = [f for f in os.listdir(model_path) if f.endswith(".pth")]
270
+ index_candidates = [f for f in os.listdir(model_path) if f.endswith(".index")]
271
+ if pth_candidates and index_candidates:
272
+ pth_file = os.path.join(model_path, pth_candidates[0])
273
+ index_file = os.path.join(model_path, index_candidates[0])
274
+ else:
275
+ continue # इस फोल्डर में जरूरी फाइलें नहीं हैं
276
+ models.append((model_name, pth_file, index_file))
277
+ return models
278
+
279
+
280
+ def update_model_selection(model_name):
281
+ """ड्रॉपडाउन से चुने गए मॉडल के अनुसार pth और index का पथ लौटाएँ"""
282
+ models = scan_models()
283
+ for name, pth, idx in models:
284
+ if name == model_name:
285
+ return pth, idx
286
+ return None, None
287
+
288
+
289
+ # ========== ऑडियो इफेक्ट और कन्वर्जन फंक्शन (कोई बदलाव नहीं) ==========
290
+
291
  def add_audio_effects(audio_list, type_output):
292
  print("Audio effects")
293
 
 
296
  try:
297
  output_path = f'{os.path.splitext(audio_path)[0]}_effects.{type_output}'
298
 
 
299
  board = Pedalboard(
300
  [
301
  HighpassFilter(),
 
304
  ]
305
  )
306
 
 
307
  temp_wav = f'{os.path.splitext(audio_path)[0]}_temp.wav'
308
 
309
  with AudioFile(audio_path) as f:
 
313
  effected = board(chunk, f.samplerate, reset=False)
314
  o.write(effected)
315
 
 
316
  audio_seg = AudioSegment.from_file(temp_wav, format=type_output)
317
  audio_seg.export(output_path, format=type_output, bitrate=("320k" if type_output == "mp3" else None))
318
 
 
319
  os.remove(temp_wav)
320
 
321
  result.append(output_path)
 
328
 
329
 
330
  def apply_noisereduce(audio_list, type_output):
 
331
  print("Noice reduce")
332
 
333
  result = []
 
335
  out_path = f"{os.path.splitext(audio_path)[0]}_noisereduce.{type_output}"
336
 
337
  try:
 
338
  audio = AudioSegment.from_file(audio_path)
339
 
 
340
  samples = np.array(audio.get_array_of_samples())
341
 
 
342
  reduced_noise = nr.reduce_noise(samples, sr=audio.frame_rate, prop_decrease=0.6)
343
 
 
344
  reduced_audio = AudioSegment(
345
  reduced_noise.tobytes(),
346
  frame_rate=audio.frame_rate,
 
348
  channels=audio.channels
349
  )
350
 
 
351
  reduced_audio.export(out_path, format=type_output, bitrate=("320k" if type_output == "mp3" else None))
352
  result.append(out_path)
353
 
 
375
 
376
  def run(
377
  audio_files,
378
+ file_m, # अब यह hidden टेक्स्टबॉक्स से आएगा (pth का पथ)
379
  pitch_alg,
380
  pitch_lvl,
381
+ file_index, # hidden टेक्स्टबॉक्स से index का पथ
382
  index_inf,
383
  r_m_f,
384
  e_r,
 
400
  except Exception as e:
401
  print(e)
402
 
403
+ # अगर file_m टेक्स्ट फाइल है तो पुराने लॉजिक से हैंडल करें
404
  if file_m is not None and file_m.endswith(".txt"):
405
  file_m, file_index = find_my_model(file_m, file_index)
406
  print(file_m, file_index)
 
432
  return result
433
 
434
 
435
+ # ========== UI कॉन्फ़िगरेशन (बदलाव सहित) ==========
436
+
437
  def audio_conf():
438
  return gr.File(
439
  label="Audio files",
 
443
  )
444
 
445
 
446
+ def model_dropdown_conf():
447
+ """मॉडल चुनने के लिए ड्रॉपडाउन बनाएँ"""
448
+ models = scan_models()
449
+ choices = [name for name, _, _ in models]
450
+ return gr.Dropdown(
451
+ choices=choices,
452
+ value=choices[0] if choices else None,
453
+ label="Select Model",
454
+ interactive=True,
455
  )
456
 
457
 
458
+ # ये दो छुपे हुए टेक्स्टबॉक्स हैं जिनमें असली फाइल पथ जाएँगे
459
+ def hidden_model_path_conf():
460
+ return gr.Textbox(visible=False)
461
+
462
+ def hidden_index_path_conf():
463
+ return gr.Textbox(visible=False)
464
+
465
+
466
  def pitch_algo_conf():
467
  return gr.Dropdown(
468
  PITCH_ALGO_OPT,
 
485
  )
486
 
487
 
 
 
 
 
 
 
 
 
488
  def index_inf_conf():
489
  return gr.Slider(
490
  minimum=0,
 
544
  return gr.Checkbox(
545
  False,
546
  label="TTS",
 
547
  container=False,
548
  )
549
 
 
579
  return gr.Checkbox(
580
  False,
581
  label="Play",
 
582
  container=False,
583
  visible=False,
584
  )
 
588
  return gr.Audio(
589
  value=None,
590
  type="filepath",
 
591
  autoplay=True,
592
  visible=True,
593
  interactive=False,
 
617
  return gr.Checkbox(
618
  False,
619
  label="Denoise",
 
620
  container=False,
621
  visible=True,
622
  )
 
626
  return gr.Checkbox(
627
  False,
628
  label="Reverb",
 
629
  container=False,
630
  visible=True,
631
  )
 
661
  return gr.Checkbox(
662
  False,
663
  label="URL-to-Model",
 
664
  container=False,
665
  )
666
 
 
692
  visible=value_active
693
  )
694
 
695
+
696
  CSS = """
697
  #audio_tts {
698
+ visibility: hidden;
699
  height: 0px;
700
  width: 0px;
701
  max-width: 0px;
 
708
  gr.Markdown(title)
709
  gr.Markdown(description)
710
 
711
+ # ===== TTS सेक्शन (कोई बदलाव नहीं) =====
712
  active_tts = active_tts_conf()
713
  with gr.Row():
714
  with gr.Column(scale=1):
 
730
  )
731
 
732
  aud = audio_conf()
 
733
 
734
  tts_button.click(
735
  fn=infer_tts_audio,
 
737
  outputs=[aud, tts_play],
738
  )
739
 
740
+ # ===== URL-to-Model सेक्शन (कोई बदलाव नहीं) =====
741
  down_active_gui = down_active_conf()
742
  down_info = gr.Markdown(
743
+ f"Provide a link to a zip file, like this one: `https://huggingface.co/MrDawg/ToothBrushing/resolve/main/ToothBrushing.zip?download=true`, or