From 91f614934fb92765ca2d4166a416ab354769320f Mon Sep 17 00:00:00 2001
From: 夜雨飘零 <yeyupiaoling@foxmail.com>
Date: 星期四, 14 九月 2023 17:24:50 +0800
Subject: [PATCH] Support for multiple processes (#951)

---
 funasr/runtime/python/http/client.py       |    4 +
 funasr/runtime/python/http/start_server.sh |   21 ++++++++++
 funasr/runtime/python/http/asr_nginx.conf  |   44 ++++++++++++++++++++++
 funasr/runtime/python/http/README.md       |   19 +++++++++
 funasr/runtime/python/http/server.py       |    6 +-
 5 files changed, 90 insertions(+), 4 deletions(-)

diff --git a/funasr/runtime/python/http/README.md b/funasr/runtime/python/http/README.md
index 5b3fbb3..ee37f56 100644
--- a/funasr/runtime/python/http/README.md
+++ b/funasr/runtime/python/http/README.md
@@ -45,3 +45,22 @@
 --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.conf
new file mode 100644
index 0000000..769774f
--- /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 file
diff --git a/funasr/runtime/python/http/client.py b/funasr/runtime/python/http/client.py
index 272ce97..c237619 100644
--- a/funasr/runtime/python/http/client.py
+++ b/funasr/runtime/python/http/client.py
@@ -1,3 +1,5 @@
+import os
+
 import requests
 import argparse
 
@@ -32,7 +34,7 @@
 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.py
index 3fe3b55..b1d4761 100644
--- a/funasr/runtime/python/http/server.py
+++ b/funasr/runtime/python/http/server.py
@@ -1,8 +1,7 @@
 import argparse
 import logging
 import os
-import random
-import time
+import uuid
 
 import aiofiles
 import ffmpeg
@@ -92,7 +91,7 @@
 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 @@
     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 ret
 
 
diff --git a/funasr/runtime/python/http/start_server.sh b/funasr/runtime/python/http/start_server.sh
new file mode 100644
index 0000000..7eaa682
--- /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 &

--
Gitblit v1.9.1