File size: 35,946 Bytes
e71a85c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1291663
 
bdfa06b
e71a85c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bd8aed8
 
 
e71a85c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96e3ef5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2a362c7
 
580bab3
e71a85c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
import gradio as gr
import requests
import random
import os
import zipfile 
import librosa
import time
from infer_rvc_python import BaseLoader
from pydub import AudioSegment
from tts_voice import tts_order_voice
import edge_tts
import tempfile
from audio_separator.separator import Separator
import model_handler
import psutil
import cpuinfo
from gradio.themes.base import Base
from gradio.themes.utils import colors, fonts, sizes
from typing import Iterable

language_dict = tts_order_voice

async def text_to_speech_edge(text, language_code):
    voice = language_dict[language_code]
    communicate = edge_tts.Communicate(text, voice)
    with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
        tmp_path = tmp_file.name

    await communicate.save(tmp_path)

    return tmp_path

try:
    import spaces
    spaces_status = True
except ImportError:
    spaces_status = False

separator = Separator()
converter = BaseLoader(only_cpu=False, hubert_path=None, rmvpe_path=None)

global pth_file
global index_file

pth_file = "model.pth"
index_file = "model.index"

#CONFIGS
TEMP_DIR = "temp"
MODEL_PREFIX = "model"
PITCH_ALGO_OPT = [
    "pm",
    "harvest",
    "crepe",
    "rmvpe",
    "rmvpe+",
]
UVR_5_MODELS = [
    {"model_name": "BS-Roformer-Viperx-1297", "checkpoint": "model_bs_roformer_ep_317_sdr_12.9755.ckpt"},
    {"model_name": "MDX23C-InstVoc HQ 2", "checkpoint": "MDX23C-8KFFT-InstVoc_HQ_2.ckpt"},
    {"model_name": "Kim Vocal 2", "checkpoint": "Kim_Vocal_2.onnx"},
    {"model_name": "5_HP-Karaoke", "checkpoint": "5_HP-Karaoke-UVR.pth"},
    {"model_name": "UVR-DeNoise by FoxJoy", "checkpoint": "UVR-DeNoise.pth"},
    {"model_name": "UVR-DeEcho-DeReverb by FoxJoy", "checkpoint": "UVR-DeEcho-DeReverb.pth"},
]
MODELS = [
    {"model": "model.pth", "index": "model.index", "model_name": "Test Model"},
]

os.makedirs(TEMP_DIR, exist_ok=True)

def unzip_file(file):
    filename = os.path.basename(file).split(".")[0] 
    with zipfile.ZipFile(file, 'r') as zip_ref:
        zip_ref.extractall(os.path.join(TEMP_DIR, filename)) 
    return True
    

def progress_bar(total, current):
    return "[" + "=" * int(current / total * 20) + ">" + " " * (20 - int(current / total * 20)) + "] " + str(int(current / total * 100)) + "%"

def download_from_url(url, name=None):
    if name is None:
        raise ValueError("The model name must be provided")
    if "/blob/" in url:
        url = url.replace("/blob/", "/resolve/") 
    if "huggingface" not in url:
        return ["The URL must be from huggingface", "Failed", "Failed"]
    filename = os.path.join(TEMP_DIR, MODEL_PREFIX + str(random.randint(1, 1000)) + ".zip")
    response = requests.get(url)
    total = int(response.headers.get('content-length', 0)) 
    if total > 500000000:

        return ["The file is too large. You can only download files up to 500 MB in size.", "Failed", "Failed"]
    current = 0
    with open(filename, "wb") as f:
        for data in response.iter_content(chunk_size=4096): 
            f.write(data)
            current += len(data)
            print(progress_bar(total, current), end="\r") #
    
    

    try:
        unzip_file(filename)
    except Exception as e:
        return ["Failed to unzip the file", "Failed", "Failed"] 
    unzipped_dir = os.path.join(TEMP_DIR, os.path.basename(filename).split(".")[0])
    pth_files = []
    index_files = []
    for root, dirs, files in os.walk(unzipped_dir): 
        for file in files:
            if file.endswith(".pth"):
                pth_files.append(os.path.join(root, file))
            elif file.endswith(".index"):
                index_files.append(os.path.join(root, file))
    
    print(pth_files, index_files) 
    global pth_file
    global index_file
    pth_file = pth_files[0]
    index_file = index_files[0]

    print(pth_file)
    print(index_file)

    if name == "":
        name = pth_file.split(".")[0]

    MODELS.append({"model": pth_file, "index": index_file, "model_name": name})
    return ["Downloaded as " + name, pth_files[0], index_files[0]]

