R-Kentaren commited on
Commit
ee9bbe0
·
verified ·
1 Parent(s): 660307f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +313 -641
app.py CHANGED
@@ -8,735 +8,407 @@ 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
- )
 
 
8
  import soundfile as sf
9
  from infer_rvc_python.main import download_manager
10
  import zipfile
 
11
  import asyncio
12
  import librosa
13
  import traceback
 
 
 
 
 
14
  import numpy as np
15
  import urllib.request
16
  import shutil
17
  import threading
18
+ from pedalboard import Pedalboard, Reverb, Compressor, HighpassFilter
19
+ from pedalboard.io import AudioFile
20
+ from pydub import AudioSegment
21
+ import noisereduce as nr
22
+ import edge_tts
23
 
24
+ # Suppress logging
25
  logging.getLogger("infer_rvc_python").setLevel(logging.ERROR)
26
 
27
+ # Initialize converter
28
  converter = BaseLoader(only_cpu=False, hubert_path=None, rmvpe_path=None)
29
 
30
+ # Theme & Title
31
+ title = "<center><strong><font size='7'>🔊 RVC+</font></strong></center>"
32
+ description = """
33
+ <div style="text-align: center; font-size: 1.1em; color: #aaa;">
34
+ This demo is for educational and research purposes only.<br>
35
+ Misuse of voice conversion technology is unethical. Use responsibly.<br>
36
+ Authors are not liable for inappropriate usage.
37
+ </div>
38
+ """
39
+ theme = gr.themes.Soft(primary_hue="indigo", secondary_hue="blue").set(
40
+ body_background_fill="*neutral_50"
41
+ )
42
 
43
+ # Global constants
44
+ PITCH_ALGO_OPT = ["pm", "harvest", "crepe", "rmvpe", "rmvpe+"]
45
+ MAX_FILE_SIZE = 500 * 1024 * 1024 # 500 MB
46
+ DOWNLOAD_DIR = "downloads"
47
+ OUTPUT_DIR = "output"
48
 
49
+ os.makedirs(DOWNLOAD_DIR, exist_ok=True)
50
+ os.makedirs(OUTPUT_DIR, exist_ok=True)
 
 
 
 
 
51
 
 
52
 
53
+ # --- Utility Functions ---
54
+ def find_files(directory, exts=(".pth", ".index", ".zip")):
55
+ return [os.path.join(directory, f) for f in os.listdir(directory)
56
+ if f.endswith(exts)]
57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
 
59
+ def unzip_in_folder(zip_path, extract_to):
60
+ with zipfile.ZipFile(zip_path, 'r') as zip_ref:
61
+ for member in zip_ref.infolist():
62
+ if not member.is_dir():
63
+ member.filename = os.path.basename(member.filename)
64
+ zip_ref.extract(member, extract_to)
 
 
 
 
 
 
 
 
 
65
 
66
 
67
  def get_file_size(url):
68
+ if "huggingface" not in url.lower():
69
+ raise ValueError(" Only Hugging Face links are allowed.")
 
 
70
  try:
71
  with urllib.request.urlopen(url) as response:
72
+ file_size = int(response.headers.get("Content-Length", 0))
73
+ if file_size > MAX_FILE_SIZE:
74
+ raise ValueError(f"⚠️ File too large: {file_size / 1e6:.1f} MB (>500MB)")
75
+ return file_size
76
+ except Exception as e:
77
+ raise RuntimeError(f"❌ Failed to fetch file info: {str(e)}")
78
 
 
 
 
79
 
80
+ def clear_directory_later(directory, delay=15):
81
+ """Clear temp directory after delay in a background thread."""
82
+ def _clear():
83
+ time.sleep(delay)
84
+ if os.path.exists(directory):
85
+ shutil.rmtree(directory, ignore_errors=True)
86
+ print(f"🧹 Cleaned up: {directory}")
87
+ threading.Thread(target=_clear, daemon=True).start()
88
 
89
 
90
+ def find_model_and_index(directory):
91
+ files = find_files(directory)
92
+ model = next((f for f in files if f.endswith(".pth")), None)
93
+ index = next((f for f in files if f.endswith(".index")), None)
94
+ return model, index
95
 
96
 
97
+ # --- Model Download Handler ---
98
+ @spaces.GPU(duration=60)
99
+ def download_model(url_data):
100
+ if not url_data.strip():
101
+ raise ValueError("❌ No URL provided.")
102
 
