From aea39a00aed394f1a4855c824aebaab81bb24185 Mon Sep 17 00:00:00 2001 From: dekun Date: Fri, 12 Jun 2026 14:53:47 +0800 Subject: [PATCH] Support .env for server-local Ollama config to avoid git pull conflicts. Co-authored-by: Cursor --- .env.example | 9 +++++ .gitignore | 93 ++++++++++++++++++++++++++-------------------------- DEPLOY.md | 14 ++++++++ config.py | 41 +++++++++++++++++++++-- 4 files changed, 108 insertions(+), 49 deletions(-) create mode 100644 .env.example diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..33cbe2b --- /dev/null +++ b/.env.example @@ -0,0 +1,9 @@ +# Trading Studio 服务器本地配置(复制为 .env 后修改,不会被 git 跟踪) +# cp .env.example .env + +# Ollama 局域网地址(按实际环境修改) +OLLAMA_HOST=192.168.8.64 +OLLAMA_PORT=11434 + +# 可选:覆盖默认模型名 +# MODEL_NAME=huihui_ai/gemma-4-abliterated:e4b diff --git a/.gitignore b/.gitignore index f71b090..c0286a4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,46 +1,47 @@ -# Python 虚拟环境 -venv/ -.venv/ -env/ - -# 模型权重与音色文件(体积大,不入库) -*.pt -*.pth -*.onnx -*.bin -*.safetensors - -# 音频产物 -*.wav -*.mp3 -*.flac -*.ogg -*.m4a - -# 日志 -*.log - -# 运行时目录 -uploads/ -outputs/ -__pycache__/ -*.py[cod] -*$py.class -.Python - -# IDE -.idea/ -.vscode/ -*.swp -*.swo - -# 系统文件 -.DS_Store -Thumbs.db - -# 环境变量与密钥 -.env -.env.* - -# Gradio 临时 -gradio_cached_examples/ +# Python 虚拟环境 +venv/ +.venv/ +env/ + +# 模型权重与音色文件(体积大,不入库) +*.pt +*.pth +*.onnx +*.bin +*.safetensors + +# 音频产物 +*.wav +*.mp3 +*.flac +*.ogg +*.m4a + +# 日志 +*.log + +# 运行时目录 +uploads/ +outputs/ +__pycache__/ +*.py[cod] +*$py.class +.Python + +# IDE +.idea/ +.vscode/ +*.swp +*.swo + +# 系统文件 +.DS_Store +Thumbs.db + +# 环境变量与密钥 +.env +.env.* +!.env.example + +# Gradio 临时 +gradio_cached_examples/ diff --git a/DEPLOY.md b/DEPLOY.md index dde6c03..39bdeaa 100644 --- a/DEPLOY.md +++ b/DEPLOY.md @@ -41,6 +41,20 @@ > **Git 认证:** 若 `git clone` 需要登录,请先在 root 下配置 HTTPS 凭据或 SSH 密钥,再执行部署脚本。 +> **服务器本地配置(Ollama IP 等):** 不要直接改 `config.py`,请使用 `.env` 文件: +> ```bash +> cp .env.example .env +> nano .env # 修改 OLLAMA_HOST=你的局域网IP +> pm2 restart trading_studio +> ``` + +> **git pull 报本地修改冲突?** 执行: +> ```bash +> git stash push -m "backup" -- config.py llm_service.py +> git pull +> pm2 restart trading_studio +> ``` + ### 0.2 首次一键部署 ```bash diff --git a/config.py b/config.py index 106bcc8..7e19fac 100644 --- a/config.py +++ b/config.py @@ -1,20 +1,55 @@ """ Trading Studio 全局配置模块 统一存放局域网节点、模型名称、固定 Prompt 及本地路径。 + +服务器本地覆盖:复制 .env.example 为 .env 并修改(不入 Git),无需改 config.py。 """ +import os from pathlib import Path + +def _load_dotenv() -> None: + """加载项目根目录 .env(简单 KEY=VALUE 格式,无第三方依赖)。""" + env_path = Path(__file__).resolve().parent / ".env" + if not env_path.is_file(): + return + for raw in env_path.read_text(encoding="utf-8").splitlines(): + line = raw.strip() + if not line or line.startswith("#") or "=" not in line: + continue + key, val = line.split("=", 1) + key = key.strip() + val = val.strip().strip('"').strip("'") + if key and key not in os.environ: + os.environ[key] = val + + +_load_dotenv() + + +def _env_str(key: str, default: str) -> str: + return os.environ.get(key, default).strip() + + +def _env_int(key: str, default: int) -> int: + try: + return int(os.environ.get(key, str(default)).strip()) + except ValueError: + return default + + # --------------------------------------------------------------------------- # 网络与服务 # --------------------------------------------------------------------------- # 远程 Ollama 节点(局域网大模型审查润色) -OLLAMA_HOST = "192.168.8.64" -OLLAMA_PORT = 11434 +# 生产环境请在 .env 中设置 OLLAMA_HOST,避免 git pull 冲突 +OLLAMA_HOST = _env_str("OLLAMA_HOST", "192.168.8.64") +OLLAMA_PORT = _env_int("OLLAMA_PORT", 11434) OLLAMA_URL = f"http://{OLLAMA_HOST}:{OLLAMA_PORT}/api/chat" # 指定无限制版 Gemma4 模型 -MODEL_NAME = "huihui_ai/gemma-4-abliterated:e4b" +MODEL_NAME = _env_str("MODEL_NAME", "huihui_ai/gemma-4-abliterated:e4b") # Gradio 中控固定端口(硬性死规则) HOST = "0.0.0.0"