def inference(audio, model_name):
        output_data = inf_handler(audio, model_name)
        vocals = output_data[0]
        inst = output_data[1]

        return vocals, inst

if spaces_status:
    @spaces.GPU()
    def convert_now(audio_files, random_tag, converter):
        return converter(
            audio_files,
            random_tag,
            overwrite=False,
            parallel_workers=8
        )

        
else:
    def convert_now(audio_files, random_tag, converter):
        return converter(
            audio_files,
            random_tag,
            overwrite=False,
            parallel_workers=8
        )

def calculate_remaining_time(epochs, seconds_per_epoch):
    total_seconds = epochs * seconds_per_epoch

    hours = total_seconds // 3600
    minutes = (total_seconds % 3600) // 60
    seconds = total_seconds % 60

    if hours == 0:
        return f"{int(minutes)} minutes"
    elif hours == 1:
        return f"{int(hours)} hour and {int(minutes)} minutes"
    else:
        return f"{int(hours)} hours and {int(minutes)} minutes"

def inf_handler(audio, model_name): 
    model_found = False
    for model_info in UVR_5_MODELS:
        if model_info["model_name"] == model_name:
            separator.load_model(model_info["checkpoint"])
            model_found = True
            break
    if not model_found:
        separator.load_model()
    output_files = separator.separate(audio)
    vocals = output_files[0]
    inst = output_files[1]
    return vocals, inst

    
def run(
    model,
    audio_files,
    pitch_alg,
    pitch_lvl,
    index_inf,
    r_m_f,
    e_r,
    c_b_p,
):
    if not audio_files:
        raise ValueError("The audio pls")

    if isinstance(audio_files, str):
        audio_files = [audio_files]

    try:
        duration_base = librosa.get_duration(filename=audio_files[0])
        print("Duration:", duration_base)
    except Exception as e:
        print(e)

    random_tag = "USER_"+str(random.randint(10000000, 99999999))

    file_m = model
    print("File model:", file_m)

    # get from MODELS
    for model in MODELS:
        if model["model_name"] == file_m:
            print(model)
            file_m = model["model"]
            file_index = model["index"]
            break
    
    if not file_m.endswith(".pth"):
        raise ValueError("The model file must be a .pth file")


    print("Random tag:", random_tag)
    print("File model:", file_m)
    print("Pitch algorithm:", pitch_alg)
    print("Pitch level:", pitch_lvl)
    print("File index:", file_index)
    print("Index influence:", index_inf)
    print("Respiration median filtering:", r_m_f)
    print("Envelope ratio:", e_r)

    converter.apply_conf(
        tag=random_tag,
        file_model=file_m,
        pitch_algo=pitch_alg,
        pitch_lvl=pitch_lvl,
        file_index=file_index,
        index_influence=index_inf,
        respiration_median_filtering=r_m_f,
        envelope_ratio=e_r,
        consonant_breath_protection=c_b_p,
        resample_sr=44100 if audio_files[0].endswith('.mp3') else 0, 
    )
    time.sleep(0.1)

    result = convert_now(audio_files, random_tag, converter)
    print("Result:", result)

    return result[0]

def upload_model(index_file, pth_file, model_name):
    pth_file = pth_file.name
    index_file = index_file.name
    MODELS.append({"model": pth_file, "index": index_file, "model_name": model_name})
    return "Uploaded!"  

