Доработка управления моделями

This commit is contained in:
Shaman
2026-10-05 02:19:46 +03:00
parent f5ec608fc2
commit 0c5c0b423b
16 changed files with 629 additions and 252 deletions
+32 -32
View File
@@ -1,30 +1,20 @@
"""Environment settings and the parameter -> workflow node mapping."""
"""Environment settings and the model registry loaded from a JSON file."""
import json
from pathlib import Path
from pydantic import BaseModel, ValidationError
from pydantic_settings import BaseSettings, SettingsConfigDict
from .errors import ConfigError
REPO_ROOT = Path(__file__).resolve().parent.parent
CREATE_MODEL = "z-image-turbo"
EDIT_MODEL = "qwen-image-edit"
AVAILABLE_MODELS = [CREATE_MODEL, EDIT_MODEL]
# Node ids refer to workflow_create.json / workflow_edit.json.
# Override the whole map via MAPPING_FILE (JSON with "create" and "edit" keys).
DEFAULT_MAPPING: dict[str, dict[str, dict[str, str]]] = {
"create": {
"prompt": {"node": "83:27", "field": "text"},
"size": {"node": "83:13", "width_field": "width", "height_field": "height"},
"seed": {"node": "83:3", "field": "seed"},
},
"edit": {
"prompt": {"node": "115:111", "field": "prompt"},
"negative": {"node": "115:110", "field": "prompt"},
"seed": {"node": "115:3", "field": "seed"},
"image": {"node": "78", "field": "image"},
},
}
class ModelEntry(BaseModel):
"""A registry record: an API model id maps to one workflow file."""
workflow: Path
owned_by: str = "comfy-adapter"
class Settings(BaseSettings):
@@ -34,21 +24,31 @@ class Settings(BaseSettings):
adapter_api_key: str = ""
execution_timeout: float = 300.0
poll_interval: float = 1.5
create_template_path: Path = REPO_ROOT / "workflow_create.json"
edit_template_path: Path = REPO_ROOT / "workflow_edit.json"
mapping_file: Path | None = None
# Validate graphs on the ComfyUI server (POST /prompt?front=false) at startup.
# JSON file mapping an API model id to its workflow. Required; no defaults in code.
models_file: Path = REPO_ROOT / "models.json"
# Validate collected graphs on the ComfyUI server (POST /prompt?front=false) at startup.
validate_templates_on_server: bool = True
def mapping(self) -> dict[str, dict[str, dict[str, str]]]:
if self.mapping_file is None:
return DEFAULT_MAPPING
with open(self.mapping_file, encoding="utf-8") as f:
loaded = json.load(f)
merged = {k: dict(v) for k, v in DEFAULT_MAPPING.items()}
for section, section_map in loaded.items():
merged.setdefault(section, {}).update(section_map)
return merged
def load_registry(self) -> dict[str, ModelEntry]:
"""Load the model registry, preserving declaration order (first = preferred).
Fail-fast: a missing or malformed file raises ConfigError and blocks startup.
"""
try:
raw = json.loads(self.models_file.read_text(encoding="utf-8"))
except FileNotFoundError as e:
raise ConfigError(f"model registry file not found: {self.models_file}") from e
except json.JSONDecodeError as e:
raise ConfigError(f"model registry is not valid JSON ({self.models_file}): {e}") from e
if not isinstance(raw, dict) or not raw:
raise ConfigError(f"model registry must be a non-empty object of model entries: {self.models_file}")
registry: dict[str, ModelEntry] = {}
for model_id, spec in raw.items():
try:
registry[model_id] = ModelEntry.model_validate(spec)
except ValidationError as e:
raise ConfigError(f"model '{model_id}' has an invalid entry: {e}") from e
return registry
def get_settings() -> Settings: