feat: 试用过程中的细节优化
This commit is contained in:
+46
-15
@@ -9,6 +9,7 @@ watermark + services/prompts),任务持久化到数据库,由独立 Worker
|
||||
from __future__ import annotations
|
||||
|
||||
import io
|
||||
import re
|
||||
import tempfile
|
||||
import uuid
|
||||
import zipfile
|
||||
@@ -49,25 +50,39 @@ from services.suite_service import texts_to_raw, validate_model
|
||||
|
||||
router = APIRouter(prefix="/api", tags=["suite"])
|
||||
|
||||
_DIMENSION_PARAM_RE = re.compile(
|
||||
r"尺寸|长宽高|长度|宽度|高度|厚度|深度|直径|dimension|length|width|height|depth|thickness|diameter",
|
||||
re.IGNORECASE,
|
||||
)
|
||||
|
||||
|
||||
def _with_dimensions(raw: dict, dimensions, *, required: bool = False) -> dict:
|
||||
"""将表单长宽高放入提示词上下文,不依赖采集参数是否已有尺寸字段。"""
|
||||
"""将已填写的尺寸方向放入提示词上下文;未填写方向绝不传给绘制提示词。"""
|
||||
if dimensions is None:
|
||||
if required:
|
||||
raise HTTPException(status_code=400, detail="生成尺寸标注图需要完整填写商品长、宽、高")
|
||||
raise HTTPException(status_code=400, detail="生成尺寸标注图至少需要填写一项商品尺寸")
|
||||
return raw
|
||||
values = (dimensions.length, dimensions.width, dimensions.height)
|
||||
if not all(value is not None and value > 0 for value in values):
|
||||
known_dimensions = {
|
||||
key: value for key, value in {
|
||||
"length": dimensions.length,
|
||||
"width": dimensions.width,
|
||||
"height": dimensions.height,
|
||||
}.items() if value is not None and value > 0
|
||||
}
|
||||
if not known_dimensions:
|
||||
if required:
|
||||
raise HTTPException(status_code=400, detail="生成尺寸标注图需要完整填写商品长、宽、高")
|
||||
raise HTTPException(status_code=400, detail="生成尺寸标注图至少需要填写一项商品尺寸")
|
||||
return raw
|
||||
enriched = dict(raw)
|
||||
enriched["dimensions"] = {
|
||||
"length": dimensions.length,
|
||||
"width": dimensions.width,
|
||||
"height": dimensions.height,
|
||||
"unit": dimensions.unit,
|
||||
}
|
||||
# 表单尺寸是尺寸标注图的唯一事实来源。原始商品参数常含完整 L×W×H,
|
||||
# 若继续带进 prompt,模型会把未填写的方向也画出来。
|
||||
params = raw.get("params") or []
|
||||
if isinstance(params, list):
|
||||
enriched["params"] = [
|
||||
pair for pair in params
|
||||
if isinstance(pair, dict) and not _DIMENSION_PARAM_RE.search(str(pair.get("key") or ""))
|
||||
]
|
||||
enriched["dimensions"] = {**known_dimensions, "unit": dimensions.unit}
|
||||
return enriched
|
||||
|
||||
|
||||
@@ -315,7 +330,19 @@ async def suite_image_edit(req: ImageEditSingleRequest) -> ImageEditTaskResponse
|
||||
except ValueError as exc:
|
||||
raise HTTPException(status_code=400, detail="product_id 不是合法 UUID") from exc
|
||||
|
||||
# 单图编辑优先级高于套图;prompt_extra 清空展示文案,避免中文 UI 文案进入模型 prompt。
|
||||
# 单图编辑优先级高于套图;支持多张参考底图,保持传入顺序并去重。
|
||||
ref_urls = list(dict.fromkeys(url for url in [*req.image_urls, req.image_url] if url))
|
||||
if not ref_urls:
|
||||
raise HTTPException(status_code=400, detail="请至少选择一张参考底图")
|
||||
if req.mode == "outpaint":
|
||||
if provider_name != "pcore":
|
||||
raise HTTPException(status_code=400, detail="仅扩图当前仅支持 Pcore gpt-image-2")
|
||||
if len(ref_urls) != 1:
|
||||
raise HTTPException(status_code=400, detail="仅扩图只能选择一张参考底图")
|
||||
if req.generate_size_chart:
|
||||
raise HTTPException(status_code=400, detail="仅扩图不能同时生成尺寸标注图")
|
||||
|
||||
# prompt_extra 清空展示文案,避免中文 UI 文案进入模型 prompt。
|
||||
is_size_chart = bool(req.generate_size_chart)
|
||||
task = await create_image_job(
|
||||
product_id=product_id,
|
||||
@@ -326,20 +353,24 @@ async def suite_image_edit(req: ImageEditSingleRequest) -> ImageEditTaskResponse
|
||||
platform="ozon",
|
||||
lang=spec["lang"],
|
||||
ratio=spec["ratio"],
|
||||
style_set=5,
|
||||
# 仅扩图与用户明确填写的 AI 编辑要求都不得继承默认「纯净棚拍」风格。
|
||||
style_set=0 if req.mode == "outpaint" or bool(req.prompt.strip()) else 5,
|
||||
request_payload={
|
||||
"context": _with_dimensions({"title": "AI 精修"}, req.dimensions, required=is_size_chart),
|
||||
"style_prompt": None,
|
||||
"requirements": req.prompt,
|
||||
"operation": req.mode,
|
||||
# 用户写了要求时,这一轮只执行其要求,不再拼营销图、默认风格或默认俄文文案。
|
||||
"requirements_exclusive": bool(req.mode == "edit" and not is_size_chart and req.prompt.strip()),
|
||||
"watermark": req.watermark.model_dump() if req.watermark else None,
|
||||
"append": bool(req.append and product_id),
|
||||
"after_asset_id": req.after_asset_id,
|
||||
"ref_images": [{"url": req.image_url, "group_key": "main", "variant_name": None}],
|
||||
"ref_images": [{"url": url, "group_key": "main", "variant_name": None} for url in ref_urls],
|
||||
},
|
||||
item_payloads=[{
|
||||
"position": 0,
|
||||
"kind": "size_chart" if is_size_chart else "custom",
|
||||
"title": "尺寸标注图" if is_size_chart else "AI生图",
|
||||
"title": "尺寸标注图" if is_size_chart else ("仅扩图" if req.mode == "outpaint" else "AI生图"),
|
||||
"detail": "",
|
||||
"prompt_hint": "",
|
||||
"variant_name": None,
|
||||
|
||||
Reference in New Issue
Block a user