class Applio(Base):
    def __init__(
        self,
        *,
        primary_hue: colors.Color | str = colors.neutral,
        secondary_hue: colors.Color | str = colors.neutral,
        neutral_hue: colors.Color | str = colors.neutral,
        spacing_size: sizes.Size | str = sizes.spacing_md,
        radius_size: sizes.Size | str = sizes.radius_md,
        text_size: sizes.Size | str = sizes.text_lg,
        font: fonts.Font | str | Iterable[fonts.Font | str] = (
            "Syne V",
            fonts.GoogleFont("Syne"),
            "ui-sans-serif",
            "system-ui",
        ),
        font_mono: fonts.Font | str | Iterable[fonts.Font | str] = (
            "ui-monospace",
            fonts.GoogleFont("Nunito Sans"),
        ),
    ):
        super().__init__(
            primary_hue=primary_hue,
            secondary_hue=secondary_hue,
            neutral_hue=neutral_hue,
            spacing_size=spacing_size,
            radius_size=radius_size,
            text_size=text_size,
            font=font,
            font_mono=font_mono,
        )
        self.name = ("Applio",)
        self.secondary_100 = ("#dbeafe",)
        self.secondary_200 = ("#bfdbfe",)
        self.secondary_300 = ("#93c5fd",)
        self.secondary_400 = ("#60a5fa",)
        self.secondary_50 = ("#eff6ff",)
        self.secondary_500 = ("#3b82f6",)
        self.secondary_600 = ("#2563eb",)
        self.secondary_700 = ("#1d4ed8",)
        self.secondary_800 = ("#1e40af",)
        self.secondary_900 = ("#1e3a8a",)
        self.secondary_950 = ("#1d3660",)

        super().set(
            # Blaise
            background_fill_primary="#110F0F",
            background_fill_primary_dark="#110F0F",
            background_fill_secondary="#110F0F",
            background_fill_secondary_dark="#110F0F",
            block_background_fill="*neutral_800",
            block_background_fill_dark="*neutral_800",
            block_border_color="*border_color_primary",
            block_border_color_dark="*border_color_primary",
            block_border_width="1px",
            block_border_width_dark="1px",
            block_info_text_color="*body_text_color_subdued",
            block_info_text_color_dark="*body_text_color_subdued",
            block_info_text_size="*text_sm",
            block_info_text_weight="400",
            block_label_background_fill="*background_fill_primary",
            block_label_background_fill_dark="*background_fill_secondary",
            block_label_border_color="*border_color_primary",
            block_label_border_color_dark="*border_color_primary",
            block_label_border_width="1px",
            block_label_border_width_dark="1px",
            block_label_margin="0",
            block_label_padding="*spacing_sm *spacing_lg",
            block_label_radius="calc(*radius_lg - 1px) 0 calc(*radius_lg - 1px) 0",
            block_label_right_radius="0 calc(*radius_lg - 1px) 0 calc(*radius_lg - 1px)",
            block_label_shadow="*block_shadow",
            block_label_text_color="*#110F0F",
            block_label_text_color_dark="*#110F0F",
            block_label_text_weight="400",
            block_padding="*spacing_xl",
            block_radius="*radius_md",
            block_shadow="none",
            block_shadow_dark="none",
            block_title_background_fill="rgb(255,255,255)",
            block_title_background_fill_dark="rgb(255,255,255)",
            block_title_border_color="none",
            block_title_border_color_dark="none",
            block_title_border_width="0px",
            block_title_padding="*block_label_padding",
            block_title_radius="*block_label_radius",
            block_title_text_color="#110F0F",
            block_title_text_color_dark="#110F0F",
            block_title_text_size="*text_md",
            block_title_text_weight="600",
            body_background_fill="#110F0F",
            body_background_fill_dark="#110F0F",
            body_text_color="white",
            body_text_color_dark="white",
            body_text_color_subdued="*neutral_400",
            body_text_color_subdued_dark="*neutral_400",
            body_text_size="*text_md",
            body_text_weight="400",
            border_color_accent="*neutral_600",
            border_color_accent_dark="*neutral_600",
            border_color_primary="*neutral_800",
            border_color_primary_dark="*neutral_800",
            button_border_width="*input_border_width",
            button_border_width_dark="*input_border_width",
            button_cancel_background_fill="*button_secondary_background_fill",
            button_cancel_background_fill_dark="*button_secondary_background_fill",
            button_cancel_background_fill_hover="*button_cancel_background_fill",
            button_cancel_background_fill_hover_dark="*button_cancel_background_fill",
            button_cancel_border_color="*button_secondary_border_color",
            button_cancel_border_color_dark="*button_secondary_border_color",
            button_cancel_border_color_hover="*button_cancel_border_color",
            button_cancel_border_color_hover_dark="*button_cancel_border_color",
            button_cancel_text_color="#110F0F",
            button_cancel_text_color_dark="#110F0F",
            button_cancel_text_color_hover="#110F0F",
            button_cancel_text_color_hover_dark="#110F0F",
            button_large_padding="*spacing_lg calc(2 * *spacing_lg)",
            button_large_radius="*radius_lg",
            button_large_text_size="*text_lg",
            button_large_text_weight="600",
            button_primary_background_fill="*primary_600",
            button_primary_background_fill_dark="*primary_600",
            button_primary_background_fill_hover="*primary_500",
            button_primary_background_fill_hover_dark="*primary_500",
            button_primary_border_color="*primary_500",
            button_primary_border_color_dark="*primary_500",
            button_primary_border_color_hover="*primary_400",
            button_primary_border_color_hover_dark="*primary_400",
            button_primary_text_color="white",
            button_primary_text_color_dark="white",
            button_primary_text_color_hover="#110F0F",
            button_primary_text_color_hover_dark="#110F0F",
            button_secondary_background_fill="transparent",
            button_secondary_background_fill_dark="transparent",
            button_secondary_background_fill_hover="*neutral_800",
            button_secondary_background_fill_hover_dark="*neutral_800",
            button_secondary_border_color="*neutral_700",
            button_secondary_border_color_dark="*neutral_700",
            button_secondary_border_color_hover="*neutral_600",
            button_secondary_border_color_hover_dark="*neutral_600",
            button_secondary_text_color="white",
            button_secondary_text_color_dark="white",
            button_secondary_text_color_hover="*button_secondary_text_color",
            button_secondary_text_color_hover_dark="*button_secondary_text_color",
            button_shadow="none",
            button_shadow_active="*shadow_inset",
            button_shadow_hover="none",
            button_small_padding="*spacing_sm calc(2 * *spacing_sm)",
            button_small_radius="*radius_lg",
            button_small_text_size="*text_md",
            button_small_text_weight="400",
            button_transition="0.3s ease all",
            checkbox_background_color="*neutral_700",
            checkbox_background_color_dark="*neutral_700",
            checkbox_background_color_focus="*checkbox_background_color",
            checkbox_background_color_focus_dark="*checkbox_background_color",
            checkbox_background_color_hover="*checkbox_background_color",
            checkbox_background_color_hover_dark="*checkbox_background_color",
            checkbox_background_color_selected="*secondary_600",
            checkbox_background_color_selected_dark="*secondary_600",
            checkbox_border_color="*neutral_700",
            checkbox_border_color_dark="*neutral_700",
            checkbox_border_color_focus="*secondary_500",
            checkbox_border_color_focus_dark="*secondary_500",
            checkbox_border_color_hover="*neutral_600",
            checkbox_border_color_hover_dark="*neutral_600",
            checkbox_border_color_selected="*secondary_600",
            checkbox_border_color_selected_dark="*secondary_600",
            checkbox_border_radius="*radius_sm",
            checkbox_border_width="*input_border_width",
            checkbox_border_width_dark="*input_border_width",
            checkbox_check="url(\"data:image/svg+xml,%3csvg viewBox='0 0 16 16' fill='white' xmlns='http://www.w3.org/2000/svg'%3e%3cpath d='M12.207 4.793a1 1 0 010 1.414l-5 5a1 1 0 01-1.414 0l-2-2a1 1 0 011.414-1.414L6.5 9.086l4.293-4.293a1 1 0 011.414 0z'/%3e%3c/svg%3e\")",
            checkbox_label_background_fill="transparent",
            checkbox_label_background_fill_dark="transparent",
            checkbox_label_background_fill_hover="transparent",
            checkbox_label_background_fill_hover_dark="transparent",
            checkbox_label_background_fill_selected="transparent",
            checkbox_label_background_fill_selected_dark="transparent",
            checkbox_label_border_color="transparent",
            checkbox_label_border_color_dark="transparent",
            checkbox_label_border_color_hover="transparent",
            checkbox_label_border_color_hover_dark="transparent",
            checkbox_label_border_width="transparent",
            checkbox_label_border_width_dark="transparent",
            checkbox_label_gap="*spacing_lg",
            checkbox_label_padding="*spacing_md calc(2 * *spacing_md)",
            checkbox_label_shadow="none",
            checkbox_label_text_color="*body_text_color",
            checkbox_label_text_color_dark="*body_text_color",
            checkbox_label_text_color_selected="*checkbox_label_text_color",
            checkbox_label_text_color_selected_dark="*checkbox_label_text_color",
            checkbox_label_text_size="*text_md",
            checkbox_label_text_weight="400",
            checkbox_shadow="*input_shadow",
            color_accent="*primary_500",
            color_accent_soft="*primary_50",
            color_accent_soft_dark="*neutral_700",
            container_radius="*radius_xl",
            embed_radius="*radius_lg",
            error_background_fill="*background_fill_primary",
            error_background_fill_dark="*background_fill_primary",
            error_border_color="*border_color_primary",
            error_border_color_dark="*border_color_primary",
            error_border_width="1px",
            error_border_width_dark="1px",
            error_text_color="#ef4444",
            error_text_color_dark="#ef4444",
            form_gap_width="0px",
            input_background_fill="*neutral_900",
            input_background_fill_dark="*neutral_900",
            input_background_fill_focus="*secondary_600",
            input_background_fill_focus_dark="*secondary_600",
            input_background_fill_hover="*input_background_fill",
            input_background_fill_hover_dark="*input_background_fill",
            input_border_color="*neutral_700",
            input_border_color_dark="*neutral_700",
            input_border_color_focus="*secondary_600",
            input_border_color_focus_dark="*primary_600",
            input_border_color_hover="*input_border_color",
            input_border_color_hover_dark="*input_border_color",
            input_border_width="1px",
            input_border_width_dark="1px",
            input_padding="*spacing_xl",
            input_placeholder_color="*neutral_500",
            input_placeholder_color_dark="*neutral_500",
            input_radius="*radius_lg",
            input_shadow="none",
            input_shadow_dark="none",
            input_shadow_focus="*input_shadow",
            input_shadow_focus_dark="*input_shadow",
            input_text_size="*text_md",
            input_text_weight="400",
            layout_gap="*spacing_xxl",
            link_text_color="*secondary_500",
            link_text_color_active="*secondary_500",
            link_text_color_active_dark="*secondary_500",
            link_text_color_dark="*secondary_500",
            link_text_color_hover="*secondary_400",
            link_text_color_hover_dark="*secondary_400",
            link_text_color_visited="*secondary_600",
            link_text_color_visited_dark="*secondary_600",
            loader_color="*color_accent",
            loader_color_dark="*color_accent",
            panel_background_fill="*background_fill_secondary",
            panel_background_fill_dark="*background_fill_secondary",
            panel_border_color="*border_color_primary",
            panel_border_color_dark="*border_color_primary",
            panel_border_width="1px",
            panel_border_width_dark="1px",
            prose_header_text_weight="600",
            prose_text_size="*text_md",
            prose_text_weight="400",
            radio_circle="url(\"data:image/svg+xml,%3csvg viewBox='0 0 16 16' fill='white' xmlns='http://www.w3.org/2000/svg'%3e%3ccircle cx='8' cy='8' r='3'/%3e%3c/svg%3e\")",
            section_header_text_size="*text_md",
            section_header_text_weight="400",
            shadow_drop="rgba(0,0,0,0.05) 0px 1px 2px 0px",
            shadow_drop_lg="0 1px 3px 0 rgb(0 0 0 / 0.1), 0 1px 2px -1px rgb(0 0 0 / 0.1)",
            shadow_inset="rgba(0,0,0,0.05) 0px 2px 4px 0px inset",
            shadow_spread="3px",
            shadow_spread_dark="1px",
            slider_color="#9E9E9E",
            slider_color_dark="#9E9E9E",
            stat_background_fill="*primary_500",
            stat_background_fill_dark="*primary_500",
            table_border_color="*neutral_700",
            table_border_color_dark="*neutral_700",
            table_even_background_fill="*neutral_950",
            table_even_background_fill_dark="*neutral_950",
            table_odd_background_fill="*neutral_900",
            table_odd_background_fill_dark="*neutral_900",
            table_radius="*radius_lg",
            table_row_focus="*color_accent_soft",
            table_row_focus_dark="*color_accent_soft",
        )


