Ezmary commited on
Commit
a6c46a0
·
verified ·
1 Parent(s): f286d81

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +88 -672
app.py CHANGED
@@ -1,18 +1,14 @@
1
  import os
2
  import gradio as gr
3
- import spaces
4
  from infer_rvc_python import BaseLoader
5
  import random
6
  import logging
7
  import time
8
- import soundfile as sf
9
- from infer_rvc_python.main import download_manager, load_hu_bert, Config
10
  import zipfile
11
  import edge_tts
12
  import asyncio
13
  import librosa
14
  import traceback
15
- import soundfile as sf
16
  from pedalboard import Pedalboard, Reverb, Compressor, HighpassFilter
17
  from pedalboard.io import AudioFile
18
  from pydub import AudioSegment
@@ -23,19 +19,11 @@ import shutil
23
  import threading
24
  import argparse
25
  import sys
 
26
 
27
- parser = argparse.ArgumentParser(description="Run the app with optional sharing")
28
- parser.add_argument(
29
- '--share',
30
- action='store_true',
31
- help='Enable sharing mode'
32
- )
33
- parser.add_argument(
34
- '--theme',
35
- type=str,
36
- default="aliabid94/new-theme",
37
- help='Set the theme (default: aliabid94/new-theme)'
38
- )
39
  args = parser.parse_args()
40
 
41
  IS_COLAB = True if ('google.colab' in sys.modules or args.share) else False
@@ -43,314 +31,100 @@ IS_ZERO_GPU = os.getenv("SPACES_ZERO_GPU")
43
 
44
  logging.getLogger("infer_rvc_python").setLevel(logging.ERROR)
45
 
46
- converter = BaseLoader(only_cpu=False, hubert_path=None, rmvpe_path=None)
47
- converter.hu_bert_model = load_hu_bert(Config(only_cpu=False), converter.hubert_path)
 
 
 
 
 
48
 
49
  test_model = "https://huggingface.co/sail-rvc/Aldeano_Minecraft__RVC_V2_-_500_Epochs_/resolve/main/model.pth?download=true, https://huggingface.co/sail-rvc/Aldeano_Minecraft__RVC_V2_-_500_Epochs_/resolve/main/model.index?download=true"
50
  test_names = ["model.pth", "model.index"]
51
 
52
  for url, filename in zip(test_model.split(", "), test_names):
53
  try:
54
- download_manager(
55
- url=url,
56
- path=".",
57
- extension="",
58
- overwrite=False,
59
- progress=True,
60
- )
61
- if not os.path.isfile(filename):
62
- raise FileNotFoundError
63
  except Exception:
64
- with open(filename, "wb") as f:
65
- pass
66
 
67
- title = "<center><strong><font size='7'>RVC⚡ZERO</font></strong></center>"
68
- description = "This demo is provided for educational and research purposes only. The authors and contributors of this project do not endorse or encourage any misuse or unethical use of this software. Any use of this software for purposes other than those intended is solely at the user's own risk. The authors and contributors shall not be held responsible for any damages or liabilities arising from the use of this demo inappropriately." if IS_ZERO_GPU else ""
69
- RESOURCES = "- You can also try `RVC⚡ZERO` in Colab’s free tier, which provides free GPU [link](https://github.com/R3gm/rvc_zero_ui?tab=readme-ov-file#rvczero)."
70
  theme = args.theme
71
- delete_cache_time = (3200, 3200) if IS_ZERO_GPU else (86400, 86400)
72
-
73
- PITCH_ALGO_OPT = [
74
- "pm",
75
- "harvest",
76
- "crepe",
77
- "rmvpe",
78
- "rmvpe+",
79
- ]
80
-
81
 
82
  async def get_voices_list(proxy=None):
83
- """Print all available voices."""
84
  from edge_tts import list_voices
85
  voices = await list_voices(proxy=proxy)
86
  voices = sorted(voices, key=lambda voice: voice["ShortName"])
