File size: 4,093 Bytes
0019e18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f0aa454
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
import gradio as gr
from infer_rvc_python import BaseLoader
import random
from urllib.request import urlretrieve

files_to_retrieve = [
    "https://replicate.delivery/pbxt/N97QM3XNFrooJhV6Fb0meBff0aAG1rEDfvuxcdLS6fTx1vmWC/test.zip",
    "https://huggingface.co/lj1995/VoiceConversionWebUI/resolve/main/hubert_base.pt?download=true",
    "https://huggingface.co/lj1995/VoiceConversionWebUI/resolve/main/rmvpe.pt?download=true"
    ]

for file in files_to_retrieve:
    print(f"Downloading {file}")
    urlretrieve(file, file.split("/")[-1])

# unzip test.zip
import zipfile
with zipfile.ZipFile("test.zip", 'r') as zip_ref:
    zip_ref.extractall(".")


converter = BaseLoader(
    only_cpu=True, hubert_path="./hubert_base.pt", rmvpe_path="./rmvpe.pt"
)

model = "test.pth"
index = "added_IVF839_Flat_nprobe_1_test_v2.index"





def voice_conversion(
    audio,
    pitch_change,
    filter_radius,
    envelope_ratio,
    index_influence,
    consonant_breath_protection,
):
    audio_out = run(
        [str(audio)],
        model,
        "rmvpe+",
        pitch_change,
        index,
        index_influence,
        filter_radius,
        envelope_ratio,
        consonant_breath_protection,
    )
    print(audio_out)
    return audio_out[0]



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


def run(
    audio_files,
    file_m,
    pitch_alg,
    pitch_lvl,
    file_index,
    index_inf,
    r_m_f,
    e_r,
    c_b_p,
):
    random_tag = "USER_" + str(random.randint(10000000, 99999999))

    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,
    )

    return convert_now(audio_files, random_tag)



# Create the Gradio interface
# audio_input = gr.Audio(type="file")
# audio_output = gr.Audio(type="file")

# gr.Interface(fn=voice_conversion, inputs=audio_input, outputs=audio_output).launch()


def ui():
    with gr.Blocks() as demo:
        audio_input = gr.Audio(sources=["microphone", "upload"], type="filepath")
        with gr.Row():
            pitch_slider = gr.Slider(
                minimum=-24,
                maximum=24,
                value=0,
                step=1,
                label="Pitch",
                interactive=True,
            )
            index_influence_slider = gr.Slider(
                minimum=0,
                maximum=1,
                value=0.75,
                step=0.01,
                label="Index Influence",
                interactive=True,
            )
            respiration_median_filtering = gr.Slider(
                minimum=0,
                maximum=10,
                value=3,
                step=1,
                label="Resp. Median Filtering",
                interactive=True,
            )
            envelope_ratio = gr.Slider(
                minimum=0,
                maximum=1,
                value=0.25,
                step=0.01,
                label="Envelope Ratio",
                interactive=True,
            )
            consonant_breath_protection = gr.Slider(
                minimum=0,
                maximum=1,
                value=0.5,
                step=0.01,
                label="Consonant Breath Protection",
                interactive=True,
            )
        button = gr.Button("Convert")
        audio_output = gr.Audio(type="filepath")
        button.click(
            voice_conversion,
            inputs=[
                audio_input,
                pitch_slider,
                respiration_median_filtering,
                envelope_ratio,
                index_influence_slider,
                consonant_breath_protection,
            ],
            outputs=audio_output,
        )

    return demo


ui().launch(auth=("output", "becreative"))