applio = Applio()


with gr.Blocks(theme=applio, title="Ilaria RVC 💖") as demo:
    gr.Markdown("## Ilaria RVC 💖")
    with gr.Tab("Inference"):
        sound_gui = gr.Audio(value=None,type="filepath",autoplay=False,visible=True,)
        def update():
            print(MODELS)
            return gr.Dropdown(label="Model",choices=[model["model_name"] for model in MODELS],visible=True,interactive=True, value=MODELS[0]["model_name"],)
        with gr.Row():
            models_dropdown = gr.Dropdown(label="Model",choices=[model["model_name"] for model in MODELS],visible=True,interactive=True, value=MODELS[0]["model_name"],)
            refresh_button = gr.Button("Refresh Models")
            refresh_button.click(update, outputs=[models_dropdown])

        with gr.Accordion("Ilaria TTS", open=False):
            text_tts = gr.Textbox(label="Text", placeholder="Hello!", lines=3, interactive=True,)
            dropdown_tts = gr.Dropdown(label="Language and Model",choices=list(language_dict.keys()),interactive=True, value=list(language_dict.keys())[0])

            button_tts = gr.Button("Speak", variant="primary",)
            button_tts.click(text_to_speech_edge, inputs=[text_tts, dropdown_tts], outputs=[sound_gui])

        with gr.Accordion("Settings", open=False):
            pitch_algo_conf = gr.Dropdown(PITCH_ALGO_OPT,value=PITCH_ALGO_OPT[4],label="Pitch algorithm",visible=True,interactive=True,)
            pitch_lvl_conf = gr.Slider(label="Pitch level (lower -> 'male' while higher -> 'female')",minimum=-24,maximum=24,step=1,value=0,visible=True,interactive=True,)
            index_inf_conf =  gr.Slider(minimum=0,maximum=1,label="Index influence -> How much accent is applied",value=0.75,)
            respiration_filter_conf = gr.Slider(minimum=0,maximum=7,label="Respiration median filtering",value=3,step=1,interactive=True,)
            envelope_ratio_conf = gr.Slider(minimum=0,maximum=1,label="Envelope ratio",value=0.25,interactive=True,)
            consonant_protec_conf = gr.Slider(minimum=0,maximum=0.5,label="Consonant breath protection",value=0.5,interactive=True,)

        button_conf = gr.Button("Convert",variant="primary",)
        output_conf = gr.Audio(type="filepath",label="Output",)
    	
        button_conf.click(lambda :None, None, output_conf)
        button_conf.click(
            run,
            inputs=[
                models_dropdown,
                sound_gui,
                pitch_algo_conf,
                pitch_lvl_conf,
                index_inf_conf,
                respiration_filter_conf,
                envelope_ratio_conf,
                consonant_protec_conf,
            ],
            outputs=[output_conf],
        )


    with gr.Tab("Model Loader (Download and Upload)"):
        with gr.Accordion("Model Downloader", open=False):
            gr.Markdown(
                "Download the model from the following URL and upload it here. (Huggingface RVC model)"
            )
            model = gr.Textbox(lines=1, label="Model URL")
            name = gr.Textbox(lines=1, label="Model Name", placeholder="Model Name")
            download_button = gr.Button("Download Model")
            status = gr.Textbox(lines=1, label="Status", placeholder="Waiting....", interactive=False)
            model_pth = gr.Textbox(lines=1, label="Model pth file", placeholder="Waiting....", interactive=False)
            index_pth = gr.Textbox(lines=1, label="Index pth file", placeholder="Waiting....", interactive=False)
            download_button.click(download_from_url, [model, name], outputs=[status, model_pth, index_pth])
        with gr.Accordion("Upload A Model", open=False):
            index_file_upload = gr.File(label="Index File (.index)")
            pth_file_upload = gr.File(label="Model File (.pth)")

            model_name = gr.Textbox(label="Model Name", placeholder="Model Name")
            upload_button = gr.Button("Upload Model")
            upload_status = gr.Textbox(lines=1, label="Status", placeholder="Waiting....", interactive=False)

            upload_button.click(upload_model, [index_file_upload, pth_file_upload, model_name], upload_status)
    

    with gr.Tab("Vocal Separator (UVR)"):
        gr.Markdown("Separate vocals and instruments from an audio file using UVR models. - This is only on CPU due to ZeroGPU being ZeroGPU :(")
        uvr5_audio_file = gr.Audio(label="Audio File",type="filepath")

        with gr.Row():
            uvr5_model = gr.Dropdown(label="Model", choices=[model["model_name"] for model in UVR_5_MODELS])
            uvr5_button = gr.Button("Separate Vocals", variant="primary",)

        uvr5_output_voc = gr.Audio(type="filepath", label="Output 1",)
        uvr5_output_inst = gr.Audio(type="filepath", label="Output 2",)

        uvr5_button.click(inference, [uvr5_audio_file, uvr5_model], [uvr5_output_voc, uvr5_output_inst])
    
    with gr.Tab("Extra"):
        with gr.Accordion("Model Information", open=False):
            def json_to_markdown_table(json_data):
                table = "| Key | Value |\n| --- | --- |\n"
                for key, value in json_data.items():
                    table += f"| {key} | {value} |\n"
                return table
            def model_info(name):
                for model in MODELS:
                    if model["model_name"] == name:
                        print(model["model"])
                        info = model_handler.model_info(model["model"])
                        info2 = {
                            "Model Name": model["model_name"],
                            "Model Config": info['config'],
                            "Epochs Trained": info['epochs'],
                            "Sample Rate": info['sr'],
                            "Pitch Guidance": info['f0'],
                            "Model Precision": info['size'],
                        }
                        return gr.Markdown(json_to_markdown_table(info2))

                return "Model not found"
            def update():
                print(MODELS)
                return gr.Dropdown(label="Model", choices=[model["model_name"] for model in MODELS])
            with gr.Row():
                model_info_dropdown = gr.Dropdown(label="Model", choices=[model["model_name"] for model in MODELS])
                refresh_button = gr.Button("Refresh Models")
                refresh_button.click(update, outputs=[model_info_dropdown])
            model_info_button = gr.Button("Get Model Information")
            model_info_output = gr.Textbox(value="Waiting...",label="Output", interactive=False)
            model_info_button.click(model_info, [model_info_dropdown], [model_info_output])
            


        with gr.Accordion("Training Time Calculator", open=False):
            with gr.Column():
                epochs_input = gr.Number(label="Number of Epochs")
                seconds_input = gr.Number(label="Seconds per Epoch")
                calculate_button = gr.Button("Calculate Time Remaining")
                remaining_time_output = gr.Textbox(label="Remaining Time", interactive=False)
                
                calculate_button.click(calculate_remaining_time,inputs=[epochs_input, seconds_input],outputs=[remaining_time_output])

        with gr.Accordion("Model Fusion", open=False): 
                    with gr.Group():
                        def merge(ckpt_a, ckpt_b, alpha_a, sr_, if_f0_, info__, name_to_save0, version_2):
                            for model in MODELS:
                                if model["model_name"] == ckpt_a:
                                    ckpt_a = model["model"]
                                if model["model_name"] == ckpt_b:
                                    ckpt_b = model["model"]
                            
                            path = model_handler.merge(ckpt_a, ckpt_b, alpha_a, sr_, if_f0_, info__, name_to_save0, version_2)
                            if path == "Fail to merge the models. The model architectures are not the same.":
                                return "Fail to merge the models. The model architectures are not the same."
                            else:
                                MODELS.append({"model": path, "index": None, "model_name": name_to_save0})
                                return "Merged, saved as " + name_to_save0

                        gr.Markdown(value="Strongly suggested to use only very clean models.")
                        with gr.Row():
                            def update():
                                print(MODELS)
                                return gr.Dropdown(label="Model A", choices=[model["model_name"] for model in MODELS]), gr.Dropdown(label="Model B", choices=[model["model_name"] for model in MODELS])
                            refresh_button_fusion = gr.Button("Refresh Models")
                            ckpt_a = gr.Dropdown(label="Model A", choices=[model["model_name"] for model in MODELS])
                            ckpt_b = gr.Dropdown(label="Model B", choices=[model["model_name"] for model in MODELS])
                            refresh_button_fusion.click(update, outputs=[ckpt_a, ckpt_b])
                            alpha_a = gr.Slider(
                                minimum=0,
                                maximum=1,
                                label="Weight of the first model over the second",
                                value=0.5,
                                interactive=True,
                            )
                    with gr.Group():
                        with gr.Row():
                            sr_ = gr.Radio(
                                label="Sample rate of both models",
                                choices=["32k","40k", "48k"],
                                value="32k",
                                interactive=True,
                            )
                            if_f0_ = gr.Radio(
                                label="Pitch Guidance",
                                choices=["Yes", "Nah"],
                                value="Yes",
                                interactive=True,
                            )
                            info__ = gr.Textbox(
                                label="Add informations to the model",
                                value="",
                                max_lines=8,
                                interactive=True,
                                visible=False
                            )
                            name_to_save0 = gr.Textbox(
                                label="Final Model name",
                                value="",
                                max_lines=1,
                                interactive=True,
                            )
                            version_2 = gr.Radio(
                                label="Versions of the models",
                                choices=["v1", "v2"],
                                value="v2",
                                interactive=True,
                            )
                    with gr.Group():
                        with gr.Row():
                            but6 = gr.Button("Fuse the two models", variant="primary")
                            info4 = gr.Textbox(label="Output", value="", max_lines=8)
                        but6.click(
                            merge,
                            [ckpt_a,ckpt_b,alpha_a,sr_,if_f0_,info__,name_to_save0,version_2,],info4,api_name="ckpt_merge",)

        with gr.Accordion("Model Quantization", open=False):
            gr.Markdown("Quantize the model to a lower precision. - soon™ or never™ 😎")

        with gr.Accordion("Debug", open=False):
            def json_to_markdown_table(json_data):
                table = "| Key | Value |\n| --- | --- |\n"
                for key, value in json_data.items():
                    table += f"| {key} | {value} |\n"
                return table
            gr.Markdown("View the models that are currently loaded in the instance.")

            gr.Markdown(json_to_markdown_table({"Models": len(MODELS), "UVR Models": len(UVR_5_MODELS)}))

            gr.Markdown("View the current status of the instance.")
            status = {
                "Status": "Running", # duh lol
                "Models": len(MODELS),
                "UVR Models": len(UVR_5_MODELS),
                "CPU Usage": f"{psutil.cpu_percent()}%",
                "RAM Usage": f"{psutil.virtual_memory().percent}%",
                "CPU": f"{cpuinfo.get_cpu_info()['brand_raw']}",
                "System Uptime": f"{round(time.time() - psutil.boot_time(), 2)} seconds",
                "System Load Average": f"{psutil.getloadavg()}",
                "====================": "====================",
                "CPU Cores": psutil.cpu_count(),
                "CPU Threads": psutil.cpu_count(logical=True),
                "RAM Total": f"{round(psutil.virtual_memory().total / 1024**3, 2)} GB",
                "RAM Used": f"{round(psutil.virtual_memory().used / 1024**3, 2)} GB",
                "CPU Frequency": f"{psutil.cpu_freq().current} MHz",
                "====================": "====================",
                "GPU": "A100 - Do a request (Inference, you won't see it either way)",
            }
            gr.Markdown(json_to_markdown_table(status))

    with gr.Tab("Credits"):
        gr.Markdown(
            """
            Ilaria RVC made by [Ilaria](https://huggingface.co/TheStinger) suport her on [ko-fi](https://ko-fi.com/ilariaowo)
            
            The Inference code is made by [r3gm](https://huggingface.co/r3gm) (his module helped form this space 💖)

            made with ❤️ by [mikus](https://github.com/cappuch) - made the ui!

            ## In loving memory of JLabDX 🕊️
            """
        )
    with gr.Tab(("")):
        gr.Markdown('''
            ![ilaria](https://i.ytimg.com/vi/5PWqt2Wg-us/maxresdefault.jpg)
        ''')

demo.queue(api_open=False).launch(show_api=False)