-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathapp.py
154 lines (127 loc) · 5.24 KB
/
app.py
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
import torch
from TTS.api import TTS
import gradio as gr
from rvc import Config, load_hubert, get_vc, rvc_infer
import gc , os, sys, argparse, requests
from pathlib import Path
parser = argparse.ArgumentParser(
prog='XTTS-RVC-UI',
description='Gradio UI for XTTSv2 and RVC'
)
parser.add_argument('-s', '--silent', action=argparse.BooleanOptionalAction, default=False)
args = parser.parse_args()
if args.silent:
print('Enabling silent mode.')
sys.stdout = open(os.devnull, 'w')
def download_models():
rvc_files = ['hubert_base.pt', 'rmvpe.pt']
for file in rvc_files:
if(not os.path.isfile(f'./models/{file}')):
print(f'Downloading{file}')
r = requests.get(f'https://huggingface.co/lj1995/VoiceConversionWebUI/resolve/main/{file}')
with open(f'./models/{file}', 'wb') as f:
f.write(r.content)
xtts_files = ['vocab.json', 'config.json', 'dvae.path', 'mel_stats.pth', 'model.pth']
for file in xtts_files:
if(not os.path.isfile(f'./models/xtts/{file}')):
print(f'Downloading {file}')
r = requests.get(f'https://huggingface.co/coqui/XTTS-v2/resolve/v2.0.2/{file}')
with open(f'./models/xtts/{file}', 'wb') as f:
f.write(r.content)
[Path(_dir).mkdir(parents=True, exist_ok=True) for _dir in ['./models/xtts', './voices', './rvcs']]
download_models()
device = "cuda:0" if torch.cuda.is_available() else "cpu"
print("Device: " + device)
config = Config(device, device != 'cpu')
hubert_model = load_hubert(device, config.is_half, "./models/hubert_base.pt")
tts = TTS(model_path="./models/xtts", config_path='./models/xtts/config.json').to(device)
voices = []
rvcs = []
langs = ["en", "es", "fr", "de", "it", "pt", "pl", "tr", "ru", "nl", "cs", "ar", "zh-cn", "hu", "ko", "ja", "hi"]
def get_rvc_voices():
global voices
voices = os.listdir("./voices")
global rvcs
rvcs = list(filter(lambda x:x.endswith(".pth"), os.listdir("./rvcs")))
return [rvcs, voices]
def runtts(rvc, voice, text, pitch_change, index_rate, language):
audio = tts.tts_to_file(text=text, speaker_wav="./voices/" + voice, language=language, file_path="./output.wav")
voice_change(rvc, pitch_change, index_rate)
return ["./output.wav" , "./outputrvc.wav"]
def main():
get_rvc_voices()
print(rvcs)
print(voices)
with gr.Blocks(title='TTS RVC UI') as interface:
with gr.Row():
gr.Markdown("""
#XTTS RVC UI
""")
with gr.Row():
with gr.Column():
lang_dropdown = gr.Dropdown(choices=langs, value=langs[0], label='Language')
rvc_dropdown = gr.Dropdown(choices=rvcs, value=rvcs[0] if len(rvcs) > 0 else '', label='RVC model')
voice_dropdown = gr.Dropdown(choices=voices, value=voices[0] if len(voices) > 0 else '', label='Voice sample')
refresh_button = gr.Button(value='Refresh')
text_input = gr.Textbox(placeholder="Write here...")
submit_button = gr.Button(value='Submit')
with gr.Row():
pitch_slider = gr.Slider(minimum=-12, maximum=12, value=0, step=1, label="Pitch")
index_rate_slider = gr.Slider(minimum=0, maximum=1, value=0.75, step=0.05, label="Index Rate")
with gr.Column():
audio_output = gr.Audio(label="TTS result", type="filepath", interactive=False)
rvc_audio_output = gr.Audio(label="RVC result", type="filepath", interactive=False)
submit_button.click(inputs=[rvc_dropdown, voice_dropdown, text_input, pitch_slider, index_rate_slider, lang_dropdown], outputs=[audio_output, rvc_audio_output], fn=runtts)
def refresh_dropdowns():
get_rvc_voices()
print('Refreshed voice and RVC list!')
return [gr.update(choices=rvcs, value=rvcs[0] if len(rvcs) > 0 else ''), gr.update(choices=voices, value=voices[0] if len(voices) > 0 else '')]
refresh_button.click(fn=refresh_dropdowns, outputs=[rvc_dropdown, voice_dropdown])
interface.launch(server_name="0.0.0.0", server_port=5000, quiet=True)
# delete later
class RVC_Data:
def __init__(self):
self.current_model = {}
self.cpt = {}
self.version = {}
self.net_g = {}
self.tgt_sr = {}
self.vc = {}
def load_cpt(self, modelname, rvc_model_path):
if self.current_model != modelname:
print("Loading new model")
del self.cpt, self.version, self.net_g, self.tgt_sr, self.vc
self.cpt, self.version, self.net_g, self.tgt_sr, self.vc = get_vc(device, config.is_half, config, rvc_model_path)
self.current_model = modelname
rvc_data = RVC_Data()
def voice_change(rvc, pitch_change, index_rate):
modelname = os.path.splitext(rvc)[0]
print("Using RVC model: "+ modelname)
rvc_model_path = "./rvcs/" + rvc
rvc_index_path = "./rvcs/" + modelname + ".index" if os.path.isfile("./rvcs/" + modelname + ".index") and index_rate != 0 else ""
if rvc_index_path != "" :
print("Index file found!")
#load_cpt(modelname, rvc_model_path)
#cpt, version, net_g, tgt_sr, vc = get_vc(device, config.is_half, config, rvc_model_path)
rvc_data.load_cpt(modelname, rvc_model_path)
rvc_infer(
index_path=rvc_index_path,
index_rate=index_rate,
input_path="./output.wav",
output_path="./outputrvc.wav",
pitch_change=pitch_change,
f0_method="rmvpe",
cpt=rvc_data.cpt,
version=rvc_data.version,
net_g=rvc_data.net_g,
filter_radius=3,
tgt_sr=rvc_data.tgt_sr,
rms_mix_rate=0.25,
protect=0,
crepe_hop_length=0,
vc=rvc_data.vc,
hubert_model=hubert_model
)
gc.collect()
if __name__ == "__main__":
main()