feat(images): 期2 图片趴三类 — 模特上身图/平台套图/自由创作 归类+成组+接模特库

后端:
- 独立生图三类归类:model+product→model_tryon(模特上身图,引用模特库,不送审) / cover→platform_kit(平台套图) / image→free_create(自由创作);生成演员(model 无 product)仍→person(视频角色,留期3)
- 成组:每次提交一个 batch_id 串起整批,落 asset.metadata;另记 mode + model_entity_id(上身图溯源模特库)
- generate-image 端点透传 model_entity_id;资产库 _tab_q+summary 加 tryon/kits/creations 三类桶
- 单测 StandaloneCategoryTests 直跑 worker 验四态归类全绿(绕开异步 .delay)

前端:
- 资产库 library.tsx 加三类 tab(模特上身图/平台套图/自由创作)+ 计数 + 筛选维度
- ai-tools 模特选择器数据源 person→模特库(listModels 映射,选中传形象图当参考 = 引用模特库),ActorLibrary 同源
- api.ts submitGenerateImage 加 model_entity_id

验收:tsc+build 全绿;无头 0 console error(资产库三类 tab 各归类正确、模特选择器 5 张取自模特库)
基线既有 3 失败(StandaloneImageReferenceTests 异步漂移)零新增

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
seaislee1209
2026-06-21 16:36:29 +08:00
co-authored by Claude Opus 4.8
parent 44e90233ff
commit c8f3f91d38
8 changed files with 187 additions and 29 deletions
+21 -9
View File
@@ -1494,10 +1494,13 @@ def create_export_job(*, timeline, user) -> ExportJob:
return ExportJob.objects.create(timeline=timeline, status=ExportJob.Status.QUEUED)
# 图片趴三类(模特库+资产模型重构 期2):
# · model + product → model_tryon(模特上身图);model 无 product → person(视频角色「生成演员」,留期3)
# · cover → platform_kit(平台套图);image → free_create(自由创作)
_STANDALONE_CATEGORY = {
"model": Asset.Category.PERSON,
"cover": Asset.Category.PRODUCT_IMAGE,
"image": Asset.Category.PRODUCT_IMAGE,
"cover": Asset.Category.PLATFORM_KIT,
"image": Asset.Category.FREE_CREATE,
}
_STANDALONE_TASK_TYPE = {
"model": AITask.Type.PERSON_IMAGE,
@@ -1535,7 +1538,7 @@ def _reap_stale_standalone_image_tasks(*, team) -> None:
continue
def enqueue_standalone_images(*, team, user, prompt: str, mode: str = "image", count: int = 1, product_id: str | None = None, reference_product: bool = False, model_id: str | None = None, ratio: str | None = None) -> list[AITask]:
def enqueue_standalone_images(*, team, user, prompt: str, mode: str = "image", count: int = 1, product_id: str | None = None, reference_product: bool = False, model_id: str | None = None, model_entity_id: str | None = None, ratio: str | None = None) -> list[AITask]:
"""独立生图(图片创作 / 模特上身图 / 平台套图)改为**异步**:本函数在 Web 请求里只做「建任务 +
预留额度」这种秒级的活,真正 ~30s 的 ARK 出图交给 Celery worker(generate_standalone_image_task)。
@@ -1550,6 +1553,8 @@ def enqueue_standalone_images(*, team, user, prompt: str, mode: str = "image", c
raise ValueError("no active image model configured")
task_type = _STANDALONE_TASK_TYPE.get(mode, AITask.Type.PRODUCT_IMAGE)
count = max(1, min(int(count or 1), 12))
# 本次提交 = 一组(模特上身图组 / 平台套图组):同一 batch_id 串起这批图,前端可成组展示。
batch_id = str(uuid.uuid4())
tasks: list[AITask] = []
for index in range(count):
cost = estimate_cost(model_config)
@@ -1561,7 +1566,7 @@ def enqueue_standalone_images(*, team, user, prompt: str, mode: str = "image", c
status=AITask.Status.CREATED,
model_config=model_config,
idempotency_key=f"standalone-image:{team.id}:{uuid.uuid4()}",
request_payload={"model": model_config.name, "endpoint": model_config.endpoint, "prompt": prompt, "mode": mode, "index": index, "product_id": str(product_id) if product_id else None, "reference_product": bool(reference_product), "model_id": str(model_id) if model_id else None, "ratio": str(ratio) if ratio else None},
request_payload={"model": model_config.name, "endpoint": model_config.endpoint, "prompt": prompt, "mode": mode, "index": index, "product_id": str(product_id) if product_id else None, "reference_product": bool(reference_product), "model_id": str(model_id) if model_id else None, "model_entity_id": str(model_entity_id) if model_entity_id else None, "batch_id": batch_id, "ratio": str(ratio) if ratio else None},
estimated_cost=cost,
)
# 预留额度若余额不足会抛 ValueError,在同步的 Web 请求里立刻反馈给前端(不会先建半套任务)
@@ -1588,10 +1593,10 @@ def run_standalone_image_task(*, task_id: str) -> None:
mode = str(payload.get("mode") or "image")
index = int(payload.get("index") or 0)
product_id = payload.get("product_id") or None
# 模特上身图(mode=model 且绑了商品)= 该商品的商品图,归到对应商品的 AI 资产,不进人物库;
# 「生成演员」同样走 mode=model 但无 product_id,仍归人物库(PERSON)。
# 模特上身图(mode=model 且绑了商品)= 图片趴「模特上身图」(引用模特库),归 model_tryon、不送审;
# 「生成演员」同样走 mode=model 但无 product_id = 视频角色,仍归 person(送审,留期3 收编为「角色」)。
if mode == "model" and product_id:
category = Asset.Category.PRODUCT_IMAGE
category = Asset.Category.MODEL_TRYON
else:
category = _STANDALONE_CATEGORY.get(mode, Asset.Category.UNCATEGORIZED)
model_config = task.model_config
@@ -1652,11 +1657,18 @@ def run_standalone_image_task(*, task_id: str) -> None:
object_key = f"teams/{team.id}/standalone/{asset_id}{suffix}"
stored = TosStorage().upload_fileobj(fileobj=fileobj, object_key=object_key, content_type=content_type)
asset_label = {"model": "模特上身图", "cover": "平台套图", "image": "图片创作"}.get(mode, mode)
# 资产元数据:product_id(商品详情页据此只展示该商品素材)+ batch_id(成组)+ mode + model_entity_id(上身图溯源模特库)
asset_meta: dict = {"mode": mode}
if product_id:
asset_meta["product_id"] = str(product_id)
if payload.get("batch_id"):
asset_meta["batch_id"] = str(payload["batch_id"])
if payload.get("model_entity_id"):
asset_meta["model_entity_id"] = str(payload["model_entity_id"])
asset = Asset.objects.create(
id=asset_id, team=team, created_by=user, name=f"AI 生成 · {asset_label} · {index + 1}",
asset_type=Asset.Type.IMAGE, source=Asset.Source.AI_GENERATED, category=category, origin_task=task,
# 记下生图时选中的商品,商品详情页据此只展示「该商品」的 AI 素材(而非全团队)
metadata={"product_id": str(product_id)} if product_id else {},
metadata=asset_meta,
)
AssetFile.objects.create(asset=asset, object_key=stored.object_key, bucket=stored.bucket, content_type=stored.content_type, size_bytes=stored.size_bytes, is_primary=True)
except Exception as exc: # noqa: BLE001 — 失败要退费并把错误记进 AITask 供前端轮询读取;不向上抛(避免 celery 重试二次扣费)
+63
View File
@@ -212,6 +212,69 @@ class StandaloneImageReferenceTests(TestCase):
prov.image_edit.assert_not_called()
class StandaloneCategoryTests(TestCase):
"""图片趴三类归类(期2):模特上身图→model_tryon / 平台套图→platform_kit / 自由创作→free_create;
生成演员(model 无 product)仍→person(视频角色)。直接跑 worker 函数避开异步 .delay。"""
def setUp(self):
self.user = User.objects.create_user(username="catowner", password="pass")
self.team = Team.objects.create(name="CT", owner=self.user)
CreditAccount.objects.create(team=self.team, balance="100.0000")
self.product = Product.objects.create(team=self.team, created_by=self.user, title="测试商品")
cover = Asset.objects.create(
team=self.team, created_by=self.user, name="主图", asset_type=Asset.Type.IMAGE,
source=Asset.Source.UPLOAD, category=Asset.Category.PRODUCT_IMAGE,
)
AssetFile.objects.create(asset=cover, object_key="c.png", bucket="b", content_type="image/png", preview_url="http://x/cover.png", is_primary=True)
self.product.cover_asset = cover
self.product.save(update_fields=["cover_asset"])
def _patch_provider(self):
provider = patch("apps.ai.services.get_image_provider").start()
prov = provider.return_value
prov.image_edit.return_value = {"data": [{"url": "http://x/out.png"}]}
prov.image_generation.return_value = {"data": [{"url": "http://x/out.png"}]}
prov.extract_first_media_url.return_value = "http://x/out.png"
media = patch("apps.ai.services.VolcanoArkProvider.media_to_bytes").start()
media.return_value = (BytesIO(b"img"), "image/png")
store = patch("apps.ai.services.TosStorage").start()
stored = store.return_value.upload_fileobj.return_value
stored.object_key, stored.bucket, stored.content_type, stored.size_bytes = "o.png", "b", "image/png", 3
self.addCleanup(patch.stopall)
return prov
def _run(self, mode, *, product_id=None, model_id=None):
from apps.ai.services import run_standalone_image_task
self._patch_provider()
tasks = enqueue_standalone_images(
team=self.team, user=self.user, prompt="x", mode=mode, count=1,
product_id=product_id, model_id=model_id, model_entity_id="ent-1", ratio="4:5",
)
run_standalone_image_task(task_id=str(tasks[0].id))
return Asset.objects.filter(origin_task=tasks[0]).first()
def test_model_tryon_category(self):
a = self._run("model", product_id=str(self.product.id))
self.assertEqual(a.category, Asset.Category.MODEL_TRYON)
self.assertEqual(a.metadata.get("mode"), "model")
self.assertTrue(a.metadata.get("batch_id")) # 成组
self.assertEqual(a.metadata.get("model_entity_id"), "ent-1") # 溯源模特库
def test_platform_kit_category(self):
a = self._run("cover", product_id=str(self.product.id))
self.assertEqual(a.category, Asset.Category.PLATFORM_KIT)
def test_free_create_category(self):
a = self._run("image")
self.assertEqual(a.category, Asset.Category.FREE_CREATE)
def test_generate_actor_stays_person(self):
# 生成演员:mode=model 但无 product → 视频角色,仍 person(送审范围内)
a = self._run("model")
self.assertEqual(a.category, Asset.Category.PERSON)
class _FakeStreamResp:
"""模拟 requests 流式响应:支持 with、raise_for_status、可写 encoding、iter_lines。"""
status_code = 200
+2 -1
View File
@@ -32,10 +32,11 @@ class GenerateImageView(APIView):
product_id = str(request.data.get("product_id") or "").strip() or None
reference_product = bool(request.data.get("reference_product"))
model_id = str(request.data.get("model_id") or "").strip() or None
model_entity_id = str(request.data.get("model_entity_id") or "").strip() or None
ratio = str(request.data.get("ratio") or "").strip() or None
team = get_current_team(request.user)
try:
tasks = enqueue_standalone_images(team=team, user=request.user, prompt=prompt, mode=mode, count=count, product_id=product_id, reference_product=reference_product, model_id=model_id, ratio=ratio)
tasks = enqueue_standalone_images(team=team, user=request.user, prompt=prompt, mode=mode, count=count, product_id=product_id, reference_product=reference_product, model_id=model_id, model_entity_id=model_entity_id, ratio=ratio)
except ValueError as exc: # 无可用模型 / 余额不足等,立即反馈
return Response({"detail": str(exc)}, status=status.HTTP_400_BAD_REQUEST)
return Response(