fix: 减少图片生成数量(当前3个section对应一张图)

This commit is contained in:
yangshilin
2026-03-13 16:24:28 +08:00
parent 672abf5ec7
commit c0906723ac

View File

@@ -302,22 +302,26 @@ def _save_narrative_to_sections(db: Session, chapter, narrative: str, title: str
settings = MemoirImageSettings.from_env()
prompt_service = MemoirImagePromptService(llm=None, settings=settings) if settings.enabled else None
effective_max = settings.effective_max_images(len(narrative_to_parse)) if settings.enabled else 0
all_placeholders = [s["placeholder_info"] for s in segments if s.get("placeholder_info")]
placeholders = _select_placeholders_for_effective_max(
placeholders=all_placeholders,
existing_images=[],
effective_max=effective_max,
) if settings.enabled else []
selected_placeholder_set = {p.get("placeholder") for p in placeholders}
# 按顺序创建 section,保证每个 section 的 content 与 image 一一对应order_index 严格递增)
# 每 3 个 section 对应 1 张图片,其他 section 的 image_id 为空
def _should_have_image(order_idx: int) -> bool:
return (order_idx % 3) == 2
def _placeholder_for_segment(seg: dict, order_idx: int) -> dict | None:
ph = seg.get("placeholder_info")
if ph and ph.get("placeholder") and ph.get("description"):
return ph
content = (seg.get("content") or "").strip()
desc = (content[:50] + "") if len(content) > 50 else (content or "章节配图")
return {"placeholder": f"{{{{{{{{IMAGE:{desc}}}}}}}}}", "description": desc}
# 按顺序创建 section每 3 个 section 对应 1 张配图
for i, seg in enumerate(segments):
order_idx = order_base + i
content = (seg.get("content") or "").strip()
ph = seg.get("placeholder_info")
image_asset = None
if ph and settings.enabled and ph.get("placeholder") in selected_placeholder_set:
if settings.enabled and _should_have_image(order_idx):
ph = _placeholder_for_segment(seg, order_idx)
style = prompt_service.CATEGORY_STYLE_MAP.get(category, settings.default_style) if prompt_service else settings.default_style
image_asset = build_initial_image_assets(
[ph],