| | |
| | | # 替换场景信息 |
| | | return template.format(name=name, desc=desc, style=style) |
| | | |
| | | @staticmethod |
| | | def _apply_eval_feedback_to_prompt(base_prompt: str, eval_result: dict, iteration: int) -> str: |
| | | suggested_prompt = (eval_result.get('suggested_prompt') or '').strip() |
| | | if suggested_prompt: |
| | | return suggested_prompt |
| | | |
| | | hard_failures = eval_result.get('hard_failures') or [] |
| | | soft_issues = eval_result.get('soft_issues') or [] |
| | | issues = eval_result.get('issues') or [] |
| | | suggestion = (eval_result.get('suggestion') or '').strip() |
| | | |
| | | feedback_lines = [] |
| | | if hard_failures: |
| | | feedback_lines.append("硬性失败项:" + ";".join(map(str, hard_failures))) |
| | | if issues: |
| | | feedback_lines.append("主要问题:" + ";".join(map(str, issues))) |
| | | if soft_issues: |
| | | feedback_lines.append("软性问题:" + ";".join(map(str, soft_issues))) |
| | | if suggestion: |
| | | feedback_lines.append("修改建议:" + suggestion) |
| | | if not feedback_lines: |
| | | return base_prompt |
| | | |
| | | return ( |
| | | f"{base_prompt}\n\n" |
| | | f"【第{iteration + 1}轮VLM评估反馈】\n" |
| | | f"上一轮生成未通过评估,请在下一轮生成时优先修正以下问题,同时保持原始角色/场景设定不变:\n" |
| | | + "\n".join(f"- {line}" for line in feedback_lines) |
| | | ) |
| | | |
| | | # ─── 文件管理(基于唯一ID) ─── |
| | | |
| | | @staticmethod |
| | |
| | | 文件命名: {asset_id}.png, {asset_id}_v2.png, {asset_id}_v3.png, ... |
| | | """ |
| | | adir = os.path.join(self._asset_base(sid), asset_type) |
| | | pattern = os.path.join(adir, f"{asset_id}*.png") |
| | | files = sorted(glob.glob(pattern), key=os.path.getmtime) |
| | | files = [] |
| | | for ext in ("png", "jpg", "jpeg", "webp", "bmp"): |
| | | pattern = os.path.join(adir, f"{asset_id}*.{ext}") |
| | | files.extend(glob.glob(pattern)) |
| | | files = sorted(set(files), key=os.path.getmtime) |
| | | return files |
| | | |
| | | def _next_version_path(self, sid: str, asset_type: str, asset_id: str) -> str: |
| | |
| | | versions = self._list_versions(sid, asset_type, asset_id) |
| | | if not selected_path and versions: |
| | | selected_path = versions[-1] |
| | | status = "done" if selected_path or versions else "failed" |
| | | return { |
| | | "id": asset_id, |
| | | "name": name, |
| | | "description": desc, |
| | | "selected": selected_path, |
| | | "versions": versions, |
| | | "status": status, |
| | | } |
| | | |
| | | # ─── 图片生成 ─── |
| | |
| | | # VLM 评估 |
| | | eval_result = self._evaluate_with_vlm(save_path, desc, asset_type, vlm_model) |
| | | |
| | | # 使用固定阈值判断是否接受(8分及以上通过) |
| | | score = eval_result.get('score', 0) |
| | | issues = eval_result.get('issues', []) |
| | | suggestion = eval_result.get('suggestion', '') |
| | | is_acceptable = score >= 8 |
| | | hard_failures = eval_result.get('hard_failures') or [] |
| | | if 'is_acceptable' in eval_result: |
| | | is_acceptable = bool(eval_result.get('is_acceptable')) and not hard_failures |
| | | else: |
| | | is_acceptable = not hard_failures and score >= 7 |
| | | |
| | | if is_acceptable: |
| | | logger.info(f"[{asset_type}] {name} ✓ VLM评估通过 - 评分: {score}/10") |
| | | else: |
| | | logger.warning(f"[{asset_type}] {name} ✗ VLM评估不通过 - 评分: {score}/10") |
| | | logger.warning(f"[{asset_type}] 问题: {issues}") |
| | | if hard_failures: |
| | | logger.warning(f"[{asset_type}] 硬性失败项: {hard_failures}") |
| | | if suggestion: |
| | | logger.warning(f"[{asset_type}] 建议: {suggestion}") |
| | | |
| | |
| | | return asset_id, save_path, eval_result |
| | | else: |
| | | # 评估不通过,记录问题并继续循环 |
| | | current_prompt = self._apply_eval_feedback_to_prompt(base_prompt, eval_result, iteration) |
| | | logger.info(f"[{asset_type}] {name} 下一轮将使用VLM反馈优化提示词") |
| | | # 报告进度 |
| | | self._report_progress("角色设计", f"重新生成中 ({iteration + 2}/{max_iterations}): {name}", 0) |
| | | |
| | |
| | | |
| | | # ─── 从剧本JSON读取角色/场景数据 ─── |
| | | |
| | | @staticmethod |
| | | def _read_script_data(sid: str) -> dict: |
| | | """从 code/data/sessions/{sid}.json 提取剧本生成的角色和场景数据""" |
| | | # Session 文件路径 |
| | | session_path = os.path.join('code/data/sessions', f'{sid}.json') |
| | | |
| | | if not os.path.exists(session_path): |
| | | logger.warning(f"Session file not found: {session_path}") |
| | | return {"characters": {}, "settings": {}} |
| | | def _read_script_data(self, input_data: Dict) -> dict: |
| | | """从编排器注入的 script_generation artifact 提取角色和场景数据。""" |
| | | script_gen = self._session_artifact(input_data, "script_generation") |
| | | chars = {} |
| | | for c in script_gen.get("characters", []): |
| | | cid = c.get("character_id") or c.get("id") or "" |
| | | if cid: |
| | | chars[cid] = { |
| | | "name": c.get("name", ""), |
| | | "description": c.get("description", ""), |
| | | "species": c.get("species", ""), |
| | | } |
| | | |
| | | try: |
| | | with open(session_path, 'r', encoding='utf-8') as f: |
| | | data = json.load(f) |
| | | |
| | | # 从 artifacts.script_generation 中提取数据 |
| | | script_gen = data.get("artifacts", {}).get("script_generation", {}) |
| | | |
| | | chars = {} |
| | | for c in script_gen.get("characters", []): |
| | | cid = c.get("character_id", "") |
| | | if cid: |
| | | chars[cid] = { |
| | | "name": c.get("name", ""), |
| | | "description": c.get("description", ""), |
| | | "species": c.get("species", ""), |
| | | } |
| | | |
| | | sets = {} |
| | | for s in script_gen.get("settings", []): |
| | | sid_val = s.get("setting_id", "") |
| | | if sid_val: |
| | | sets[sid_val] = { |
| | | "name": s.get("name", ""), |
| | | "description": s.get("description", ""), |
| | | } |
| | | |
| | | return {"characters": chars, "settings": sets} |
| | | except Exception as e: |
| | | logger.error(f"Error reading session data for {sid}: {e}") |
| | | return {"characters": {}, "settings": {}} |
| | | sets = {} |
| | | for s in script_gen.get("settings", []): |
| | | sid_val = s.get("setting_id") or s.get("id") or "" |
| | | if sid_val: |
| | | sets[sid_val] = { |
| | | "name": s.get("name", ""), |
| | | "description": s.get("description", ""), |
| | | } |
| | | |
| | | return {"characters": chars, "settings": sets} |
| | | |
| | | # ─── 核心流程 ─── |
| | | |
| | |
| | | |
| | | # 如果 input_data 为空(比如后端重启后的第一次介入),再读原始文件 |
| | | if not chars_desc and not sets_desc: |
| | | script_data = self._read_script_data(sid) |
| | | script_data = self._read_script_data(input_data) |
| | | chars_desc = script_data["characters"] |
| | | sets_desc = script_data["settings"] |
| | | |
| | |
| | | # 简单字符串格式:直接更新 description |
| | | sets_desc[asset_id]["description"] = str(info) |
| | | |
| | | # 写回会话数据文件 |
| | | session_file = os.path.join('code/data/sessions', f'{sid}.json') |
| | | if os.path.exists(session_file): |
| | | with open(session_file, 'r', encoding='utf-8') as f: |
| | | session_data = json.load(f) |
| | | |
| | | # 更新 artifacts.script_generation 中的描述 |
| | | script_gen = session_data.get("artifacts", {}).get("script_generation", {}) |
| | | if script_gen: |
| | | # 更新角色 |
| | | for c in script_gen.get("characters", []): |
| | | cid = c.get("character_id") |
| | | if cid in updated_chars: |
| | | info = updated_chars[cid] |
| | | if isinstance(info, dict): |
| | | if "name" in info: c["name"] = info["name"] |
| | | if "description" in info: c["description"] = info["description"] |
| | | if "species" in info: c["species"] = info["species"] |
| | | else: |
| | | c["description"] = str(info) |
| | | |
| | | # 更新场景 |
| | | for s in script_gen.get("settings", []): |
| | | sid_val = s.get("setting_id") |
| | | if sid_val in updated_sets: |
| | | info = updated_sets[sid_val] |
| | | if isinstance(info, dict): |
| | | if "name" in info: s["name"] = info["name"] |
| | | if "description" in info: s["description"] = info["description"] |
| | | else: |
| | | s["description"] = str(info) |
| | | |
| | | with open(session_file, 'w', encoding='utf-8') as f: |
| | | json.dump(session_data, f, ensure_ascii=False, indent=2) |
| | | |
| | | logger.info(f"[CharacterAgent] Updated descriptions for session {sid}") |
| | | |
| | | if regen_chars or regen_sets: |
| | |
| | | with ThreadPoolExecutor(max_workers=concurrency) as executor: |
| | | futs = {} |
| | | for atype, aid, name, desc, species in tasks: |
| | | existing_versions = self._list_versions(sid, atype, aid) |
| | | self._report_progress("角色设计", f"正在生成: {name}", 10, data={ |
| | | "asset_complete": { |
| | | "type": atype, |
| | | "id": aid, |
| | | "status": "running", |
| | | "versions": existing_versions, |
| | | } |
| | | }) |
| | | fut = executor.submit( |
| | | self._generate_one, img_client, |
| | | aid, name, desc, atype, style, species, t2i_model, vlm_model, sid |
| | |
| | | sets_desc = input_data["settings"] |
| | | |
| | | if not chars_desc and not sets_desc: |
| | | script_data = self._read_script_data(sid) |
| | | script_data = self._read_script_data(input_data) |
| | | chars_desc = script_data["characters"] |
| | | sets_desc = script_data["settings"] |
| | | |
| | |
| | | with ThreadPoolExecutor(max_workers=concurrency) as executor: |
| | | futs = {} |
| | | for atype, aid, name, desc, species in all_tasks: |
| | | existing_versions = self._list_versions(sid, atype, aid) |
| | | self._report_progress("角色设计", f"正在生成: {name}", 10, data={ |
| | | "asset_complete": { |
| | | "type": atype, |
| | | "id": aid, |
| | | "status": "running", |
| | | "versions": existing_versions, |
| | | } |
| | | }) |
| | | fut = executor.submit( |
| | | self._generate_one, img_client, |
| | | aid, name, desc, atype, style, species, t2i_model, vlm_model, sid, |