Open-source speech recognition toolkit for training, inference, streaming ASR, VAD, punctuation, speaker diarization pipelines, and OpenAI-compatible/MCP serving.
Python asraudiochineseemotion-recognitionfunasrmcp-servermultilingual-asropenai-compatible-apiparaformerpunctuationpytorchreal-time-asrspeaker-diarizationspeech-recognitionspeech-to-textstreaming-asrtranscriptionvllmvoice-activity-detectionwhisper-alternative
Support for multiple processes (#951)
夜雨飘零91f614934fb9
diff --git a/funasr/runtime/python/http/README.md b/funasr/runtime/python/http/README.mdindex 5b3fbb353..ee37f569f 100644--- a/funasr/runtime/python/http/README.md+++ b/funasr/runtime/python/http/README.md@@ -45,3 +45,22 @@ python server.py \--add_pun [add pun to result] \--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`部分,增加新启动的服务,可以使其他服务器的服务。diff --git a/funasr/runtime/python/http/asr_nginx.conf b/funasr/runtime/python/http/asr_nginx.confnew file mode 100644index 000000000..769774f9d--- /dev/null+++ b/funasr/runtime/python/http/asr_nginx.conf@@ -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;+}\ No newline at end of filediff --git a/funasr/runtime/python/http/client.py b/funasr/runtime/python/http/client.pyindex 272ce9780..c23761958 100644--- a/funasr/runtime/python/http/client.py+++ b/funasr/runtime/python/http/client.py@@ -1,3 +1,5 @@+import os+import requestsimport argparse@@ -32,7 +34,7 @@ print("------------------------------------------------")url = f'http://{args.host}:{args.port}/recognition'data = {'add_pun': args.add_pun}headers = {}-files = [('audio', ('file', open(args.audio_path, 'rb'), 'application/octet-stream'))]+files = [('audio', (os.path.basename(args.audio_path), open(args.audio_path, 'rb'), 'application/octet-stream'))]response = requests.post(url, headers=headers, data=data, files=files)print(response.text)diff --git a/funasr/runtime/python/http/server.py b/funasr/runtime/python/http/server.pyindex 3fe3b55d0..b1d4761e3 100644--- a/funasr/runtime/python/http/server.py+++ b/funasr/runtime/python/http/server.py@@ -1,8 +1,7 @@import argparseimport loggingimport os-import random-import time+import uuidimport aiofilesimport ffmpeg@@ -92,7 +91,7 @@ app = FastAPI(title="FunASR")async def api_recognition(audio: UploadFile = File(..., description="audio file"),add_pun: int = Body(1, description="add punctuation", embed=True)):suffix = audio.filename.split('.')[-1]- audio_path = f'{args.temp_dir}/{int(time.time() * 1000)}_{random.randint(100, 999)}.{suffix}'+ 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)@@ -105,6 +104,7 @@ async def api_recognition(audio: UploadFile = File(..., description="audio file"if add_pun:rec_result = inference_pipeline_punc(text_in=rec_result['text'], param_dict={'cache': list()})ret = {"results": rec_result['text'], "code": 0}+ print(ret)return retdiff --git a/funasr/runtime/python/http/start_server.sh b/funasr/runtime/python/http/start_server.shnew file mode 100644index 000000000..7eaa68200--- /dev/null+++ b/funasr/runtime/python/http/start_server.sh@@ -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 &