/
app.py
348 lines (310 loc) · 13.8 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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import json
import os
import time
from uuid import uuid4
from flask import Flask, request, jsonify, render_template, url_for, redirect
from flask_restful.reqparse import RequestParser
import requests
from utils import tts_helper
from utils.sovits import inference_main
from utils import gpu_woker_quene
from utils import tools
from utils.chatglm.infer import chatInfer
import yaml
config = yaml.safe_load(open("./config.yaml", encoding="utf-8"))
app = Flask(__name__)
app.debug = config["flask"]["debug"]
app.secret_key = config["flask"]["secret_key"]
sqlite_path = config["sqlite"]["path"]
worker_quene = gpu_woker_quene.GPUWorkerQuene()
worker_quene.start()
@app.route("/GetVoice")
def giveBackURL():
if request.method == "GET":
# 从./db/character.db中随机取出一条数据,以json返回
import sqlite3
conn = sqlite3.connect(sqlite_path + "character.db")
c = conn.cursor()
c.execute(
"SELECT * FROM character WHERE id >= (ABS(RANDOM()) % (SELECT MAX(id) FROM character)) LIMIT 1;"
)
result = c.fetchone()
conn.close()
# 生成返回的json
return jsonify(
{
"character": result[1],
"topic": result[2],
"text": result[3],
"audio": result[4],
}
)
@app.route("/GetVoice/v2")
def giveBackURLv2():
if request.method == "GET":
try:
parser = RequestParser()
parser.add_argument("character", location="args", required=False)
parser.add_argument("topic", location="args", required=False)
parser.add_argument("text", location="args", required=False)
parser.add_argument("sex", location="args", required=False)
parser.add_argument("type", location="args", required=False)
args = parser.parse_args()
print(args)
# 替换掉空参数
if args["character"] == None:
args["character"] = ""
if args["topic"] == None:
args["topic"] = ""
if args["text"] == None:
args["text"] = ""
if args["sex"] == None:
args["sex"] = ""
if args["type"] == None:
args["type"] = ""
import sqlite3
conn = sqlite3.connect(sqlite_path + "genshinVoice.db")
c = conn.cursor()
with conn:
# 如果没有参数,随机返回一条数据
if (
args["character"] == None
and args["topic"] == None
and args["text"] == None
):
c.execute(
"SELECT * FROM character WHERE id >= (ABS(RANDOM()) % (SELECT MAX(id) FROM character)) LIMIT 1;"
)
result = c.fetchone()
return jsonify(result)
# 如果有参数,根据参数返回数据
else:
sql = """
with filtered as (SELECT id
FROM character
WHERE npcNameLocal like ?
AND topic like ?
AND "text" like ?
AND sex like ?
AND type like ?)
select *
from character
where id in (select id from filtered order by random() limit 1)
"""
c.execute(
sql,
(
f"%{args['character']}%",
f"%{args['topic']}%",
f"%{args['text']}%",
f"%{args['sex']}%",
f"%{args['type']}%",
),
)
# 从result中随机取出一条数据
results = c.fetchall()
# 如果没有数据,返回404
if len(results) == 0:
return jsonify({"text": "进不去……"}), 404
result = results[0]
return jsonify(
{
"npcNameLocal": result[1],
"sex": result[2],
"type": result[3],
"topic": result[4],
"text": result[5],
"npcNameCode": result[6],
"language": result[7],
"fileName": result[8],
"audioURL": result[9],
}
)
except Exception as e:
print(e)
return jsonify({"text": "进不去……", "err": str(e)}), 404
@app.route("/GetVoice/v3")
def generateCharaVoice():
if request.method == "GET":
try:
parser = RequestParser()
# 选择的角色以及将要生成的文本
parser.add_argument("character", location="args", required=False)
parser.add_argument("text", location="args", required=True)
args = parser.parse_args()
# 如果角色为空,则从配置文件随机选一个角色
voice_model = {}
if args["character"] == None:
import random
voice_model = random.choice(config["genshin"]["voice_model"])
else:
if any(
model["character_name"] == args["character"]
for model in config["genshin"]["voice_model"]
):
voice_model = next(
model
for model in config["genshin"]["voice_model"]
if model["character_name"] == args["character"]
)
else:
return jsonify({"text": "角色不存在……"}), 404
tts_output_path = tts_helper.tts_azure(
tts_config=config["azure"], txt=args["text"]
)
infer_args = {
"model_path": voice_model["model_path"],
"config_path": voice_model["config_path"],
"trans": voice_model["key"],
"spk_list": voice_model["speaker_name"],
"clean_names": tts_output_path,
}
worker_quene.add_task((inference_main.inference, infer_args))
genshin_voice_path = f'./{tts_output_path.replace("tts", "infer")}_{str(voice_model["key"])+"key"}_{voice_model["speaker_name"]}.flac'
# 等待生成的音频文件生成, 检查文件是否存在
start_time = time.time()
while not os.path.exists(genshin_voice_path):
time.sleep(0.1)
# 最多等待120秒
if time.time() - start_time > 120:
return jsonify({"text": "进不去……"}), 404
# 调用ffmpeg将音频文件转码为ogg
os.system('/usr/bin/ffmpeg -i '+ genshin_voice_path +' -c:a libopus -b:a 96K {}.ogg -y'.format(genshin_voice_path.replace('.flac', '')))
# 删除原始文件
os.remove(genshin_voice_path)
genshin_voice_path = genshin_voice_path.replace('.flac', '.ogg')
upload_url = "{}/upload?token={}".format(
config["genshin"]["file_server"]["host"],
config["genshin"]["file_server"]["token"],
)
# 将音频文件上传到api服务器
file_path = tools.upload_file(upload_url, genshin_voice_path)
if file_path != None:
voice_url = "{}{}?token={}".format(
config["genshin"]["file_server"]["host"],
file_path,
config["genshin"]["file_server"]["token"],
)
return jsonify(
{
"voice_url": voice_url,
"character": voice_model["character_name"],
"text": args["text"],
}
)
else:
return jsonify({"text": "进不去……"}), 404
except Exception as e:
print(e)
return jsonify({"text": "进不去……", "err": str(e)}), 404
# 自定义SSML生成语音文件
@app.route("/GetVoice/v4", methods=['POST'])
def generateCharaVoicePro():
if request.method == "POST":
try:
# 从表单导入参数,设置必填参数
args = request.form.to_dict()
# 必填
if 'text' not in args:
return jsonify({"text": "进不去……"}), 404
# 如果角色为空,则从配置文件随机选一个角色
voice_model = {}
if 'character' not in args:
import random
voice_model = random.choice(config["genshin"]["voice_model"])
else:
if any(
model["character_name"] == args["character"]
for model in config["genshin"]["voice_model"]
):
voice_model = next(
model
for model in config["genshin"]["voice_model"]
if model["character_name"] == args["character"]
)
else:
return jsonify({"text": "角色不存在……"}), 404
tts_output_path = tts_helper.tts_azure_customized(
tts_config=config["azure"], ssml=args["text"]
)
infer_args = {
"model_path": voice_model["model_path"],
"config_path": voice_model["config_path"],
"trans": voice_model["key"],
"spk_list": voice_model["speaker_name"],
"clean_names": tts_output_path,
}
worker_quene.add_task((inference_main.inference, infer_args))
genshin_voice_path = f'./{tts_output_path.replace("tts", "infer")}_{str(voice_model["key"])+"key"}_{voice_model["speaker_name"]}.flac'
# 等待生成的音频文件生成, 检查文件是否存在
start_time = time.time()
while not os.path.exists(genshin_voice_path):
time.sleep(0.1)
# 最多等待120秒
if time.time() - start_time > 120:
return jsonify({"text": "进不去……"}), 404
# 调用ffmpeg将音频文件转码为ogg
os.system('/usr/bin/ffmpeg -i '+ genshin_voice_path +' -c:a libopus -b:a 96K {}.ogg -y'.format(genshin_voice_path.replace('.flac', '')))
# 删除原始文件
os.remove(genshin_voice_path)
genshin_voice_path = genshin_voice_path.replace('.flac', '.ogg')
upload_url = "{}/upload?token={}".format(
config["genshin"]["file_server"]["host"],
config["genshin"]["file_server"]["token"],
)
# 将音频文件上传到api服务器
file_path = tools.upload_file(upload_url, genshin_voice_path)
if file_path != None:
voice_url = "{}{}?token={}".format(
config["genshin"]["file_server"]["host"],
file_path,
config["genshin"]["file_server"]["token"],
)
return jsonify(
{
"voice_url": voice_url,
"character": voice_model["character_name"],
"text": args["text"],
}
)
else:
return jsonify({"text": "进不去……"}), 404
except Exception as e:
print(e)
return jsonify({"text": "进不去……", "err": str(e)}), 404
# chatGLM 调用
@app.route("/Chat", methods=['GET'])
def chatGML_rq():
if request.method == 'GET':
try:
parser = RequestParser()
# 输入的文本
parser.add_argument("text", location="args", required=False)
args = parser.parse_args()
text = ""
if args["text"] != None:
text = args["text"]
# 生成一个uuid
uuid = str(uuid4())
args = {'text': text, 'filename': 'audio_temp/chat_temp/{}.json'.format(uuid)}
worker_quene.add_task((chatInfer, args))
# 等待生成的回复文件生成, 检查文件是否存在
start_time = time.time()
while not os.path.exists(args["filename"]):
time.sleep(0.1)
# 最多等待30秒
if time.time() - start_time > 30:
return jsonify({"text": "进不去……"}), 404
out_text = ''
with open(args["filename"], 'r',encoding='UTF8') as f:
out_text = json.loads(f.read())['text']
if out_text == '':
return jsonify({"text": "进不去……", "err": str(e)}), 404
return jsonify({"text": tools.remove_spaces_between_chinese_characters(out_text)})
except Exception as e:
print(e)
return jsonify({"text": "进不去……", "err": str(e)}), 404
def run():
app.run(host="*0.0.0.0", port=8000)
if __name__ == "__main__":
app.run(host="127.0.0.1", port=8000)