File size: 5,422 Bytes
b5a064f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import glob
import os
import traceback

import gradio as gr

from modules import models, ui
from modules.ui import Tab


def inference_options_ui(show_out_dir=True):
    with gr.Row(equal_height=False):
        with gr.Column():
            source_audio = gr.Textbox(label="Source Audio")
            out_dir = gr.Textbox(
                label="Out folder",
                visible=show_out_dir,
                placeholder=models.AUDIO_OUT_DIR,
            )
        with gr.Column():
            transpose = gr.Slider(
                minimum=-20, maximum=20, value=0, step=1, label="Transpose"
            )
            pitch_extraction_algo = gr.Radio(
                choices=["dio", "harvest", "mangio-crepe", "crepe"],
                value="crepe",
                label="Pitch Extraction Algorithm",
            )
            embedding_model = gr.Radio(
                choices=["auto", *models.EMBEDDINGS_LIST.keys()],
                value="auto",
                label="Embedder Model",
            )
            embedding_output_layer = gr.Radio(
                choices=["auto", "9", "12"],
                value="auto",
                label="Embedder Output Layer",
            )
        with gr.Column():
            auto_load_index = gr.Checkbox(value=False, label="Auto Load Index")
            faiss_index_file = gr.Textbox(value="", label="Faiss Index File Path")
            retrieval_feature_ratio = gr.Slider(
                minimum=0,
                maximum=1,
                value=1,
                step=0.01,
                label="Retrieval Feature Ratio",
            )
        with gr.Column():
            fo_curve_file = gr.File(label="F0 Curve File")

    return (
        source_audio,
        out_dir,
        transpose,
        embedding_model,
        embedding_output_layer,
        pitch_extraction_algo,
        auto_load_index,
        faiss_index_file,
        retrieval_feature_ratio,
        fo_curve_file,
    )


class Inference(Tab):
    def title(self):
        return "Inference"

    def sort(self):
        return 1

    def ui(self, outlet):
        def infer(
            sid,
            input_audio,
            out_dir,
            embedder_model,
            embedding_output_layer,
            f0_up_key,
            f0_file,
            f0_method,
            auto_load_index,
            faiss_index_file,
            index_rate,
        ):
            model = models.vc_model
            try:
                yield "Infering...", None
                if out_dir == "":
                    out_dir = models.AUDIO_OUT_DIR

                if "*" in input_audio:
                    assert (
                        out_dir is not None
                    ), "Out folder is required for batch processing"
                    files = glob.glob(input_audio, recursive=True)
                elif os.path.isdir(input_audio):
                    assert (
                        out_dir is not None
                    ), "Out folder is required for batch processing"
                    files = glob.glob(
                        os.path.join(input_audio, "**", "*.wav"), recursive=True
                    )
                else:
                    files = [input_audio]
                for file in files:
                    audio = model.single(
                        sid,
                        file,
                        embedder_model,
                        embedding_output_layer,
                        f0_up_key,
                        f0_file,
                        f0_method,
                        auto_load_index,
                        faiss_index_file,
                        index_rate,
                        output_dir=out_dir,
                    )
                yield "Success", (model.tgt_sr, audio) if len(files) == 1 else None
            except:
                yield "Error: " + traceback.format_exc(), None

        with gr.Group():
            with gr.Box():
                with gr.Column():
                    _, speaker_id = ui.create_model_list_ui()

                    (
                        source_audio,
                        out_dir,
                        transpose,
                        embedder_model,
                        embedding_output_layer,
                        pitch_extraction_algo,
                        auto_load_index,
                        faiss_index_file,
                        retrieval_feature_ratio,
                        f0_curve_file,
                    ) = inference_options_ui()

                    with gr.Row(equal_height=False):
                        with gr.Column():
                            status = gr.Textbox(value="", label="Status")
                            output = gr.Audio(label="Output", interactive=False)

                    with gr.Row():
                        infer_button = gr.Button("Infer", variant="primary")

        infer_button.click(
            infer,
            inputs=[
                speaker_id,
                source_audio,
                out_dir,
                embedder_model,
                embedding_output_layer,
                transpose,
                f0_curve_file,
                pitch_extraction_algo,
                auto_load_index,
                faiss_index_file,
                retrieval_feature_ratio,
            ],
            outputs=[status, output],
            queue=True,
        )