forked from modelscope/FunASR
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunasr_server_http.py
More file actions
38 lines (32 loc) · 1.24 KB
/
funasr_server_http.py
File metadata and controls
38 lines (32 loc) · 1.24 KB
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
import argparse
import base64
import io
import soundfile as sf
import uvicorn
from fastapi import FastAPI, Body
app = FastAPI()
from funasr_onnx import Paraformer
model_dir = "damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-onnx"
model = Paraformer(model_dir, batch_size=1, quantize=True)
async def recognition_onnx(waveform):
result=model(waveform)[0]["preds"][0]
return result
@app.post("/api/asr")
async def asr(item: dict = Body(...)):
try:
audio_bytes= base64.b64decode(bytes(item['wav_base64'], 'utf-8'))
waveform, _ = sf.read(io.BytesIO(audio_bytes))
result=await recognition_onnx(waveform)
ret = {"results": result, "code": 0}
except:
print('请求出错,这里是处理出错的')
ret = {"results": '', "code": 1}
return ret
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='API Service')
parser.add_argument('--listen', default='0.0.0.0', type=str, help='the network to listen')
parser.add_argument('--port', default=8888, type=int, help='the port to listen')
args = parser.parse_args()
print('start...')
print('server on:',args)
uvicorn.run(app, host=args.listen, port=args.port)