from dataclasses import dataclass import os from pathlib import Path from sqlalchemy.engine import URL, make_url def boolean(value): return str(value).lower() in {"1", "true", "yes", "on"} @dataclass class Settings: mode: str = "production" # 独立于真实/预览模型模式;本地真实服务通过环境配置显式关闭夜窗。 gpu_window_enabled: bool | None = None database_url: str | None = None mysql_host: str = "192.168.28.10" mysql_port: int = 3306 mysql_database: str = "cmvr-visual" mysql_user: str = "" mysql_password: str = "" workspace_root: Path = Path("/workspace") asset_root: Path | None = None job_work_root: Path | None = None state_root: Path | None = None admin_username: str = "" admin_password: str = "" admin_display_name: str = "管理员" cookie_secure: bool = True session_hours: int = 12 public_access: bool = False # Zero disables this optional per-owner unfinished interactive GPU job cap. max_interactive_gpu_jobs: int = 0 max_creation_commands_per_minute: int = 6 max_owner_asset_bytes: int = 100 * 1024**3 max_data_bytes: int = 2 * 1024**4 min_free_bytes: int = 50 * 1024**3 max_owner_jobs: int = 500 max_global_jobs: int = 2000 # 等待补队的持久候选与活动Job独立限额,默认容纳1000镜头各4个候选。 max_owner_story_candidates: int = 4000 max_global_story_candidates: int = 16000 max_owner_runs: int = 100 max_global_runs: int = 1000 max_owner_batches: int = 20 max_global_batches: int = 200 run_coordinator: bool = True poll_seconds: float = 1.0 event_heartbeat_seconds: float = 15.0 max_upload_bytes: int = 512 * 1024 * 1024 max_output_bytes: int = 2 * 1024 * 1024 * 1024 request_timeout_seconds: float = 30.0 inference_base_url: str = "http://llmuser-workspace:18190" inference_remote_files: bool = False # 真实多图通道尚未完成资源、抢占及质量认证,默认关闭。 reference_generation_enabled: bool = False reference_generation_profile: str = "qwen-image-edit-2511-v1" reference_max_images: int = 3 video_generation_profile: str = "minimax-h3-fl2va-bf16-v1" # 能力可编辑不等于语义质检已实测通过;上线验收后显式开启认证。 dialogue_quality_certified: bool = False dialogue_speech_enabled: bool = False dialogue_lipsync_enabled: bool = False dialogue_two_faces_enabled: bool = False native_dialogue_enabled: bool = False native_dialogue_tp2_enabled: bool = False continuity_quality_certified: bool = False video_edit_enabled: bool = False video_edit_profile: str = "wan-animate-replace-v1" video_edit_max_seconds: int = 120 video_edit_max_people: int = 12 video_edit_max_pixels: int = 399360 inference_api_key: str = "" inference_api_key_file: Path | None = Path("/workspace/projects/llm-services/config/api_key") gpu_status_path: Path | None = Path("/run/llm-gpu-policy/status.json") gpu_scheduler_path: Path | None = Path( "/workspace/projects/llm-services/state/gpu-scheduler.json" ) runtime_policy_file: Path | None = Path( "/workspace/projects/llm-services/config/runtime-policy.json" ) qwen_base_url: str = "http://192.168.28.10:14081/v1" qwen_model: str = "Qwen/Qwen3.8-27B-FP8" qwen_api_key: str = "" # 包含 GPU 交接和冷加载;普通模型控制接口仍使用原来的短超时。 qwen_timeout_seconds: float = 1800.0 ffmpeg_bin: str = "ffmpeg" ffprobe_bin: str = "ffprobe" tts_base_url: str = "" tts_model: str = "kokoro-82m" tts_api_key: str = "" tts_voices: tuple[str, ...] = ("zf_xiaobei", "zm_yunxi") speech_timeout_seconds: float = 330.0 speech_total_timeout_seconds: float = 360.0 def __post_init__(self): import math if ( not math.isfinite(self.speech_timeout_seconds) or not math.isfinite(self.speech_total_timeout_seconds) or self.speech_timeout_seconds <= 0 or self.speech_total_timeout_seconds < self.speech_timeout_seconds ): raise ValueError("Speech timeouts must be finite, positive and ordered") if self.mode not in {"production", "preview", "test"}: raise ValueError("CMVR_MODE must be production, preview or test") if self.gpu_window_enabled is None: self.gpu_window_enabled = self.mode == "production" elif type(self.gpu_window_enabled) is not bool: raise ValueError("GPU_WINDOW_ENABLED must be a boolean") if self.max_interactive_gpu_jobs < 0: raise ValueError("MAX_INTERACTIVE_GPU_JOBS must be non-negative; 0 disables the limit") if any( type(value) is not int or value < 0 for value in (self.max_owner_story_candidates, self.max_global_story_candidates) ): raise ValueError("Story candidate plan limits must be non-negative integers") if self.video_edit_profile != "wan-animate-replace-v1": raise ValueError("Unsupported VIDEO_EDIT_PROFILE") from app.domain.video_generation import H3_PROFILES, LEGACY_PROFILE if self.video_generation_profile not in {*H3_PROFILES, LEGACY_PROFILE}: raise ValueError("Unsupported VIDEO_GENERATION_PROFILE") if any( type(v) is not int or v < 1 for v in (self.video_edit_max_seconds, self.video_edit_max_people) ): raise ValueError("Video edit limits must be positive integers") if type(self.video_edit_max_pixels) is not int or self.video_edit_max_pixels not in { 399360, 921600, }: raise ValueError("VIDEO_EDIT_MAX_PIXELS must be a certified 480p/720p budget") profiles = { "qwen-image-edit-2511-v1", "qwen-image-edit-2509-v1", "qwen-image-edit-2511-experimental-4-v1", } if self.reference_generation_profile not in profiles: raise ValueError("Unsupported REFERENCE_GENERATION_PROFILE") maximum = 4 if "experimental-4" in self.reference_generation_profile else 3 if ( type(self.reference_max_images) is not int or not 1 <= self.reference_max_images <= maximum ): raise ValueError("REFERENCE_MAX_IMAGES exceeds the selected profile") self.workspace_root = Path(self.workspace_root).resolve() for field, leaf in ( ("asset_root", "assets"), ("job_work_root", "jobs"), ("state_root", "state"), ): value = Path( getattr(self, field) or self.workspace_root / "cmvr-visual" / leaf ).resolve() if not value.is_relative_to(self.workspace_root): raise ValueError(field + " must stay inside WORKSPACE_ROOT") setattr(self, field, value) for field in ( "inference_api_key_file", "gpu_status_path", "gpu_scheduler_path", "runtime_policy_file", ): if getattr(self, field): setattr(self, field, Path(getattr(self, field))) if self.database_url: url = make_url(self.database_url) if url.drivername.startswith("sqlite"): if self.mode not in {"preview", "test"} or url.drivername != "sqlite+aiosqlite": raise ValueError( "SQLite requires explicit test/preview mode and the aiosqlite driver" ) elif url.drivername != "mysql+aiomysql": raise ValueError("Production database must use mysql+aiomysql") elif self.mode != "production": raise ValueError("Test/preview mode requires an explicit DATABASE_URL") elif not self.mysql_user or not self.mysql_password: raise ValueError("Production MySQL credentials must be configured") @property def sqlalchemy_url(self): if self.database_url: return make_url(self.database_url) return URL.create( "mysql+aiomysql", username=self.mysql_user, password=self.mysql_password, host=self.mysql_host, port=self.mysql_port, database=self.mysql_database, query={"charset": "utf8mb4"}, ) @classmethod def from_env(cls): env = os.environ values = {} strings = ( "database_url", "mysql_host", "mysql_database", "mysql_user", "mysql_password", "admin_username", "admin_password", "admin_display_name", "inference_base_url", "inference_api_key", "reference_generation_profile", "video_edit_profile", "video_generation_profile", "qwen_base_url", "qwen_model", "qwen_api_key", "ffmpeg_bin", "ffprobe_bin", "tts_base_url", "tts_model", "tts_api_key", ) for field in strings: if field.upper() in env: values[field] = env[field.upper()] for field in ( "workspace_root", "asset_root", "job_work_root", "state_root", "inference_api_key_file", "gpu_status_path", "gpu_scheduler_path", ): if field.upper() in env: values[field] = Path(env[field.upper()]) if env[field.upper()] else None if env.get("RUNTIME_POLICY_FILE"): values["runtime_policy_file"] = Path(env["RUNTIME_POLICY_FILE"]) for field in ( "mysql_port", "session_hours", "max_upload_bytes", "max_output_bytes", "max_owner_jobs", "max_global_jobs", "max_owner_story_candidates", "max_global_story_candidates", "max_interactive_gpu_jobs", "reference_max_images", "video_edit_max_seconds", "video_edit_max_people", "video_edit_max_pixels", "max_creation_commands_per_minute", "max_owner_asset_bytes", "max_data_bytes", "min_free_bytes", "max_owner_runs", "max_global_runs", "max_owner_batches", "max_global_batches", ): if field.upper() in env: values[field] = int(env[field.upper()]) for field in ( "poll_seconds", "event_heartbeat_seconds", "request_timeout_seconds", "qwen_timeout_seconds", "speech_timeout_seconds", "speech_total_timeout_seconds", ): if field.upper() in env: values[field] = float(env[field.upper()]) for field in ( "cookie_secure", "run_coordinator", "public_access", "inference_remote_files", "reference_generation_enabled", "video_edit_enabled", "dialogue_quality_certified", "dialogue_speech_enabled", "dialogue_lipsync_enabled", "dialogue_two_faces_enabled", "native_dialogue_enabled", "native_dialogue_tp2_enabled", "continuity_quality_certified", ): if field.upper() in env: values[field] = boolean(env[field.upper()]) values["mode"] = env.get("CMVR_MODE", "production") if "GPU_WINDOW_ENABLED" in env: switch = env["GPU_WINDOW_ENABLED"].strip().lower() if switch not in {"1", "true", "yes", "on", "0", "false", "no", "off"}: raise ValueError("GPU_WINDOW_ENABLED must be a boolean") values["gpu_window_enabled"] = switch in {"1", "true", "yes", "on"} if "TTS_VOICES" in env: values["tts_voices"] = tuple( v.strip() for v in env["TTS_VOICES"].split(",") if v.strip() ) return cls(**values)