feat: add material recognition mock flow
这个提交存在于:
父节点
aae0ec5df7
当前提交
b44b1767cf
共有 3 个文件被更改,包括 190 次插入 和 0 次删除
|
|
@ -146,6 +146,49 @@ def delete_material(
|
||||||
return handle_request(ctx, action)
|
return handle_request(ctx, action)
|
||||||
|
|
||||||
|
|
||||||
|
def upload_material(
|
||||||
|
ctx: RequestContext,
|
||||||
|
payload: dict[str, Any],
|
||||||
|
service: MaterialService | None = None,
|
||||||
|
) -> dict:
|
||||||
|
material_service = service or _create_material_service()
|
||||||
|
|
||||||
|
def action() -> dict[str, Any]:
|
||||||
|
require_permission(ctx, "can_manage_materials")
|
||||||
|
return material_service.upload_material(payload, ctx).to_dict()
|
||||||
|
|
||||||
|
return handle_request(ctx, action)
|
||||||
|
|
||||||
|
|
||||||
|
def recognize_material(
|
||||||
|
ctx: RequestContext,
|
||||||
|
material_id: str,
|
||||||
|
service: MaterialService | None = None,
|
||||||
|
) -> dict:
|
||||||
|
material_service = service or _create_material_service()
|
||||||
|
|
||||||
|
def action() -> dict[str, Any]:
|
||||||
|
require_permission(ctx, "can_manage_materials")
|
||||||
|
return material_service.recognize_material(material_id, ctx).to_dict()
|
||||||
|
|
||||||
|
return handle_request(ctx, action)
|
||||||
|
|
||||||
|
|
||||||
|
def confirm_material(
|
||||||
|
ctx: RequestContext,
|
||||||
|
material_id: str,
|
||||||
|
payload: dict[str, Any],
|
||||||
|
service: MaterialService | None = None,
|
||||||
|
) -> dict:
|
||||||
|
material_service = service or _create_material_service()
|
||||||
|
|
||||||
|
def action() -> dict[str, Any]:
|
||||||
|
require_permission(ctx, "can_manage_materials")
|
||||||
|
return material_service.confirm_material(material_id, payload, ctx).to_dict()
|
||||||
|
|
||||||
|
return handle_request(ctx, action)
|
||||||
|
|
||||||
|
|
||||||
def _create_material_service() -> MaterialService:
|
def _create_material_service() -> MaterialService:
|
||||||
database = Database(load_database_settings())
|
database = Database(load_database_settings())
|
||||||
return MaterialService(
|
return MaterialService(
|
||||||
|
|
|
||||||
|
|
@ -172,6 +172,77 @@ class MaterialService:
|
||||||
def delete_material(self, material_id: str, ctx: RequestContext) -> Material:
|
def delete_material(self, material_id: str, ctx: RequestContext) -> Material:
|
||||||
return self.update_material(material_id, {"recognition_status": "archived"}, ctx)
|
return self.update_material(material_id, {"recognition_status": "archived"}, ctx)
|
||||||
|
|
||||||
|
def upload_material(self, payload: dict[str, Any], ctx: RequestContext) -> Material:
|
||||||
|
image_uri = payload.get("image_uri")
|
||||||
|
if not image_uri:
|
||||||
|
raise ApiError(ErrorCode.VALIDATION_FAILED, "image_uri is required", {"field": "image_uri"})
|
||||||
|
display_name = str(payload.get("display_name") or payload.get("filename") or "Uploaded material").strip()
|
||||||
|
return self.create_material(
|
||||||
|
{
|
||||||
|
"category_id": payload.get("category_id"),
|
||||||
|
"display_name": display_name,
|
||||||
|
"image_uri": image_uri,
|
||||||
|
"thumbnail_uri": payload.get("thumbnail_uri") or image_uri,
|
||||||
|
"recognition_status": "pending_recognition",
|
||||||
|
"metadata": {
|
||||||
|
"upload": {
|
||||||
|
"filename": payload.get("filename"),
|
||||||
|
"content_type": payload.get("content_type"),
|
||||||
|
"mock": True,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
ctx,
|
||||||
|
)
|
||||||
|
|
||||||
|
def recognize_material(self, material_id: str, ctx: RequestContext) -> Material:
|
||||||
|
material = self.get_material(material_id)
|
||||||
|
if material.recognition_status == "confirmed":
|
||||||
|
return material
|
||||||
|
generated_tags = _mock_tags(material)
|
||||||
|
prompt = material.prompt or _mock_prompt(material)
|
||||||
|
after = self.update_material(
|
||||||
|
material_id,
|
||||||
|
{
|
||||||
|
"prompt": prompt,
|
||||||
|
"negative_prompt": material.negative_prompt or "body, face, background, watermark",
|
||||||
|
"tags": sorted(set(material.tags + generated_tags)),
|
||||||
|
"recognition_status": "pending_confirm",
|
||||||
|
"metadata": {**material.metadata, "recognition": {"engine": "mock_vlm", "confidence": 0.72}},
|
||||||
|
},
|
||||||
|
ctx,
|
||||||
|
)
|
||||||
|
self.audit_service.record(
|
||||||
|
ctx=ctx,
|
||||||
|
action="recognize_material",
|
||||||
|
target_type="material",
|
||||||
|
target_id=material_id,
|
||||||
|
before_snapshot=material.to_dict(),
|
||||||
|
after_snapshot=after.to_dict(),
|
||||||
|
)
|
||||||
|
return after
|
||||||
|
|
||||||
|
def confirm_material(self, material_id: str, payload: dict[str, Any], ctx: RequestContext) -> Material:
|
||||||
|
material = self.get_material(material_id)
|
||||||
|
update_payload = {
|
||||||
|
"recognition_status": "confirmed",
|
||||||
|
"display_name": payload.get("display_name", material.display_name),
|
||||||
|
"prompt": payload.get("prompt", material.prompt),
|
||||||
|
"negative_prompt": payload.get("negative_prompt", material.negative_prompt),
|
||||||
|
"tags": payload.get("tags", material.tags),
|
||||||
|
"metadata": {**material.metadata, "confirmed": True},
|
||||||
|
}
|
||||||
|
after = self.update_material(material_id, update_payload, ctx)
|
||||||
|
self.audit_service.record(
|
||||||
|
ctx=ctx,
|
||||||
|
action="confirm_material",
|
||||||
|
target_type="material",
|
||||||
|
target_id=material_id,
|
||||||
|
before_snapshot=material.to_dict(),
|
||||||
|
after_snapshot=after.to_dict(),
|
||||||
|
)
|
||||||
|
return after
|
||||||
|
|
||||||
def _validate_category(self, category_id: Any) -> None:
|
def _validate_category(self, category_id: Any) -> None:
|
||||||
if category_id and self.repository.get_category(str(category_id)) is None:
|
if category_id and self.repository.get_category(str(category_id)) is None:
|
||||||
raise ApiError(ErrorCode.RESOURCE_NOT_FOUND, "category not found", {"category_id": category_id})
|
raise ApiError(ErrorCode.RESOURCE_NOT_FOUND, "category not found", {"category_id": category_id})
|
||||||
|
|
@ -197,3 +268,17 @@ def new_category_id() -> str:
|
||||||
|
|
||||||
def new_material_id() -> str:
|
def new_material_id() -> str:
|
||||||
return f"mat_{uuid4().hex}"
|
return f"mat_{uuid4().hex}"
|
||||||
|
|
||||||
|
|
||||||
|
def _mock_prompt(material: Material) -> str:
|
||||||
|
base = material.display_name.strip().lower().replace("_", " ")
|
||||||
|
return f"{base}, clean product reference, transparent background"
|
||||||
|
|
||||||
|
|
||||||
|
def _mock_tags(material: Material) -> list[str]:
|
||||||
|
text = f"{material.display_name} {material.prompt or ''}".lower()
|
||||||
|
tags = ["material"]
|
||||||
|
for keyword in ("white", "blue", "black", "red", "ribbon", "shirt", "blouse", "dress", "skirt", "shoe"):
|
||||||
|
if keyword in text:
|
||||||
|
tags.append(keyword)
|
||||||
|
return tags
|
||||||
|
|
|
||||||
|
|
@ -5,13 +5,16 @@ import unittest
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from backend.routes.material_routes import (
|
from backend.routes.material_routes import (
|
||||||
|
confirm_material,
|
||||||
create_material,
|
create_material,
|
||||||
create_material_category,
|
create_material_category,
|
||||||
delete_material,
|
delete_material,
|
||||||
get_material,
|
get_material,
|
||||||
list_material_categories,
|
list_material_categories,
|
||||||
list_materials,
|
list_materials,
|
||||||
|
recognize_material,
|
||||||
update_material,
|
update_material,
|
||||||
|
upload_material,
|
||||||
)
|
)
|
||||||
from backend.services.material_service import MaterialService
|
from backend.services.material_service import MaterialService
|
||||||
from core.audit_service import AuditService
|
from core.audit_service import AuditService
|
||||||
|
|
@ -89,6 +92,36 @@ class MaterialServiceTests(unittest.TestCase):
|
||||||
self.ctx,
|
self.ctx,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_upload_recognize_and_confirm_material(self):
|
||||||
|
category = self.material_service.create_category({"name": "Upper"}, self.ctx)
|
||||||
|
uploaded = self.material_service.upload_material(
|
||||||
|
{
|
||||||
|
"category_id": category.category_id,
|
||||||
|
"filename": "white_blouse.png",
|
||||||
|
"image_uri": "storage://materials/white_blouse.png",
|
||||||
|
},
|
||||||
|
self.ctx,
|
||||||
|
)
|
||||||
|
self.assertEqual(uploaded.recognition_status, "pending_recognition")
|
||||||
|
self.assertEqual(uploaded.thumbnail_uri, "storage://materials/white_blouse.png")
|
||||||
|
|
||||||
|
recognized = self.material_service.recognize_material(uploaded.material_id, self.ctx)
|
||||||
|
self.assertEqual(recognized.recognition_status, "pending_confirm")
|
||||||
|
self.assertIn("white", recognized.tags)
|
||||||
|
self.assertIn("clean product reference", recognized.prompt)
|
||||||
|
|
||||||
|
confirmed = self.material_service.confirm_material(
|
||||||
|
uploaded.material_id,
|
||||||
|
{
|
||||||
|
"display_name": "White short-sleeve blouse",
|
||||||
|
"prompt": "white short-sleeve blouse, clean collar",
|
||||||
|
"tags": ["upper", "white", "summer"],
|
||||||
|
},
|
||||||
|
self.ctx,
|
||||||
|
)
|
||||||
|
self.assertEqual(confirmed.recognition_status, "confirmed")
|
||||||
|
self.assertEqual(confirmed.tags, ["upper", "white", "summer"])
|
||||||
|
|
||||||
def test_material_routes_wrap_responses_and_require_permission(self):
|
def test_material_routes_wrap_responses_and_require_permission(self):
|
||||||
category = create_material_category(self.ctx, {"name": "Accessory"}, self.material_service)
|
category = create_material_category(self.ctx, {"name": "Accessory"}, self.material_service)
|
||||||
self.assertTrue(category["ok"])
|
self.assertTrue(category["ok"])
|
||||||
|
|
@ -132,6 +165,35 @@ class MaterialServiceTests(unittest.TestCase):
|
||||||
self.assertFalse(missing["ok"])
|
self.assertFalse(missing["ok"])
|
||||||
self.assertEqual(missing["error_code"], "RESOURCE_NOT_FOUND")
|
self.assertEqual(missing["error_code"], "RESOURCE_NOT_FOUND")
|
||||||
|
|
||||||
|
def test_material_upload_recognize_confirm_routes(self):
|
||||||
|
category = create_material_category(self.ctx, {"name": "Dress"}, self.material_service)
|
||||||
|
uploaded = upload_material(
|
||||||
|
self.ctx,
|
||||||
|
{
|
||||||
|
"category_id": category["data"]["category_id"],
|
||||||
|
"filename": "blue_dress.png",
|
||||||
|
"image_uri": "storage://materials/blue_dress.png",
|
||||||
|
},
|
||||||
|
self.material_service,
|
||||||
|
)
|
||||||
|
self.assertTrue(uploaded["ok"])
|
||||||
|
self.assertEqual(uploaded["data"]["recognition_status"], "pending_recognition")
|
||||||
|
|
||||||
|
material_id = uploaded["data"]["material_id"]
|
||||||
|
recognized = recognize_material(self.ctx, material_id, self.material_service)
|
||||||
|
self.assertTrue(recognized["ok"])
|
||||||
|
self.assertEqual(recognized["data"]["recognition_status"], "pending_confirm")
|
||||||
|
self.assertIn("blue", recognized["data"]["tags"])
|
||||||
|
|
||||||
|
confirmed = confirm_material(
|
||||||
|
self.ctx,
|
||||||
|
material_id,
|
||||||
|
{"display_name": "Blue dress", "tags": ["dress", "blue"]},
|
||||||
|
self.material_service,
|
||||||
|
)
|
||||||
|
self.assertTrue(confirmed["ok"])
|
||||||
|
self.assertEqual(confirmed["data"]["recognition_status"], "confirmed")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
||||||
正在加载…
添加表格
在新工单中引用