chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
# Service with http-python
|
||||
|
||||
## Server
|
||||
|
||||
1. Install requirements
|
||||
|
||||
```shell
|
||||
cd funasr/runtime/python/http
|
||||
pip install -r requirements.txt
|
||||
```
|
||||
|
||||
2. Start server
|
||||
|
||||
```shell
|
||||
python server.py --port 8000
|
||||
```
|
||||
|
||||
More parameters:
|
||||
```shell
|
||||
python server.py \
|
||||
--host [host ip] \
|
||||
--port [server port] \
|
||||
--asr_model [asr model_name] \
|
||||
--vad_model [vad model_name] \
|
||||
--punc_model [punc model_name] \
|
||||
--device [cuda or cpu] \
|
||||
--ngpu [0 or 1] \
|
||||
--ncpu [1 or 4] \
|
||||
--hotword_path [path of hot word txt] \
|
||||
--certfile [path of certfile for ssl] \
|
||||
--keyfile [path of keyfile for ssl] \
|
||||
--temp_dir [upload file temp dir]
|
||||
```
|
||||
|
||||
## Client
|
||||
|
||||
```shell
|
||||
# get test audio file
|
||||
wget https://isv-data.oss-cn-hangzhou.aliyuncs.com/ics/MaaS/ASR/test_audio/asr_example_zh.wav
|
||||
python client.py --host=127.0.0.1 --port=8000 --audio_path=asr_example_zh.wav
|
||||
```
|
||||
|
||||
More parameters:
|
||||
```shell
|
||||
python server.py \
|
||||
--host [sever ip] \
|
||||
--port [sever port] \
|
||||
--audio_path [use audio path]
|
||||
```
|
||||
|
||||
|
||||
## 支持多进程
|
||||
|
||||
方法是启动多个`server.py`,然后通过Nginx的负载均衡分发请求,达到支持多用户同时连效果,处理方式如下,默认您已经安装了Nginx,没安装的请参考[官方安装教程](https://nginx.org/en/linux_packages.html#Ubuntu)。
|
||||
|
||||
配置Nginx。
|
||||
```shell
|
||||
sudo cp -f asr_nginx.conf /etc/nginx/nginx.conf
|
||||
sudo service nginx reload
|
||||
```
|
||||
|
||||
然后使用脚本启动多个服务,每个服务的端口号不一样。
|
||||
```shell
|
||||
sudo chmod +x start_server.sh
|
||||
./start_server.sh
|
||||
```
|
||||
|
||||
**说明:** 默认是3个进程,如果需要修改,首先修改`start_server.sh`的最后那部分,可以添加启动数量。然后修改`asr_nginx.conf`配置文件的`upstream backend`部分,增加新启动的服务,可以使其他服务器的服务。
|
||||
@@ -0,0 +1,44 @@
|
||||
user nginx;
|
||||
worker_processes auto;
|
||||
|
||||
error_log /var/log/nginx/error.log notice;
|
||||
pid /var/run/nginx.pid;
|
||||
|
||||
events {
|
||||
worker_connections 1024;
|
||||
}
|
||||
|
||||
|
||||
http {
|
||||
include /etc/nginx/mime.types;
|
||||
default_type application/octet-stream;
|
||||
|
||||
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
|
||||
'$status $body_bytes_sent "$http_referer" '
|
||||
'"$http_user_agent" "$http_x_forwarded_for"';
|
||||
|
||||
access_log /var/log/nginx/access.log main;
|
||||
|
||||
sendfile on;
|
||||
keepalive_timeout 65;
|
||||
|
||||
upstream backend {
|
||||
# 最少连接算法
|
||||
least_conn;
|
||||
# 启动的服务地址
|
||||
server localhost:8001;
|
||||
server localhost:8002;
|
||||
server localhost:8003;
|
||||
}
|
||||
|
||||
server {
|
||||
# 实际访问的端口
|
||||
listen 8000;
|
||||
|
||||
location / {
|
||||
proxy_pass http://backend;
|
||||
}
|
||||
}
|
||||
|
||||
include /etc/nginx/conf.d/*.conf;
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import os
|
||||
|
||||
import requests
|
||||
import argparse
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--host", type=str, default="127.0.0.1", required=False, help="sever ip")
|
||||
parser.add_argument("--port", type=int, default=8000, required=False, help="server port")
|
||||
parser.add_argument(
|
||||
"--audio_path", type=str, default="asr_example_zh.wav", required=False, help="use audio path"
|
||||
)
|
||||
args = parser.parse_args()
|
||||
print("----------- Configuration Arguments -----------")
|
||||
for arg, value in vars(args).items():
|
||||
print("%s: %s" % (arg, value))
|
||||
print("------------------------------------------------")
|
||||
|
||||
|
||||
url = f"http://{args.host}:{args.port}/recognition"
|
||||
headers = {}
|
||||
files = [
|
||||
(
|
||||
"audio",
|
||||
(
|
||||
os.path.basename(args.audio_path),
|
||||
open(args.audio_path, "rb"),
|
||||
"application/octet-stream",
|
||||
),
|
||||
)
|
||||
]
|
||||
|
||||
response = requests.post(url, headers=headers, files=files)
|
||||
print(response.text)
|
||||
@@ -0,0 +1,2 @@
|
||||
阿里巴巴
|
||||
通义实验室
|
||||
@@ -0,0 +1,6 @@
|
||||
modelscope>=1.11.1
|
||||
funasr>=1.0.5
|
||||
fastapi>=0.95.1
|
||||
aiofiles
|
||||
uvicorn
|
||||
requests
|
||||
@@ -0,0 +1,133 @@
|
||||
import argparse
|
||||
import logging
|
||||
import os
|
||||
import uuid
|
||||
|
||||
import aiofiles
|
||||
import ffmpeg
|
||||
import uvicorn
|
||||
from fastapi import FastAPI, File, UploadFile
|
||||
from modelscope.utils.logger import get_logger
|
||||
|
||||
from funasr import AutoModel
|
||||
|
||||
logger = get_logger(log_level=logging.INFO)
|
||||
logger.setLevel(logging.INFO)
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument(
|
||||
"--host", type=str, default="0.0.0.0", required=False, help="host ip, localhost, 0.0.0.0"
|
||||
)
|
||||
parser.add_argument("--port", type=int, default=8000, required=False, help="server port")
|
||||
parser.add_argument(
|
||||
"--asr_model",
|
||||
type=str,
|
||||
default="paraformer-zh",
|
||||
help="asr model from https://github.com/alibaba-damo-academy/FunASR?tab=readme-ov-file#model-zoo",
|
||||
)
|
||||
parser.add_argument("--asr_model_revision", type=str, default="v2.0.4", help="")
|
||||
parser.add_argument(
|
||||
"--vad_model",
|
||||
type=str,
|
||||
default="fsmn-vad",
|
||||
help="vad model from https://github.com/alibaba-damo-academy/FunASR?tab=readme-ov-file#model-zoo",
|
||||
)
|
||||
parser.add_argument("--vad_model_revision", type=str, default="v2.0.4", help="")
|
||||
parser.add_argument(
|
||||
"--punc_model",
|
||||
type=str,
|
||||
default="ct-punc-c",
|
||||
help="model from https://github.com/alibaba-damo-academy/FunASR?tab=readme-ov-file#model-zoo",
|
||||
)
|
||||
parser.add_argument("--punc_model_revision", type=str, default="v2.0.4", help="")
|
||||
parser.add_argument("--ngpu", type=int, default=1, help="0 for cpu, 1 for gpu")
|
||||
parser.add_argument("--device", type=str, default="cuda", help="cuda, cpu")
|
||||
parser.add_argument("--ncpu", type=int, default=4, help="cpu cores")
|
||||
parser.add_argument(
|
||||
"--hotword_path",
|
||||
type=str,
|
||||
default="hotwords.txt",
|
||||
help="hot word txt path, only the hot word model works",
|
||||
)
|
||||
parser.add_argument("--certfile", type=str, default=None, required=False, help="certfile for ssl")
|
||||
parser.add_argument("--keyfile", type=str, default=None, required=False, help="keyfile for ssl")
|
||||
parser.add_argument("--temp_dir", type=str, default="temp_dir/", required=False, help="temp dir")
|
||||
args = parser.parse_args()
|
||||
logger.info("----------- Configuration Arguments -----------")
|
||||
for arg, value in vars(args).items():
|
||||
logger.info("%s: %s" % (arg, value))
|
||||
logger.info("------------------------------------------------")
|
||||
|
||||
os.makedirs(args.temp_dir, exist_ok=True)
|
||||
|
||||
logger.info("model loading")
|
||||
# load funasr model
|
||||
model = AutoModel(
|
||||
model=args.asr_model,
|
||||
model_revision=args.asr_model_revision,
|
||||
vad_model=args.vad_model,
|
||||
vad_model_revision=args.vad_model_revision,
|
||||
punc_model=args.punc_model,
|
||||
punc_model_revision=args.punc_model_revision,
|
||||
ngpu=args.ngpu,
|
||||
ncpu=args.ncpu,
|
||||
device=args.device,
|
||||
disable_pbar=True,
|
||||
disable_log=True,
|
||||
)
|
||||
logger.info("loaded models!")
|
||||
|
||||
app = FastAPI(title="FunASR")
|
||||
|
||||
param_dict = {"sentence_timestamp": True, "batch_size_s": 300}
|
||||
if args.hotword_path is not None and os.path.exists(args.hotword_path):
|
||||
with open(args.hotword_path, "r", encoding="utf-8") as f:
|
||||
lines = f.readlines()
|
||||
lines = [line.strip() for line in lines]
|
||||
hotword = " ".join(lines)
|
||||
logger.info(f"热词:{hotword}")
|
||||
param_dict["hotword"] = hotword
|
||||
|
||||
|
||||
@app.post("/recognition")
|
||||
async def api_recognition(audio: UploadFile = File(..., description="audio file")):
|
||||
suffix = audio.filename.split(".")[-1]
|
||||
audio_path = f"{args.temp_dir}/{str(uuid.uuid1())}.{suffix}"
|
||||
async with aiofiles.open(audio_path, "wb") as out_file:
|
||||
content = await audio.read()
|
||||
await out_file.write(content)
|
||||
try:
|
||||
audio_bytes, _ = (
|
||||
ffmpeg.input(audio_path, threads=0)
|
||||
.output("-", format="s16le", acodec="pcm_s16le", ac=1, ar=16000)
|
||||
.run(cmd=["ffmpeg", "-nostdin"], capture_stdout=True, capture_stderr=True)
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error(f"读取音频文件发生错误,错误信息:{e}")
|
||||
return {"msg": "读取音频文件发生错误", "code": 1}
|
||||
rec_results = model.generate(input=audio_bytes, is_final=True, **param_dict)
|
||||
# 结果为空
|
||||
if len(rec_results[0]["text"] ) == 0:
|
||||
return {"text": "", "sentences": [], "code": 0}
|
||||
elif len(rec_results[0]["text"] ) > 0:
|
||||
# 解析识别结果
|
||||
rec_result = rec_results[0]
|
||||
text = rec_result["text"]
|
||||
sentences = []
|
||||
for sentence in rec_result["sentence_info"]:
|
||||
# 每句话的时间戳
|
||||
sentences.append(
|
||||
{"text": sentence["text"], "start": sentence["start"], "end": sentence["end"]}
|
||||
)
|
||||
ret = {"text": text, "sentences": sentences, "code": 0}
|
||||
logger.info(f"识别结果:{ret}")
|
||||
return ret
|
||||
else:
|
||||
logger.info(f"识别结果:{rec_results}")
|
||||
return {"msg": "未知错误", "code": -1}
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
uvicorn.run(
|
||||
app, host=args.host, port=args.port, ssl_keyfile=args.keyfile, ssl_certfile=args.certfile
|
||||
)
|
||||
@@ -0,0 +1,21 @@
|
||||
#!/bin/bash
|
||||
|
||||
# 创建日志文件夹
|
||||
if [ ! -d "log/" ];then
|
||||
mkdir log
|
||||
fi
|
||||
|
||||
# kill掉之前的进程
|
||||
server_id=`ps -ef | grep server.py | grep -v "grep" | awk '{print $2}'`
|
||||
echo $server_id
|
||||
|
||||
for id in $server_id
|
||||
do
|
||||
kill -9 $id
|
||||
echo "killed $id"
|
||||
done
|
||||
|
||||
# 启动多个服务,可以设置使用不同的显卡
|
||||
CUDA_VISIBLE_DEVICES=0 nohup python -u server.py --host=localhost --port=8001 >> log/output1.log 2>&1 &
|
||||
CUDA_VISIBLE_DEVICES=0 nohup python -u server.py --host=localhost --port=8002 >> log/output2.log 2>&1 &
|
||||
CUDA_VISIBLE_DEVICES=0 nohup python -u server.py --host=localhost --port=8003 >> log/output3.log 2>&1 &
|
||||
Reference in New Issue
Block a user