Spaces:
Sleeping
Sleeping
File size: 11,181 Bytes
7e680fd a46a731 56cfd66 a46a731 7e680fd 6c897db 7e680fd cfab7f0 7e680fd 6117706 7e680fd 6117706 |
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 |
import re
import gradio as gr
import torch
import unicodedata
import commons
import utils
import pathlib
from models import SynthesizerTrn
from text import text_to_sequence
import time
import os
import io
from scipy.io.wavfile import write
from flask import Flask, request
from threading import Thread
import openai
import requests
import json
import soundfile as sf
from scipy import signal
class VitsGradio:
def __init__(self):
self.lan = ["中文","日文","自动"]
self.chatapi = ["gpt-3.5-turbo","gpt3"]
self.modelPaths = []
for root,dirs,files in os.walk("checkpoints"):
for dir in dirs:
self.modelPaths.append(dir)
with gr.Blocks() as self.Vits:
gr.Markdown(
"## <center> Vits chatbot后端演示,直接在本机上克隆并部署该space即可。搭配前端使用: https://github.com/Arkueid/Live2DMascot\n"
"### <center> 我移除了pyopenjtalk,如果合成不了日语就直接摆烂,所以不存在安装困难的情况.运行main.py后直接打开ui界面配置:http://127.0.0.1:7860/\n"
"### <center> 如果有能力,可自行替换text文件夹\n")
with gr.Tab("调试用"):
with gr.Row():
with gr.Column():
with gr.Row():
with gr.Column():
self.text = gr.TextArea(label="Text", value="你好")
with gr.Accordion(label="测试api", open=False):
self.local_chat1 = gr.Checkbox(value=False, label="使用网址+文本进行模拟")
self.url_input = gr.TextArea(label="键入测试", value="http://127.0.0.1:8080/chat?Text=")
butto = gr.Button("模拟前端抓取语音文件")
btnVC = gr.Button("测试tts+对话程序")
with gr.Column():
output2 = gr.TextArea(label="回复")
output1 = gr.Audio(label="采样率22050")
output3 = gr.outputs.File(label="44100hz: output.wav")
butto.click(self.Simul, inputs=[self.text, self.url_input], outputs=[output2,output3])
btnVC.click(self.tts_fn, inputs=[self.text], outputs=[output1,output2])
with gr.Tab("控制面板"):
with gr.Row():
with gr.Column():
with gr.Row():
with gr.Column():
self.api_input1 = gr.TextArea(label="输入api-key或本地存储说话模型的路径", value="https://platform.openai.com/account/api-keys")
with gr.Accordion(label="chatbot选择", open=False):
self.api_input2 = gr.Checkbox(value=True, label="采用gpt3.5")
self.local_chat1 = gr.Checkbox(value=False, label="启动本地chatbot,这里用不了,别点")
self.local_chat2 = gr.Checkbox(value=True, label="是否量化")
res = gr.TextArea()
Botselection = gr.Button("完成chatbot设定")
Botselection.click(self.check_bot, inputs=[self.api_input1,self.api_input2,self.local_chat1,self.local_chat2], outputs = [res])
self.input1 = gr.Dropdown(label = "模型", choices = self.modelPaths, value = self.modelPaths[0], type = "value")
self.input2 = gr.Dropdown(label="Language", choices=self.lan, value="自动", interactive=True)
with gr.Column():
btnVC = gr.Button("完成vits TTS端设定")
self.input3 = gr.Dropdown(label="Speaker", choices=list(range(100)), value=21, interactive=True)
self.input4 = gr.Slider(minimum=0, maximum=1.0, label="更改噪声比例(noise scale),以控制情感", value=0.6)
self.input5 = gr.Slider(minimum=0, maximum=1.0, label="更改噪声偏差(noise scale w),以控制音素长短", value=0.668)
self.input6 = gr.Slider(minimum=0.1, maximum=10, label="duration", value=1)
statusa = gr.TextArea()
btnVC.click(self.create_tts_fn, inputs=[self.input1, self.input2, self.input3, self.input4, self.input5, self.input6], outputs = [statusa])
def Simul(self,text,url_input):
web = url_input + text
res = requests.get(web)
music = res.content
with open('output.wav', 'wb') as code:
code.write(music)
file_path = "output.wav"
return web,file_path
def chatgpt(self,text):
self.messages.append({"role": "user", "content": text},)
chat = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages= self.messages)
reply = chat.choices[0].message.content
return reply
def ChATGLM(self,text):
if text == 'clear':
self.history = []
response, new_history = self.model.chat(self.tokenizer, text, self.history)
response = response.replace(" ",'').replace("\n",'.')
self.history = new_history
return response
def gpt3_chat(self,text):
call_name = "Waifu"
openai.api_key = args.key
identity = ""
start_sequence = '\n'+str(call_name)+':'
restart_sequence = "\nYou: "
if 1 == 1:
prompt0 = text #当期prompt
if text == 'quit':
return prompt0
prompt = identity + prompt0 + start_sequence
response = openai.Completion.create(
model="text-davinci-003",
prompt=prompt,
temperature=0.5,
max_tokens=1000,
top_p=1.0,
frequency_penalty=0.5,
presence_penalty=0.0,
stop=["\nYou:"]
)
return response['choices'][0]['text'].strip()
def check_bot(self,api_input1,api_input2,local_chat1,local_chat2):
if local_chat1:
from transformers import AutoTokenizer, AutoModel
self.tokenizer = AutoTokenizer.from_pretrained(api_input1, trust_remote_code=True)
if local_chat2:
self.model = AutoModel.from_pretrained(api_input1, trust_remote_code=True).half().quantize(4).cuda()
else:
self.model = AutoModel.from_pretrained(api_input1, trust_remote_code=True)
self.history = []
else:
self.messages = []
openai.api_key = api_input1
return "Finished"
def is_japanese(self,string):
for ch in string:
if ord(ch) > 0x3040 and ord(ch) < 0x30FF:
return True
return False
def is_english(self,string):
import re
pattern = re.compile('^[A-Za-z0-9.,:;!?()_*"\' ]+$')
if pattern.fullmatch(string):
return True
else:
return False
def get_text(self,text, hps, cleaned=False):
if cleaned:
text_norm = text_to_sequence(text, self.hps_ms.symbols, [])
else:
text_norm = text_to_sequence(text, self.hps_ms.symbols, self.hps_ms.data.text_cleaners)
if self.hps_ms.data.add_blank:
text_norm = commons.intersperse(text_norm, 0)
text_norm = torch.LongTensor(text_norm)
return text_norm
def get_label(self,text, label):
if f'[{label}]' in text:
return True, text.replace(f'[{label}]', '')
else:
return False, text
def sle(self,language,text):
text = text.replace('\n','。').replace(' ',',')
if language == "中文":
tts_input1 = "[ZH]" + text + "[ZH]"
return tts_input1
elif language == "自动":
tts_input1 = f"[JA]{text}[JA]" if self.is_japanese(text) else f"[ZH]{text}[ZH]"
return tts_input1
elif language == "日文":
tts_input1 = "[JA]" + text + "[JA]"
return tts_input1
def create_tts_fn(self,path, input2, input3, n_scale= 0.667,n_scale_w = 0.8, l_scale = 1 ):
self.language = input2
self.speaker_id = int(input3)
self.n_scale = n_scale
self.n_scale_w = n_scale_w
self.l_scale = l_scale
self.dev = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
self.hps_ms = utils.get_hparams_from_file(f"checkpoints/{path}/config.json")
self.n_speakers = self.hps_ms.data.n_speakers if 'n_speakers' in self.hps_ms.data.keys() else 0
self.n_symbols = len(self.hps_ms.symbols) if 'symbols' in self.hps_ms.keys() else 0
self.net_g_ms = SynthesizerTrn(
self.n_symbols,
self.hps_ms.data.filter_length // 2 + 1,
self.hps_ms.train.segment_size // self.hps_ms.data.hop_length,
n_speakers=self.n_speakers,
**self.hps_ms.model).to(self.dev)
_ = self.net_g_ms.eval()
_ = utils.load_checkpoint(f"checkpoints/{path}/model.pth", self.net_g_ms)
return 'success'
def tts_fn(self,text):
if self.local_chat1:
text = self.chatgpt(text)
elif self.api_input2:
text = self.ChATGLM(text)
else:
text = self.gpt3_chat(text)
print(text)
text =self.sle(self.language,text)
with torch.no_grad():
stn_tst = self.get_text(text, self.hps_ms, cleaned=False)
x_tst = stn_tst.unsqueeze(0).to(self.dev)
x_tst_lengths = torch.LongTensor([stn_tst.size(0)]).to(self.dev)
sid = torch.LongTensor([self.speaker_id]).to(self.dev)
audio = self.net_g_ms.infer(x_tst, x_tst_lengths, sid=sid, noise_scale=self.n_scale, noise_scale_w=self.n_scale_w, length_scale=self.l_scale)[0][
0, 0].data.cpu().float().numpy()
resampled_audio_data = signal.resample(audio, len(audio) * 2)
sf.write('temp.wav', resampled_audio_data, 44100, 'PCM_24')
return (self.hps_ms.data.sampling_rate, audio),text.replace('[JA]','').replace('[ZH]','')
app = Flask(__name__)
print("开始部署")
grVits = VitsGradio()
@app.route('/chat')
def text_api():
message = request.args.get('Text','')
audio,text = grVits.tts_fn(message)
text = text.replace('[JA]','').replace('[ZH]','')
with open('temp.wav','rb') as bit:
wav_bytes = bit.read()
headers = {
'Content-Type': 'audio/wav',
'Text': text.encode('utf-8')}
return wav_bytes, 200, headers
'''
def gradio_interface():
return grVits.Vits.launch()
if __name__ == '__main__':
api_thread = Thread(target=app.run, args=("0.0.0.0", 8080))
gradio_thread = Thread(target=gradio_interface)
api_thread.start()
gradio_thread.start()
'''
grVits.Vits.launch() |