Mahiruoshi commited on
Commit
d15d7a0
1 Parent(s): 5794787

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +63 -12
main.py CHANGED
@@ -35,7 +35,7 @@ class VitsGradio:
35
  self.text = gr.TextArea(label="Text", value="你好")
36
  with gr.Accordion(label="测试api", open=False):
37
  self.local_chat1 = gr.Checkbox(value=False, label="使用网址+文本进行模拟")
38
- self.url_input = gr.TextArea(label="键入测试", value="http://YourHost:8080/chat?Text=")
39
  butto = gr.Button("模拟前端抓取语音文件")
40
  btnVC = gr.Button("测试tts+对话程序")
41
  with gr.Column():
@@ -49,11 +49,11 @@ class VitsGradio:
49
  with gr.Column():
50
  with gr.Row():
51
  with gr.Column():
52
- self.api_input1 = gr.TextArea(label="输入api-key或本地存储说话模型的路径", value="https://platform.openai.com/account/api-keys")
53
- with gr.Accordion(label="chatbot选择", open=False):
54
- self.api_input2 = gr.Checkbox(value=True, label="采用gpt3.5")
55
  self.local_chat1 = gr.Checkbox(value=False, label="启动本地chatbot")
56
- self.local_chat2 = gr.Checkbox(value=True, label="是否量化")
57
  res = gr.TextArea()
58
  Botselection = gr.Button("完成chatbot设定")
59
  Botselection.click(self.check_bot, inputs=[self.api_input1,self.api_input2,self.local_chat1,self.local_chat2], outputs = [res])
@@ -61,9 +61,9 @@ class VitsGradio:
61
  self.input2 = gr.Dropdown(label="Language", choices=self.lan, value="自动", interactive=True)
62
  with gr.Column():
63
  btnVC = gr.Button("完成vits TTS端设定")
64
- self.input3 = gr.Dropdown(label="Speaker", choices=list(range(114514)), value=21, interactive=True)
65
  self.input4 = gr.Slider(minimum=0, maximum=1.0, label="更改噪声比例(noise scale),以控制情感", value=0.6)
66
- self.input5 = gr.Slider(minimum=0, maximum=1.0, label="更改噪声偏差(noise scale w),以控制音素长短", value=0.668)
67
  self.input6 = gr.Slider(minimum=0.1, maximum=10, label="duration", value=1)
68
  statusa = gr.TextArea()
69
  btnVC.click(self.create_tts_fn, inputs=[self.input1, self.input2, self.input3, self.input4, self.input5, self.input6], outputs = [statusa])
@@ -78,6 +78,34 @@ class VitsGradio:
78
  return web,file_path
79
 
80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
  def chatgpt(self,text):
82
  self.messages.append({"role": "user", "content": text},)
83
  chat = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages= self.messages)
@@ -116,6 +144,7 @@ class VitsGradio:
116
  return response['choices'][0]['text'].strip()
117
 
118
  def check_bot(self,api_input1,api_input2,local_chat1,local_chat2):
 
119
  if local_chat1:
120
  from transformers import AutoTokenizer, AutoModel
121
  self.tokenizer = AutoTokenizer.from_pretrained(api_input1, trust_remote_code=True)
@@ -125,8 +154,11 @@ class VitsGradio:
125
  self.model = AutoModel.from_pretrained(api_input1, trust_remote_code=True)
126
  self.history = []
127
  else:
128
- self.messages = []
129
- openai.api_key = api_input1
 
 
 
130
  return "Finished"
131
 
132
  def is_japanese(self,string):
@@ -197,11 +229,11 @@ class VitsGradio:
197
 
198
  def tts_fn(self,text):
199
  if self.local_chat1:
200
- text = self.chatgpt(text)
201
  elif self.api_input2:
202
  text = self.ChATGLM(text)
203
  else:
204
- text = self.gpt3_chat(text)
205
  print(text)
206
  text =self.sle(self.language,text)
207
  with torch.no_grad():
@@ -218,5 +250,24 @@ class VitsGradio:
218
  app = Flask(__name__)
219
  print("开始部署")
220
  grVits = VitsGradio()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
221
  if __name__ == '__main__':
222
- grVits.Vits.launch()
 
 
 
 
35
  self.text = gr.TextArea(label="Text", value="你好")
36
  with gr.Accordion(label="测试api", open=False):
37
  self.local_chat1 = gr.Checkbox(value=False, label="使用网址+文本进行模拟")
38
+ self.url_input = gr.TextArea(label="键入测试", value="http://127.0.0.1:8080/chat?Text=")
39
  butto = gr.Button("模拟前端抓取语音文件")
40
  btnVC = gr.Button("测试tts+对话程序")
41
  with gr.Column():
 
49
  with gr.Column():
50
  with gr.Row():
51
  with gr.Column():
