feat: add .env support, max version limits for generation, and update model configurations
| | |
| | | from typing import Any, Dict, Optional |
| | | |
| | | import yaml |
| | | from dotenv import load_dotenv |
| | | |
| | | logger = logging.getLogger(__name__) |
| | | |
| | | BASE_DIR = Path(__file__).resolve().parent |
| | | load_dotenv(BASE_DIR / ".env") |
| | | |
| | | CONFIG_PATH = BASE_DIR / "config.yaml" |
| | | CONFIG_EXAMPLE_PATH = BASE_DIR / "config.yaml.example" |
| | | |
| | |
| | | "models": { |
| | | "llm": "ark-code-latest", |
| | | "vlm": "ark-code-latest", |
| | | "image_it2i": "doubao-seedream-5-0-260128", |
| | | "image_t2i": "doubao-seedream-5-0-260128", |
| | | "video": "doubao-seedance-2-0-260128", |
| | | "video_first_frame": "doubao-seedance-2-0-260128", |
| | | "image_it2i": "ep-20260622221940-sb69g", |
| | | "image_t2i": "ep-20260622221940-sb69g", |
| | | "video": "doubao-seedance-2-0-mini-260615", |
| | | "video_first_frame": "doubao-seedance-2-0-mini-260615", |
| | | "video_start_end": "wan2.7-i2v", |
| | | "video_reference": "wan2.7-r2v", |
| | | }, |
| | |
| | | |
| | | def _coerce_config(data: Dict[str, Any]) -> Dict[str, Any]: |
| | | clean = _deep_merge(DEFAULT_CONFIG, data) |
| | | |
| | | # 允许使用环境变量(如 .env)来覆盖 API 秘钥配置 |
| | | if "api_providers" in clean: |
| | | for provider, values in clean["api_providers"].items(): |
| | | if provider == "common" or not isinstance(values, dict): |
| | | continue |
| | | for key in values.keys(): |
| | | env_key = f"{provider.upper()}_{key.upper()}" |
| | | if os.getenv(env_key): |
| | | values[key] = os.getenv(env_key) |
| | | clean.pop("llm", None) |
| | | raw_server = data.get("server", {}) if isinstance(data, dict) else {} |
| | | |
| | |
| | | models: |
| | | llm: ark-code-latest |
| | | vlm: ark-code-latest |
| | | image_it2i: doubao-seedream-5-0-260128 |
| | | image_t2i: doubao-seedream-5-0-260128 |
| | | video: doubao-seedance-2-0-260128 |
| | | video_first_frame: doubao-seedance-2-0-260128 |
| | | image_it2i: ep-20260622221940-sb69g |
| | | image_t2i: ep-20260622221940-sb69g |
| | | video: doubao-seedance-2-0-mini-260615 |
| | | video_first_frame: doubao-seedance-2-0-mini-260615 |
| | | video_start_end: wan2.7-i2v |
| | | video_reference: wan2.7-r2v |
| | | generation: |
| | |
| | | logger.info(f"ReferenceGeneratorAgent: {segment_id} 跳过(用户取消)") |
| | | return segment_id, None, None |
| | | |
| | | # 限制最大总版本数(防止无限计费) |
| | | existing_versions = self._list_versions(sid, segment_id) |
| | | if len(existing_versions) >= 10: |
| | | logger.warning(f"[{segment_id}] 历史版本已达最大限制(10个),为保护计费不再继续生成。") |
| | | eval_res = { |
| | | "score": 0, |
| | | "hard_failures": ["系统限制:为避免计费失控,单个片段最多只允许生成10个版本。"], |
| | | "issues": ["达到最大版本数限制"], |
| | | "is_acceptable": False |
| | | } |
| | | return segment_id, existing_versions[-1] if existing_versions else None, eval_res |
| | | |
| | | model = it2i_model if refs else t2i_model |
| | | logger.info(f"[{segment_id}] 使用模型: {model}, 参考图数量: {len(refs) if refs else 0}") |
| | | if refs: |
| | |
| | | "artifacts": artifacts, |
| | | } |
| | | |
| | | # 提取已经存在于 session 中的 visual_prompts |
| | | # 提取已经存在于 session 中的 visual_prompts 和 selected |
| | | session_visual_prompts = {} |
| | | selected_map = {} |
| | | ref_gen = artifacts.get("reference_generation", {}) |
| | | for scene in ref_gen.get("scenes", []): |
| | | sid_in_json = scene.get("id") |
| | | vp = scene.get("visual_prompt") |
| | | if sid_in_json and vp: |
| | | session_visual_prompts[sid_in_json] = vp |
| | | sel = scene.get("selected") |
| | | if sid_in_json: |
| | | if vp: |
| | | session_visual_prompts[sid_in_json] = vp |
| | | if sel: |
| | | selected_map[sid_in_json] = sel |
| | | |
| | | img_client = ImageClient( |
| | | dashscope_api_key=settings.DASHSCOPE_API_KEY, |
| | |
| | | segment_id = seg['segment_id'] |
| | | existing = self._list_versions(sid, segment_id) |
| | | if existing: |
| | | # 立即通知前端该片段已存在,避免 UI 一直显示 running |
| | | selected_path = selected_map.get(segment_id) or existing[-1] |
| | | self._report_progress("参考图", f"跳过已生成: {segment_id}", 5, data={ |
| | | "asset_complete": { |
| | | "type": "scenes", |
| | | "id": segment_id, |
| | | "status": "done", |
| | | "selected": selected_path, |
| | | "versions": existing, |
| | | } |
| | | }) |
| | | continue |
| | | pending_segments.append(seg) |
| | | |
| | |
| | | logger.info(f"VideoDirectorAgent: {segment_id} 跳过(用户取消)") |
| | | return segment_id, None |
| | | |
| | | # 限制最大总版本数(防止无限计费) |
| | | existing_versions = self._list_versions(sid, segment_id) |
| | | if len(existing_versions) >= 10: |
| | | logger.warning(f"[{segment_id}] 历史版本已达最大限制(10个),为保护计费不再继续生成视频。") |
| | | return segment_id, existing_versions[-1] if existing_versions else None |
| | | |
| | | reference_image_paths = reference_image_paths or [] |
| | | if video_generation_mode == "reference": |
| | | missing_refs = [path for path in reference_image_paths if not os.path.exists(path)] |
| | |
| | | |
| | | def run(): |
| | | tasks = [] |
| | | selected_map = {c.get("id"): c.get("selected") for c in video_clips if c.get("id")} |
| | | for seg_index, seg in enumerate(segments): |
| | | seg_id = seg["segment_id"] |
| | | existing = self._list_versions(sid, seg_id) |
| | | if existing: continue |
| | | if existing: |
| | | # 立即报告进度,使 UI 可以显示已跳过的视频 |
| | | selected_path = selected_map.get(seg_id) or existing[-1] |
| | | self._report_progress("视频生成", f"跳过已生成: {seg_id}", 5, data={ |
| | | "asset_complete": { |
| | | "type": "clips", |
| | | "id": seg_id, |
| | | "status": "done", |
| | | "selected": selected_path, |
| | | "versions": existing, |
| | | } |
| | | }) |
| | | continue |
| | | prompt = self._assemble_prompt(seg, style_prompt, character_art) |
| | | reference_image_paths = None |
| | | if video_generation_mode == "reference": |
| | |
| | | 'concurrency': 10, |
| | | 'price_per_1k_input_token': 0.001, |
| | | 'price_per_1k_output_token': 0.003}, |
| | | 'ark-code-latest': {'name': 'ARK Code Latest', |
| | | 'provider': 'ark', |
| | | 'type': ['llm', 'vlm'], |
| | | 'concurrency': 10, |
| | | 'price_per_1k_input_token': 0.001, |
| | | 'price_per_1k_output_token': 0.002}, |
| | | 'gemini-2.5-flash': {'name': 'Gemini 2.5 Flash', |
| | | 'provider': 'gemini', |
| | | 'type': ['llm'], |
| | |
| | | 'resolutions': ['720P', '1080P', '2K', '4K'], |
| | | 'ratios': ['16:9', '9:16', '1:1', '4:3', '3:4']}}, |
| | | 'doubao-seedream-5-0-260128': {'name': 'Seedream 5.0', |
| | | 'provider': 'ark', |
| | | 'family': 'seedream', |
| | | 'type': ['t2i', 'i2i'], |
| | | 'concurrency': 10, |
| | | 'price_per_image': 0.22, |
| | | 'capabilities': {'ability_type': 'image_generation', |
| | | 'ability_types': ['text_to_image', |
| | | 'image_to_image', |
| | | 'reference_image', |
| | | 'high_quality'], |
| | | 'adapter_ability_types': ['text_to_image', |
| | | 'image_to_image', |
| | | 'reference_image'], |
| | | 'input_modalities': ['text', 'image'], |
| | | 'adapter_input_modalities': ['text', 'image'], |
| | | 'api_contract_verified': True, |
| | | 'resolutions': ['720P', '1080P', '2K', '4K'], |
| | | 'ratios': ['16:9', '9:16', '1:1', '4:3', '3:4']}}, |
| | | 'ep-20260622221940-sb69g': {'name': 'Seedream 5.0 (EP)', |
| | | 'provider': 'ark', |
| | | 'family': 'seedream', |
| | | 'type': ['t2i', 'i2i'], |
| | |
| | | '21:9', |
| | | 'adaptive'], |
| | | 'api_contract_verified': True}}, |
| | | 'doubao-seedance-2-0-mini-260615': {'name': 'Seedance 2.0 Mini', |
| | | 'provider': 'ark', |
| | | 'family': 'seedance', |
| | | 'type': ['video'], |
| | | 'concurrency': 10, |
| | | 'price_per_second': 0.2, |
| | | 'capabilities': {'ability_type': 'image_to_video', |
| | | 'ability_types': ['text_to_video', 'image_to_video'], |
| | | 'adapter_ability_types': ['first_frame_i2v', 'native_audio'], |
| | | 'input_modalities': ['text', 'image'], |
| | | 'adapter_input_modalities': ['text', 'image'], |
| | | 'duration': {'min': 2, 'max': 12, 'integer': True, 'verified': True}, |
| | | 'resolutions': ['720p', '1080p'], |
| | | 'ratios': ['16:9', '4:3', '1:1', '3:4', '9:16', '21:9', 'adaptive'], |
| | | 'api_contract_verified': True}}, |
| | | 'wan2.7-r2v': {'name': 'Wan 2.7 R2V', |
| | | 'provider': 'dashscope', |
| | | 'family': 'wan', |
| | |
| | | logger.info("\n%s", "\n".join(lines)) |
| | | |
| | | # Determine backend provider |
| | | is_seedream = "seedream" in model.lower() |
| | | is_seedream = "seedream" in model.lower() or "ep-" in model.lower() or "ark" in model.lower() |
| | | is_sora = "sora" in model.lower() or "gpt" in model.lower() |
| | | |
| | | # Prepare save directory |
| | |
| | | prompt: str, |
| | | image_path: str, |
| | | save_path: str, |
| | | model: str = "doubao-seedance-2-0-260128", |
| | | model: str = "doubao-seedance-2-0-mini-260615", |
| | | duration: int = 5, |
| | | **kwargs |
| | | ) -> str: |
| | |
| | | gemini_base_url: Optional[str] = None, |
| | | gpt_api_key: Optional[str] = None, |
| | | gpt_base_url: Optional[str] = None, |
| | | doubao_api_key: Optional[str] = None, |
| | | doubao_base_url: Optional[str] = None, |
| | | proxy: Optional[str] = None): |
| | | """ |
| | | Unified VLM (Vision Language Model) Client |
| | |
| | | self._gemini_base_url = gemini_base_url |
| | | self._gpt_api_key = gpt_api_key |
| | | self._gpt_base_url = gpt_base_url |
| | | self._doubao_api_key = doubao_api_key |
| | | self._doubao_base_url = doubao_base_url |
| | | self._proxy = Config.provider_proxy("openai") if proxy is None else proxy |
| | | |
| | | self._dashscope_client = None |
| | | self._gemini_client = None |
| | | self._gpt_client = None |
| | | self._doubao_client = None |
| | | |
| | | @property |
| | | def dashscope_client(self): |
| | |
| | | ) |
| | | return self._gpt_client |
| | | |
| | | @property |
| | | def doubao_client(self): |
| | | if self._doubao_client is None: |
| | | self._doubao_client = GPTVLClient( |
| | | api_key=self._doubao_api_key or Config.DOUBAO_API_KEY, |
| | | base_url=self._doubao_base_url or Config.DOUBAO_BASE_URL, |
| | | proxy=self._proxy, |
| | | ) |
| | | return self._doubao_client |
| | | |
| | | def query(self, |
| | | prompt: str, |
| | | image_paths: Optional[List[str]] = None, |
| | |
| | | model_lower = model.lower() |
| | | is_gemini = "gemini" in model_lower |
| | | is_gpt = "gpt" in model_lower |
| | | is_doubao = "ep-" in model_lower or "doubao" in model_lower or "ark" in model_lower |
| | | |
| | | if is_gemini: |
| | | # 处理图片路径 |
| | |
| | | return self.gemini_client.chat(text=prompt, images=processed_images, model=model) |
| | | elif is_gpt: |
| | | return self.gpt_client.chat(text=prompt, images=image_paths or [], model=model) |
| | | elif is_doubao: |
| | | return self.doubao_client.chat(text=prompt, images=image_paths or [], model=model) |
| | | else: |
| | | # DashScope (Qwen/Kimi) - 需要将 base64 保存为临时文件 |
| | | file_urls = [] |