87
-
88
- table = [
89
- {
90
- "ShortName": voice["ShortName"],
91
- "Gender": voice["Gender"],
92
- "ContentCategories": ", ".join(voice["VoiceTag"]["ContentCategories"]),
93
- "VoicePersonalities": ", ".join(voice["VoiceTag"]["VoicePersonalities"]),
94
- "FriendlyName": voice["FriendlyName"],
95
- }
96
- for voice in voices
97
- ]
98
-
99
- return table
100
-
101
 
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
-
113
  def unzip_in_folder(my_zip, my_dir):
114
  with zipfile.ZipFile(my_zip) as zip:
115
  for zip_info in zip.infolist():
116
- if zip_info.is_dir():
117
- continue
118
  zip_info.filename = os.path.basename(zip_info.filename)
119
  zip.extract(zip_info, 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
-
127
- txt_files = []
128
- for base_file in [a_, b_]:
129
- if base_file is not None and base_file.endswith(".txt"):
130
- txt_files.append(base_file)
131
-
132
- directory = os.path.dirname(a_)
133
-
134
- for txt in txt_files:
135
- with open(txt, 'r') as file:
136
- first_line = file.readline()
137
-
138
- download_manager(
139
- url=first_line.strip(),
140
- path=directory,
141
- extension="",
142
- )
143
-
144
- for f in find_files(directory):
145
- if f.endswith(".zip"):
146
- unzip_in_folder(f, directory)
147
-
148
- model = None
149
- index = None
150
- end_files = find_files(directory)
151
-
152
- for ff in end_files:
153
- if ff.endswith(".pth"):
154
- model = os.path.join(directory, ff)
155
- gr.Info(f"Model found: {ff}")
156
- if ff.endswith(".index"):
157
- index = os.path.join(directory, ff)
158
- gr.Info(f"Index found: {ff}")
159
-
160
- if not model:
161
- gr.Error(f"Model not found in: {end_files}")
162
-
163
- if not index:
164
- gr.Warning("Index not found")
165
-
166
- return model, index
167
-
168
-
169
- def ensure_valid_file(url):
170
- if "huggingface" not in url:
171
- raise ValueError("Only downloads from Hugging Face are allowed")
172
-
173
- try:
174
- request = urllib.request.Request(url, method="HEAD")
175
- with urllib.request.urlopen(request) as response:
176
- content_length = response.headers.get("Content-Length")
177
-
178
- if content_length is None:
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
-
186
- return file_size
187
-
188
- except Exception as e:
189
- raise e
190
-
191
-
192
- def clear_files(directory):
193
- time.sleep(15)
194
- print(f"Clearing files: {directory}.")
195
- shutil.rmtree(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
-
203
- if "," in url_data:
204
- a_, b_ = url_data.split(",")
205
- a_, b_ = a_.strip().replace("/blob/", "/resolve/"), b_.strip().replace("/blob/", "/resolve/")
206
- else:
207
- a_, b_ = url_data.strip().replace("/blob/", "/resolve/"), None
208
-
209
- out_dir = "downloads"
210
- folder_download = str(random.randint(1000, 9999))
211
- directory = os.path.join(out_dir, folder_download)
212
- os.makedirs(directory, exist_ok=True)
213
-
214
- try:
215
- valid_url = [a_] if not b_ else [a_, b_]
216
- for link in valid_url:
217
- ensure_valid_file(link)
218
- download_manager(
219
- url=link,
220
- path=directory,
221
- extension="",
222
- )
223
-
224
- for f in find_files(directory):
225
- if f.endswith(".zip"):
226
- unzip_in_folder(f, directory)
227
-
228
- model = None
229
- index = None
230
- end_files = find_files(directory)
231
-
232
- for ff in end_files:
233
- if ff.endswith(".pth"):
234
- model = ff
235
- gr.Info(f"Model found: {ff}")
236
- if ff.endswith(".index"):
237
- index = ff
238
- gr.Info(f"Index found: {ff}")
239
-
240
- if not model:
241
- raise ValueError(f"Model not found in: {end_files}")
242
-
243
- if not index:
244
- gr.Warning("Index not found")
245
- else:
246
- index = os.path.abspath(index)
247
-
248
- return os.path.abspath(model), index
249
-
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
-
262
  result = []
263
  for audio_path in audio_list:
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(),
271
- Compressor(ratio=4, threshold_db=-15),
272
- Reverb(room_size=0.10, dry_level=0.8, wet_level=0.2, damping=0.7)
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:
280
  with AudioFile(temp_wav, 'w', f.samplerate, f.num_channels) as o:
281
  while f.tell() < f.frames:
282
  chunk = f.read(int(f.samplerate))
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)
294
- except Exception as e:
295
- traceback.print_exc()
296
- print(f"Error noisereduce: {str(e)}")
297
  result.append(audio_path)
298
-
299
  return result
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 = []
307
  for audio_path in audio_list:
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,
324
- sample_width=audio.sample_width,
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
-
332
- except Exception as e:
333
- traceback.print_exc()
334
- print(f"Error noisereduce: {str(e)}")
335
  result.append(audio_path)
336
-
337
  return result
338
 
339
-
340
- @spaces.GPU()
341
  def convert_now(audio_files, random_tag, converter, type_output, steps):
 
342
  for step in range(steps):
343
  audio_files = converter(
344
  audio_files,
345
  random_tag,
346
  overwrite=False,
347
- parallel_workers=(2 if IS_COLAB else 8),
348
  type_output=type_output,
349
  )
350
-
351
  return audio_files
352
 
353
-
354
  def run(
355
  audio_files,
356
  file_m,
@@ -366,24 +140,18 @@ def run(
366
  type_output,
367
  steps,
368
  ):
369
- if not audio_files:
370
- raise ValueError("The audio pls")
371
-
372
- if isinstance(audio_files, str):
373
- audio_files = [audio_files]
374
-
375
- try:
376
- duration_base = librosa.get_duration(filename=audio_files[0])
377
- print("Duration:", duration_base)
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)
384
 
385
  random_tag = "USER_"+str(random.randint(10000000, 99999999))
386
 
 
387
  converter.apply_conf(
388
  tag=random_tag,
389
  file_model=file_m,
@@ -398,461 +166,109 @@ def run(
398
  )
399
  time.sleep(0.1)
400
 
 
401
  result = convert_now(audio_files, random_tag, converter, type_output, steps)
402
 
403
  if active_noise_reduce:
404
  result = apply_noisereduce(result, type_output)
405
-
406
  if audio_effects:
407
  result = add_audio_effects(result, type_output)
408
-
 
 
409
  return result
410
 
411
-
412
- def audio_conf():
413
- return gr.File(
414
- label="Audio files",
415
- file_count="multiple",
416
- type="filepath",
417
- container=True,
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,
432
- value=PITCH_ALGO_OPT[4],
433
- label="Pitch algorithm",
434
- visible=True,
435
- interactive=True,
436
- )
437
-
438
-
439
- def pitch_lvl_conf():
440
- return gr.Slider(
441
- label="Pitch level",
442
- minimum=-24,
443
- maximum=24,
444
- step=1,
445
- value=0,
446
- visible=True,
447
- interactive=True,
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,
462
- maximum=1,
463
- label="Index influence",
464
- value=0.75,
465
- )
466
-
467
-
468
- def respiration_filter_conf():
469
- return gr.Slider(
470
- minimum=0,
471
- maximum=7,
472
- label="Respiration median filtering",
473
- value=3,
474
- step=1,
475
- interactive=True,
476
- )
477
-
478
-
479
- def envelope_ratio_conf():
480
- return gr.Slider(
481
- minimum=0,
482
- maximum=1,
483
- label="Envelope ratio",
484
- value=0.25,
485
- interactive=True,
486
- )
487
-
488
-
489
- def consonant_protec_conf():
490
- return gr.Slider(
491
- minimum=0,
492
- maximum=0.5,
493
- label="Consonant breath protection",
494
- value=0.5,
495
- interactive=True,
496
- )
497
-
498
-
499
- def button_conf():
500
- return gr.Button(
501
- "Inference",
502
- variant="primary",
503
- )
504
-
505
-
506
- def output_conf():
507
- return gr.File(
508
- label="Result",
509
- file_count="multiple",
510
- interactive=False,
511
- )
512
-
513
-
514
- def active_tts_conf():
515
- return gr.Checkbox(
516
- False,
517
- label="TTS",
518
- # info="",
519
- container=False,
520
- )
521
-
522
-
523
- def tts_voice_conf():
524
- return gr.Dropdown(
525
- label="tts voice",
526
- choices=voices,
527
- visible=False,
528
- value="en-US-EmmaMultilingualNeural-Female",
529
- )
530
-
531
-
532
- def tts_text_conf():
533
- return gr.Textbox(
534
- value="",
535
- placeholder="Write the text here...",
536
- label="Text",
537
- visible=False,
538
- lines=3,
539
- )
540
-
541
-
542
- def tts_button_conf():
543
- return gr.Button(
544
- "Process TTS",
545
- variant="secondary",
546
- visible=False,
547
- )
548
-
549
-
550
- def tts_play_conf():
551
- return gr.Checkbox(
552
- False,
553
- label="Play",
554
- # info="",
555
- container=False,
556
- visible=False,
557
- )
558
-
559
-
560
- 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,
568
- elem_id="audio_tts",
569
- )
570
-
571
-
572
- def steps_conf():
573
- return gr.Slider(
574
- minimum=1,
575
- maximum=3,
576
- label="Steps",
577
- value=1,
578
- step=1,
579
- interactive=True,
580
- )
581
-
582
-
583
- def format_output_gui():
584
- return gr.Dropdown(
585
- label="Format output:",
586
- choices=["wav", "mp3", "flac"],
587
- value="wav",
588
- )
589
-
590
- def denoise_conf():
591
- return gr.Checkbox(
592
- False,
593
- label="Denoise",
594
- # info="",
595
- container=False,
596
- visible=True,
597
- )
598
-
599
-
600
- def effects_conf():
601
- return gr.Checkbox(
602
- False,
603
- label="Reverb",
604
- # info="",
605
- container=False,
606
- visible=True,
607
- )
608
-
609
-
610
  def infer_tts_audio(tts_voice, tts_text, play_tts):
611
  out_dir = "output"
612
  folder_tts = "USER_"+str(random.randint(10000, 99999))
613
-
614
  os.makedirs(out_dir, exist_ok=True)
615
  os.makedirs(os.path.join(out_dir, folder_tts), exist_ok=True)
616
  out_path = os.path.join(out_dir, folder_tts, "tts.mp3")
617
-
618
  asyncio.run(edge_tts.Communicate(tts_text, "-".join(tts_voice.split('-')[:-1])).save(out_path))
619
- if play_tts:
620
- return [out_path], out_path
621
  return [out_path], None
 
 
 
 
 
622
 
623
-
624
- def show_components_tts(value_active):
625
- return gr.update(
626
- visible=value_active
627
- ), gr.update(
628
- visible=value_active
629
- ), gr.update(
630
- visible=value_active
631
- ), gr.update(
632
- visible=value_active
633
- )
634
-
635
-
636
- def down_active_conf():
637
- return gr.Checkbox(
638
- False,
639
- label="URL-to-Model",
640
- # info="",
641
- container=False,
642
- )
643
-
644
-
645
- def down_url_conf():
646
- return gr.Textbox(
647
- value="",
648
- placeholder="Write the url here...",
649
- label="Enter URL",
650
- visible=False,
651
- lines=1,
652
- )
653
-
654
-
655
- def down_button_conf():
656
- return gr.Button(
657
- "Process",
658
- variant="secondary",
659
- visible=False,
660
- )
661
-
662
-
663
- def show_components_down(value_active):
664
- return gr.update(
665
- visible=value_active
666
- ), gr.update(
667
- visible=value_active
668
- ), gr.update(
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;
678
- max-height: 0px;
679
- }
680
- """
681
 
682
  def get_gui(theme):
683
- with gr.Blocks(theme=theme, css=CSS, fill_width=True, fill_height=False, delete_cache=delete_cache_time) as app:
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):
690
- tts_text = tts_text_conf()
691
  with gr.Column(scale=2):
692
  with gr.Row():
693
  with gr.Column():
694
- with gr.Row():
695
- tts_voice = tts_voice_conf()
696
- tts_active_play = tts_play_conf()
697
-
698
  tts_button = tts_button_conf()
699
  tts_play = sound_gui()
700
-
701
- active_tts.change(
702
- fn=show_components_tts,
703
- inputs=[active_tts],
704
- outputs=[tts_voice, tts_text, tts_button, tts_active_play],
705
- )
706
-
707
  aud = audio_conf()
708
- # gr.HTML("<hr>")
709
-
710
- tts_button.click(
711
- fn=infer_tts_audio,
712
- inputs=[tts_voice, tts_text, tts_active_play],
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
- # تغییرات برای صف‌بندی تکی (Sequential)
848
- # default_concurrency_limit=1 یعنی فقط ۱ نفر همزمان اجرا می‌شود
849
  app.queue(default_concurrency_limit=1)
850
-
851
- app.launch(
852
- max_threads=1, # فقط ۱ ترد برای پردازش
853
- share=IS_COLAB,
854
- show_error=True,
855
- quiet=False,
856
- debug=IS_COLAB,
857
- ssr_mode=False,
858
- )
 
1
  import os
2
  import gradio as gr
 
3
  from infer_rvc_python import BaseLoader
4
  import random
5
  import logging
6
  import time
 
 
7
  import zipfile
8
  import edge_tts
9
  import asyncio
10
  import librosa
11
  import traceback
 
12
  from pedalboard import Pedalboard, Reverb, Compressor, HighpassFilter
13
  from pedalboard.io import AudioFile
14
  from pydub import AudioSegment
 
19
  import threading
20
  import argparse
21
  import sys
22
+ import gc # Garbage Collection برای مدیریت حافظه
23
 
24
+ parser = argparse.ArgumentParser()
25
+ parser.add_argument('--share', action='store_true')
26
+ parser.add_argument('--theme', type=str, default="aliabid94/new-theme")
 
 
 
 
 
 
 
 
 
27
  args = parser.parse_args()
28
 
29
  IS_COLAB = True if ('google.colab' in sys.modules or args.share) else False
 
31
 
32
  logging.getLogger("infer_rvc_python").setLevel(logging.ERROR)
33
 
34
+ # تنظیم لودر (برای CPU)
35
+ converter = BaseLoader(only_cpu=True, hubert_path=None, rmvpe_path=None)
36
+
37
+ # --- توابع کمکی ---
38
+ def download_manager(url, path, extension="", overwrite=False, progress=True):
39
+ from infer_rvc_python.main import download_manager as dm
40
+ return dm(url, path, extension, overwrite, progress)
41
 
42
  test_model = "https://huggingface.co/sail-rvc/Aldeano_Minecraft__RVC_V2_-_500_Epochs_/resolve/main/model.pth?download=true, https://huggingface.co/sail-rvc/Aldeano_Minecraft__RVC_V2_-_500_Epochs_/resolve/main/model.index?download=true"
43
  test_names = ["model.pth", "model.index"]
44
 
45
  for url, filename in zip(test_model.split(", "), test_names):
46
  try:
47
+ download_manager(url=url, path=".", extension="", overwrite=False, progress=True)
 
 
 
 
 
 
 
 
48
  except Exception:
49
+ pass
 
50
 
51
+ title = "<center><strong><font size='7'>RVC⚡ZERO (Worker)</font></strong></center>"
52
+ description = "Worker Node - Optimized for CPU"
 
53
  theme = args.theme
 
 
 
 
 
 
 
 
 
 
54
 
55
  async def get_voices_list(proxy=None):
 
56
  from edge_tts import list_voices
57
  voices = await list_voices(proxy=proxy)
58
  voices = sorted(voices, key=lambda voice: voice["ShortName"])
59
+ return [{"ShortName": v["ShortName"], "Gender": v["Gender"], "FriendlyName": v["FriendlyName"]} for v in voices]
 
 
 
 
 
 
 
 
 
 
 
 
 
60
 
61
  def find_files(directory):
62
  file_paths = []
63
+ if os.path.exists(directory):
64
+ for filename in os.listdir(directory):
65
+ if filename.endswith('.pth') or filename.endswith('.zip') or filename.endswith('.index'):
66
+ file_paths.append(os.path.join(directory, filename))
 
 
67
  return file_paths
68
 
 
69
  def unzip_in_folder(my_zip, my_dir):
70
  with zipfile.ZipFile(my_zip) as zip:
71
  for zip_info in zip.infolist():
72
+ if zip_info.is_dir(): continue
 
73
  zip_info.filename = os.path.basename(zip_info.filename)
74
  zip.extract(zip_info, my_dir)
75
 
 
76
  def find_my_model(a_, b_):
77
+ if a_ is None or a_.endswith(".pth"): return a_, b_
78
+ return a_, b_
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
 
80
  def add_audio_effects(audio_list, type_output):
 
 
81
  result = []
82
  for audio_path in audio_list:
83
  try:
84
  output_path = f'{os.path.splitext(audio_path)[0]}_effects.{type_output}'
85
+ board = Pedalboard([HighpassFilter(), Compressor(ratio=4, threshold_db=-15), Reverb(room_size=0.10, dry_level=0.8, wet_level=0.2, damping=0.7)])
 
 
 
 
 
 
 
 
 
 
86
  temp_wav = f'{os.path.splitext(audio_path)[0]}_temp.wav'
 
87
  with AudioFile(audio_path) as f:
88
  with AudioFile(temp_wav, 'w', f.samplerate, f.num_channels) as o:
89
  while f.tell() < f.frames:
90
  chunk = f.read(int(f.samplerate))
91
  effected = board(chunk, f.samplerate, reset=False)
92
  o.write(effected)
 
 
93
  audio_seg = AudioSegment.from_file(temp_wav, format=type_output)
94
+ audio_seg.export(output_path, format=type_output, bitrate="320k" if type_output == "mp3" else None)
 
 
95
  os.remove(temp_wav)
 
96
  result.append(output_path)
97
+ except Exception:
 
 
98
  result.append(audio_path)
 
99
  return result
100
 
 
101
  def apply_noisereduce(audio_list, type_output):
 
 
 
102
  result = []
103
  for audio_path in audio_list:
104
  out_path = f"{os.path.splitext(audio_path)[0]}_noisereduce.{type_output}"
 
105
  try:
 
106
  audio = AudioSegment.from_file(audio_path)
 
 
107
  samples = np.array(audio.get_array_of_samples())
 
 
108
  reduced_noise = nr.reduce_noise(samples, sr=audio.frame_rate, prop_decrease=0.6)
109
+ reduced_audio = AudioSegment(reduced_noise.tobytes(), frame_rate=audio.frame_rate, sample_width=audio.sample_width, channels=audio.channels)
110
+ reduced_audio.export(out_path, format=type_output, bitrate="320k" if type_output == "mp3" else None)
 
 
 
 
 
 
 
 
 
111
  result.append(out_path)
112
+ except Exception:
 
 
 
113
  result.append(audio_path)
 
114
  return result
115
 
 
 
116
  def convert_now(audio_files, random_tag, converter, type_output, steps):
117
+ # اجبار به اجرای متوالی و تک‌رشته‌ای برای جلوگیری از خطای رم و پردازش در CPU
118
  for step in range(steps):
119
  audio_files = converter(
120
  audio_files,
121
  random_tag,
122
  overwrite=False,
123
+ parallel_workers=1, # مهم برای CPU: فقط ۱
124
  type_output=type_output,
125
  )
 
126
  return audio_files
127
 
 
128
  def run(
129
  audio_files,
130
  file_m,
 
140
  type_output,
141
  steps,
142
  ):
143
+ # پاکسازی حافظه قبل از شروع
144
+ gc.collect()
145
+
146
+ if not audio_files: raise ValueError("No audio files provided")
147
+ if isinstance(audio_files, str): audio_files = [audio_files]
 
 
 
 
 
 
148
 
149
  if file_m is not None and file_m.endswith(".txt"):
150
  file_m, file_index = find_my_model(file_m, file_index)
 
151
 
152
  random_tag = "USER_"+str(random.randint(10000000, 99999999))
153
 
154
+ # بارگذاری مدل
155
  converter.apply_conf(
156
  tag=random_tag,
157
  file_model=file_m,
 
166
  )
167
  time.sleep(0.1)
168
 
169
+ # شروع پردازش
170
  result = convert_now(audio_files, random_tag, converter, type_output, steps)
171
 
172
  if active_noise_reduce:
173
  result = apply_noisereduce(result, type_output)
 
174
  if audio_effects:
175
  result = add_audio_effects(result, type_output)
176
+
177
+ # پاکسازی نهایی حافظه
178
+ gc.collect()
179
  return result
180
 
181
+ # --- GUI ---
182
+ def audio_conf(): return gr.File(label="Audio files", file_count="multiple", type="filepath", container=True)
183
+ def model_conf(): return gr.File(label="Model file", type="filepath", height=130)
184
+ def pitch_algo_conf(): return gr.Dropdown(["pm", "harvest", "crepe", "rmvpe", "rmvpe+"], value="rmvpe+", label="Pitch algorithm", visible=True, interactive=True)
185
+ def pitch_lvl_conf(): return gr.Slider(label="Pitch level", minimum=-24, maximum=24, step=1, value=0, visible=True, interactive=True)
186
+ def index_conf(): return gr.File(label="Index file", type="filepath", height=130)
187
+ def index_inf_conf(): return gr.Slider(minimum=0, maximum=1, label="Index influence", value=0.75)
188
+ def respiration_filter_conf(): return gr.Slider(minimum=0, maximum=7, label="Respiration median filtering", value=3, step=1, interactive=True)
189
+ def envelope_ratio_conf(): return gr.Slider(minimum=0, maximum=1, label="Envelope ratio", value=0.25, interactive=True)
190
+ def consonant_protec_conf(): return gr.Slider(minimum=0, maximum=0.5, label="Consonant breath protection", value=0.5, interactive=True)
191
+ def button_conf(): return gr.Button("Inference", variant="primary")
192
+ def output_conf(): return gr.File(label="Result", file_count="multiple", interactive=False)
193
+ def active_tts_conf(): return gr.Checkbox(False, label="TTS", container=False)
194
+ def tts_voice_conf(): return gr.Dropdown(label="tts voice", choices=voices, visible=False, value="en-US-EmmaMultilingualNeural-Female")
195
+ def tts_text_conf(): return gr.Textbox(value="", placeholder="Write the text here...", label="Text", visible=False, lines=3)
196
+ def tts_button_conf(): return gr.Button("Process TTS", variant="secondary", visible=False)
197
+ def tts_play_conf(): return gr.Checkbox(False, label="Play", container=False, visible=False)
198
+ def sound_gui(): return gr.Audio(value=None, type="filepath", autoplay=True, visible=True, interactive=False, elem_id="audio_tts")
199
+ def steps_conf(): return gr.Slider(minimum=1, maximum=3, label="Steps", value=1, step=1, interactive=True)
200
+ def format_output_gui(): return gr.Dropdown(label="Format output:", choices=["wav", "mp3", "flac"], value="wav")
201
+ def denoise_conf(): return gr.Checkbox(False, label="Denoise", container=False, visible=True)
202
+ def effects_conf(): return gr.Checkbox(False, label="Reverb", container=False, visible=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
203
  def infer_tts_audio(tts_voice, tts_text, play_tts):
204
  out_dir = "output"
205
  folder_tts = "USER_"+str(random.randint(10000, 99999))
 
206
  os.makedirs(out_dir, exist_ok=True)
207
  os.makedirs(os.path.join(out_dir, folder_tts), exist_ok=True)
208
  out_path = os.path.join(out_dir, folder_tts, "tts.mp3")
 
209
  asyncio.run(edge_tts.Communicate(tts_text, "-".join(tts_voice.split('-')[:-1])).save(out_path))
210
+ if play_tts: return [out_path], out_path
 
211
  return [out_path], None
212
+ def show_components_tts(value_active): return gr.update(visible=value_active), gr.update(visible=value_active), gr.update(visible=value_active), gr.update(visible=value_active)
213
+ def down_active_conf(): return gr.Checkbox(False, label="URL-to-Model", container=False)
214
+ def down_url_conf(): return gr.Textbox(value="", placeholder="Write the url here...", label="Enter URL", visible=False, lines=1)
215
+ def down_button_conf(): return gr.Button("Process", variant="secondary", visible=False)
216
+ def show_components_down(value_active): return gr.update(visible=value_active), gr.update(visible=value_active), gr.update(visible=value_active)
217
 
218
+ CSS = """#audio_tts { visibility: hidden; height: 0px; width: 0px; max-width: 0px; max-height: 0px; }"""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
219
 
220
  def get_gui(theme):
221
+ with gr.Blocks(theme=theme, css=CSS, fill_width=True, fill_height=False, delete_cache=(3600, 3600)) as app:
222
  gr.Markdown(title)
223
  gr.Markdown(description)
224
 
225
  active_tts = active_tts_conf()
226
  with gr.Row():
227
+ with gr.Column(scale=1): tts_text = tts_text_conf()
 
228
  with gr.Column(scale=2):
229
  with gr.Row():
230
  with gr.Column():
231
+ with gr.Row(): tts_voice = tts_voice_conf(); tts_active_play = tts_play_conf()
 
 
 
232
  tts_button = tts_button_conf()
233
  tts_play = sound_gui()
234
+ active_tts.change(fn=show_components_tts, inputs=[active_tts], outputs=[tts_voice, tts_text, tts_button, tts_active_play])
 
 
 
 
 
 
235
  aud = audio_conf()
236
+ tts_button.click(fn=infer_tts_audio, inputs=[tts_voice, tts_text, tts_active_play], outputs=[aud, tts_play])
 
 
 
 
 
 
237
 
238
  down_active_gui = down_active_conf()
239
+ down_info = gr.Markdown(f"Provide link... {test_model}", visible=False)
 
 
 
240
  with gr.Row():
241
+ with gr.Column(scale=3): down_url_gui = down_url_conf()
242
+ with gr.Column(scale=1): down_button_gui = down_button_conf()
 
 
 
243
  with gr.Column():
244
+ with gr.Row(): model = model_conf(); indx = index_conf()
245
+ down_active_gui.change(show_components_down, [down_active_gui], [down_info, down_url_gui, down_button_gui])
246
+ down_button_gui.click(lambda x: (None, None), [down_url_gui], [model, indx]) # Dummy
 
 
 
 
 
 
 
 
 
 
 
 
247
 
248
  with gr.Accordion(label="Advanced settings", open=False):
249
+ algo = pitch_algo_conf(); algo_lvl = pitch_lvl_conf(); indx_inf = index_inf_conf()
250
+ res_fc = respiration_filter_conf(); envel_r = envelope_ratio_conf(); const = consonant_protec_conf()
251
+ steps_gui = steps_conf(); format_out = format_output_gui()
 
 
 
 
 
252
  with gr.Row():
253
  with gr.Column():
254
+ with gr.Row(): denoise_gui = denoise_conf(); effects_gui = effects_conf()
 
 
255
  button_base = button_conf()
256
  output_base = output_conf()
257
 
258
  button_base.click(
259
  run,
260
+ inputs=[aud, model, algo, algo_lvl, indx, indx_inf, res_fc, envel_r, const, denoise_gui, effects_gui, format_out, steps_gui],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
261
  outputs=[output_base],
 
262
  )
 
 
263
  return app
264
 
 
265
  if __name__ == "__main__":
266
  tts_voice_list = asyncio.new_event_loop().run_until_complete(get_voices_list(proxy=None))
267
  voices = sorted([
268
  (" - ".join(reversed(v["FriendlyName"].split("-"))).replace("Microsoft ", "").replace("Online (Natural)", f"({v['Gender']})").strip(), f"{v['ShortName']}-{v['Gender']}")
269
  for v in tts_voice_list
270
  ])
 
271
  app = get_gui(theme)
272
+ # صف کارگر فعال باشد
 
 
273
  app.queue(default_concurrency_limit=1)
274
+ app.launch(max_threads=1, share=IS_COLAB, show_error=True)