返回 MoneyPrinterTurbo
webui.sh
根目录 / webui.sh
1 #!/usr/bin/env sh
2
3 # If you could not download the model from the official site, you can use the mirror site.
4 # Just remove the comment of the following line .
5 # 如果你无法从官方网站下载模型,你可以使用镜像网站。
6 # 只需要移除下面一行的注释即可。
7
8 # export HF_ENDPOINT=https://hf-mirror.com
9
10 CURRENT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
11 export PYTHONPATH="$CURRENT_DIR${PYTHONPATH:+:$PYTHONPATH}"
12
13 # 0.0.0.0 只能表示“监听所有网卡”,不适合作为浏览器访问地址。
14 # macOS/Linux 下浏览器打开 http://0.0.0.0:8501 可能会经过代理或网关,
15 # 最终出现 502。默认绑定并打开 127.0.0.1,与 Windows 启动脚本保持一致。
16 MPT_WEBUI_HOST="${MPT_WEBUI_HOST:-127.0.0.1}"
17 MPT_WEBUI_PORT="${MPT_WEBUI_PORT:-8501}"
18
19 if [ -x "$CURRENT_DIR/.venv/bin/python" ]; then
20 PORT_CHECK_CMD="$CURRENT_DIR/.venv/bin/python"
21 set -- "$CURRENT_DIR/.venv/bin/python" -m streamlit
22 elif command -v uv >/dev/null 2>&1; then
23 PORT_CHECK_CMD="uv run python"
24 set -- uv run streamlit
25 elif command -v streamlit >/dev/null 2>&1; then
26 echo "***** Warning: using streamlit from PATH. If dependencies fail, run 'uv sync --frozen' first. *****"
27 PORT_CHECK_CMD="python3"
28 set -- streamlit
29 else
30 echo "***** Neither project Python, uv, nor streamlit was found. Please install dependencies first. *****"
31 exit 1
32 fi
33
34 find_available_port() {
35 WEBUI_HOST="$MPT_WEBUI_HOST" WEBUI_PORT="$MPT_WEBUI_PORT" "$@" - <<'PY' 2>/dev/null
36 import os
37 import socket
38 import sys
39
40 host = os.environ.get("WEBUI_HOST", "127.0.0.1")
41 preferred = int(os.environ.get("WEBUI_PORT", "8501"))
42 candidates = [preferred] + [port for port in range(8502, 8600) if port != preferred]
43
44 for port in candidates:
45 with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
46 try:
47 sock.bind((host, port))
48 except OSError:
49 continue
50 print(port)
51 sys.exit(0)
52
53 sys.exit(1)
54 PY
55 }
56
57 # 用 Python 做端口探测,避免依赖 lsof/nc 在不同 macOS/Linux 发行版上的差异。
58 # shellcheck disable=SC2086
59 SELECTED_WEBUI_PORT=$(find_available_port $PORT_CHECK_CMD)
60
61 if [ -z "$SELECTED_WEBUI_PORT" ]; then
62 echo "***** No available WebUI port found in 8501-8599 for $MPT_WEBUI_HOST. *****"
63 exit 1
64 fi
65
66 if [ "$SELECTED_WEBUI_PORT" != "$MPT_WEBUI_PORT" ]; then
67 echo "***** Port $MPT_WEBUI_PORT is unavailable, using $SELECTED_WEBUI_PORT instead. *****"
68 fi
69
70 MPT_WEBUI_PORT="$SELECTED_WEBUI_PORT"
71
72 echo "***** WebUI address: http://$MPT_WEBUI_HOST:$MPT_WEBUI_PORT *****"
73 "$@" run "$CURRENT_DIR/webui/Main.py" \
74 --server.address="$MPT_WEBUI_HOST" \
75 --server.port="$MPT_WEBUI_PORT" \
76 --browser.serverAddress="$MPT_WEBUI_HOST" \
77 --browser.gatherUsageStats=False \
78 --server.showEmailPrompt=False \
79 --server.enableCORS=True
80
80 lines BASH