278 lines
14 KiB
Python
278 lines
14 KiB
Python
"""平台运行时配置(环境变量 / .env 驱动)。"""
|
||
from __future__ import annotations
|
||
|
||
from functools import lru_cache
|
||
from pathlib import Path
|
||
|
||
from pydantic import Field
|
||
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||
|
||
# 仓库根:本文件位于 <root>/backend/app/config.py
|
||
PROJECT_ROOT = Path(__file__).resolve().parents[2]
|
||
|
||
|
||
def _dotenv_candidates() -> tuple[Path | str, ...]:
|
||
"""按「仓库根 → 当前目录」列出 .env 候选。
|
||
|
||
uvicorn 习惯在 backend/ 下启动,而 .env 按约定放在仓库根(和 docker-compose 同级)。
|
||
只写 env_file=".env" 时 pydantic-settings 只在当前工作目录找,找不到就**静默**
|
||
用回内置默认值,表现是「.env 明明配了 DATABASE_URL,库却建在默认串上、
|
||
上传的文件也找不到」。这里显式把两处都纳入。
|
||
|
||
顺序很关键:pydantic-settings 是**后加载的覆盖先加载的**,所以仓库根在前、
|
||
当前目录在后,实现「就近优先」——在本机临时改配置不用动仓库根那份。
|
||
"""
|
||
ordered: list[Path] = []
|
||
root_env = PROJECT_ROOT / ".env"
|
||
if root_env.is_file():
|
||
ordered.append(root_env)
|
||
# 当前目录逐级向上到仓库根,越靠近当前目录的越靠后(优先级越高)
|
||
chain = [cwd_env := Path.cwd() / ".env"]
|
||
for base in Path.cwd().parents:
|
||
if base == PROJECT_ROOT:
|
||
break
|
||
chain.append(base / ".env")
|
||
for cand in chain: # chain 是自下而上,反转后就近者最后加载
|
||
if cand.is_file() and cand not in ordered:
|
||
ordered.append(cand)
|
||
if cwd_env not in ordered and not any(c == cwd_env for c in ordered):
|
||
ordered.append(cwd_env) # 让 pydantic-settings 自己处理「文件不存在」
|
||
return tuple(ordered) or (".env",)
|
||
|
||
|
||
class Settings(BaseSettings):
|
||
model_config = SettingsConfigDict(env_file=_dotenv_candidates(), env_file_encoding="utf-8",
|
||
extra="ignore")
|
||
|
||
# ---- 基础设施 ----
|
||
app_name: str = "Nex Animation Studio"
|
||
api_prefix: str = "/api"
|
||
database_url: str = "postgresql+psycopg://nex:change_this_password@db:5432/nexanimation"
|
||
redis_url: str = "redis://redis:6379/0"
|
||
media_root: str = "./media"
|
||
media_url_prefix: str = "/media"
|
||
cors_origins: str = "*"
|
||
# 后端是否顺带托管前端构建产物(vite build 的输出)。
|
||
# off = 只提供 API(默认;开发模式前端由 Vite 起,生产模式交给 nginx)
|
||
# on = 必须托管,缺 dist 直接告警(server.sh / systemd 用这个)
|
||
# auto = 有 dist 就托管,没有就退回纯 API
|
||
# 单机部署开 on 的好处:对外只有一个端口,/api、/media、页面同源,
|
||
# 不用再配反代和 CORS,也不会出现「代理漂到另一台后端上」。
|
||
serve_frontend: str = "off"
|
||
frontend_dist_dir: str = "" # 留空 = <仓库根>/frontend/dist
|
||
log_level: str = "INFO"
|
||
|
||
# ---- 用户体系 / 认证 ----
|
||
# 关掉它(AUTH_ENABLED=false)等于回到「局域网内无登录」的裸奔模式,
|
||
# 只建议在自动化压测或纯单机试跑时临时使用。
|
||
auth_enabled: bool = True
|
||
session_ttl_hours: int = 168 # 会话有效期(滑动续期,活跃用户不会掉线)
|
||
cookie_name: str = "nex_session"
|
||
cookie_secure: bool = False # 站点走 HTTPS 后置 true
|
||
password_min_length: int = 8
|
||
# PBKDF2 迭代次数:默认 180k(约 70ms/次),自检里压到几千以免拖慢用例。
|
||
password_iterations: int = 180_000
|
||
bootstrap_username: str = "admin"
|
||
bootstrap_display_name: str = "平台管理员"
|
||
bootstrap_password: str = "admin@123" # 仅首次播种时使用,登录后强制改密
|
||
|
||
# ---- 任务执行后端 ----
|
||
# celery = 生产模式(Redis broker + worker)
|
||
# inline = 本地模式(进程内线程池,无需 Redis,便于开发/自检)
|
||
task_executor: str = "celery"
|
||
celery_broker_url: str = "" # 为空则复用 redis_url
|
||
celery_result_backend: str = ""
|
||
inline_workers: int = 2
|
||
celery_concurrency: int = 2
|
||
|
||
# ---- 调度中心(限流 / 重试 / 断点续跑)----
|
||
dispatch_interval_seconds: int = 5 # 调度器扫描间隔
|
||
dispatch_batch_size: int = 32 # 单次调度最多派发任务数
|
||
max_shot_retries: int = 3 # 单镜头最大重试次数
|
||
max_compose_retries: int = 2
|
||
retry_backoff_base_seconds: int = 5
|
||
retry_backoff_max_seconds: int = 300
|
||
stale_running_seconds: int = 900 # running 超过该时长视为中断(断点续跑)
|
||
default_provider_concurrency: int = 2
|
||
default_provider_rpm: int = 30
|
||
|
||
# ---- 模型相关字段:只保留为**代码级兜底**,.env / .env.example 里不再出现 ----
|
||
# 唯一事实源是数据库 model_specs 表(页面:模型管理中心;服务器首启由
|
||
# scripts/seed/ 数据包导入)。下面这些字段只剩两个用处:
|
||
# ① 空库 / 无 db 时 resolve_* 造一条伪 spec,保证离线自检能跑完;
|
||
# ② seed_specs 首次登记 mock/comfyui 行时的初始值(已存在的行不会被覆盖)。
|
||
# 想临时改模型端点:改库(页面/接口)、改数据包,或用 COMFYUI_BASE_URL 这个逃生门
|
||
# 让首启自动登记;别把它当常规配置项写回 .env——两处都能配,必然出现「改了没生效」。
|
||
# ---- 视频模型 ----
|
||
video_provider: str = "mock"
|
||
video_model: str = "mock-video-1"
|
||
video_api_base_url: str = ""
|
||
video_api_key: str = ""
|
||
video_poll_interval_seconds: int = 5
|
||
video_poll_timeout_seconds: int = 1800
|
||
|
||
# ---- ComfyUI(本地大模型,如 MiniMax H3)----
|
||
comfyui_base_url: str = "" # 例:http://10.100.53.199:9200
|
||
comfyui_workflow: str = "minimax_h3" # providers/comfyui.py 的内置图模板
|
||
comfyui_model: str = "minimax-h3"
|
||
comfyui_steps: int = 20
|
||
# 单镜头画布的像素预算(megapixels)。这是本地视频模型最大的耗时变量:
|
||
# DGX Spark(GB10) 实测 0.98MP(1312x736) 单镜头 ≈208s,0.4MP(832x480) ≈91s。
|
||
# 这里**只是缺键兜底**(可用环境变量 COMFYUI_MEGAPIXELS 覆盖):真正的口径写在
|
||
# model_specs.config_json 里,由控制台/其它进程按机型与任务维护,适配器无权改写。
|
||
comfyui_megapixels: float = 0.98
|
||
# 画布与项目档位的关系(providers/comfyui.py: canvas)。默认 **config:完全照配置,
|
||
# 一个字都不改** —— 分辨率是「任务 + 机型」参数,别的进程会直接改 model_specs,
|
||
# 代码里抬档/压档都会静默覆盖它的决定(历史上既把过低的预算顶高过,
|
||
# 也把外部有意调高的高分档顶回去过),所以钳制只做显式 opt-in:
|
||
# config —— 默认:按 megapixels 换算,档位差异交给预检提示;
|
||
# tier —— 档位作**下限**:720p 项目不会出 480p 糊片,代价是顶掉更小的预算;
|
||
# budget —— 档位作**上限**:480p 交付不按 720p 烧卡,放大交给合成端。
|
||
comfyui_resolution_policy: str = "config"
|
||
comfyui_max_edge: int = 1344 # H3 原生画布长边上限
|
||
# 任务 LoRA:H3 只有一个基座权重(ref2va_pruned),t2v/fl2v 与 ref2v 是两种任务,
|
||
# 官方按任务发 turbo LoRA。挂错任务 LoRA 不会报错,只会静默掉质量,所以拆开配置。
|
||
comfyui_lora_ref2v: str = "" # 配 MiniMaxH3ReferenceToVideo(参考图)
|
||
comfyui_lora_i2v: str = "" # 配 MiniMaxH3ImageToVideo(首帧/首尾帧)
|
||
comfyui_timeout_seconds: int = 5400 # 本地大模型单镜头出图上限(含排队)
|
||
comfyui_concurrency: int = 1 # 单卡串行,避免显存争抢
|
||
comfyui_rpm: int = 6
|
||
# ---- ComfyUI 断连容忍(providers/comfy_common.py: OutageTracker)----
|
||
# 出图要十几分钟,中途 ComfyUI 重启一次是常态。以前轮询一次 ConnectError
|
||
# 就把已经烧完的卡时判死;现在按「连续失败次数」和「累计中断时长」双阈值容忍,
|
||
# 任一超预算才失败(错误码 provider_unreachable,可重试)。
|
||
# 9200 实例装载 35GB 权重重启约 1~2 分钟,默认给到 6 次 / 180s 刚好覆盖一次重启。
|
||
provider_outage_tolerance: int = 6
|
||
provider_outage_seconds: int = 180
|
||
|
||
# ---- 语音(TTS)----(同上方模型段:库里的 tts 模型行说了算)
|
||
tts_provider: str = "mock"
|
||
tts_api_base_url: str = ""
|
||
tts_api_key: str = ""
|
||
tts_voice: str = "demo-voice"
|
||
|
||
# ---- 合成引擎 ----
|
||
default_fps: int = 24
|
||
default_width: int = 1920
|
||
default_height: int = 1080
|
||
target_lufs: float = -16.0
|
||
xfade_max_duration_ms: int = 1200
|
||
ffmpeg_threads: int = 0
|
||
ffmpeg_preset: str = "medium"
|
||
ffmpeg_crf: int = 20
|
||
# 留空 = 自动探测(优先选带 libass 的完整版构建,能烧字幕);
|
||
# 需要强制指定时填绝对路径,例如 FFMPEG_BIN=/opt/homebrew/opt/ffmpeg-full/bin/ffmpeg
|
||
ffmpeg_bin: str = ""
|
||
ffprobe_bin: str = ""
|
||
|
||
# ---- 质检 ----
|
||
qc_duration_tolerance: float = 0.35 # 时长偏差容忍比例
|
||
qc_min_bitrate_kbps: int = 200
|
||
qc_black_threshold: float = 0.98
|
||
# ---- 画面质检(多模态大模型看片)----
|
||
# 技术指标只能证明「文件是好的」,证明不了「画面是对的」:人物分身、鬼影、
|
||
# 服装串人这类一眼可见的穿帮以前全部 100 分放行,所以补一路看图复核。
|
||
qc_visual_enabled: bool = True # 关掉即退回纯技术质检(分值与历史一致)
|
||
qc_visual_frames: int = 4 # 每镜抽几帧给模型看
|
||
qc_visual_width: int = 640 # 送模型的帧宽(像素)
|
||
qc_visual_references: int = 3 # 附带几张定妆照供认人(0=不带)
|
||
qc_visual_weight: float = 0.4 # 视觉分在总分里的权重
|
||
qc_visual_timeout: int = 180 # 单镜「看图」请求等待上限(秒)
|
||
qc_visual_frame_timeout: int = 90 # 单帧抽取等待上限(秒)
|
||
# ---- 台词听写质检(whisper.cpp 本地转写,不花钱不联网)----
|
||
# 画面质检看帧,回答不了「镜头开头那两句乱语念的是什么」——那是声音的问题,只能听。
|
||
qc_asr_enabled: bool = True
|
||
asr_bin: str = "" # 留空 = 自动探测 whisper-cli / whisper
|
||
asr_model: str = "models/asr/ggml-small.bin" # 相对仓库根;填绝对路径亦可
|
||
asr_timeout: int = 240 # 单镜转写等待上限(秒)
|
||
|
||
# ---- 成本 ----
|
||
# 币种留在 env(全站一个口径,跟具体模型无关);单价是**每个模型自己的**属性,
|
||
# 存在 model_specs.price_per_second / price_per_1k_chars,在页面上改。
|
||
# 下面两个默认值只在新建模型行没填单价时用一次。
|
||
cost_currency: str = "CNY"
|
||
default_video_price_per_second: float = 0.0
|
||
default_tts_price_per_1k_chars: float = 0.0
|
||
|
||
upload_image_extensions: str = ".png,.jpg,.jpeg,.webp,.gif,.bmp,.tif,.tiff"
|
||
upload_video_extensions: str = ".mp4,.mov,.webm,.mkv,.m4v,.avi"
|
||
upload_audio_extensions: str = ".mp3,.wav,.m4a,.aac,.flac,.ogg,.opus"
|
||
upload_doc_extensions: str = ".txt,.md,.csv,.tsv,.json,.srt,.ass,.vtt,.zip,.docx"
|
||
|
||
def media_path(self, *parts: str) -> Path:
|
||
return Path(self.media_root).joinpath(*parts)
|
||
|
||
def serve_frontend_enabled(self) -> bool:
|
||
"""配置上是否要求托管前端(与目录在不在无关,中间件只看这个)。"""
|
||
return (self.serve_frontend or "off").strip().lower() not in ("", "off", "0", "false", "no")
|
||
|
||
def frontend_dist(self) -> Path | None:
|
||
"""解析 SERVE_FRONTEND,返回可托管的构建目录;不托管时返回 None。
|
||
|
||
判定放在配置层,不放 main.py:鉴权中间件(api/auth.py)也要问同一个问题
|
||
「这个 GET 是不是页面壳」,而 api 是被 main.py 反向导入的,
|
||
从 api 里 import app.main 会撞循环导入(模块尚未初始化完)。
|
||
"""
|
||
if not self.serve_frontend_enabled():
|
||
return None
|
||
dist = Path(self.frontend_dist_dir) if self.frontend_dist_dir else (
|
||
PROJECT_ROOT / "frontend" / "dist")
|
||
if not dist.is_absolute():
|
||
dist = (PROJECT_ROOT / dist).resolve()
|
||
return dist if (dist / "index.html").is_file() else None
|
||
|
||
@property
|
||
def broker_url(self) -> str:
|
||
return self.celery_broker_url or self.redis_url
|
||
|
||
@property
|
||
def result_backend(self) -> str:
|
||
return self.celery_result_backend or self.redis_url
|
||
|
||
@property
|
||
def cors_origin_list(self) -> list[str]:
|
||
return [o.strip() for o in self.cors_origins.split(",") if o.strip()]
|
||
|
||
def allowed_extensions(self, group: str) -> tuple[str, ...]:
|
||
raw = {
|
||
"image": self.upload_image_extensions,
|
||
"video": self.upload_video_extensions,
|
||
"audio": self.upload_audio_extensions,
|
||
"doc": self.upload_doc_extensions,
|
||
}.get(group, self.upload_doc_extensions)
|
||
return tuple(e.strip().lower() for e in raw.split(",") if e.strip())
|
||
|
||
|
||
@lru_cache
|
||
def get_settings() -> Settings:
|
||
s = Settings()
|
||
# 相对 media 路径锚定到仓库根:否则「在 backend/ 下启动」和「在根目录启动」
|
||
# 会各自写出一个 media 目录,之前上传的关键帧/已生成的镜头在新进程里全部变成 404。
|
||
media = Path(s.media_root)
|
||
if not media.is_absolute():
|
||
s.media_root = str((PROJECT_ROOT / media).resolve())
|
||
Path(s.media_root).mkdir(parents=True, exist_ok=True)
|
||
s.database_url = _anchor_sqlite(s.database_url)
|
||
return s
|
||
|
||
|
||
def _anchor_sqlite(url: str) -> str:
|
||
"""把相对路径的 SQLite URL 同样锚定到仓库根(理由与 media_root 一致)。
|
||
|
||
只处理「有文件名的相对路径」:内存库与绝对路径原样返回,
|
||
测试用的 sqlite:////tmp/... 也不会被动过。
|
||
"""
|
||
if not url.startswith("sqlite"):
|
||
return url
|
||
marker = "sqlite:///"
|
||
if not url.startswith(marker) or ":memory:" in url:
|
||
return url
|
||
tail = url[len(marker):] # 相对路径形如 ./nex.db 或 nex.db
|
||
if not tail or tail.startswith("/"): # sqlite:////tmp/x.db 是绝对路径
|
||
return url
|
||
resolved = (PROJECT_ROOT / tail).resolve()
|
||
return marker + str(resolved)
|
||
|
||
|
||
settings = get_settings()
|