Spaces:
Sleeping
Sleeping
| from __future__ import annotations | |
| from pydantic import BaseModel | |
| from typing import Optional | |
| from pathlib import Path | |
| import json | |
| CONFIG_FILE = Path(__file__).parent / "config.json" | |
| class AppConfig(BaseModel): | |
| # 将 token 重命名为 key | |
| key: Optional[str] = None | |
| # 图片生成默认参数(与原项目保持一致的关键项) | |
| model: str = "nai-diffusion-3" | |
| sampler: str = "k_euler" | |
| steps: int = 28 | |
| scale: float = 5.0 | |
| cfg_rescale: float = 0.0 | |
| noise_schedule: Optional[str] = "karras" | |
| uc_preset: int = 4 | |
| quality_toggle: bool = True | |
| legacy_uc: bool = False | |
| # 通用配置 | |
| port: int = 7860 | |
| save_output: bool = True | |
| # 输出根目录(默认:项目根/output) | |
| output_dir: str = str(Path(__file__).resolve().parent.parent / "output") | |
| # 提示音配置 | |
| sound_enabled: bool = False | |
| sound_url: Optional[str] = "/ring/ring.mp3" | |
| def load_config() -> AppConfig: | |
| if CONFIG_FILE.exists(): | |
| with open(CONFIG_FILE, "r", encoding="utf-8") as f: | |
| data = json.load(f) | |
| return AppConfig(**data) | |
| cfg = AppConfig() | |
| save_config(cfg) | |
| return cfg | |
| def save_config(cfg: AppConfig) -> None: | |
| CONFIG_FILE.parent.mkdir(parents=True, exist_ok=True) | |
| with open(CONFIG_FILE, "w", encoding="utf-8") as f: | |
| json.dump(cfg.model_dump(), f, ensure_ascii=False, indent=2) |