103
+ urls = [u.strip().replace("/blob/", "/resolve/") for u in url_data.split(",") if u.strip()]
104
+ if len(urls) > 2:
105
+ raise ValueError("❌ Provide up to two URLs (model.pth, index.index).")
106
 
107
+ # Validate size first
108
+ for url in urls:
109
+ get_file_size(url)
 
 
110
 
111
+ folder_name = f"model_{random.randint(1000, 9999)}"
112
+ directory = os.path.join(DOWNLOAD_DIR, folder_name)
 
113
  os.makedirs(directory, exist_ok=True)
114
 
115
  try:
116
+ for url in urls:
117
+ download_manager(url=url, path=directory, extension="")
 
 
 
 
 
 
 
 
 
 
 
 
 
118
 
119
+ # Unzip if needed
120
+ for f in find_files(directory, (".zip",)):
121
+ unzip_in_folder(f, directory)
122
 
123
+ model, index = find_model_and_index(directory)
 
 
 
 
 
 
124
 
125
  if not model:
126
+ raise ValueError(" .pth model file not found in downloaded content.")
127
+ gr.Info(f"✅ Model loaded: {os.path.basename(model)}")
128
+ if index:
129
+ gr.Info(f"📌 Index loaded: {os.path.basename(index)}")
130
  else:
131
+ gr.Warning("⚠️ Index file not found – conversion may be less accurate.")
132
 
133
+ # Schedule cleanup
134
+ clear_directory_later(directory, delay=30)
135
 
136
+ return os.path.abspath(model), os.path.abspath(index) if index else None
 
 
 
 
 
 
137
 
138
+ except Exception as e:
139
+ shutil.rmtree(directory, ignore_errors=True)
140
+ raise gr.Error(f"❌ Download failed: {str(e)}")
141
 
 
 
142
 
143
+ # --- Audio Processing ---
144
+ def apply_noisereduce(audio_paths):
145
+ results = []
146
+ for path in audio_paths:
147
+ out_path = f"{os.path.splitext(path)[0]}_denoised.wav"
148
  try:
149
+ audio = AudioSegment.from_file(path)
150
+ samples = np.array(audio.get_array_of_samples())
151
+ sr = audio.frame_rate
152
+ reduced = nr.reduce_noise(y=samples.astype(np.float32), sr=sr, prop_decrease=0.6)
153
+ reduced_audio = AudioSegment(
154
+ reduced.tobytes(),
155
+ frame_rate=sr,
156
+ sample_width=audio.sample_width,
157
+ channels=audio.channels
158
  )
159
+ reduced_audio.export(out_path, format="wav")
160
+ results.append(out_path)
161
+ gr.Info("🔊 Noise reduction applied.")
162
+ except Exception as e:
163
+ print(f"Noise reduction failed: {e}")
164
+ results.append(path)
165
+ return results
166
+
167
+
168
+ def apply_audio_effects(audio_paths):
169
+ results = []
170
+ board = Pedalboard([
171
+ HighpassFilter(cutoff_frequency_hz=80),
172
+ Compressor(ratio=4, threshold_db=-15),
173
+ Reverb(room_size=0.15, damping=0.7, wet_level=0.15, dry_level=0.85)
174
+ ])
175
+ for path in audio_paths:
176
+ out_path = f"{os.path.splitext(path)[0]}_reverb.wav"
177
+ try:
178
+ with AudioFile(path) as f:
179
+ with AudioFile(out_path, 'w', f.samplerate, f.num_channels) as o:
180
  while f.tell() < f.frames:
181
  chunk = f.read(int(f.samplerate))
182
+ effected = board(chunk, f.samplerate)
183
  o.write(effected)
184
+ results.append(out_path)
185
+ gr.Info("🎛️ Audio effects applied.")
186
  except Exception as e:
187
+ print(f"Effects failed: {e}")
188
+ results.append(path)
189
+ return results
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
190
 
 
 
191
 
192
+ # --- TTS Handler ---
193
+ async def generate_tts(text, voice, output_path):
194
+ communicate = edge_tts.Communicate(text, voice.split("-")[0])
195
+ await communicate.save(output_path)
196
 
 
 
 
 
 
 
 
197
 
198
+ def infer_tts(tts_voice, tts_text, play_tts):
199
+ if not tts_text.strip():
200
+ raise ValueError("❌ Text is empty.")
201
+ folder = f"tts_{random.randint(10000, 99999)}"
202
+ out_dir = os.path.join(OUTPUT_DIR, folder)
203
+ os.makedirs(out_dir, exist_ok=True)
204
+ out_path = os.path.join(out_dir, "tts_output.mp3")
 
