| 1 | # Copyright (C) 2025 AIDC-AI |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | # Unless required by applicable law or agreed to in writing, software |
| 8 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | # See the License for the specific language governing permissions and |
| 11 | # limitations under the License. |
| 12 | |
| 13 | """ |
| 14 | Configuration schema with Pydantic models |
| 15 | |
| 16 | Single source of truth for all configuration defaults and validation. |
| 17 | """ |
| 18 | from typing import Optional |
| 19 | from pydantic import BaseModel, Field |
| 20 | |
| 21 | |
| 22 | class LLMConfig(BaseModel): |
| 23 | """LLM configuration""" |
| 24 | api_key: str = Field(default="", description="LLM API Key") |
| 25 | base_url: str = Field(default="", description="LLM API Base URL") |
| 26 | model: str = Field(default="", description="LLM Model Name") |
| 27 | |
| 28 | |
| 29 | class APIProviderCommonConfig(BaseModel): |
| 30 | """Common API provider settings""" |
| 31 | print_model_input: bool = Field(default=False, description="Print provider request parameters for debugging") |
| 32 | local_proxy: str = Field(default="", description="Local HTTP proxy for providers that need it") |
| 33 | |
| 34 | |
| 35 | class APIKeyProviderConfig(BaseModel): |
| 36 | """Provider settings with API key and optional base URL""" |
| 37 | api_key: str = Field(default="", description="Provider API Key") |
| 38 | base_url: str = Field(default="", description="Provider API Base URL") |
| 39 | use_proxy: bool = Field(default=False, description="Route provider requests through common local proxy") |
| 40 | |
| 41 | |
| 42 | class AccessSecretProviderConfig(BaseModel): |
| 43 | """Provider settings with access key / secret key credentials""" |
| 44 | base_url: str = Field(default="", description="Provider API Base URL") |
| 45 | access_key: str = Field(default="", description="Provider Access Key") |
| 46 | secret_key: str = Field(default="", description="Provider Secret Key") |
| 47 | use_proxy: bool = Field(default=False, description="Route provider requests through common local proxy") |
| 48 | |
| 49 | |
| 50 | class APIProvidersConfig(BaseModel): |
| 51 | """Direct model provider API configuration""" |
| 52 | common: APIProviderCommonConfig = Field(default_factory=APIProviderCommonConfig) |
| 53 | openai: APIKeyProviderConfig = Field(default_factory=APIKeyProviderConfig) |
| 54 | dashscope: APIKeyProviderConfig = Field(default_factory=APIKeyProviderConfig) |
| 55 | deepseek: APIKeyProviderConfig = Field(default_factory=APIKeyProviderConfig) |
| 56 | gemini: APIKeyProviderConfig = Field(default_factory=APIKeyProviderConfig) |
| 57 | ark: APIKeyProviderConfig = Field(default_factory=APIKeyProviderConfig) |
| 58 | kling: AccessSecretProviderConfig = Field(default_factory=AccessSecretProviderConfig) |
| 59 | |
| 60 | |
| 61 | class TTSLocalConfig(BaseModel): |
| 62 | """Local TTS configuration (Edge TTS)""" |
| 63 | voice: str = Field(default="zh-CN-YunjianNeural", description="Edge TTS voice ID") |
| 64 | speed: float = Field(default=1.2, ge=0.5, le=2.0, description="Speech speed multiplier (0.5-2.0)") |
| 65 | |
| 66 | |
| 67 | class TTSComfyUIConfig(BaseModel): |
| 68 | """ComfyUI TTS configuration""" |
| 69 | default_workflow: Optional[str] = Field(default=None, description="Default TTS workflow (optional)") |
| 70 | |
| 71 | |
| 72 | class TTSSubConfig(BaseModel): |
| 73 | """TTS-specific configuration (under comfyui.tts)""" |
| 74 | inference_mode: str = Field(default="local", description="TTS inference mode: 'local' or 'comfyui'") |
| 75 | local: TTSLocalConfig = Field(default_factory=TTSLocalConfig, description="Local TTS (Edge TTS) configuration") |
| 76 | comfyui: TTSComfyUIConfig = Field(default_factory=TTSComfyUIConfig, description="ComfyUI TTS configuration") |
| 77 | |
| 78 | # Backward compatibility: keep default_workflow at top level |
| 79 | @property |
| 80 | def default_workflow(self) -> Optional[str]: |
| 81 | """Get default workflow (for backward compatibility)""" |
| 82 | return self.comfyui.default_workflow |
| 83 | |
| 84 | |
| 85 | class ImageSubConfig(BaseModel): |
| 86 | """Image-specific configuration (under comfyui.image)""" |
| 87 | default_workflow: Optional[str] = Field(default=None, description="Default image workflow (optional)") |
| 88 | prompt_prefix: str = Field( |
| 89 | default="Minimalist black-and-white matchstick figure style illustration, clean lines, simple sketch style", |
| 90 | description="Prompt prefix for all image generation" |
| 91 | ) |
| 92 | |
| 93 | |
| 94 | class VideoSubConfig(BaseModel): |
| 95 | """Video-specific configuration (under comfyui.video)""" |
| 96 | default_workflow: Optional[str] = Field(default=None, description="Default video workflow (optional)") |
| 97 | prompt_prefix: str = Field( |
| 98 | default="Minimalist black-and-white matchstick figure style illustration, clean lines, simple sketch style", |
| 99 | description="Prompt prefix for all video generation" |
| 100 | ) |
| 101 | |
| 102 | |
| 103 | class ComfyUIConfig(BaseModel): |
| 104 | """ComfyUI configuration (includes global settings and service-specific configs)""" |
| 105 | comfyui_url: str = Field(default="http://127.0.0.1:8188", description="ComfyUI Server URL") |
| 106 | comfyui_api_key: Optional[str] = Field(default=None, description="ComfyUI API Key (optional)") |
| 107 | runninghub_api_key: Optional[str] = Field(default=None, description="RunningHub API Key (optional)") |
| 108 | runninghub_concurrent_limit: int = Field(default=1, ge=1, le=10, description="RunningHub concurrent execution limit (1-10)") |
| 109 | runninghub_instance_type: Optional[str] = Field(default=None, description="RunningHub instance type (optional, set to 'plus' for 48GB VRAM)") |
| 110 | tts: TTSSubConfig = Field(default_factory=TTSSubConfig, description="TTS-specific configuration") |
| 111 | image: ImageSubConfig = Field(default_factory=ImageSubConfig, description="Image-specific configuration") |
| 112 | video: VideoSubConfig = Field(default_factory=VideoSubConfig, description="Video-specific configuration") |
| 113 | |
| 114 | |
| 115 | class TemplateConfig(BaseModel): |
| 116 | """Template configuration""" |
| 117 | default_template: str = Field( |
| 118 | default="1080x1920/default.html", |
| 119 | description="Default frame template path" |
| 120 | ) |
| 121 | |
| 122 | |
| 123 | class PixelleVideoConfig(BaseModel): |
| 124 | """Pixelle-Video main configuration""" |
| 125 | project_name: str = Field(default="Pixelle-Video", description="Project name") |
| 126 | llm: LLMConfig = Field(default_factory=LLMConfig) |
| 127 | api_providers: APIProvidersConfig = Field(default_factory=APIProvidersConfig) |
| 128 | comfyui: ComfyUIConfig = Field(default_factory=ComfyUIConfig) |
| 129 | template: TemplateConfig = Field(default_factory=TemplateConfig) |
| 130 | |
| 131 | def is_llm_configured(self) -> bool: |
| 132 | """Check if LLM is properly configured""" |
| 133 | return bool( |
| 134 | self.llm.api_key and self.llm.api_key.strip() and |
| 135 | self.llm.base_url and self.llm.base_url.strip() and |
| 136 | self.llm.model and self.llm.model.strip() |
| 137 | ) |
| 138 | |
| 139 | def validate_required(self) -> bool: |
| 140 | """Validate required configuration""" |
| 141 | return self.is_llm_configured() |
| 142 | |
| 143 | def to_dict(self) -> dict: |
| 144 | """Convert to dictionary (for backward compatibility)""" |
| 145 | return self.model_dump() |
| 146 |