-
Notifications
You must be signed in to change notification settings - Fork 108
/
infer_server.py
163 lines (147 loc) · 6.84 KB
/
infer_server.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
import argparse
import functools
import os
import random
import sys
import time
import wave
from datetime import datetime
import aiofiles
import uvicorn
from fastapi import FastAPI, WebSocket, UploadFile, File, Request
from starlette.staticfiles import StaticFiles
from starlette.templating import Jinja2Templates
from starlette.websockets import WebSocketState
from masr.predict import MASRPredictor
from masr.utils.logger import setup_logger
from masr.utils.utils import add_arguments, print_arguments
logger = setup_logger(__name__)
parser = argparse.ArgumentParser(description=__doc__)
add_arg = functools.partial(add_arguments, argparser=parser)
add_arg('configs', str, 'configs/conformer.yml', "配置文件")
add_arg("host", str, '0.0.0.0', "监听主机的IP地址")
add_arg("port", int, 5000, "服务所使用的端口号")
add_arg("save_path", str, 'dataset/upload/', "上传音频文件的保存目录")
add_arg('use_gpu', bool, True, "是否使用GPU预测")
add_arg('use_pun', bool, False, "是否给识别结果加标点符号")
add_arg('is_itn', bool, False, "是否对文本进行反标准化")
add_arg('model_path', str, 'models/conformer_streaming_fbank/inference.pt', "导出的预测模型文件路径")
add_arg('pun_model_dir', str, 'models/pun_models/', "加标点符号的模型文件夹路径")
args = parser.parse_args()
print_arguments(args=args)
app = FastAPI(title="PPASR")
app.mount('/static', StaticFiles(directory='static'), name='static')
templates = Jinja2Templates(directory="templates")
# 创建预测器
predictor = MASRPredictor(configs=args.configs,
model_path=args.model_path,
use_gpu=args.use_gpu,
use_pun=args.use_pun,
pun_model_dir=args.pun_model_dir)
# 语音识别接口
@app.post("/recognition")
async def recognition(audio: UploadFile = File(..., description="音频文件")):
# 保存路径
save_dir = os.path.join(args.save_path, datetime.now().strftime('%Y-%m-%d'))
os.makedirs(save_dir, exist_ok=True)
suffix = audio.filename.split('.')[-1]
file_path = os.path.join(save_dir, f'{int(time.time() * 1000)}_{random.randint(100, 999)}.{suffix}')
async with aiofiles.open(file_path, 'wb') as out_file:
content = await audio.read()
await out_file.write(content)
try:
start = time.time()
# 执行识别
result = predictor.predict(audio_data=file_path, use_pun=args.use_pun, is_itn=args.is_itn)
score, text = result['score'], result['text']
end = time.time()
print("识别时间:%dms,识别结果:%s, 得分: %f" % (round((end - start) * 1000), text, score))
result = {"code": 0, "msg": "success", "result": text, "score": round(score, 3)}
return result
except Exception as e:
print(f'[{datetime.now()}] 短语音识别失败,错误信息:{e}', file=sys.stderr)
return {"error": 1, "msg": "audio read fail!"}
# 长语音识别接口
@app.post("/recognition_long_audio")
async def recognition_long_audio(audio: UploadFile = File(..., description="音频文件")):
# 保存路径
save_dir = os.path.join(args.save_path, datetime.now().strftime('%Y-%m-%d'))
os.makedirs(save_dir, exist_ok=True)
suffix = audio.filename.split('.')[-1]
file_path = os.path.join(save_dir, f'{int(time.time() * 1000)}_{random.randint(100, 999)}.{suffix}')
async with aiofiles.open(file_path, 'wb') as out_file:
content = await audio.read()
await out_file.write(content)
try:
start = time.time()
result = predictor.predict_long(audio_data=file_path, use_pun=args.use_pun, is_itn=args.is_itn)
score, text = result['score'], result['text']
end = time.time()
print("识别时间:%dms,识别结果:%s, 得分: %f" % (round((end - start) * 1000), text, score))
result = {"code": 0, "msg": "success", "result": text, "score": score}
return result
except Exception as e:
print(f'[{datetime.now()}] 长语音识别失败,错误信息:{e}', file=sys.stderr)
return {"error": 1, "msg": "audio read fail!"}
@app.get("/")
async def index(request: Request):
return templates.TemplateResponse("index.html", {"request": request, "id": id})
@app.websocket("/")
async def websocket_endpoint(websocket: WebSocket):
await websocket.accept()
logger.info(f'有WebSocket连接建立')
if not predictor.running:
frames = []
score, text = 0, ""
while True:
try:
data = await websocket.receive_bytes()
frames.append(data)
if len(data) == 0: continue
is_end = False
# 判断是不是结束预测
if b'end' == data[-3:]:
is_end = True
data = data[:-3]
# 开始预测
result = predictor.predict_stream(audio_data=data, use_pun=args.use_pun, is_itn=args.is_itn,
is_end=is_end)
if result is not None:
score, text = result['score'], result['text']
send_data = {"code": 0, "result": text}
logger.info(f'向客户端发生消息:{send_data}')
await websocket.send_json(send_data)
# 结束了要关闭当前的连接
if is_end: await websocket.close()
except Exception as e:
if websocket.client_state == WebSocketState.DISCONNECTED:
logger.info("用户已断开连接")
break
logger.error(f'识别发生错误:错误信息:{e}')
try:
await websocket.send_json({"code": 2, "msg": "recognition fail!"})
except:
break
# 重置流式识别
predictor.reset_stream()
predictor.running = False
# 保存录音
save_dir = os.path.join(args.save_path, datetime.now().strftime('%Y-%m-%d'))
os.makedirs(save_dir, exist_ok=True)
file_path = os.path.join(save_dir, f'{int(time.time() * 1000)}.wav')
audio_bytes = b''.join(frames)
wf = wave.open(file_path, 'wb')
wf.setnchannels(1)
wf.setsampwidth(2)
wf.setframerate(16000)
wf.writeframes(audio_bytes)
wf.close()
else:
logger.error(f'语音识别失败,预测器不足')
await websocket.send_json({"code": 1, "msg": "recognition fail, no resource!"})
await websocket.close()
if __name__ == '__main__':
# 创建保存路径
if not os.path.exists(args.save_path):
os.makedirs(args.save_path)
uvicorn.run(app, host=args.host, port=args.port)