52
+ self.api_input1 = gr.TextArea(label="输入gpt/茉莉云的api-key或本地存储说话模型的路径.如果要用茉莉云则用'|'隔开key和密码", value="49eig5nu3rllvg6e|itcn9760")
53
+ with gr.Accordion(label="chatbot选择(默认gpt3.5)", open=False):
54
+ self.api_input2 = gr.Checkbox(value=False, label="茉莉云")
55
  self.local_chat1 = gr.Checkbox(value=False, label="启动本地chatbot")
56
+ self.local_chat2 = gr.Checkbox(value=False, label="是否量化")
57
  res = gr.TextArea()
58
  Botselection = gr.Button("完成chatbot设定")
59
  Botselection.click(self.check_bot, inputs=[self.api_input1,self.api_input2,self.local_chat1,self.local_chat2], outputs = [res])
 
61
  self.input2 = gr.Dropdown(label="Language", choices=self.lan, value="自动", interactive=True)
62
  with gr.Column():
63
  btnVC = gr.Button("完成vits TTS端设定")
64
+ self.input3 = gr.Dropdown(label="Speaker", choices=list(range(1001)), value=0, interactive=True)
65
  self.input4 = gr.Slider(minimum=0, maximum=1.0, label="更改噪声比例(noise scale),以控制情感", value=0.6)
66
+ self.input5 = gr.Slider(minimum=0, maximum=1.0, label="更改噪声偏差(noise scale w),以控制音素长短", value=0.667)
67
  self.input6 = gr.Slider(minimum=0.1, maximum=10, label="duration", value=1)
68
  statusa = gr.TextArea()
69
  btnVC.click(self.create_tts_fn, inputs=[self.input1, self.input2, self.input3, self.input4, self.input5, self.input6], outputs = [statusa])
 
78
  return web,file_path
79
 
80
 
81
+ def mori(self,text):
82
+ import http.client
83
+ conn = http.client.HTTPSConnection("api.mlyai.com")
84
+ payload = json.dumps({
85
+ "content": text,
86
+ "type": 1,
87
+ "from": "123456",
88
+ "fromName": "侑"
89
+ })
90
+ headers = {
91
+ 'Api-Key': self.api_key,
92
+ 'Api-Secret': self.api_secret,
93
+ 'Content-Type': 'application/json'
94
+ }
95
+ conn.request("POST", "/reply", payload, headers)
96
+ res = conn.getresponse()
97
+ data = res.read()
98
+ decoded_data = json.loads(data.decode("utf-8"))
99
+
100
+ if decoded_data["code"] == "00000":
101
+ answer = decoded_data["data"][0]["content"]
102
+ if text == 'exit':
103
+ conn.close()
104
+ return answer
105
+ else:
106
+ conn.close()
107
+ return '对不起,做不到'
108
+
109
  def chatgpt(self,text):
110
  self.messages.append({"role": "user", "content": text},)
111
  chat = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages= self.messages)
 
144
  return response['choices'][0]['text'].strip()
145
 
146
  def check_bot(self,api_input1,api_input2,local_chat1,local_chat2):
147
+ self.api_key, self.api_secret = api_input1.split("|")
148
  if local_chat1:
149
  from transformers import AutoTokenizer, AutoModel
150
  self.tokenizer = AutoTokenizer.from_pretrained(api_input1, trust_remote_code=True)
 
154
  self.model = AutoModel.from_pretrained(api_input1, trust_remote_code=True)
155
  self.history = []
156
  else:
157
+ try:
158
+ self.messages = []
159
+ openai.api_key = api_input1
160
+ except:
161
+ pass
162
  return "Finished"
163
 
164
  def is_japanese(self,string):
 
229
 
230
  def tts_fn(self,text):
231
  if self.local_chat1:
232
+ text = self.mori(text)
233
  elif self.api_input2:
234
  text = self.ChATGLM(text)
235
  else:
236
+ text = text = self.chatgpt(text)
237
  print(text)
238
  text =self.sle(self.language,text)
239
  with torch.no_grad():
 
250
  app = Flask(__name__)
251
  print("开始部署")
252
  grVits = VitsGradio()
253
+
254
+ @app.route('/chat')
255
+ def text_api():
256
+ message = request.args.get('Text','')
257
+ audio,text = grVits.tts_fn(message)
258
+ text = text.replace('[JA]','').replace('[ZH]','')
259
+ with open('temp.wav','rb') as bit:
260
+ wav_bytes = bit.read()
261
+ headers = {
262
+ 'Content-Type': 'audio/wav',
263
+ 'Text': text.encode('utf-8')}
264
+ return wav_bytes, 200, headers
265
+
266
+ def gradio_interface():
267
+ return grVits.Vits.launch()
268
+
269
  if __name__ == '__main__':
270
+ api_thread = Thread(target=app.run, args=("0.0.0.0", 8080))
271
+ gradio_thread = Thread(target=gradio_interface)
272
+ api_thread.start()
273
+ gradio_thread.start()