Mahiruoshi commited on
Commit
10b14bb
1 Parent(s): 6c0dc2f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -62
app.py CHANGED
@@ -61,38 +61,6 @@ def to_numpy(tensor: torch.Tensor):
61
  return tensor.detach().cpu().numpy() if tensor.requires_grad \
62
  else tensor.detach().numpy()
63
 
64
- def to_html(chat_history):
65
- chat_html = ""
66
- for item in chat_history:
67
- if item['role'] == 'user':
68
- chat_html += f"""
69
- <div style="margin-bottom: 20px;">
70
- <div style="text-align: right; margin-right: 20px;">
71
- <span style="background-color: #4CAF50; color: black; padding: 10px; border-radius: 10px; display: inline-block; max-width: 80%; word-wrap: break-word;">
72
- {item['content']}
73
- </span>
74
- </div>
75
- </div>
76
- """
77
- else:
78
- chat_html += f"""
79
- <div style="margin-bottom: 20px;">
80
- <div style="text-align: left; margin-left: 20px;">
81
- <span style="background-color: white; color: black; padding: 10px; border-radius: 10px; display: inline-block; max-width: 80%; word-wrap: break-word;">
82
- {item['content']}
83
- </span>
84
- </div>
85
- </div>
86
- """
87
- output_html = f"""
88
- <div style="height: 400px; overflow-y: scroll; padding: 10px;">
89
- {chat_html}
90
- </div>
91
- """
92
- return output_html
93
-
94
-
95
-
96
  def chatgpt(text):
97
  messages = []
98
  try:
@@ -108,7 +76,7 @@ def chatgpt(text):
108
  del messages[-2:]
109
  with open('log.pickle', 'wb') as f:
110
  pickle.dump(messages, f)
111
- return reply,messages
112
  except:
113
  messages.append({"role": "user", "content": text},)
114
  chat = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=messages)
@@ -120,7 +88,7 @@ def chatgpt(text):
120
  del messages[-2:]
121
  with open('log.pickle', 'wb') as f:
122
  pickle.dump(messages, f)
123
- return reply,messages
124
 
125
  def get_symbols_from_json(path):
126
  assert os.path.isfile(path)
@@ -154,14 +122,12 @@ def get_text(text,hps_ms):
154
 
155
  def create_tts_fn(net_g,hps,speaker_id):
156
  speaker_id = int(speaker_id)
157
- def tts_fn(is_gpt,api_key,is_audio,audiopath,repeat_time,text, language, extract, n_scale= 0.667,n_scale_w = 0.8, l_scale = 1 ):
158
  repeat_time = int(repeat_time)
159
  if is_gpt:
160
  openai.api_key = api_key
161
- text,messages = chatgpt(text)
162
- htm = to_html(messages)
163
- else:
164
- htm = ""
165
  if not extract:
166
  print(text)
167
  t1 = time.time()
@@ -175,6 +141,7 @@ def create_tts_fn(net_g,hps,speaker_id):
175
  spending_time = "推理时间为:"+str(t2-t1)+"s"
176
  print(spending_time)
177
  file_path = "subtitles.srt"
 
178
  try:
179
  write(audiopath + '.wav',22050,audio)
180
  if is_audio:
@@ -183,7 +150,7 @@ def create_tts_fn(net_g,hps,speaker_id):
183
  os.system(cmd)
184
  except:
185
  pass
186
- return (hps.data.sampling_rate, audio),file_path,htm
187
  else:
188
  a = ['【','[','(','(']
189
  b = ['】',']',')',')']
@@ -215,19 +182,22 @@ def create_tts_fn(net_g,hps,speaker_id):
215
  print(time_end)
216
  f1.write(str(c-1)+'\n'+time_start+' --> '+time_end+'\n'+sentence+'\n\n')
217
  audio_fin.append(audio)
218
- try:
219
- write(audiopath + '.wav',22050,np.concatenate(audio_fin))
220
- if is_audio:
221
- for i in range(repeat_time):
222
- cmd = 'ffmpeg -y -i ' + audiopath + '.wav' + ' -ar 44100 '+ audiopath.replace('temp','temp'+str(i))
223
- os.system(cmd)
 
 
 
224
 
225
- except:
226
- pass
227
  file_path = "subtitles.srt"
228
- return (hps.data.sampling_rate, np.concatenate(audio_fin)),file_path,htm
229
  return tts_fn
230
 
 
 
231
 
232
  if __name__ == '__main__':
233
  hps = utils.get_hparams_from_file('checkpoints/tmp/config.json')
@@ -275,13 +245,13 @@ if __name__ == '__main__':
275
  f'<img style="width:auto;height:400px;" src="file/image/{name}.png">'
276
  '</div>'
277
  )
278
- with gr.Accordion(label="聊天记录", open=False):
279
- with gr.Row():
280
- output_UI = gr.outputs.HTML()
281
- with gr.Column():
282
- input1 = gr.TextArea(label="Text", value=example, lines=3)
283
- btnVC = gr.Button("Submit")
284
- output1 = gr.Audio(label="采样率22050")
285
  with gr.Accordion(label="Setting", open=False):
286
  input2 = gr.Dropdown(label="Language", choices=lan, value="自动", interactive=True)
287
  input3 = gr.Checkbox(value=False, label="长句切割(小说合成)")
@@ -291,12 +261,13 @@ if __name__ == '__main__':
291
  with gr.Accordion(label="Advanced Setting", open=False):
292
  audio_input3 = gr.Dropdown(label="重复次数", choices=list(range(101)), value='0', interactive=True)
