File size: 5,244 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
import io
import json

import gradio as gr
import requests
import soundfile as sf
import torch.multiprocessing as multiprocessing
from scipy.io.wavfile import write

from modules.ui import Tab
from server import app

proc = None

def server_options_ui(show_out_dir=True):
    with gr.Row().style(equal_height=False):
        with gr.Row():
            host = gr.Textbox(value="127.0.0.1", label="host")
            port = gr.Textbox(value="5001", label="port")
    with gr.Row().style(equal_height=False):
        with gr.Row():
            rvc_model_file = gr.Textbox(value="", label="RVC model file path")
            faiss_index_file = gr.Textbox(value="", label="Faiss index file path")
    with gr.Row().style(equal_height=False):
        with gr.Row():
            input_voice_file = gr.Textbox(value="", label="input voice file path")
            speaker_id = gr.Number(
                value=0,
                label="speaker_id",
            )
            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_algo",
            )
            retrieval_feature_ratio = gr.Slider(
                minimum=0,
                maximum=1,
                value=1,
                step=0.01,
                label="retrieval_feature_ratio",
            )
    return (
        host,
        port,
        rvc_model_file,
        faiss_index_file,
        input_voice_file,
        speaker_id,
        transpose,
        pitch_extraction_algo,
        retrieval_feature_ratio,
    )

def run(**kwargs):
    app.run(**kwargs)

class Server(Tab):
    def title(self):
        return "Server(experimental)"

    def sort(self):
        return 6

    def ui(self, outlet):
        def start(host, port):
            if multiprocessing.get_start_method() == 'fork':
                multiprocessing.set_start_method('spawn', force=True)
            proc = multiprocessing.Process(target = run, kwargs = {'host': host, 'port': port})
            proc.start()
            yield "start server"

        def upload(host, port, rvc_model_file, faiss_index_file):
            file_names = {"rvc_model_file": rvc_model_file, "faiss_index_file": faiss_index_file}
            res = requests.post(f"http://{host}:{port}/upload_model", json=file_names)
            yield res.text

        def convert(host, port, input_voice_file, speaker_id, transpose, pitch_extraction_algo, retrieval_feature_ratio):
            params = {
                "speaker_id": speaker_id,
                "transpose": transpose,
                "pitch_extraction_algo": pitch_extraction_algo,
                "retrieval_feature_ratio": retrieval_feature_ratio
            }

            audio, sr = sf.read(input_voice_file)
            audio_buffer = io.BytesIO()
            write(audio_buffer, rate=sr, data=audio)
            json_buffer = io.BytesIO(json.dumps(params).encode('utf-8'))
            files = {
                "input_wav": audio_buffer,
                "params": json_buffer
            }
            res = requests.post(f"http://{host}:{port}/convert_sound", files=files)
            audio, sr = sf.read(io.BytesIO(res.content))
            yield "convert succeed", (sr, audio)

        with gr.Group():
            with gr.Box():
                with gr.Column():
                    (
                        host,
                        port,
                        rvc_model_file,
                        faiss_index_file,
                        input_voice_file,
                        speaker_id,
                        transpose,
                        pitch_extraction_algo,
                        retrieval_feature_ratio,
                    ) = server_options_ui()

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

                    with gr.Row():
                        start_button = gr.Button("Start server", variant="primary")
                        upload_button = gr.Button("Upload Model")
                        convert_button = gr.Button("Convert Voice")

        start_button.click(
            start,
            inputs=[
                host,
                port
            ],
            outputs=[status],
            queue=True,
        )
        upload_button.click(
            upload,
            inputs=[
                host,
                port,
                rvc_model_file,
                faiss_index_file
            ],
            outputs=[status],
            queue=True,
        )
        convert_button.click(
            convert,
            inputs=[
                host,
                port,
                input_voice_file,
                speaker_id,
                transpose,
                pitch_extraction_algo,
                retrieval_feature_ratio
            ],
            outputs=[status, output],
            queue=True,
        )