fix/various fixes
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
import json
|
||||
from app.core.logging import get_logger
|
||||
import re
|
||||
from typing import Any, Optional
|
||||
|
||||
from app.core.langchain_llm import bind_json_object_mode
|
||||
from app.core.logging import get_logger
|
||||
|
||||
from .json_payload import extract_json_payload
|
||||
from .settings import MemoirImageSettings
|
||||
|
||||
@@ -59,10 +61,7 @@ class MemoirImagePromptService:
|
||||
if self.llm:
|
||||
raw_response = None
|
||||
try:
|
||||
json_llm = self.llm.bind(
|
||||
model_kwargs={"response_format": {"type": "json_object"}},
|
||||
max_tokens=512,
|
||||
)
|
||||
json_llm = bind_json_object_mode(self.llm, max_tokens=512)
|
||||
response = json_llm.invoke(
|
||||
"Return JSON only with keys prompt, style, size. "
|
||||
"Convert the memoir scene into an image-generation prompt.\n"
|
||||
@@ -123,10 +122,7 @@ class MemoirImagePromptService:
|
||||
|
||||
if self.llm:
|
||||
try:
|
||||
json_llm = self.llm.bind(
|
||||
model_kwargs={"response_format": {"type": "json_object"}},
|
||||
max_tokens=512,
|
||||
)
|
||||
json_llm = bind_json_object_mode(self.llm, max_tokens=512)
|
||||
response = json_llm.invoke(
|
||||
"Return JSON only with keys prompt, style, size. "
|
||||
"Create an image-generation prompt for a memoir chapter COVER. "
|
||||
|
||||
@@ -19,22 +19,51 @@ _PLACEHOLDER_DESCRIPTION_RE = re.compile(
|
||||
|
||||
|
||||
def normalize_image_asset(asset: dict[str, Any] | None) -> dict[str, Any] | None:
|
||||
"""归一化单条图片 dict。
|
||||
|
||||
- 兼容旧正文中的 {{{{IMAGE:…}}}} / {{IMAGE:…}} 占位符(需能解析出 description)。
|
||||
- 新模型:插图不嵌入 markdown,可无占位符;已完成且带 url/storage_key 即可通过,
|
||||
description 缺省时用「插图」;pending/processing 至少要有 description、占位符或 prompt。
|
||||
"""
|
||||
if not isinstance(asset, dict):
|
||||
return None
|
||||
|
||||
placeholder = _as_non_empty_string(asset.get("placeholder"))
|
||||
description = _as_non_empty_string(
|
||||
asset.get("description")
|
||||
) or _extract_description_from_placeholder(placeholder)
|
||||
if not placeholder or not description:
|
||||
ph_in = _as_non_empty_string(asset.get("placeholder"))
|
||||
desc_in = _as_non_empty_string(asset.get("description"))
|
||||
desc_from_ph = _extract_description_from_placeholder(ph_in) if ph_in else None
|
||||
merged_description = desc_in or desc_from_ph
|
||||
|
||||
prompt_s = _as_non_empty_string(asset.get("prompt"))
|
||||
error_s = _as_optional_string(asset.get("error"))
|
||||
url_s = _as_optional_string(asset.get("url"))
|
||||
storage_key_s = _as_optional_string(asset.get("storage_key"))
|
||||
has_url_or_key = bool(url_s or storage_key_s)
|
||||
|
||||
status = _as_non_empty_string(asset.get("status")) or IMAGE_STATUS_PENDING
|
||||
|
||||
if ph_in and merged_description:
|
||||
placeholder_out = ph_in
|
||||
description_out = merged_description
|
||||
elif status == IMAGE_STATUS_COMPLETED and has_url_or_key:
|
||||
placeholder_out = ph_in or ""
|
||||
description_out = merged_description or "插图"
|
||||
elif status in (IMAGE_STATUS_PENDING, IMAGE_STATUS_PROCESSING):
|
||||
if not (merged_description or ph_in or prompt_s):
|
||||
return None
|
||||
placeholder_out = ph_in or ""
|
||||
description_out = merged_description or prompt_s or "插图"
|
||||
elif status == IMAGE_STATUS_FAILED:
|
||||
if not (merged_description or ph_in or error_s):
|
||||
return None
|
||||
placeholder_out = ph_in or ""
|
||||
description_out = merged_description or "插图"
|
||||
else:
|
||||
return None
|
||||
|
||||
normalized = dict(asset)
|
||||
normalized["index"] = _coerce_int(asset.get("index"), default=0)
|
||||
normalized["placeholder"] = placeholder
|
||||
normalized["description"] = description
|
||||
|
||||
status = _as_non_empty_string(asset.get("status")) or IMAGE_STATUS_PENDING
|
||||
normalized["placeholder"] = placeholder_out
|
||||
normalized["description"] = description_out
|
||||
if status not in VALID_IMAGE_STATUSES:
|
||||
normalized["status"] = IMAGE_STATUS_FAILED
|
||||
normalized["error"] = asset.get("error") or f"invalid image status: {status}"
|
||||
|
||||
@@ -41,7 +41,7 @@ def memoir_image_to_dict(m: MemoirImage | None) -> dict[str, Any] | None:
|
||||
|
||||
|
||||
def image_dict_to_row_kwargs(d: dict[str, Any] | None) -> dict[str, Any]:
|
||||
"""从单条图片 dict 提取可写入 MemoirImage 的字段(不含 id/chapter_id/section_id/order_index)。"""
|
||||
"""从单条图片 dict 提取可写入 MemoirImage 的字段(不含 id/chapter_id/order_index)。"""
|
||||
if not d or not isinstance(d, dict):
|
||||
return {}
|
||||
created = d.get("created_at")
|
||||
|
||||
Reference in New Issue
Block a user