Files changed (4) hide show
  1. README.md +14 -14
  2. app.py +742 -741
  3. pre-requirements.txt +1 -0
  4. requirements.txt +8 -6
README.md CHANGED
@@ -1,14 +1,14 @@
1
- ---
2
- title: RVC⚡ZERO
3
- emoji: ⚡
4
- colorFrom: gray
5
- colorTo: indigo
6
- sdk: gradio
7
- sdk_version: 4.28.3
8
- app_file: app.py
9
- license: mit
10
- pinned: true
11
- short_description: Voice conversion framework based on VITS
12
- ---
13
-
14
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
+ ---
2
+ title: RVC⚡ZERO
3
+ emoji: ⚡
4
+ colorFrom: gray
5
+ colorTo: indigo
6
+ sdk: gradio
7
+ sdk_version: 5.34.2
8
+ app_file: app.py
9
+ license: mit
10
+ pinned: true
11
+ short_description: Voice conversion framework based on VITS
12
+ ---
13
+
14
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py CHANGED
@@ -1,741 +1,742 @@
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
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
19
- import noisereduce as nr
20
- import numpy as np
21
- import urllib.request
22
- import shutil
23
- import threading
24
-
25
- logging.getLogger("infer_rvc_python").setLevel(logging.ERROR)
26
-
27
- converter = BaseLoader(only_cpu=False, hubert_path=None, rmvpe_path=None)
28
-
29
- title = "<center><strong><font size='7'>RVC⚡ZERO</font></strong></center>"
30
- 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."
31
- theme = "aliabid94/new-theme"
32
-
33
- PITCH_ALGO_OPT = [
34
- "pm",
35
- "harvest",
36
- "crepe",
37
- "rmvpe",
38
- "rmvpe+",
39
- ]
40
-
41
-
42
- def find_files(directory):
43
- file_paths = []
44
- for filename in os.listdir(directory):
45
- # Check if the file has the desired extension
46
- if filename.endswith('.pth') or filename.endswith('.zip') or filename.endswith('.index'):
47
- # If yes, add the file path to the list
48
- file_paths.append(os.path.join(directory, filename))
49
-
50
- return file_paths
51
-
52
-
53
- def unzip_in_folder(my_zip, my_dir):
54
- with zipfile.ZipFile(my_zip) as zip:
55
- for zip_info in zip.infolist():
56
- if zip_info.is_dir():
57
- continue
58
- zip_info.filename = os.path.basename(zip_info.filename)
59
- zip.extract(zip_info, my_dir)
60
-
61
-
62
- def find_my_model(a_, b_):
63
-
64
- if a_ is None or a_.endswith(".pth"):
65
- return a_, b_
66
-
67
- txt_files = []
68
- for base_file in [a_, b_]:
69
- if base_file is not None and base_file.endswith(".txt"):
70
- txt_files.append(base_file)
71
-
72
- directory = os.path.dirname(a_)
73
-
74
- for txt in txt_files:
75
- with open(txt, 'r') as file:
76
- first_line = file.readline()
77
-
78
- download_manager(
79
- url=first_line.strip(),
80
- path=directory,
81
- extension="",
82
- )
83
-
84
- for f in find_files(directory):
85
- if f.endswith(".zip"):
86
- unzip_in_folder(f, directory)
87
-
88
- model = None
89
- index = None
90
- end_files = find_files(directory)
91
-
92
- for ff in end_files:
93
- if ff.endswith(".pth"):
94
- model = os.path.join(directory, ff)
95
- gr.Info(f"Model found: {ff}")
96
- if ff.endswith(".index"):
97
- index = os.path.join(directory, ff)
98
- gr.Info(f"Index found: {ff}")
99
-
100
- if not model:
101
- gr.Error(f"Model not found in: {end_files}")
102
-
103
- if not index:
104
- gr.Warning("Index not found")
105
-
106
- return model, index
107
-
108
-
109
- def get_file_size(url):
110
-
111
- if "huggingface" not in url:
112
- raise ValueError("Only downloads from Hugging Face are allowed")
113
-
114
- try:
115
- with urllib.request.urlopen(url) as response:
116
- info = response.info()
117
- content_length = info.get("Content-Length")
118
-
119
- file_size = int(content_length)
120
- if file_size > 500000000:
121
- raise ValueError("The file is too large. You can only download files up to 500 MB in size.")
122
-
123
- except Exception as e:
124
- raise e
125
-
126
-
127
- def clear_files(directory):
128
- time.sleep(15)
129
- print(f"Clearing files: {directory}.")
130
- shutil.rmtree(directory)
131
-
132
-
133
- def get_my_model(url_data):
134
-
135
- if not url_data:
136
- return None, None
137
-
138
- if "," in url_data:
139
- a_, b_ = url_data.split()
140
- a_, b_ = a_.strip().replace("/blob/", "/resolve/"), b_.strip().replace("/blob/", "/resolve/")
141
- else:
142
- a_, b_ = url_data.strip().replace("/blob/", "/resolve/"), None
143
-
144
- out_dir = "downloads"
145
- folder_download = str(random.randint(1000, 9999))
146
- directory = os.path.join(out_dir, folder_download)
147
- os.makedirs(directory, exist_ok=True)
148
-
149
- try:
150
- get_file_size(a_)
151
- if b_:
152
- get_file_size(b_)
153
-
154
- valid_url = [a_] if not b_ else [a_, b_]
155
- for link in valid_url:
156
- download_manager(
157
- url=link,
158
- path=directory,
159
- extension="",
160
- )
161
-
162
- for f in find_files(directory):
163
- if f.endswith(".zip"):
164
- unzip_in_folder(f, directory)
165
-
166
- model = None
167
- index = None
168
- end_files = find_files(directory)
169
-
170
- for ff in end_files:
171
- if ff.endswith(".pth"):
172
- model = ff
173
- gr.Info(f"Model found: {ff}")
174
- if ff.endswith(".index"):
175
- index = ff
176
- gr.Info(f"Index found: {ff}")
177
-
178
- if not model:
179
- raise ValueError(f"Model not found in: {end_files}")
180
-
181
- if not index:
182
- gr.Warning("Index not found")
183
- else:
184
- index = os.path.abspath(index)
185
-
186
- return os.path.abspath(model), index
187
-
188
- except Exception as e:
189
- raise e
190
- finally:
191
- # time.sleep(10)
192
- # shutil.rmtree(directory)
193
- t = threading.Thread(target=clear_files, args=(directory,))
194
- t.start()
195
-
196
-
197
- def add_audio_effects(audio_list):
198
- print("Audio effects")
199
-
200
- result = []
201
- for audio_path in audio_list:
202
- try:
203
- output_path = f'{os.path.splitext(audio_path)[0]}_effects.wav'
204
-
205
- # Initialize audio effects plugins
206
- board = Pedalboard(
207
- [
208
- HighpassFilter(),
209
- Compressor(ratio=4, threshold_db=-15),
210
- Reverb(room_size=0.10, dry_level=0.8, wet_level=0.2, damping=0.7)
211
- ]
212
- )
213
-
214
- with AudioFile(audio_path) as f:
215
- with AudioFile(output_path, 'w', f.samplerate, f.num_channels) as o:
216
- # Read one second of audio at a time, until the file is empty:
217
- while f.tell() < f.frames:
218
- chunk = f.read(int(f.samplerate))
219
- effected = board(chunk, f.samplerate, reset=False)
220
- o.write(effected)
221
- result.append(output_path)
222
- except Exception as e:
223
- traceback.print_exc()
224
- print(f"Error noisereduce: {str(e)}")
225
- result.append(audio_path)
226
-
227
- return result
228
-
229
-
230
- def apply_noisereduce(audio_list):
231
- # https://github.com/sa-if/Audio-Denoiser
232
- print("Noice reduce")
233
-
234
- result = []
235
- for audio_path in audio_list:
236
- out_path = f'{os.path.splitext(audio_path)[0]}_noisereduce.wav'
237
-
238
- try:
239
- # Load audio file
240
- audio = AudioSegment.from_file(audio_path)
241
-
242
- # Convert audio to numpy array
243
- samples = np.array(audio.get_array_of_samples())
244
-
245
- # Reduce noise
246
- reduced_noise = nr.reduce_noise(samples, sr=audio.frame_rate, prop_decrease=0.6)
247
-
248
- # Convert reduced noise signal back to audio
249
- reduced_audio = AudioSegment(
250
- reduced_noise.tobytes(),
251
- frame_rate=audio.frame_rate,
252
- sample_width=audio.sample_width,
253
- channels=audio.channels
254
- )
255
-
256
- # Save reduced audio to file
257
- reduced_audio.export(out_path, format="wav")
258
- result.append(out_path)
259
-
260
- except Exception as e:
261
- traceback.print_exc()
262
- print(f"Error noisereduce: {str(e)}")
263
- result.append(audio_path)
264
-
265
- return result
266
-
267
-
268
- @spaces.GPU()
269
- def convert_now(audio_files, random_tag, converter):
270
- return converter(
271
- audio_files,
272
- random_tag,
273
- overwrite=False,
274
- parallel_workers=8
275
- )
276
-
277
-
278
- def run(
279
- audio_files,
280
- file_m,
281
- pitch_alg,
282
- pitch_lvl,
283
- file_index,
284
- index_inf,
285
- r_m_f,
286
- e_r,
287
- c_b_p,
288
- active_noise_reduce,
289
- audio_effects,
290
- ):
291
- if not audio_files:
292
- raise ValueError("The audio pls")
293
-
294
- if isinstance(audio_files, str):
295
- audio_files = [audio_files]
296
-
297
- try:
298
- duration_base = librosa.get_duration(filename=audio_files[0])
299
- print("Duration:", duration_base)
300
- except Exception as e:
301
- print(e)
302
-
303
- if file_m is not None and file_m.endswith(".txt"):
304
- file_m, file_index = find_my_model(file_m, file_index)
305
- print(file_m, file_index)
306
-
307
- random_tag = "USER_"+str(random.randint(10000000, 99999999))
308
-
309
- converter.apply_conf(
310
- tag=random_tag,
311
- file_model=file_m,
312
- pitch_algo=pitch_alg,
313
- pitch_lvl=pitch_lvl,
314
- file_index=file_index,
315
- index_influence=index_inf,
316
- respiration_median_filtering=r_m_f,
317
- envelope_ratio=e_r,
318
- consonant_breath_protection=c_b_p,
319
- resample_sr=44100 if audio_files[0].endswith('.mp3') else 0,
320
- )
321
- time.sleep(0.1)
322
-
323
- result = convert_now(audio_files, random_tag, converter)
324
-
325
- if active_noise_reduce:
326
- result = apply_noisereduce(result)
327
-
328
- if audio_effects:
329
- result = add_audio_effects(result)
330
-
331
- return result
332
-
333
-
334
- def audio_conf():
335
- return gr.File(
336
- label="Audio files",
337
- file_count="multiple",
338
- type="filepath",
339
- container=True,
340
- )
341
-
342
-
343
- def model_conf():
344
- return gr.File(
345
- label="Model file",
346
- type="filepath",
347
- height=130,
348
- )
349
-
350
-
351
- def pitch_algo_conf():
352
- return gr.Dropdown(
353
- PITCH_ALGO_OPT,
354
- value=PITCH_ALGO_OPT[4],
355
- label="Pitch algorithm",
356
- visible=True,
357
- interactive=True,
358
- )
359
-
360
-
361
- def pitch_lvl_conf():
362
- return gr.Slider(
363
- label="Pitch level",
364
- minimum=-24,
365
- maximum=24,
366
- step=1,
367
- value=0,
368
- visible=True,
369
- interactive=True,
370
- )
371
-
372
-
373
- def index_conf():
374
- return gr.File(
375
- label="Index file",
376
- type="filepath",
377
- height=130,
378
- )
379
-
380
-
381
- def index_inf_conf():
382
- return gr.Slider(
383
- minimum=0,
384
- maximum=1,
385
- label="Index influence",
386
- value=0.75,
387
- )
388
-
389
-
390
- def respiration_filter_conf():
391
- return gr.Slider(
392
- minimum=0,
393
- maximum=7,
394
- label="Respiration median filtering",
395
- value=3,
396
- step=1,
397
- interactive=True,
398
- )
399
-
400
-
401
- def envelope_ratio_conf():
402
- return gr.Slider(
403
- minimum=0,
404
- maximum=1,
405
- label="Envelope ratio",
406
- value=0.25,
407
- interactive=True,
408
- )
409
-
410
-
411
- def consonant_protec_conf():
412
- return gr.Slider(
413
- minimum=0,
414
- maximum=0.5,
415
- label="Consonant breath protection",
416
- value=0.5,
417
- interactive=True,
418
- )
419
-
420
-
421
- def button_conf():
422
- return gr.Button(
423
- "Inference",
424
- variant="primary",
425
- )
426
-
427
-
428
- def output_conf():
429
- return gr.File(
430
- label="Result",
431
- file_count="multiple",
432
- interactive=False,
433
- )
434
-
435
-
436
- def active_tts_conf():
437
- return gr.Checkbox(
438
- False,
439
- label="TTS",
440
- # info="",
441
- container=False,
442
- )
443
-
444
-
445
- def tts_voice_conf():
446
- return gr.Dropdown(
447
- label="tts voice",
448
- choices=voices,
449
- visible=False,
450
- value="en-US-EmmaMultilingualNeural-Female",
451
- )
452
-
453
-
454
- def tts_text_conf():
455
- return gr.Textbox(
456
- value="",
457
- placeholder="Write the text here...",
458
- label="Text",
459
- visible=False,
460
- lines=3,
461
- )
462
-
463
-
464
- def tts_button_conf():
465
- return gr.Button(
466
- "Process TTS",
467
- variant="secondary",
468
- visible=False,
469
- )
470
-
471
-
472
- def tts_play_conf():
473
- return gr.Checkbox(
474
- False,
475
- label="Play",
476
- # info="",
477
- container=False,
478
- visible=False,
479
- )
480
-
481
-
482
- def sound_gui():
483
- return gr.Audio(
484
- value=None,
485
- type="filepath",
486
- # format="mp3",
487
- autoplay=True,
488
- visible=False,
489
- )
490
-
491
-
492
- def denoise_conf():
493
- return gr.Checkbox(
494
- False,
495
- label="Denoise",
496
- # info="",
497
- container=False,
498
- visible=True,
499
- )
500
-
501
-
502
- def effects_conf():
503
- return gr.Checkbox(
504
- False,
505
- label="Reverb",
506
- # info="",
507
- container=False,
508
- visible=True,
509
- )
510
-
511
-
512
- def infer_tts_audio(tts_voice, tts_text, play_tts):
513
- out_dir = "output"
514
- folder_tts = "USER_"+str(random.randint(10000, 99999))
515
-
516
- os.makedirs(out_dir, exist_ok=True)
517
- os.makedirs(os.path.join(out_dir, folder_tts), exist_ok=True)
518
- out_path = os.path.join(out_dir, folder_tts, "tts.mp3")
519
-
520
- asyncio.run(edge_tts.Communicate(tts_text, "-".join(tts_voice.split('-')[:-1])).save(out_path))
521
- if play_tts:
522
- return [out_path], out_path
523
- return [out_path], None
524
-
525
-
526
- def show_components_tts(value_active):
527
- return gr.update(
528
- visible=value_active
529
- ), gr.update(
530
- visible=value_active
531
- ), gr.update(
532
- visible=value_active
533
- ), gr.update(
534
- visible=value_active
535
- )
536
-
537
-
538
- def down_active_conf():
539
- return gr.Checkbox(
540
- False,
541
- label="URL-to-Model",
542
- # info="",
543
- container=False,
544
- )
545
-
546
-
547
- def down_url_conf():
548
- return gr.Textbox(
549
- value="",
550
- placeholder="Write the url here...",
551
- label="Enter URL",
552
- visible=False,
553
- lines=1,
554
- )
555
-
556
-
557
- def down_button_conf():
558
- return gr.Button(
559
- "Process",
560
- variant="secondary",
561
- visible=False,
562
- )
563
-
564
-
565
- def show_components_down(value_active):
566
- return gr.update(
567
- visible=value_active
568
- ), gr.update(
569
- visible=value_active
570
- ), gr.update(
571
- visible=value_active
572
- )
573
-
574
-
575
- def get_gui(theme):
576
- with gr.Blocks(theme=theme, delete_cache=(3200, 3200)) as app:
577
- gr.Markdown(title)
578
- gr.Markdown(description)
579
-
580
- active_tts = active_tts_conf()
581
- with gr.Row():
582
- with gr.Column(scale=1):
583
- tts_text = tts_text_conf()
584
- with gr.Column(scale=2):
585
- with gr.Row():
586
- with gr.Column():
587
- with gr.Row():
588
- tts_voice = tts_voice_conf()
589
- tts_active_play = tts_play_conf()
590
-
591
- tts_button = tts_button_conf()
592
- tts_play = sound_gui()
593
-
594
- active_tts.change(
595
- fn=show_components_tts,
596
- inputs=[active_tts],
597
- outputs=[tts_voice, tts_text, tts_button, tts_active_play],
598
- )
599
-
600
- aud = audio_conf()
601
- # gr.HTML("<hr>")
602
-
603
- tts_button.click(
604
- fn=infer_tts_audio,
605
- inputs=[tts_voice, tts_text, tts_active_play],
606
- outputs=[aud, tts_play],
607
- )
608
-
609
- down_active_gui = down_active_conf()
610
- down_info = gr.Markdown(
611
- "Provide a link to a zip file, like this one: `https://huggingface.co/mrmocciai/Models/resolve/main/Genshin%20Impact/ayaka-v2.zip?download=true`, or separate links with a comma for the .pth and .index files, like this: `https://huggingface.co/sail-rvc/ayaka-jp/resolve/main/model.pth?download=true, https://huggingface.co/sail-rvc/ayaka-jp/resolve/main/model.index?download=true`",
612
- visible=False
613
- )
614
- with gr.Row():
615
- with gr.Column(scale=3):
616
- down_url_gui = down_url_conf()
617
- with gr.Column(scale=1):
618
- down_button_gui = down_button_conf()
619
-
620
- with gr.Column():
621
- with gr.Row():
622
- model = model_conf()
623
- indx = index_conf()
624
-
625
- down_active_gui.change(
626
- show_components_down,
627
- [down_active_gui],
628
- [down_info, down_url_gui, down_button_gui]
629
- )
630
-
631
- down_button_gui.click(
632
- get_my_model,
633
- [down_url_gui],
634
- [model, indx]
635
- )
636
-
637
- algo = pitch_algo_conf()
638
- algo_lvl = pitch_lvl_conf()
639
- indx_inf = index_inf_conf()
640
- res_fc = respiration_filter_conf()
641
- envel_r = envelope_ratio_conf()
642
- const = consonant_protec_conf()
643
- with gr.Row():
644
- with gr.Column():
645
- with gr.Row():
646
- denoise_gui = denoise_conf()
647
- effects_gui = effects_conf()
648
- button_base = button_conf()
649
- output_base = output_conf()
650
-
651
- button_base.click(
652
- run,
653
- inputs=[
654
- aud,
655
- model,
656
- algo,
657
- algo_lvl,
658
- indx,
659
- indx_inf,
660
- res_fc,
661
- envel_r,
662
- const,
663
- denoise_gui,
664
- effects_gui,
665
- ],
666
- outputs=[output_base],
667
- )
668
-
669
- gr.Examples(
670
- examples=[
671
- [
672
- ["./test.ogg"],
673
- "./model.pth",
674
- "rmvpe+",
675
- 0,
676
- "./model.index",
677
- 0.75,
678
- 3,
679
- 0.25,
680
- 0.50,
681
- ],
682
- [
683
- ["./example2/test2.ogg"],
684
- "./example2/model_link.txt",
685
- "rmvpe+",
686
- 0,
687
- "./example2/index_link.txt",
688
- 0.75,
689
- 3,
690
- 0.25,
691
- 0.50,
692
- ],
693
- [
694
- ["./example3/test3.wav"],
695
- "./example3/zip_link.txt",
696
- "rmvpe+",
697
- 0,
698
- None,
699
- 0.75,
700
- 3,
701
- 0.25,
702
- 0.50,
703
- ],
704
-
705
- ],
706
- fn=run,
707
- inputs=[
708
- aud,
709
- model,
710
- algo,
711
- algo_lvl,
712
- indx,
713
- indx_inf,
714
- res_fc,
715
- envel_r,
716
- const,
717
- ],
718
- outputs=[output_base],
719
- cache_examples=False,
720
- )
721
-
722
- return app
723
-
724
-
725
- if __name__ == "__main__":
726
-
727
- tts_voice_list = asyncio.new_event_loop().run_until_complete(edge_tts.list_voices())
728
- voices = sorted([f"{v['ShortName']}-{v['Gender']}" for v in tts_voice_list])
729
-
730
- app = get_gui(theme)
731
-
732
- app.queue(default_concurrency_limit=40)
733
-
734
- app.launch(
735
- max_threads=40,
736
- share=False,
737
- show_error=True,
738
- quiet=False,
739
- debug=False,
740
- allowed_paths=["./downloads/"],
741
- )
 
 
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
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
19
+ import noisereduce as nr
20
+ import numpy as np
21
+ import urllib.request
22
+ import shutil
23
+ import threading
24
+
25
+ logging.getLogger("infer_rvc_python").setLevel(logging.ERROR)
26
+
27
+ converter = BaseLoader(only_cpu=False, hubert_path=None, rmvpe_path=None)
28
+
29
+ title = "<center><strong><font size='7'>RVC⚡ZERO</font></strong></center>"
30
+ 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."
31
+ theme = "aliabid94/new-theme"
32
+
33
+ PITCH_ALGO_OPT = [
34
+ "pm",
35
+ "harvest",
36
+ "crepe",
37
+ "rmvpe",
38
+ "rmvpe+",
39
+ ]
40
+
41
+
42
+ def find_files(directory):
43
+ file_paths = []
44
+ for filename in os.listdir(directory):
45
+ # Check if the file has the desired extension
46
+ if filename.endswith('.pth') or filename.endswith('.zip') or filename.endswith('.index'):
47
+ # If yes, add the file path to the list
48
+ file_paths.append(os.path.join(directory, filename))
49
+
50
+ return file_paths
51
+
52
+
53
+ def unzip_in_folder(my_zip, my_dir):
54
+ with zipfile.ZipFile(my_zip) as zip:
55
+ for zip_info in zip.infolist():
56
+ if zip_info.is_dir():
57
+ continue
58
+ zip_info.filename = os.path.basename(zip_info.filename)
59
+ zip.extract(zip_info, my_dir)
60
+
61
+
62
+ def find_my_model(a_, b_):
63
+
64
+ if a_ is None or a_.endswith(".pth"):
65
+ return a_, b_
66
+
67
+ txt_files = []
68
+ for base_file in [a_, b_]:
69
+ if base_file is not None and base_file.endswith(".txt"):
70
+ txt_files.append(base_file)
71
+
72
+ directory = os.path.dirname(a_)
73
+
74
+ for txt in txt_files:
75
+ with open(txt, 'r') as file:
76
+ first_line = file.readline()
77
+
78
+ download_manager(
79
+ url=first_line.strip(),
80
+ path=directory,
81
+ extension="",
82
+ )
83
+
84
+ for f in find_files(directory):
85
+ if f.endswith(".zip"):
86
+ unzip_in_folder(f, directory)
87
+
88
+ model = None
89
+ index = None
90
+ end_files = find_files(directory)
91
+
92
+ for ff in end_files:
93
+ if ff.endswith(".pth"):
94
+ model = os.path.join(directory, ff)
95
+ gr.Info(f"Model found: {ff}")
96
+ if ff.endswith(".index"):
97
+ index = os.path.join(directory, ff)
98
+ gr.Info(f"Index found: {ff}")
99
+
100
+ if not model:
101
+ gr.Error(f"Model not found in: {end_files}")
102
+
103
+ if not index:
104
+ gr.Warning("Index not found")
105
+
106
+ return model, index
107
+
108
+
109
+ def get_file_size(url):
110
+
111
+ if "huggingface" not in url:
112
+ raise ValueError("Only downloads from Hugging Face are allowed")
113
+
114
+ try:
115
+ with urllib.request.urlopen(url) as response:
116
+ info = response.info()
117
+ content_length = info.get("Content-Length")
118
+
119
+ file_size = int(content_length)
120
+ if file_size > 500000000:
121
+ raise ValueError("The file is too large. You can only download files up to 500 MB in size.")
122
+
123
+ except Exception as e:
124
+ raise e
125
+
126
+
127
+ def clear_files(directory):
128
+ time.sleep(15)
129
+ print(f"Clearing files: {directory}.")
130
+ shutil.rmtree(directory)
131
+
132
+
133
+ def get_my_model(url_data):
134
+
135
+ if not url_data:
136
+ return None, None
137
+
138
+ if "," in url_data:
139
+ a_, b_ = url_data.split()
140
+ a_, b_ = a_.strip().replace("/blob/", "/resolve/"), b_.strip().replace("/blob/", "/resolve/")
141
+ else:
142
+ a_, b_ = url_data.strip().replace("/blob/", "/resolve/"), None
143
+
144
+ out_dir = "downloads"
145
+ folder_download = str(random.randint(1000, 9999))
146
+ directory = os.path.join(out_dir, folder_download)
147
+ os.makedirs(directory, exist_ok=True)
148
+
149
+ try:
150
+ get_file_size(a_)
151
+ if b_:
152
+ get_file_size(b_)
153
+
154
+ valid_url = [a_] if not b_ else [a_, b_]
155
+ for link in valid_url:
156
+ download_manager(
157
+ url=link,
158
+ path=directory,
159
+ extension="",
160
+ )
161
+
162
+ for f in find_files(directory):
163
+ if f.endswith(".zip"):
164
+ unzip_in_folder(f, directory)
165
+
166
+ model = None
167
+ index = None
168
+ end_files = find_files(directory)
169
+
170
+ for ff in end_files:
171
+ if ff.endswith(".pth"):
172
+ model = ff
173
+ gr.Info(f"Model found: {ff}")
174
+ if ff.endswith(".index"):
175
+ index = ff
176
+ gr.Info(f"Index found: {ff}")
177
+
178
+ if not model:
179
+ raise ValueError(f"Model not found in: {end_files}")
180
+
181
+ if not index:
182
+ gr.Warning("Index not found")
183
+ else:
184
+ index = os.path.abspath(index)
185
+
186
+ return os.path.abspath(model), index
187
+
188
+ except Exception as e:
189
+ raise e
190
+ finally:
191
+ # time.sleep(10)
192
+ # shutil.rmtree(directory)
193
+ t = threading.Thread(target=clear_files, args=(directory,))
194
+ t.start()
195
+
196
+
197
+ def add_audio_effects(audio_list):
198
+ print("Audio effects")
199
+
200
+ result = []
201
+ for audio_path in audio_list:
202
+ try:
203
+ output_path = f'{os.path.splitext(audio_path)[0]}_effects.wav'
204
+
205
+ # Initialize audio effects plugins
206
+ board = Pedalboard(
207
+ [
208
+ HighpassFilter(),
209
+ Compressor(ratio=4, threshold_db=-15),
210
+ Reverb(room_size=0.10, dry_level=0.8, wet_level=0.2, damping=0.7)
211
+ ]
212
+ )
213
+
214
+ with AudioFile(audio_path) as f:
215
+ with AudioFile(output_path, 'w', f.samplerate, f.num_channels) as o:
216
+ # Read one second of audio at a time, until the file is empty:
217
+ while f.tell() < f.frames:
218
+ chunk = f.read(int(f.samplerate))
219
+ effected = board(chunk, f.samplerate, reset=False)
220
+ o.write(effected)
221
+ result.append(output_path)
222
+ except Exception as e:
223
+ traceback.print_exc()
224
+ print(f"Error noisereduce: {str(e)}")
225
+ result.append(audio_path)
226
+
227
+ return result
228
+
229
+
230
+ def apply_noisereduce(audio_list):
231
+ # https://github.com/sa-if/Audio-Denoiser
232
+ print("Noice reduce")
233
+
234
+ result = []
235
+ for audio_path in audio_list:
236
+ out_path = f'{os.path.splitext(audio_path)[0]}_noisereduce.wav'
237
+
238
+ try:
239
+ # Load audio file
240
+ audio = AudioSegment.from_file(audio_path)
241
+
242
+ # Convert audio to numpy array
243
+ samples = np.array(audio.get_array_of_samples())
244
+
245
+ # Reduce noise
246
+ reduced_noise = nr.reduce_noise(samples, sr=audio.frame_rate, prop_decrease=0.6)
247
+
248
+ # Convert reduced noise signal back to audio
249
+ reduced_audio = AudioSegment(
250
+ reduced_noise.tobytes(),
251
+ frame_rate=audio.frame_rate,
252
+ sample_width=audio.sample_width,
253
+ channels=audio.channels
254
+ )
255
+
256
+ # Save reduced audio to file
257
+ reduced_audio.export(out_path, format="wav")
258
+ result.append(out_path)
259
+
260
+ except Exception as e:
261
+ traceback.print_exc()
262
+ print(f"Error noisereduce: {str(e)}")
263
+ result.append(audio_path)
264
+
265
+ return result
266
+
267
+
268
+ @spaces.GPU()
269
+ def convert_now(audio_files, random_tag, converter):
270
+ return converter(
271
+ audio_files,
272
+ random_tag,
273
+ overwrite=False,
274
+ parallel_workers=8
275
+ )
276
+
277
+
278
+ def run(
279
+ audio_files,
280
+ file_m,
281
+ pitch_alg,
282
+ pitch_lvl,
283
+ file_index,
284
+ index_inf,
285
+ r_m_f,
286
+ e_r,
287
+ c_b_p,
288
+ active_noise_reduce,
289
+ audio_effects,
290
+ ):
291
+ if not audio_files:
292
+ raise ValueError("The audio pls")
293
+
294
+ if isinstance(audio_files, str):
295
+ audio_files = [audio_files]
296
+
297
+ try:
298
+ duration_base = librosa.get_duration(filename=audio_files[0])
299
+ print("Duration:", duration_base)
300
+ except Exception as e:
301
+ print(e)
302
+
303
+ if file_m is not None and file_m.endswith(".txt"):
304
+ file_m, file_index = find_my_model(file_m, file_index)
305
+ print(file_m, file_index)
306
+
307
+ random_tag = "USER_"+str(random.randint(10000000, 99999999))
308
+
309
+ converter.apply_conf(
310
+ tag=random_tag,
311
+ file_model=file_m,
312
+ pitch_algo=pitch_alg,
313
+ pitch_lvl=pitch_lvl,
314
+ file_index=file_index,
315
+ index_influence=index_inf,
316
+ respiration_median_filtering=r_m_f,
317
+ envelope_ratio=e_r,
318
+ consonant_breath_protection=c_b_p,
319
+ resample_sr=44100 if audio_files[0].endswith('.mp3') else 0,
320
+ )
321
+ time.sleep(0.1)
322
+
323
+ result = convert_now(audio_files, random_tag, converter)
324
+
325
+ if active_noise_reduce:
326
+ result = apply_noisereduce(result)
327
+
328
+ if audio_effects:
329
+ result = add_audio_effects(result)
330
+
331
+ return result
332
+
333
+
334
+ def audio_conf():
335
+ return gr.File(
336
+ label="Audio files",
337
+ file_count="multiple",
338
+ type="filepath",
339
+ container=True,
340
+ )
341
+
342
+
343
+ def model_conf():
344
+ return gr.File(
345
+ label="Model file",
346
+ type="filepath",
347
+ height=130,
348
+ )
349
+
350
+
351
+ def pitch_algo_conf():
352
+ return gr.Dropdown(
353
+ PITCH_ALGO_OPT,
354
+ value=PITCH_ALGO_OPT[4],
355
+ label="Pitch algorithm",
356
+ visible=True,
357
+ interactive=True,
358
+ )
359
+
360
+
361
+ def pitch_lvl_conf():
362
+ return gr.Slider(
363
+ label="Pitch level",
364
+ minimum=-24,
365
+ maximum=24,
366
+ step=1,
367
+ value=0,
368
+ visible=True,
369
+ interactive=True,
370
+ )
371
+
372
+
373
+ def index_conf():
374
+ return gr.File(
375
+ label="Index file",
376
+ type="filepath",
377
+ height=130,
378
+ )
379
+
380
+
381
+ def index_inf_conf():
382
+ return gr.Slider(
383
+ minimum=0,
384
+ maximum=1,
385
+ label="Index influence",
386
+ value=0.75,
387
+ )
388
+
389
+
390
+ def respiration_filter_conf():
391
+ return gr.Slider(
392
+ minimum=0,
393
+ maximum=7,
394
+ label="Respiration median filtering",
395
+ value=3,
396
+ step=1,
397
+ interactive=True,
398
+ )
399
+
400
+
401
+ def envelope_ratio_conf():
402
+ return gr.Slider(
403
+ minimum=0,
404
+ maximum=1,
405
+ label="Envelope ratio",
406
+ value=0.25,
407
+ interactive=True,
408
+ )
409
+
410
+
411
+ def consonant_protec_conf():
412
+ return gr.Slider(
413
+ minimum=0,
414
+ maximum=0.5,
415
+ label="Consonant breath protection",
416
+ value=0.5,
417
+ interactive=True,
418
+ )
419
+
420
+
421
+ def button_conf():
422
+ return gr.Button(
423
+ "Inference",
424
+ variant="primary",
425
+ )
426
+
427
+
428
+ def output_conf():
429
+ return gr.File(
430
+ label="Result",
431
+ file_count="multiple",
432
+ interactive=False,
433
+ )
434
+
435
+
436
+ def active_tts_conf():
437
+ return gr.Checkbox(
438
+ False,
439
+ label="TTS",
440
+ # info="",
441
+ container=False,
442
+ )
443
+
444
+
445
+ def tts_voice_conf():
446
+ return gr.Dropdown(
447
+ label="tts voice",
448
+ choices=voices,
449
+ visible=False,
450
+ value="en-US-EmmaMultilingualNeural-Female",
451
+ )
452
+
453
+
454
+ def tts_text_conf():
455
+ return gr.Textbox(
456
+ value="",
457
+ placeholder="Write the text here...",
458
+ label="Text",
459
+ visible=False,
460
+ lines=3,
461
+ )
462
+
463
+
464
+ def tts_button_conf():
465
+ return gr.Button(
466
+ "Process TTS",
467
+ variant="secondary",
468
+ visible=False,
469
+ )
470
+
471
+
472
+ def tts_play_conf():
473
+ return gr.Checkbox(
474
+ False,
475
+ label="Play",
476
+ # info="",
477
+ container=False,
478
+ visible=False,
479
+ )
480
+
481
+
482
+ def sound_gui():
483
+ return gr.Audio(
484
+ value=None,
485
+ type="filepath",
486
+ # format="mp3",
487
+ autoplay=True,
488
+ visible=False,
489
+ )
490
+
491
+
492
+ def denoise_conf():
493
+ return gr.Checkbox(
494
+ False,
495
+ label="Denoise",
496
+ # info="",
497
+ container=False,
498
+ visible=True,
499
+ )
500
+
501
+
502
+ def effects_conf():
503
+ return gr.Checkbox(
504
+ False,
505
+ label="Reverb",
506
+ # info="",
507
+ container=False,
508
+ visible=True,
509
+ )
510
+
511
+
512
+ def infer_tts_audio(tts_voice, tts_text, play_tts):
513
+ out_dir = "output"
514
+ folder_tts = "USER_"+str(random.randint(10000, 99999))
515
+
516
+ os.makedirs(out_dir, exist_ok=True)
517
+ os.makedirs(os.path.join(out_dir, folder_tts), exist_ok=True)
518
+ out_path = os.path.join(out_dir, folder_tts, "tts.mp3")
519
+
520
+ asyncio.run(edge_tts.Communicate(tts_text, "-".join(tts_voice.split('-')[:-1])).save(out_path))
521
+ if play_tts:
522
+ return [out_path], out_path
523
+ return [out_path], None
524
+
525
+
526
+ def show_components_tts(value_active):
527
+ return gr.update(
528
+ visible=value_active
529
+ ), gr.update(
530
+ visible=value_active
531
+ ), gr.update(
532
+ visible=value_active
533
+ ), gr.update(
534
+ visible=value_active
535
+ )
536
+
537
+
538
+ def down_active_conf():
539
+ return gr.Checkbox(
540
+ False,
541
+ label="URL-to-Model",
542
+ # info="",
543
+ container=False,
544
+ )
545
+
546
+
547
+ def down_url_conf():
548
+ return gr.Textbox(
549
+ value="",
550
+ placeholder="Write the url here...",
551
+ label="Enter URL",
552
+ visible=False,
553
+ lines=1,
554
+ )
555
+
556
+
557
+ def down_button_conf():
558
+ return gr.Button(
559
+ "Process",
560
+ variant="secondary",
561
+ visible=False,
562
+ )
563
+
564
+
565
+ def show_components_down(value_active):
566
+ return gr.update(
567
+ visible=value_active
568
+ ), gr.update(
569
+ visible=value_active
570
+ ), gr.update(
571
+ visible=value_active
572
+ )
573
+
574
+
575
+ def get_gui(theme):
576
+ with gr.Blocks(theme=theme, fill_width=True, fill_height=True, delete_cache=(3200, 3200)) as app:
577
+ gr.Markdown(title)
578
+ gr.Markdown(description)
579
+
580
+ active_tts = active_tts_conf()
581
+ with gr.Row():
582
+ with gr.Column(scale=1):
583
+ tts_text = tts_text_conf()
584
+ with gr.Column(scale=2):
585
+ with gr.Row():
586
+ with gr.Column():
587
+ with gr.Row():
588
+ tts_voice = tts_voice_conf()
589
+ tts_active_play = tts_play_conf()
590
+
591
+ tts_button = tts_button_conf()
592
+ tts_play = sound_gui()
593
+
594
+ active_tts.change(
595
+ fn=show_components_tts,
596
+ inputs=[active_tts],
597
+ outputs=[tts_voice, tts_text, tts_button, tts_active_play],
598
+ )
599
+
600
+ aud = audio_conf()
601
+ # gr.HTML("<hr>")
602
+
603
+ tts_button.click(
604
+ fn=infer_tts_audio,
605
+ inputs=[tts_voice, tts_text, tts_active_play],
606
+ outputs=[aud, tts_play],
607
+ )
608
+
609
+ down_active_gui = down_active_conf()
610
+ down_info = gr.Markdown(
611
+ "Provide a link to a zip file, like this one: `https://huggingface.co/mrmocciai/Models/resolve/main/Genshin%20Impact/ayaka-v2.zip?download=true`, or separate links with a comma for the .pth and .index files, like this: `https://huggingface.co/sail-rvc/ayaka-jp/resolve/main/model.pth?download=true, https://huggingface.co/sail-rvc/ayaka-jp/resolve/main/model.index?download=true`",
612
+ visible=False
613
+ )
614
+ with gr.Row():
615
+ with gr.Column(scale=3):
616
+ down_url_gui = down_url_conf()
617
+ with gr.Column(scale=1):
618
+ down_button_gui = down_button_conf()
619
+
620
+ with gr.Column():
621
+ with gr.Row():
622
+ model = model_conf()
623
+ indx = index_conf()
624
+
625
+ down_active_gui.change(
626
+ show_components_down,
627
+ [down_active_gui],
628
+ [down_info, down_url_gui, down_button_gui]
629
+ )
630
+
631
+ down_button_gui.click(
632
+ get_my_model,
633
+ [down_url_gui],
634
+ [model, indx]
635
+ )
636
+
637
+ algo = pitch_algo_conf()
638
+ algo_lvl = pitch_lvl_conf()
639
+ indx_inf = index_inf_conf()
640
+ res_fc = respiration_filter_conf()
641
+ envel_r = envelope_ratio_conf()
642
+ const = consonant_protec_conf()
643
+ with gr.Row():
644
+ with gr.Column():
645
+ with gr.Row():
646
+ denoise_gui = denoise_conf()
647
+ effects_gui = effects_conf()
648
+ button_base = button_conf()
649
+ output_base = output_conf()
650
+
651
+ button_base.click(
652
+ run,
653
+ inputs=[
654
+ aud,
655
+ model,
656
+ algo,
657
+ algo_lvl,
658
+ indx,
659
+ indx_inf,
660
+ res_fc,
661
+ envel_r,
662
+ const,
663
+ denoise_gui,
664
+ effects_gui,
665
+ ],
666
+ outputs=[output_base],
667
+ )
668
+
669
+ gr.Examples(
670
+ examples=[
671
+ [
672
+ ["./test.ogg"],
673
+ "./model.pth",
674
+ "rmvpe+",
675
+ 0,
676
+ "./model.index",
677
+ 0.75,
678
+ 3,
679
+ 0.25,
680
+ 0.50,
681
+ ],
682
+ [
683
+ ["./example2/test2.ogg"],
684
+ "./example2/model_link.txt",
685
+ "rmvpe+",
686
+ 0,
687
+ "./example2/index_link.txt",
688
+ 0.75,
689
+ 3,
690
+ 0.25,
691
+ 0.50,
692
+ ],
693
+ [
694
+ ["./example3/test3.wav"],
695
+ "./example3/zip_link.txt",
696
+ "rmvpe+",
697
+ 0,
698
+ None,
699
+ 0.75,
700
+ 3,
701
+ 0.25,
702
+ 0.50,
703
+ ],
704
+
705
+ ],
706
+ fn=run,
707
+ inputs=[
708
+ aud,
709
+ model,
710
+ algo,
711
+ algo_lvl,
712
+ indx,
713
+ indx_inf,
714
+ res_fc,
715
+ envel_r,
716
+ const,
717
+ ],
718
+ outputs=[output_base],
719
+ cache_examples=False,
720
+ )
721
+
722
+ return app
723
+
724
+
725
+ if __name__ == "__main__":
726
+
727
+ tts_voice_list = asyncio.new_event_loop().run_until_complete(edge_tts.list_voices())
728
+ voices = sorted([f"{v['ShortName']}-{v['Gender']}" for v in tts_voice_list])
729
+
730
+ app = get_gui(theme)
731
+
732
+ app.queue(default_concurrency_limit=40)
733
+
734
+ app.launch(
735
+ max_threads=40,
736
+ share=False,
737
+ show_error=True,
738
+ quiet=False,
739
+ debug=False,
740
+ ssr_mode=False,
741
+ allowed_paths=["./downloads/"],
742
+ )
pre-requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ pip==24.0
requirements.txt CHANGED
@@ -1,6 +1,8 @@
1
- torch==2.2.0
2
- infer-rvc-python==1.1.0
3
- edge-tts
4
- pedalboard
5
- noisereduce
6
- numpy==1.23.5
 
 
 
1
+ torch==2.2.0
2
+ infer-rvc-python==1.1.0
3
+ edge-tts
4
+ pedalboard
5
+ noisereduce
6
+ numpy==1.23.5
7
+ transformers<=4.48.3
8
+ pydantic==2.10.6