mirror of
https://github.com/modelscope/FunASR
synced 2025-09-15 14:48:36 +08:00
funasr-onnx http
This commit is contained in:
parent
7d1886384b
commit
d83e999f59
18
runtime/python/onnxruntime/funasr_client_http.py
Normal file
18
runtime/python/onnxruntime/funasr_client_http.py
Normal file
@ -0,0 +1,18 @@
|
||||
import base64
|
||||
import requests
|
||||
import threading
|
||||
|
||||
|
||||
with open('A2_0.wav','rb') as f:
|
||||
test_wav_bytes=f.read()
|
||||
url='http://127.0.0.1:8888/api/asr'
|
||||
def send_post(i,url,wav_bytes):
|
||||
r1=requests.post(url,json={'wav_base64':str(base64.b64encode(wav_bytes), 'utf-8')})
|
||||
print('线程:',i,r1.json())
|
||||
|
||||
|
||||
for i in range(100):
|
||||
t = threading.Thread(target=send_post, args=(i,url,test_wav_bytes,))
|
||||
t.start()
|
||||
#t.join()
|
||||
print('完成测试')
|
||||
38
runtime/python/onnxruntime/funasr_server_http.py
Normal file
38
runtime/python/onnxruntime/funasr_server_http.py
Normal file
@ -0,0 +1,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)
|
||||
Loading…
Reference in New Issue
Block a user