293
  api_input1 = gr.Checkbox(value=False, label="接入chatgpt")
294
- api_input2 = gr.TextArea(label="api-key",lines=1,value = '去官网找')
295
  output2 = gr.outputs.File(label="字幕文件:subtitles.srt")
296
  audio_input1 = gr.Checkbox(value=False, label="修改音频路径(live2d)")
297
- audio_input2 = gr.TextArea(label="音频路径",lines=1,value = '示范:D:/app_develop/live2d_whole/2010002/sounds/temp.wav')
298
-
299
-
300
- btnVC.click(tts_fn, inputs=[api_input1,api_input2,audio_input1,audio_input2,audio_input3,input1,input2,input3,input4,input5,input6], outputs=[output1,output2,output_UI])
 
301
 
302
- app.launch()
 
61
  return tensor.detach().cpu().numpy() if tensor.requires_grad \
62
  else tensor.detach().numpy()
63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  def chatgpt(text):
65
  messages = []
66
  try:
 
76
  del messages[-2:]
77
  with open('log.pickle', 'wb') as f:
78
  pickle.dump(messages, f)
79
+ return reply
80
  except:
81
  messages.append({"role": "user", "content": text},)
82
  chat = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=messages)
 
88
  del messages[-2:]
89
  with open('log.pickle', 'wb') as f:
90
  pickle.dump(messages, f)
91
+ return reply
92
 
93
  def get_symbols_from_json(path):
94
  assert os.path.isfile(path)
 
122
 
123
  def create_tts_fn(net_g,hps,speaker_id):
124
  speaker_id = int(speaker_id)
125
+ def tts_fn(history,is_gpt,api_key,is_audio,audiopath,repeat_time,text, language, extract, n_scale= 0.667,n_scale_w = 0.8, l_scale = 1 ):
126
  repeat_time = int(repeat_time)
127
  if is_gpt:
128
  openai.api_key = api_key
129
+ text = chatgpt(text)
130
+ history[-1][1] = text
 
 
131
  if not extract:
132
  print(text)
133
  t1 = time.time()
 
141
  spending_time = "推理时间为:"+str(t2-t1)+"s"
142
  print(spending_time)
143
  file_path = "subtitles.srt"
144
+ write('moe/temp.wav',22050,audio)
145
  try:
146
  write(audiopath + '.wav',22050,audio)
147
  if is_audio:
 
150
  os.system(cmd)
151
  except:
152
  pass
153
+ return history,file_path,(hps.data.sampling_rate,audio)
154
  else:
155
  a = ['【','[','(','(']
156
  b = ['】',']',')',')']
 
182
  print(time_end)
183
  f1.write(str(c-1)+'\n'+time_start+' --> '+time_end+'\n'+sentence+'\n\n')
184
  audio_fin.append(audio)
185
+ try:
186
+ write(audiopath + '.wav',22050,np.concatenate(audio_fin))
187
+ if is_audio:
188
+ for i in range(repeat_time):
189
+ cmd = 'ffmpeg -y -i ' + audiopath + '.wav' + ' -ar 44100 '+ audiopath.replace('temp','temp'+str(i))
190
+ os.system(cmd)
191
+
192
+ except:
193
+ pass
194
 
 
 
195
  file_path = "subtitles.srt"
196
+ return history,file_path,(hps.data.sampling_rate, np.concatenate(audio_fin))
197
  return tts_fn
198
 
199
+ def bot(history,user_message):
200
+ return history + [[user_message, None]]
201
 
202
  if __name__ == '__main__':
203
  hps = utils.get_hparams_from_file('checkpoints/tmp/config.json')
 
245
  f'<img style="width:auto;height:400px;" src="file/image/{name}.png">'
246
  '</div>'
247
  )
248
+ chatbot = gr.Chatbot(elem_id="History")
249
+ with gr.Row():
250
+ with gr.Column(scale=0.85):
251
+ input1 = gr.TextArea(label="Text", value=example,lines = 1)
252
+ with gr.Column(scale=0.15, min_width=0):
253
+ btnVC = gr.Button("Send")
254
+ output1 = gr.Audio(label="采样率22050")
255
  with gr.Accordion(label="Setting", open=False):
256
  input2 = gr.Dropdown(label="Language", choices=lan, value="自动", interactive=True)
257
  input3 = gr.Checkbox(value=False, label="长句切割(小说合成)")
 
261
  with gr.Accordion(label="Advanced Setting", open=False):
262
  audio_input3 = gr.Dropdown(label="重复次数", choices=list(range(101)), value='0', interactive=True)
263
  api_input1 = gr.Checkbox(value=False, label="接入chatgpt")
264
+ api_input2 = gr.TextArea(label="api-key",lines=1,value = '见 https://openai.com/blog/openai-api')
265
  output2 = gr.outputs.File(label="字幕文件:subtitles.srt")
266
  audio_input1 = gr.Checkbox(value=False, label="修改音频路径(live2d)")
267
+ audio_input2 = gr.TextArea(label="音频路径",lines=1,value = '#参考 D:/app_develop/live2d_whole/2010002/sounds/temp.wav')
268
+
269
+ btnVC.click(bot, inputs = [chatbot,input1], outputs = [chatbot]).then(
270
+ tts_fn, inputs=[chatbot,api_input1,api_input2,audio_input1,audio_input2,audio_input3,input1,input2,input3,input4,input5,input6], outputs=[chatbot,output2,output1]
271
+ )
272
 
273
+ app.launch()