205
 
206
+ try:
207
+ asyncio.run(generate_tts(tts_text, tts_voice, out_path))
208
+ if play_tts:
209
+ return [out_path], out_path
210
+ return [out_path], None
211
+ except Exception as e:
212
+ raise gr.Error(f"TTS generation failed: {str(e)}")
213
 
214
 
215
+ # --- Main Conversion Function ---
216
  @spaces.GPU()
217
+ def run_conversion(
 
 
 
 
 
 
 
 
 
218
  audio_files,
219
+ model_path,
220
+ pitch_algo,
221
+ pitch_level,
222
+ index_path,
223
+ index_rate,
224
+ filter_radius,
225
+ rms_mix_rate,
226
+ protect,
227
+ denoise,
228
+ effects,
229
  ):
230
  if not audio_files:
231
+ raise ValueError(" Please upload at least one audio file.")
232
 
233
+ random_tag = f"USER_{random.randint(10000000, 99999999)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
234
 
235
+ # Configure converter
236
  converter.apply_conf(
237
  tag=random_tag,
238
+ file_model=model_path,
239
+ pitch_algo=pitch_algo,
240
+ pitch_lvl=pitch_level,
241
+ file_index=index_path,
242
+ index_influence=index_rate,
243
+ respiration_median_filtering=int(filter_radius),
244
+ envelope_ratio=rms_mix_rate,
245
+ consonant_breath_protection=protect,
246
+ resample_sr=44100 if any(f.endswith(".mp3") for f in audio_files) else 0,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
247
  )
248
 
249
+ # Run conversion
250
+ try:
251
+ results = converter(audio_files, random_tag, overwrite=False, parallel_workers=8)
252
+ except Exception as e:
253
+ raise gr.Error(f"❌ Conversion failed: {str(e)}")
254
 
255
+ # Post-processing
256
+ if denoise:
257
+ results = apply_noisereduce(results)
258
+ if effects:
259
+ results = apply_audio_effects(results)
 
260
 
261
+ return results
262
 
 
 
 
 
 
 
 
 
263
 
264
+ # --- Gradio UI Builder ---
265
+ def create_ui():
266
+ with gr.Blocks(theme=theme, title="RVC+", fill_width=True, delete_cache=(3200, 3200)) as app:
267
+ gr.HTML(title)
268
+ gr.HTML(description)
269
 
270
+ with gr.Tabs():
271
+ # ============= TAB 1: Voice Conversion =============
272
+ with gr.Tab("🎤 Voice Conversion", id=0):
273
+ gr.Markdown("### 🔊 Upload audio and select your model.")
274
 
 
 
 
 
 
275
  with gr.Row():
276
+ with gr.Column(scale=2):
277
+ audio_input = gr.File(
278
+ label="Upload Audio (WAV, MP3, OGG)",
279
+ file_count="multiple",
280
+ type="filepath"
281
+ )
282
+ with gr.Column(scale=1):
283
+ with gr.Group():
284
+ model_file = gr.File(label="Upload .pth Model", type="filepath", height=100)
285
+ index_file = gr.File(label="Upload .index File (Optional)", type="filepath", height=100)
286
+
287
+ # Download Model Section
288
+ gr.Markdown("📥 Or download model from URL:")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
289
  with gr.Row():
290
+ use_url = gr.Checkbox(label="🌐 Use Model URL", value=False)
291
+ with gr.Group(visible=False) as url_group:
292
+ gr.Markdown(
293
+ "🔗 Paste Hugging Face link(s):<br>"
294
+ "• Single ZIP: `https://hf.co/.../model.zip`<br>"
295
+ "• Two links: `https://hf.co/.../model.pth, https://hf.co/.../model.index`"
296
+ )
297
+ model_url = gr.Textbox(
298
+ placeholder="https://huggingface.co/...",
299
+ label="Model URL(s)",
300
+ lines=1
301
+ )
302
+ download_btn = gr.Button("⬇️ Download Model", variant="secondary")
303
+
304
+ download_btn.click(
305
+ download_model,
306
+ inputs=[model_url],
307
+ outputs=[model_file, index_file]
308
+ )
309
+ use_url.change(
310
+ lambda x: gr.update(visible=x),
311
+ inputs=[use_url],
312
+ outputs=[url_group]
313
+ )
314
+
315
+ # Parameters
316
+ gr.Markdown("### ⚙️ Conversion Settings")
317
+ with gr.Row():
318
+ pitch_algo = gr.Dropdown(PITCH_ALGO_OPT, value="rmvpe+", label="Pitch Algorithm")
319
+ pitch_level = gr.Slider(-24, 24, value=0, step=1, label="Pitch Level")
320
+ with gr.Row():
321
+ index_rate = gr.Slider(0, 1, value=0.75, label="Index Influence")
322
+ filter_radius = gr.Slider(0, 7, value=3, step=1, label="Median Filter")
323
+ with gr.Row():
324
+ rms_mix_rate = gr.Slider(0, 1, value=0.25, label="Volume Envelope")
325
+ protect = gr.Slider(0, 0.5, value=0.5, label="Consonant Protection")
326
 
