PRINTFILM: AI short-video marketing and AI short-drama creation platform
fix(drama): compact Seedream retry when InputTextSensitive persists
yi7649d402b56b
diff --git a/backend/app/services/ark.py b/backend/app/services/ark.pyindex 88a296d..0554ce1 100644--- a/backend/app/services/ark.py+++ b/backend/app/services/ark.py@@ -41,7 +41,7 @@ def _raise_seedream_http_error(status_code: int, body: str) -> None:)if "InputTextSensitive" in snippet or "InputTextSensitiveContentDetected" in snippet:raise RuntimeError(- "生图文案未通过内容审核(含未成年人、酒精等敏感表述),"+ "生图文案未通过内容审核(可能含敏感或历史名人相关表述),""请修改提示词后重试。"f" 详情:{snippet[:240]}")@@ -596,8 +596,8 @@ class ArkGateway:) -> ImageResult:"""调用 Seedream 生图。- 发送前仅软化易触发文本审核的措辞;不做空主体 / CG 厚涂兜底。- 策略拦截仍直接失败。+ 发送前软化易触发文本审核的措辞;InputTextSensitive 时再用短前缀压缩重试一次。+ 不做空主体 / CG 厚涂兜底。"""if self.mock:local = await asyncio.to_thread(self._write_mock_image, prompt, size)@@ -606,34 +606,62 @@ class ArkGateway:url = storage.publish_local(path) if path and path.exists() else localreturn ImageResult(local_url=url, remote_url=None)- from app.services.seedream_text_soften import soften_seedream_input_text+ from app.services.seedream_text_soften import (+ compact_seedream_prompt_for_retry,+ soften_seedream_input_text,+ )# 发送前软化易触发文本审核的措辞(保留主体,非空场景/CG 兜底)- current = soften_seedream_input_text((prompt or "").strip())- if current != (prompt or "").strip():+ original = (prompt or "").strip()+ current = soften_seedream_input_text(original)+ if current != original:logger.info("Seedream input softened shot=%s before=%s after=%s",shot_no,- len(prompt or ""),+ len(original),len(current),)- full_prompt = f"{current}。避免:{negative}" if negative else current- try:- return await self._seedream_once(- full_prompt,- ref_urls,- project_id=project_id,- shot_no=shot_no,- size=size,- model=model,- )- except Exception as exc: # noqa: BLE001- if self._is_seedream_policy_error(str(exc)):++ attempts = [current]+ compact = compact_seedream_prompt_for_retry(current)+ if compact and compact != current:+ attempts.append(compact)++ last_err: Exception | None = None+ for idx, candidate in enumerate(attempts):+ full_prompt = f"{candidate}。避免:{negative}" if negative else candidate+ try:+ return await self._seedream_once(+ full_prompt,+ ref_urls,+ project_id=project_id,+ shot_no=shot_no,+ size=size,+ model=model,+ )+ except Exception as exc: # noqa: BLE001+ last_err = exc+ msg = str(exc)+ # 仅文本审核可走压缩重试;其它策略/错误直接失败+ if not self._is_seedream_input_text_sensitive(msg):+ if self._is_seedream_policy_error(msg):+ logger.warning(+ "Seedream policy hit shot=%s; failing without fallback",+ shot_no,+ )+ raise+ if idx + 1 < len(attempts):+ logger.warning(+ "Seedream InputTextSensitive shot=%s; retrying compact prompt",+ shot_no,+ )+ continuelogger.warning(- "Seedream policy hit shot=%s; failing without fallback",+ "Seedream InputTextSensitive shot=%s; compact retry exhausted",shot_no,)- raise+ raise+ raise RuntimeError(str(last_err) if last_err else "Seedream failed")async def _seedream_once(self,@@ -700,6 +728,16 @@ class ArkGateway:upstream_cost_fen=upstream_cost_fen,)+ @staticmethod+ def _is_seedream_input_text_sensitive(msg: str) -> bool:+ """Seedream 输入文案审核拦截(可压缩提示词重试)。"""+ text = msg or ""+ return (+ "InputTextSensitive" in text+ or "InputTextSensitiveContentDetected" in text+ or "生图文案未通过内容审核" in text+ )+@staticmethoddef _is_seedream_input_privacy_error(msg: str) -> bool:"""参考图 / 输入侧真人隐私拦截(改文案无效)。"""@@ -715,6 +753,8 @@ class ArkGateway:text = msg or ""if ArkGateway._is_seedream_input_privacy_error(text):return False+ if ArkGateway._is_seedream_input_text_sensitive(text):+ return Truereturn ("PolicyViolation" in textor "SensitiveContent" in textdiff --git a/backend/app/services/seedream_text_soften.py b/backend/app/services/seedream_text_soften.pyindex f474abf..e190feb 100644--- a/backend/app/services/seedream_text_soften.py+++ b/backend/app/services/seedream_text_soften.py@@ -1,6 +1,6 @@"""Seedream 输入文案软化:保留主体语义,降低 InputTextSensitive 误杀。-不做空主体 / CG 厚涂兜底;仅替换易触发审核的未成年人、酒精、兵器与网红等表述。+不做空主体 / CG 厚涂兜底;仅替换易触发审核的表述,必要时压缩设定板前缀重试。"""from __future__ import annotations@@ -32,14 +32,37 @@ _SEEDREAM_TEXT_SOFTEN: list[tuple[str, str]] = [("校服", "学院制服"),("课堂", "学堂"),("学生", "学子"),- # 兵器 / 网红 / 名号+ # 兵器 / 网红 / 名号 / 气质("白衣佩剑", "白衣腰佩长绦"),("腰佩长剑", "腰佩长绦"),("佩剑", "腰佩长绦"),("顶流", "名士"),- ("诗仙", "盛唐豪放诗人"),+ ("诗仙", "盛唐洒脱文人"),+ ("盛唐豪放诗人", "盛唐洒脱文人"),+ ("豪放诗人", "洒脱文人"),+ ("眼神狂放不羁", "目光明亮自信"),+ ("狂放不羁", "洒脱自信"),+ ("超凡脱俗", "气度从容"),+ ("豪放", "洒脱"),+ ("电影质感", "细腻画质"),]+# 设定板长前缀结束标记(之后为用户正文)+_STRUCTURE_BODY_MARKERS: tuple[str, ...] = (+ "请严格依据以下用户描述生成上述结构的角色设定图:",+ "请严格依据以下用户描述生成上述结构的场景设计图:",+ "请严格依据以下用户描述生成上述结构的道具设定图:",+ "请严格依据以下用户描述生成白底人物角色全身照:",+ "请严格依据以下用户描述生成场景画面:",+ "请严格依据以下用户描述生成道具画面:",+ "请严格依据以下用户描述生成素材画面:",+)++_COMPACT_CHARACTER_PREFIX = (+ "纯白背景,单人全身角色立绘,无复杂背景、无文字水印,毛毡手作可爱风格。"+ "请依据以下描述生成:"+)+# 软化易触发 Seedream 文本审核的措辞;无变化则原样返回def soften_seedream_input_text(prompt: str) -> str:@@ -50,3 +73,20 @@ def soften_seedream_input_text(prompt: str) -> str:if src in out:out = out.replace(src, dst)return out+++# 拆出设定板前缀后的用户正文;无标记则返回全文+def extract_seedream_user_body(prompt: str) -> str:+ text = prompt or ""+ for marker in _STRUCTURE_BODY_MARKERS:+ if marker in text:+ return text.split(marker, 1)[1].strip()+ return text.strip()+++# 文本审核失败后的压缩重试:短前缀 + 再软化正文(仍保留主体)+def compact_seedream_prompt_for_retry(prompt: str) -> str:+ body = soften_seedream_input_text(extract_seedream_user_body(prompt))+ if not body:+ body = soften_seedream_input_text(prompt or "")+ return f"{_COMPACT_CHARACTER_PREFIX}{body}"diff --git a/backend/tests/test_seedream_text_soften.py b/backend/tests/test_seedream_text_soften.pyindex 908c501..0ff34d2 100644--- a/backend/tests/test_seedream_text_soften.py+++ b/backend/tests/test_seedream_text_soften.py@@ -1,7 +1,10 @@"""seedream_text_soften 单元测试。"""from __future__ import annotations-from app.services.seedream_text_soften import soften_seedream_input_text+from app.services.seedream_text_soften import (+ compact_seedream_prompt_for_retry,+ soften_seedream_input_text,+)def test_soften_xiaoxuesheng_and_child_terms() -> None:@@ -21,17 +24,39 @@ def test_soften_xiaoxuesheng_and_child_terms() -> None:def test_soften_libai_alcohol_weapon_and_title() -> None:- raw = "盛唐顶流,白衣佩剑,腰悬酒葫芦,身份:诗仙"+ raw = (+ "盛唐顶流,白衣佩剑,腰悬酒葫芦,月白大袖,潇洒飘逸,羊毛毡手作风格,"+ "暖阳柔光,眼神狂放不羁,气质超凡脱俗。。身份:诗仙。"+ "细节精致,电影质感。"+ )out = soften_seedream_input_text(raw)assert "酒葫芦" not in outassert "顶流" not in outassert "佩剑" not in outassert "诗仙" not in outassert "葫芦" not in out- assert "盛唐豪放诗人" in out+ assert "狂放不羁" not in out+ assert "电影质感" not in out+ assert "盛唐洒脱文人" in outassert "名士" in out+def test_compact_retry_strips_structure_prefix() -> None:+ full = (+ "【强制任务:角色设定板构图】很长前缀。"+ "请严格依据以下用户描述生成上述结构的角色设定图:"+ "盛唐顶流,白衣佩剑,腰悬酒葫芦,身份:诗仙。电影质感。"+ )+ compact = compact_seedream_prompt_for_retry(full)+ assert "设定板构图" not in compact+ assert "三视图" not in compact+ assert "酒葫芦" not in compact+ assert "诗仙" not in compact+ assert "电影质感" not in compact+ assert "纯白背景" in compact+ assert "盛唐" in compact++def test_soften_noop_when_clean() -> None:raw = "白衣诗人,月白大袖,羊毛毡手作风格"assert soften_seedream_input_text(raw) == raw