Skip to content

Commit 31450b4

Browse files
committed
web_rt: enable TLS in uvicorn and print local https URL
1 parent 751fad2 commit 31450b4

File tree

6 files changed

+102
-20
lines changed

6 files changed

+102
-20
lines changed

.github/workflows/build.yml

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,56 @@ on:
88
workflow_dispatch:
99

1010
jobs:
11-
build-x86:
12-
name: x86 Build
11+
build-ax650:
12+
name: AX650 Build
1313
runs-on: ubuntu-latest
1414
steps:
15-
- uses: actions/checkout@v3
15+
- uses: actions/checkout@v4
1616

1717
- name: Prepare 3rdparty
1818
run: git submodule update --init
1919

20-
- name: Build x86
21-
run: sh ./build.sh
20+
- name: Build AX650
21+
run: bash ./build_ax650.sh
2222

23-
- name: Upload x86 artifact
23+
- name: Upload AX650 artifact
2424
uses: actions/upload-artifact@v4
2525
with:
26-
name: build-x86
27-
path: build/install/
26+
name: build-ax650
27+
path: build_650/install/
2828

29-
build-aarch64:
30-
name: aarch64 Build
29+
build-axcl-x86:
30+
name: AXCL x86 Build
3131
runs-on: ubuntu-latest
3232
steps:
33-
- uses: actions/checkout@v3
33+
- uses: actions/checkout@v4
3434

3535
- name: Prepare 3rdparty
3636
run: git submodule update --init
3737

38-
- name: Build aarch64
39-
run: sh ./build_aarch64.sh
38+
- name: Build AXCL x86
39+
run: bash ./build_axcl_x86.sh
4040

41-
- name: Upload aarch64 artifact
41+
- name: Upload AXCL x86 artifact
4242
uses: actions/upload-artifact@v4
4343
with:
44-
name: build-aarch64
45-
path: build_aarch64/install/
44+
name: build-axcl-x86
45+
path: build_axcl_x86/install/
46+
47+
build-axcl-aarch64:
48+
name: AXCL aarch64 Build
49+
runs-on: ubuntu-latest
50+
steps:
51+
- uses: actions/checkout@v4
52+
53+
- name: Prepare 3rdparty
54+
run: git submodule update --init
55+
56+
- name: Build AXCL aarch64
57+
run: bash ./build_axcl_aarch64.sh
58+
59+
- name: Upload AXCL aarch64 artifact
60+
uses: actions/upload-artifact@v4
61+
with:
62+
name: build-axcl-aarch64
63+
path: build_axcl_aarch64/install/

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ pytranslate/__pycache__
44
__pycache__
55
pytranslate/x86_64
66
pytranslate/aarch64
7-
web_rt/certs
7+
web_rt/certs
8+
dataset

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@
77
./build_axcl_x86.sh # AXCL x86 编译
88
./build_axcl_aarch64.sh # AXCL aarch64 交叉编译
99
```
10+
编译成功之后 build目录应有以下文件
11+
```
12+
(base) axera@dell:~/libtranslate.axera/build_axcl$ tree
13+
.
14+
├── CMakeCache.txt
15+
├── libax_translate.so
16+
├── Makefile
17+
├── test_translate # 测试用例
18+
├── translate_cli # 调用服务用例
19+
└── translate_svr # 服务端程序
20+
```
1021

1122
## 模型获取
1223
[HY-MT1.5-1.8B_GPTQ_INT4](https://huggingface.co/AXERA-TECH/HY-MT1.5-1.8B_GPTQ_INT4)
@@ -36,6 +47,7 @@ TRANS_MODEL_DIR=/path/to/HY-MT1.5-1.8B_GPTQ_INT4 \
3647
PORT=8001 HOST=0.0.0.0 \
3748
./run_web_rt.sh
3849
```
50+
![web_rt.png](web_rt/image.png)
3951

4052
### Gradio
4153
```shell

run_web_rt.sh

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,30 @@ CERT_FILE="${CERT_DIR}/server.crt"
1212
KEY_FILE="${CERT_DIR}/server.key"
1313
mkdir -p "${CERT_DIR}"
1414

15+
LOCAL_IP="$(hostname -I 2>/dev/null | awk '{print $1}')"
16+
SAN="DNS:localhost,IP:127.0.0.1"
17+
if [ -n "${LOCAL_IP}" ]; then
18+
SAN="${SAN},IP:${LOCAL_IP}"
19+
fi
20+
21+
NEED_REGEN_CERT=0
1522
if [ ! -f "${CERT_FILE}" ] || [ ! -f "${KEY_FILE}" ]; then
23+
NEED_REGEN_CERT=1
24+
else
25+
CERT_SAN="$(openssl x509 -in "${CERT_FILE}" -noout -ext subjectAltName 2>/dev/null || true)"
26+
if ! echo "${CERT_SAN}" | grep -q "DNS:localhost"; then
27+
NEED_REGEN_CERT=1
28+
elif [ -n "${LOCAL_IP}" ] && ! echo "${CERT_SAN}" | grep -q "IP Address:${LOCAL_IP}"; then
29+
NEED_REGEN_CERT=1
30+
fi
31+
fi
32+
33+
if [ "${NEED_REGEN_CERT}" = "1" ]; then
1634
echo "Generating self-signed cert..."
1735
openssl req -x509 -newkey rsa:2048 -sha256 -nodes \
1836
-keyout "${KEY_FILE}" -out "${CERT_FILE}" -days 365 \
19-
-subj "/CN=localhost"
37+
-subj "/CN=localhost" \
38+
-addext "subjectAltName=${SAN}"
2039
fi
2140

2241
echo "TRANS_MODEL_DIR = ${TRANS_MODEL_DIR}"

web_rt/image.png

36.9 KB
Loading

web_rt/server.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import asyncio
44
import json
55
import os
6+
import socket
67
import sys
78
import threading
89
from dataclasses import dataclass, field
@@ -258,7 +259,38 @@ async def ws_endpoint(ws: WebSocket):
258259
if __name__ == "__main__":
259260
import uvicorn
260261

262+
def _get_local_ip() -> str:
263+
try:
264+
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock:
265+
# No packets are sent; this is used to resolve outbound interface.
266+
sock.connect(("8.8.8.8", 80))
267+
return sock.getsockname()[0]
268+
except OSError:
269+
return "127.0.0.1"
270+
261271
host = os.getenv("HOST", "0.0.0.0")
262272
port = int(os.getenv("PORT", "8001"))
263-
print(f"Web RT Translate: http://{host}:{port}")
264-
uvicorn.run("web_rt.server:app", host=host, port=port, reload=False)
273+
ssl_cert = os.getenv("SSL_CERT")
274+
ssl_key = os.getenv("SSL_KEY")
275+
use_ssl = bool(
276+
ssl_cert
277+
and ssl_key
278+
and Path(ssl_cert).is_file()
279+
and Path(ssl_key).is_file()
280+
)
281+
282+
display_host = _get_local_ip() if host in {"0.0.0.0", "::"} else host
283+
scheme = "https" if use_ssl else "http"
284+
print(f"Web RT Translate: {scheme}://{display_host}:{port}")
285+
286+
uvicorn_kwargs = {
287+
"app": "web_rt.server:app",
288+
"host": host,
289+
"port": port,
290+
"reload": False,
291+
}
292+
if use_ssl:
293+
uvicorn_kwargs["ssl_certfile"] = ssl_cert
294+
uvicorn_kwargs["ssl_keyfile"] = ssl_key
295+
296+
uvicorn.run(**uvicorn_kwargs)

0 commit comments

Comments
 (0)