327
+ # Post-processing
328
+ with gr.Row():
329
+ denoise = gr.Checkbox(False, label="🔇 Denoise Output")
330
+ reverb = gr.Checkbox(False, label="🎛️ Add Reverb")
331
+
332
+ # Run Button
333
+ convert_btn = gr.Button("🚀 Convert Voice", variant="primary")
334
+ output_files = gr.File(label="✅ Converted Audio", file_count="multiple")
335
+
336
+ convert_btn.click(
337
+ run_conversion,
338
+ inputs=[
339
+ audio_input,
340
+ model_file,
341
+ pitch_algo,
342
+ pitch_level,
343
+ index_file,
344
+ index_rate,
345
+ filter_radius,
346
+ rms_mix_rate,
347
+ protect,
348
+ denoise,
349
+ reverb,
350
+ ],
351
+ outputs=output_files,
352
+ )
353
+
354
+ # ============= TAB 2: Text-to-Speech =============
355
+ with gr.Tab("🗣️ Text-to-Speech", id=1):
356
+ gr.Markdown("### Convert text to speech using Edge TTS.")
357
+
358
+ tts_voice_list = sorted(
359
+ [f"{v['ShortName']}-{v['Gender']}" for v in asyncio.run(edge_tts.list_voices())]
360
+ )
361
+ with gr.Row():
362
+ with gr.Column():
363
+ tts_text = gr.Textbox(
364
+ placeholder="Type your message here...",
365
+ label="Text Input",
366
+ lines=4
367
+ )
368
+ tts_voice = gr.Dropdown(tts_voice_list, value=tts_voice_list[0], label="Voice")
369
+ tts_play = gr.Checkbox(False, label="🎧 Auto-play audio")
370
+ tts_btn = gr.Button("🔊 Generate Speech", variant="secondary")
371
+ with gr.Column():
372
+ tts_output_audio = gr.File(label="Generated Audio", type="filepath")
373
+ tts_preview = gr.Audio(label="Preview", visible=False, autoplay=True)
374
+
375
+ tts_btn.click(
376
+ infer_tts,
377
+ inputs=[tts_voice, tts_text, tts_play],
378
+ outputs=[tts_output_audio, tts_preview],
379
+ ).then(
380
+ lambda x: gr.update(visible=bool(x)),
381
+ inputs=[tts_preview],
382
+ outputs=[tts_preview]
383
+ )
384
+
385
+ # Examples
386
+ gr.Markdown("### 📚 Examples")
387
  gr.Examples(
388
  examples=[
389
+ ["./test.ogg", "./model.pth", "rmvpe+", 0, "./model.index", 0.75, 3, 0.25, 0.5, False, False],
390
+ ["./example3/test3.wav", "./example3/zip_link.txt", "rmvpe+", 0, None, 0.75, 3, 0.25, 0.5, True, True],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
391
  ],
 
392
  inputs=[
393
+ audio_input, model_file, pitch_algo, pitch_level, index_file,
394
+ index_rate, filter_radius, rms_mix_rate, protect, denoise, reverb
 
 
 
 
 
 
 
395
  ],
396
+ outputs=output_files,
397
+ fn=run_conversion,
398
  cache_examples=False,
399
  )
400
 
401
  return app
402
 
403
 
404
+ # --- Launch App ---
405
  if __name__ == "__main__":
406
+ app = create_ui()
407
+ app.queue(default_concurrency_limit=10)
 
 
 
 
 
 
408
  app.launch(
409
+ share=True,
 
 
 
410
  debug=False,
411
+ show_api=False,
412
+ max_threads=40,
413
+ allowed_paths=[DOWNLOAD_DIR, OUTPUT_DIR],
414
+ )