Mahiruoshi commited on
Commit
e4f56b2
1 Parent(s): f8e7b7c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -45
app.py CHANGED
@@ -18,7 +18,6 @@ import requests
18
  import json
19
  import soundfile as sf
20
  from scipy import signal
21
- import http.client
22
  class VitsGradio:
23
  def __init__(self):
24
  self.lan = ["中文","日文","自动"]
@@ -50,11 +49,11 @@ class VitsGradio:
50
  with gr.Column():
51
  with gr.Row():
52
  with gr.Column():
53
- self.api_input1 = gr.TextArea(label="输入gpt/茉莉云的api-key或本地存储说话模型的路径.如果要用茉莉云则用'|'隔开key和密码", value="49eig5nu3rllvg6e|itcn9760")
54
- with gr.Accordion(label="chatbot选择(默认gpt3.5)", open=False):
55
- self.api_input2 = gr.Checkbox(value=False, label="茉莉云")
56
  self.local_chat1 = gr.Checkbox(value=False, label="启动本地chatbot")
57
- self.local_chat2 = gr.Checkbox(value=False, label="是否量化")
58
  res = gr.TextArea()
59
  Botselection = gr.Button("完成chatbot设定")
60
  Botselection.click(self.check_bot, inputs=[self.api_input1,self.api_input2,self.local_chat1,self.local_chat2], outputs = [res])
@@ -62,9 +61,9 @@ class VitsGradio:
62
  self.input2 = gr.Dropdown(label="Language", choices=self.lan, value="自动", interactive=True)
63
  with gr.Column():
64
  btnVC = gr.Button("完成vits TTS端设定")
65
- self.input3 = gr.Dropdown(label="Speaker", choices=list(range(1001)), value=0, interactive=True)
66
  self.input4 = gr.Slider(minimum=0, maximum=1.0, label="更改噪声比例(noise scale),以控制情感", value=0.6)
67
- self.input5 = gr.Slider(minimum=0, maximum=1.0, label="更改噪声偏差(noise scale w),以控制音素长短", value=0.667)
68
  self.input6 = gr.Slider(minimum=0.1, maximum=10, label="duration", value=1)
69
  statusa = gr.TextArea()
70
  btnVC.click(self.create_tts_fn, inputs=[self.input1, self.input2, self.input3, self.input4, self.input5, self.input6], outputs = [statusa])
@@ -79,33 +78,6 @@ class VitsGradio:
79
  return web,file_path
80
 
81
 
82
- def mori(self,text):
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,7 +116,6 @@ class VitsGradio:
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,11 +125,8 @@ class VitsGradio:
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,11 +197,11 @@ class VitsGradio:
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():
@@ -246,9 +214,7 @@ class VitsGradio:
246
  resampled_audio_data = signal.resample(audio, len(audio) * 2)
247
  sf.write('temp.wav', resampled_audio_data, 44100, 'PCM_24')
248
  return (self.hps_ms.data.sampling_rate, audio),text.replace('[JA]','').replace('[ZH]','')
249
- import asyncio
250
- loop = asyncio.new_event_loop()
251
- asyncio.set_event_loop(loop)
252
  app = Flask(__name__)
253
  print("开始部署")
254
  grVits = VitsGradio()
 
18
  import json
19
  import soundfile as sf
20
  from scipy import signal
 
21
  class VitsGradio:
22
  def __init__(self):
23
  self.lan = ["中文","日文","自动"]
 
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
  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
  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
  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
  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
 
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():
 
214
  resampled_audio_data = signal.resample(audio, len(audio) * 2)
215
  sf.write('temp.wav', resampled_audio_data, 44100, 'PCM_24')
216
  return (self.hps_ms.data.sampling_rate, audio),text.replace('[JA]','').replace('[ZH]','')
217
+
 
 
218
  app = Flask(__name__)
219
  print("开始部署")
220
  grVits = VitsGradio()