nex_animation/backend/app/api/platform.py

1112 lines
55 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

"""平台级数字资产 API:跨项目人物、场景、声线的管理、生成与引入。"""
from __future__ import annotations
import hashlib
import re
from collections import Counter
from pathlib import Path
from fastapi import APIRouter, Depends, File, Form, HTTPException, UploadFile
from sqlalchemy import select
from sqlalchemy.orm import Session
from app import schemas
from app.api import deps
from app.constants import AssetKind
from app.db import get_db
from app.models import (Asset, Character, PlatformCharacter, PlatformMedia, PlatformMediaVersion,
PlatformScene, PlatformVoice, Project, Scene, VoiceAsset, now_utc)
from app.providers import registry as model_registry
from app.providers.base import AssetGenRequest, ProviderError, VoiceGenRequest
from app.services import audio, events, storage, timbre, versions
router = APIRouter(tags=["platform-assets"])
OWNER_TYPES = {"human", "scene", "voice"}
PLATFORM_KINDS: dict[str, set[str]] = {
"human": {"portrait", "turnaround", "costume", "expression", "voice_sample", "reference"},
"scene": {"scene_image", "reference"},
"voice": {"voice_sample", "reference"},
}
def _normalize_type(owner_type: str) -> str:
return {
"characters": "human", "character": "human", "human": "human", "humans": "human",
"scene": "scene", "scenes": "scene", "voice": "voice", "voices": "voice",
}.get(owner_type, owner_type)
def _owner(db: Session, owner_type: str, owner_id: int):
owner_type = _normalize_type(owner_type)
if owner_type == "human":
row = db.get(PlatformCharacter, owner_id)
elif owner_type == "scene":
row = db.get(PlatformScene, owner_id)
elif owner_type == "voice":
row = db.get(PlatformVoice, owner_id)
else:
raise HTTPException(400, f"不支持的平台资产类型:{owner_type}")
if row is None:
raise HTTPException(404, "平台资产不存在")
return row
def _media(db: Session, owner_type: str, owner_id: int) -> list[dict]:
rows = db.query(PlatformMedia).filter(
PlatformMedia.owner_type == owner_type, PlatformMedia.owner_id == owner_id,
).order_by(PlatformMedia.id).all()
return [storage.platform_media_dict(row) for row in rows]
def _reference_media(db: Session, owner_type: str, owner_id: int, ids: list[int],
*, kind: str) -> list[PlatformMedia]:
"""校验平台生成参考素材只能来自当前资产,避免跨资产误读本地路径。"""
rows: list[PlatformMedia] = []
seen: set[int] = set()
image_suffixes = {".png", ".jpg", ".jpeg", ".webp", ".bmp", ".tif", ".tiff"}
audio_suffixes = {".wav", ".mp3", ".m4a", ".aac", ".flac", ".ogg", ".opus"}
for media_id in ids:
if media_id in seen:
continue
seen.add(media_id)
row = db.get(PlatformMedia, media_id)
if (row is None or row.owner_type != owner_type or row.owner_id != owner_id):
raise HTTPException(400, "参考素材不属于当前平台资产")
if not storage.exists(row.path):
raise HTTPException(400, "参考素材文件不存在,请重新上传")
suffix = Path(row.path).suffix.lower()
if kind == "image" and suffix not in image_suffixes:
raise HTTPException(400, "图像参考素材必须是图片文件")
if kind == "audio" and suffix not in audio_suffixes:
raise HTTPException(400, "音色参考素材必须是音频文件")
rows.append(row)
return rows
def _human_dict(db: Session, row: PlatformCharacter, media: list[dict] | None = None) -> dict:
project = db.get(Project, row.project_id) if row.project_id else None
return {
"id": row.id, "name": row.name, "code": row.code, "role": row.role,
"project_id": row.project_id, "project_name": project.name if project else "",
"appearance": row.appearance, "personality": row.personality,
"persona_prompt": row.persona_prompt, "voice_profile": row.voice_profile,
"default_voice_id": row.default_voice_id,
"default_voice_name": row.default_voice.name if row.default_voice else "",
"style_version": row.style_version, "tags": row.tags or [], "notes": row.notes,
"is_active": row.is_active,
"assets": _media(db, "human", row.id) if media is None else media,
"updated_at": row.updated_at,
"created_at": getattr(row, "created_at", None),
}
def _scene_dict(db: Session, row: PlatformScene, media: list[dict] | None = None) -> dict:
return {
"id": row.id, "name": row.name, "code": row.code, "description": row.description,
"prompt": row.prompt, "lighting": row.lighting, "time_of_day": row.time_of_day,
"palette": row.palette, "tags": row.tags or [], "is_active": row.is_active,
"assets": _media(db, "scene", row.id) if media is None else media,
"updated_at": row.updated_at,
"created_at": getattr(row, "created_at", None),
}
def _voice_dict(db: Session, row: PlatformVoice, media: list[dict] | None = None) -> dict:
return {
"id": row.id, "name": row.name, "code": row.code, "provider": row.provider,
"provider_voice_id": row.provider_voice_id, "language": row.language,
"gender": row.gender, "description": row.description, "tuning": row.tuning or {},
"version": row.version, "is_default": row.is_default, "is_active": row.is_active,
"tags": row.tags or [], "assets": _media(db, "voice", row.id) if media is None else media,
"updated_at": row.updated_at,
"created_at": getattr(row, "created_at", None),
}
# ---------------------------------------------------------------- 检索 / 统计辅助
SORTS = {"updated": "按更新时间", "created": "按创建时间", "name": "按名称",
"media": "按素材数量", "size": "按素材体积", "usage": "按被引用项目数"}
def _flat_media(rows: list) -> list[dict]:
out: list[dict] = []
for row in rows:
out.extend(row.get("assets") or [])
return out
def _media_grouped(db: Session, owner_type: str, ids: list[int]) -> dict[int, list[dict]]:
"""一次查询取回这一类母版的全部媒体,替代逐个母版的 N+1 查询。"""
grouped: dict[int, list[dict]] = {}
if not ids:
return grouped
rows = db.query(PlatformMedia).filter(
PlatformMedia.owner_type == owner_type, PlatformMedia.owner_id.in_(ids),
).order_by(PlatformMedia.owner_id, PlatformMedia.id).all()
for row in rows:
grouped.setdefault(row.owner_id, []).append(storage.platform_media_dict(row))
return grouped
def _haystack(item: dict) -> str:
"""跨字段搜索的匹配范围:名称/编号/标签/描述/提示词 + 素材名,全部小写。"""
parts = [str(item.get(key) or "") for key in
("name", "code", "role", "appearance", "personality", "persona_prompt", "voice_profile",
"description", "prompt", "lighting", "palette", "language", "gender", "provider",
"notes", "kind_label")]
parts.extend(str(tag) for tag in (item.get("tags") or []))
parts.extend(str(m.get("name") or "") for m in (item.get("assets") or []))
return "\n".join(parts).lower()
def _media_stats(items: list[dict]) -> dict:
media = _flat_media(items)
# 「素材类型」下拉要跟着左侧大分类走:人物下面不该出现「音色样本 / 场景图」,
# 所以除了全库 by_kind,再按 owner_type 拆一份计数给前端筛选项用。
kinds_by_owner: dict[str, Counter] = {}
for item in items:
counter = kinds_by_owner.setdefault(str(item.get("owner_type") or "unknown"), Counter())
for media_row in (item.get("assets") or []):
counter[str(media_row.get("kind") or "unknown")] += 1
return {
"masters": len(items),
"media_count": len(media),
"media_bytes": sum(int(m.get("size_bytes") or 0) for m in media),
# PlatformMediaVersion 首版就落一行,因此 versions 列表长度即总版本数
"versions": sum(max(1, len(m.get("versions") or [])) for m in media),
"without_media": sum(1 for item in items if not (item.get("assets") or [])),
"by_kind": dict(Counter(m.get("kind") or "unknown" for m in media)),
# 素材墙按全库口径,母版视角按「当前大分类」口径,两者分开给
"by_kind_by_owner": {key: dict(counter) for key, counter in kinds_by_owner.items()},
# 左侧类型导航要显示「全库」计数,而不是当前筛选页的计数
"by_owner": dict(Counter(str(i.get("owner_type") or "unknown") for i in items)),
"media_by_owner": dict(Counter(str(i.get("owner_type") or "unknown") for i in items
for _ in (i.get("assets") or [None]))),
}
def _usage_index(db: Session) -> dict[tuple[str, int], list[dict]]:
"""平台母版 → 引用它的项目资产。项目侧三张表都带 platform_source_* 外键。"""
usage: dict[tuple[str, int], list[dict]] = {}
projects = {p.id: p.name for p in db.scalars(select(Project)).all()}
targets = ((Character, "human", "人物"), (Scene, "scene", "场景"), (VoiceAsset, "voice", "声线"))
for model, key, label in targets:
rows = db.query(model).filter(model.platform_source_id.isnot(None)).all()
for row in rows:
usage.setdefault((key, int(row.platform_source_id)), []).append({
"project_id": row.project_id,
"project_name": projects.get(row.project_id, f"项目 #{row.project_id}"),
"asset_type": key, "asset_type_label": label, "asset_id": row.id,
"asset_name": row.name,
"pulled_version": row.platform_source_version or "",
})
return usage
def _duplicate_groups(items: list[dict]) -> list[dict]:
"""疑似重复:同类型下名称(或编号)归一化后相同。只提示、不自动合并。"""
buckets: dict[tuple, list[dict]] = {}
for item in items:
norm = re.sub(r"[\s_\-·]+", "", str(item.get("name") or "")).lower()
if not norm:
continue
buckets.setdefault((item["owner_type"], norm), []).append(item)
groups = []
for (owner_type, norm), rows in buckets.items():
if len(rows) < 2:
continue
groups.append({
"owner_type": owner_type, "key": norm,
"name": rows[0].get("name") or "",
"ids": [{"owner_type": r["owner_type"], "asset_id": r["id"], "name": r.get("name") or "",
"media_count": len(r.get("assets") or [])} for r in rows],
})
return sorted(groups, key=lambda g: -len(g["ids"]))
def _mark_duplicates(items: list[dict], groups: list[dict]) -> None:
"""给每个母版标记 duplicate_of(同组其他母版的 id),供前端做「疑似重复」提示。"""
by_id = {(int(entry["asset_id"]), entry["owner_type"]): group
for group in groups for entry in group["ids"]}
for item in items:
group = by_id.get((int(item["id"]), item["owner_type"])) or {}
item["duplicate_of"] = [entry["asset_id"] for entry in group.get("ids", [])
if int(entry["asset_id"]) != int(item["id"])]
def _tag_index(items: list[dict]) -> list[dict]:
counter: Counter = Counter()
for item in items:
for tag in (item.get("tags") or []):
counter[str(tag)] += 1
return [{"tag": tag, "count": count} for tag, count in
sorted(counter.items(), key=lambda kv: (-kv[1], kv[0]))]
def _collect(db: Session) -> list[dict]:
"""把所有平台母版摊平成一个列表,并带上媒体、引用、版本信息。"""
items: list[dict] = []
usage = _usage_index(db)
specs = (("human", PlatformCharacter, _human_dict),
("scene", PlatformScene, _scene_dict),
("voice", PlatformVoice, _voice_dict))
for owner_type, model, serializer in specs:
rows = db.scalars(select(model).order_by(model.id.desc())).all()
grouped = _media_grouped(db, owner_type, [row.id for row in rows])
for row in rows:
payload = serializer(db, row, grouped.get(row.id, []))
payload["owner_type"] = owner_type
payload["type_label"] = _type_label(owner_type)
refs = usage.get((owner_type, int(row.id)), [])
payload["usage"] = refs
payload["usage_count"] = len({r["project_id"] for r in refs})
payload["media_count"] = len(payload.get("assets") or [])
payload["media_bytes"] = sum(int(m.get("size_bytes") or 0) for m in (payload.get("assets") or []))
payload["latest_media_at"] = max([str(m.get("created_at") or "") for m in (payload.get("assets") or [])]
or [str(payload.get("updated_at") or "")])
items.append(payload)
return items
@router.get("/api/platform/assets")
def list_platform_assets(db: Session = Depends(get_db), q: str = "", tag: str = "", kind: str = "",
owner_type: str = "", status: str = "", has_media: str = "",
duplicate_only: bool = False, sort: str = "updated",
page: int = 1, page_size: int = 0):
"""平台母版列表。
资源库要按「找一张图」的姿势检索,而不只是按名字翻卡片,因此这里一次性支持
跨字段搜索 / 标签 / 素材类型 / 有无素材 / 启用状态 / 疑似重复 / 排序 / 分页,
并把每个母版的引用关系(usage_count)和版本(assets[].versions)一起回吐。
"""
items = _collect(db)
stats_all = _media_stats(items)
groups = _duplicate_groups(items)
_mark_duplicates(items, groups)
tags = _tag_index(items)
needle = q.strip().lower()
filtered = []
for item in items:
if owner_type and owner_type not in ("all", "any", item["owner_type"]):
continue
if needle and needle not in _haystack(item):
continue
if tag and tag not in [str(t) for t in (item.get("tags") or [])]:
continue
if kind and not any(m.get("kind") == kind for m in (item.get("assets") or [])):
continue
if has_media == "with" and not item["media_count"]:
continue
if has_media == "without" and item["media_count"]:
continue
if status == "active" and not item.get("is_active", True):
continue
if status == "inactive" and item.get("is_active", True):
continue
if duplicate_only and not item.get("duplicate_of"):
continue
filtered.append(item)
reverse = sort in ("updated", "created", "media", "size", "usage")
key_fn = {
"updated": lambda i: str(i.get("updated_at") or ""),
"created": lambda i: str(i.get("created_at") or i.get("updated_at") or ""),
"name": lambda i: str(i.get("name") or ""),
"media": lambda i: int(i.get("media_count") or 0),
"size": lambda i: int(i.get("media_bytes") or 0),
"usage": lambda i: int(i.get("usage_count") or 0),
}.get(sort)
if key_fn is None:
filtered.sort(key=lambda i: (i["owner_type"], -i["id"]))
else:
filtered.sort(key=key_fn, reverse=reverse)
total = len(filtered)
pages = 1
if page_size and page_size > 0:
pages = max(1, (total + page_size - 1) // page_size)
current = min(max(1, page), pages)
filtered = filtered[(current - 1) * page_size:current * page_size]
else:
current = 1
buckets: dict[str, list] = {"human": [], "scene": [], "voice": []}
for item in filtered:
buckets[item["owner_type"]].append(item)
return {
**buckets,
"items": filtered,
"counts": {key: len(rows) for key, rows in buckets.items()},
"totals": stats_all,
"stats": {**stats_all,
"duplicate_groups": len(groups),
"duplicate_masters": sum(len(g["ids"]) for g in groups),
"referenced": sum(1 for i in items if i.get("usage_count")),
"tags": len(tags)},
"duplicates": groups,
"tags": tags,
"sorts": [{"value": key, "label": label} for key, label in SORTS.items()],
"filtered": total, "page": current, "page_size": page_size, "pages": pages,
}
@router.get("/api/platform/media")
def list_platform_media(db: Session = Depends(get_db), q: str = "", kind: str = "",
owner_type: str = "", owner_id: int = 0, multi_version_only: bool = False,
sort: str = "created", page: int = 1, page_size: int = 120):
"""跨母版的「全部素材」视角:找一张定妆图不必先猜它挂在哪个角色下。"""
rows = db.query(PlatformMedia).order_by(PlatformMedia.id.desc()).all()
names = {("human", row.id): row.name for row in db.scalars(select(PlatformCharacter)).all()}
names.update({("scene", row.id): row.name for row in db.scalars(select(PlatformScene)).all()})
names.update({("voice", row.id): row.name for row in db.scalars(select(PlatformVoice)).all()})
items: list[dict] = []
for row in rows:
if owner_type and owner_type not in ("all", "any", row.owner_type):
continue
if owner_id and row.owner_id != owner_id:
continue
if kind and row.kind != kind:
continue
if multi_version_only and (row.current_version or 1) < 2:
continue
payload = storage.platform_media_dict(row)
if q and q.strip().lower() not in f"{payload.get('name','')}\n{payload.get('note','')}\n{payload.get('kind','')}\n{names.get((row.owner_type, row.owner_id),'')}".lower():
continue
payload.update({
"owner_type": row.owner_type, "owner_id": row.owner_id,
"owner_name": names.get((row.owner_type, row.owner_id), f"{row.owner_type}#{row.owner_id}"),
"type_label": _type_label(row.owner_type),
"version_count": max(int(payload.get("version") or 1), len(payload.get("versions") or []), 1),
})
items.append(payload)
key_fn = {"created": lambda i: (str(i.get("created_at") or ""), int(i.get("id") or 0)),
"size": lambda i: int(i.get("size_bytes") or 0),
"name": lambda i: str(i.get("name") or "").lower(),
"version": lambda i: int(i.get("version") or 1)}.get(sort)
items.sort(key=key_fn or (lambda i: int(i.get("id") or 0)), reverse=sort != "name")
total = len(items)
pages = max(1, (total + page_size - 1) // page_size) if page_size else 1
current = min(max(1, page), pages)
page_items = items[(current - 1) * page_size:current * page_size] if page_size else items
return {
"items": page_items, "total": total, "page": current, "page_size": page_size, "pages": pages,
"stats": {
"count": total, "bytes": sum(int(i.get("size_bytes") or 0) for i in items),
"versions": sum(int(i.get("version") or 1) for i in items),
"multi_version": sum(1 for i in items if int(i.get("version") or 1) > 1),
"by_kind": dict(Counter(str(i.get("kind") or "unknown") for i in items)),
"by_owner": dict(Counter(str(i.get("owner_type") or "unknown") for i in items)),
},
"kinds": [{"value": value, "label": AssetKind.LABELS.get(value, value)}
for value in sorted({str(i.get("kind") or "") for i in items} - {""})],
}
@router.post("/api/platform/characters")
def create_platform_character(data: schemas.PlatformCharacterCreate, db: Session = Depends(get_db)):
payload = data.model_dump()
if payload.get("default_voice_id") and db.get(PlatformVoice, payload["default_voice_id"]) is None:
raise HTTPException(400, "默认声线不存在")
row = PlatformCharacter(**payload, project_id=None)
db.add(row)
db.flush()
events.log_event(db, "platform.character.created", message=f"平台人物「{row.name}」已创建",
entity_type="platform_character", entity_id=row.id)
db.commit()
return {"ok": True, "result": _human_dict(db, row)}
@router.patch("/api/platform/characters/{asset_id}")
def update_platform_character(asset_id: int, data: schemas.PlatformCharacterUpdate,
db: Session = Depends(get_db)):
row = _owner(db, "human", asset_id)
patch = data.model_dump(exclude_unset=True)
if patch.get("default_voice_id") and db.get(PlatformVoice, patch["default_voice_id"]) is None:
raise HTTPException(400, "默认声线不存在")
for key, value in patch.items():
setattr(row, key, value)
db.commit()
return {"ok": True, "result": _human_dict(db, row)}
@router.post("/api/platform/scenes")
def create_platform_scene(data: schemas.PlatformSceneCreate, db: Session = Depends(get_db)):
row = PlatformScene(**data.model_dump())
db.add(row)
db.flush()
events.log_event(db, "platform.scene.created", message=f"平台场景「{row.name}」已创建",
entity_type="platform_scene", entity_id=row.id)
db.commit()
return {"ok": True, "result": _scene_dict(db, row)}
@router.patch("/api/platform/scenes/{asset_id}")
def update_platform_scene(asset_id: int, data: schemas.PlatformSceneUpdate,
db: Session = Depends(get_db)):
row = _owner(db, "scene", asset_id)
for key, value in data.model_dump(exclude_unset=True).items():
setattr(row, key, value)
db.commit()
return {"ok": True, "result": _scene_dict(db, row)}
@router.post("/api/platform/voices")
def create_platform_voice(data: schemas.PlatformVoiceCreate, db: Session = Depends(get_db)):
payload = data.model_dump()
if payload.get("is_default"):
db.query(PlatformVoice).filter(PlatformVoice.is_default.is_(True)).update({"is_default": False})
row = PlatformVoice(**payload)
db.add(row)
db.flush()
events.log_event(db, "platform.voice.created", message=f"平台声线「{row.name}」已创建",
entity_type="platform_voice", entity_id=row.id)
db.commit()
return {"ok": True, "result": _voice_dict(db, row)}
@router.patch("/api/platform/voices/{asset_id}")
def update_platform_voice(asset_id: int, data: schemas.PlatformVoiceUpdate,
db: Session = Depends(get_db)):
row = _owner(db, "voice", asset_id)
patch = data.model_dump(exclude_unset=True)
if patch.get("is_default"):
db.query(PlatformVoice).filter(PlatformVoice.id != asset_id,
PlatformVoice.is_default.is_(True)).update({"is_default": False})
for key, value in patch.items():
setattr(row, key, value)
db.commit()
return {"ok": True, "result": _voice_dict(db, row)}
@router.post("/api/platform/{owner_type}/{asset_id}/media")
async def upload_platform_media(owner_type: str, asset_id: int, kind: str = Form(...),
name: str = Form(""), note: str = Form(""),
file: UploadFile = File(...), db: Session = Depends(get_db)):
owner_type = _normalize_type(owner_type)
_owner(db, owner_type, asset_id)
if kind not in PLATFORM_KINDS.get(owner_type, set()):
raise HTTPException(400, f"{owner_type} 不接受资产位 {kind}")
info = await storage.save_platform_upload(
file, owner_type=owner_type, owner_id=asset_id, kind=kind, stem=f"{owner_type}-{asset_id}-{kind}",
media_group="audio" if owner_type == "voice" and kind == "reference" else "",
)
media = storage.register_platform_media(
db, owner_type=owner_type, owner_id=asset_id, kind=kind, path=info["path"],
# note 以前被吞掉了:页面录音靠它留下「这段是录的、不是传的」的来源痕迹
name=name or info["original_name"], note=note, source="upload", info=info,
)
db.commit()
return {"ok": True, "result": storage.platform_media_dict(media)}
@router.delete("/api/platform/media/{media_id}")
def delete_platform_media(media_id: int, db: Session = Depends(get_db)):
media = db.get(PlatformMedia, media_id)
if media is None:
raise HTTPException(404, "平台媒体不存在")
paths = {media.path, *(version.path for version in media.versions)}
owner_type, owner_id = media.owner_type, media.owner_id
if owner_type == "voice":
voice = db.get(PlatformVoice, owner_id)
if voice is not None and voice.provider_voice_id in paths:
# 删除参考音频后清掉失效绑定,生成时回退到模型配置的默认参考音频。
voice.provider_voice_id = ""
db.delete(media)
db.commit()
removed = [path for path in paths if storage.delete_media_file(path)]
events.log_event(db, "platform.media.deleted",
message=f"删除平台媒体 #{media_id}",
entity_type=f"platform_{owner_type}", entity_id=owner_id,
data={"media_id": media_id, "files_removed": len(removed)})
db.commit()
return {"ok": True, "deleted": media_id, "files_removed": len(removed)}
@router.post("/api/platform/media/{media_id}/versions/{version_no}/current")
def set_platform_media_current(media_id: int, version_no: int, data: schemas.VersionAction | None = None,
db: Session = Depends(get_db)):
"""把平台素材的某个历史版本设为当前版本(下游取图/引入项目都跟着切换)。"""
try:
result = versions.set_current(db, versions.PLATFORM_MEDIA, media_id, version_no,
actor=(data.actor if data else "ui"))
except versions.VersionError as exc:
raise HTTPException(exc.status_code, exc.detail)
return {"ok": True, "result": result}
@router.delete("/api/platform/media/{media_id}/versions/{version_no}")
def delete_platform_media_version(media_id: int, version_no: int, data: schemas.VersionAction | None = None,
db: Session = Depends(get_db)):
"""删除平台素材的某个版本;删的正是当前版本时自动回落到剩余最新版本。"""
try:
result = versions.delete_version(db, versions.PLATFORM_MEDIA, media_id, version_no,
actor=(data.actor if data else "ui"))
except versions.VersionError as exc:
raise HTTPException(exc.status_code, exc.detail)
return {"ok": True, "result": result}
@router.delete("/api/platform/{owner_type}/{asset_id}")
def delete_platform_asset(owner_type: str, asset_id: int, db: Session = Depends(get_db)):
owner_type = _normalize_type(owner_type)
row = _owner(db, owner_type, asset_id)
masters = db.query(PlatformMedia).filter(
PlatformMedia.owner_type == owner_type, PlatformMedia.owner_id == asset_id,
).all()
media_paths = {
path for media in masters
for path in (media.path, *(version.path for version in media.versions))
}
media_ids = [media.id for media in masters]
owner_name = str(getattr(row, "name", "") or f"{owner_type}#{asset_id}")
if owner_type == "voice":
db.query(PlatformCharacter).filter(PlatformCharacter.default_voice_id == asset_id).update(
{"default_voice_id": None}
)
# 批量 delete 是纯 SQL,不会触发 PlatformMedia.versions 上的
# cascade="all, delete-orphan",SQLite 也没开 PRAGMA foreign_keys,
# 于是 platform_media_versions 会留下孤儿行;而 platform_media.id 没有
# AUTOINCREMENT,删掉最大行后新母版会复用同一个 id,孤儿版本行随即
# 撞 uq_platform_media_version 唯一约束,让下一次生成/上传直接 500。
# 因此必须先显式删子表,再删父表。
if media_ids:
db.query(PlatformMediaVersion).filter(PlatformMediaVersion.media_id.in_(media_ids)).delete(
synchronize_session=False)
db.query(PlatformMedia).filter(PlatformMedia.owner_type == owner_type,
PlatformMedia.owner_id == asset_id).delete()
db.delete(row)
db.commit()
removed = [path for path in media_paths if storage.delete_media_file(path)]
events.log_event(db, "platform.asset.deleted", commit=True,
message=f"删除平台母版「{owner_name}」,清理 {len(removed)} 个文件",
entity_type=f"platform_{owner_type}", entity_id=asset_id,
data={"files_removed": len(removed)})
return {"ok": True, "deleted": asset_id, "files_removed": len(removed)}
def _version_of(owner) -> str:
raw = getattr(owner, "style_version", None) or getattr(owner, "version", None)
if raw:
return str(raw)
updated = getattr(owner, "updated_at", None) or now_utc()
return updated.isoformat(timespec="seconds")
def _snapshot(owner) -> dict:
return {
column.name: getattr(owner, column.name)
for column in owner.__table__.columns
if column.name not in {"created_at", "updated_at"}
}
def _copy_media(db: Session, project: Project, local_type: str, local_id: int,
platform_type: str, platform_id: int) -> int:
rows = db.query(PlatformMedia).filter(
PlatformMedia.owner_type == platform_type, PlatformMedia.owner_id == platform_id,
).all()
copied = 0
for media in rows:
source = storage.to_absolute(media.path)
if not source.exists():
continue
info = storage.save_bytes(
source.read_bytes(), project_id=project.id, kind=media.kind,
stem=f"{local_type}-{local_id}-{media.kind}-platform", suffix=source.suffix,
original_name=source.name,
)
storage.register_asset(
db, project_id=project.id, owner_type=local_type, owner_id=local_id,
kind=media.kind, path=info["path"], name=media.name, note=media.note,
source="platform", info=info,
metadata={"platform_media_id": media.id, "platform_version": media.current_version,
# 出身必须跟着走:AI 试听样本一旦在复制中被洗白成「真人参考」,
# 下次生成就会克隆它自己的输出,错音色再也翻不出来(见 services/timbre.py)
**timbre.provenance_of(media)},
)
copied += 1
return copied
def _import_voice(db: Session, project: Project, platform_id: int) -> VoiceAsset:
source = db.get(PlatformVoice, platform_id)
if source is None:
raise HTTPException(404, "平台声线不存在")
row = db.query(VoiceAsset).filter(
VoiceAsset.project_id == project.id, VoiceAsset.platform_source_type == "voice",
VoiceAsset.platform_source_id == source.id,
).first()
fields = {
"name": source.name, "provider": source.provider,
"provider_voice_id": source.provider_voice_id, "language": source.language,
"gender": source.gender, "description": source.description,
"tuning": source.tuning or {}, "version": source.version,
"platform_source_type": "voice", "platform_source_id": source.id,
"platform_source_version": _version_of(source), "platform_snapshot_json": _snapshot(source),
}
if row is None:
row = VoiceAsset(project_id=project.id, **fields)
db.add(row)
db.flush()
else:
for key, value in fields.items():
setattr(row, key, value)
_copy_media(db, project, "voice", row.id, "voice", source.id)
if source.provider == "comfyui":
# IndexTTS2 的项目副本必须使用复制后的本地音色文件,不能继续指回平台目录。
# 平台母版常常只有「试听样本」而没有单独的 reference 素材,两者都能当克隆参考:
# 只认 reference 会让这条声线看起来有音色、实际配音却回落成陌生人的嗓子。
latest = audio.current_voice_audio(db, row)
if latest:
row.provider_voice_id = latest
return row
@router.post("/api/platform/assets/batch-delete")
def batch_delete_platform_assets(data: schemas.PlatformBatchDelete, db: Session = Depends(get_db)):
"""批量删除母版;逐个复用单条删除逻辑,失败不中断整批。"""
deleted: list[dict] = []
failures: list[dict] = []
for ref in data.items:
try:
result = delete_platform_asset(ref.owner_type, ref.asset_id, db=db)
deleted.append({"owner_type": _normalize_type(ref.owner_type), "asset_id": ref.asset_id,
"files_removed": result.get("files_removed", 0)})
except HTTPException as exc:
failures.append({"owner_type": ref.owner_type, "asset_id": ref.asset_id, "detail": exc.detail})
events.log_event(db, "platform.asset.batch_deleted",
message=f"批量删除平台母版 {len(deleted)} 个",
entity_type="platform_asset", data={"deleted": len(deleted), "failed": len(failures)})
db.commit()
return {"ok": True, "result": {"deleted": deleted, "deleted_count": len(deleted), "failures": failures}}
@router.post("/api/platform/media/batch-delete")
def batch_delete_platform_media(data: schemas.PlatformMediaBatchDelete, db: Session = Depends(get_db)):
"""批量删除素材(媒体墙多选后的主操作)。"""
deleted: list[int] = []
failures: list[dict] = []
files = 0
for media_id in data.media_ids:
try:
result = delete_platform_media(int(media_id), db=db)
deleted.append(int(media_id))
files += int(result.get("files_removed") or 0)
except HTTPException as exc:
failures.append({"media_id": media_id, "detail": exc.detail})
events.log_event(db, "platform.media.batch_deleted",
message=f"批量删除平台素材 {len(deleted)} 个",
entity_type="platform_media", data={"count": len(deleted), "files_removed": files})
db.commit()
return {"ok": True, "result": {"deleted": deleted, "deleted_count": len(deleted),
"files_removed": files, "failures": failures}}
@router.post("/api/platform/assets/batch-tag")
def batch_tag_platform_assets(data: schemas.PlatformBatchTag, db: Session = Depends(get_db)):
"""批量打标签 / 取消标签,用于把同一批母版归到同一个风格或项目线。"""
changed: list[dict] = []
for ref in data.items:
row = _owner(db, ref.owner_type, ref.asset_id)
current = [str(t) for t in (getattr(row, "tags", None) or [])]
merged = list(dict.fromkeys(current + [str(t).strip() for t in data.add if str(t).strip()]))
merged = [t for t in merged if t not in {str(t).strip() for t in data.remove}]
row.tags = merged
changed.append({"owner_type": _normalize_type(ref.owner_type), "asset_id": ref.asset_id, "tags": merged})
db.commit()
events.log_event(db, "platform.asset.batch_tagged",
message=f"批量整理标签 {len(changed)} 个母版",
entity_type="platform_asset",
data={"add": data.add, "remove": data.remove, "count": len(changed)})
db.commit()
return {"ok": True, "result": {"items": changed, "count": len(changed)}}
@router.post("/api/platform/assets/batch-import")
def batch_import_platform_assets(data: schemas.PlatformBatchImport, db: Session = Depends(get_db)):
"""批量引入到项目:复用单条引入,逐条记录结果,便于「一键补齐缺失母版」。"""
deps.get_project(db, data.project_id)
imported: list[dict] = []
failures: list[dict] = []
for ref in data.items:
try:
result = import_into_project(schemas.PlatformImport(
project_id=data.project_id, asset_type=_normalize_type(ref.owner_type),
asset_id=ref.asset_id), db=db)
imported.append(result.get("result") or {})
except HTTPException as exc:
failures.append({"owner_type": ref.owner_type, "asset_id": ref.asset_id, "detail": exc.detail})
events.log_event(db, "platform.asset.batch_imported", project_id=data.project_id,
message=f"批量引入平台母版 {len(imported)} 个",
entity_type="platform_asset",
data={"count": len(imported), "failed": len(failures)})
db.commit()
return {"ok": True, "result": {"imported": imported, "imported_count": len(imported),
"failures": failures}}
@router.post("/api/platform/import")
def import_into_project(data: schemas.PlatformImport, db: Session = Depends(get_db)):
project = deps.get_project(db, data.project_id)
owner_type = data.asset_type
source = _owner(db, owner_type, data.asset_id)
media_count = 0
if owner_type == "human":
local = db.query(Character).filter(
Character.project_id == project.id, Character.platform_source_type == "human",
Character.platform_source_id == source.id,
).first()
local_voice = None
if source.default_voice_id:
local_voice = _import_voice(db, project, source.default_voice_id)
fields = {
"name": data.name or source.name, "code": source.code, "role": source.role,
"personality": source.personality, "appearance": source.appearance,
"persona_prompt": source.persona_prompt, "voice_profile": source.voice_profile,
"default_voice_id": local_voice.id if local_voice else None,
"style_version": source.style_version, "tags": source.tags or [],
"notes": source.notes, "is_active": source.is_active,
"platform_source_type": "human", "platform_source_id": source.id,
"platform_source_version": _version_of(source), "platform_snapshot_json": _snapshot(source),
}
if local is None:
local = Character(project_id=project.id, **fields)
db.add(local)
db.flush()
else:
for key, value in fields.items():
setattr(local, key, value)
media_count = _copy_media(db, project, "human", local.id, "human", source.id)
elif owner_type == "scene":
local = db.query(Scene).filter(
Scene.project_id == project.id, Scene.platform_source_type == "scene",
Scene.platform_source_id == source.id,
).first()
fields = {
"name": data.name or source.name, "code": source.code, "description": source.description,
"prompt": source.prompt, "lighting": source.lighting, "time_of_day": source.time_of_day,
"palette": source.palette, "tags": source.tags or [],
"platform_source_type": "scene", "platform_source_id": source.id,
"platform_source_version": _version_of(source), "platform_snapshot_json": _snapshot(source),
}
if local is None:
local = Scene(project_id=project.id, **fields)
db.add(local)
db.flush()
else:
for key, value in fields.items():
setattr(local, key, value)
media_count = _copy_media(db, project, "scene", local.id, "scene", source.id)
else:
local = _import_voice(db, project, source.id)
if data.name:
local.name = data.name
media_count = len(_media(db, "voice", source.id))
db.commit()
events.log_event(db, "platform.asset.imported", project_id=project.id,
message=f"从平台引入{_type_label(owner_type)}「{local.name}」",
entity_type=owner_type, entity_id=local.id,
data={"platform_asset_id": source.id, "snapshot": _version_of(source),
"media_count": media_count})
return {"ok": True, "result": {"asset_type": owner_type, "id": local.id, "name": local.name,
"platform_source_id": source.id,
"platform_source_version": _version_of(source),
"media_count": media_count}}
def _project_owner(db: Session, project_id: int, owner_type: str, owner_id: int):
if owner_type == "human":
return deps.get_character(db, project_id, owner_id)
if owner_type == "scene":
return deps.get_scene(db, project_id, owner_id)
return deps.get_voice(db, project_id, owner_id)
_PUBLISH_FIELDS = {
"human": ("code", "role", "personality", "appearance", "persona_prompt", "voice_profile",
"style_version", "tags", "notes", "is_active"),
"scene": ("code", "description", "prompt", "lighting", "time_of_day", "palette", "tags"),
"voice": ("code", "provider", "provider_voice_id", "language", "gender", "description",
"tuning", "version"),
}
def _platform_twin(db: Session, asset_type: str, source):
"""同名(code 都存在时必须相同)的既有平台母版,取 id 最小者,用于发布幂等复用。"""
model = {"human": PlatformCharacter, "scene": PlatformScene, "voice": PlatformVoice}[asset_type]
name = (getattr(source, "name", "") or "").strip()
if not name:
return None
code = (getattr(source, "code", "") or "").strip()
for row in sorted(db.scalars(select(model)).all(), key=lambda r: r.id):
if (row.name or "").strip() != name:
continue
row_code = (getattr(row, "code", "") or "").strip()
if code and row_code and row_code != code:
continue
return row
return None
@router.post("/api/platform/publish")
def publish_project_asset(data: schemas.PlatformPublish, db: Session = Depends(get_db)):
project = deps.get_project(db, data.project_id)
source = _project_owner(db, project.id, data.asset_type, data.asset_id)
twin = _platform_twin(db, data.asset_type, source) if data.reuse else None
if data.asset_type == "human":
row = PlatformCharacter(
name=source.name, project_id=None, code=source.code, role=source.role,
personality=source.personality, appearance=source.appearance,
persona_prompt=source.persona_prompt, voice_profile=source.voice_profile,
style_version=source.style_version, tags=source.tags or [], notes=source.notes,
is_active=source.is_active,
)
elif data.asset_type == "scene":
row = PlatformScene(
name=source.name, code=source.code, description=source.description,
prompt=source.prompt, lighting=source.lighting, time_of_day=source.time_of_day,
palette=source.palette, tags=source.tags or [],
)
else:
row = PlatformVoice(
name=source.name, code=source.name, provider=source.provider,
provider_voice_id=source.provider_voice_id, language=source.language,
gender=source.gender, description=source.description, tuning=source.tuning or {},
version=source.version,
)
if twin is not None:
row = twin
for field in _PUBLISH_FIELDS[data.asset_type]:
if not hasattr(row, field):
continue
value = getattr(source, field, None)
if value is not None:
setattr(row, field, value)
db.add(row)
db.flush()
local_media = db.query(Asset).filter(Asset.owner_type == data.asset_type,
Asset.owner_id == source.id).all()
for asset in local_media:
src = storage.to_absolute(asset.path)
if not src.exists():
continue
info = storage.save_platform_local_file(
src, owner_type=data.asset_type, owner_id=row.id, kind=asset.kind,
stem=f"{data.asset_type}-{row.id}-{asset.kind}",
)
storage.register_platform_media(
db, owner_type=data.asset_type, owner_id=row.id, kind=asset.kind,
path=info["path"], name=asset.name, note=asset.note, source="project",
info=info, metadata={"project_id": project.id, "project_asset_id": asset.id,
**timbre.provenance_of(asset)},
)
db.commit()
events.log_event(
db, "platform.asset.published", project_id=project.id,
message=(f"项目{_type_label(data.asset_type)}「{source.name}」复用同名平台母版 #{row.id} 并升版"
if twin is not None else
f"项目{_type_label(data.asset_type)}「{source.name}」已上架平台资产库"),
entity_type="platform_" + data.asset_type, entity_id=row.id,
data={"reused": twin is not None, "source_project_id": project.id})
versions = [m.get("version") or 1 for m in _media(db, data.asset_type, row.id)]
return {"ok": True, "result": {"asset_type": data.asset_type, "id": row.id, "name": row.name,
"reused": twin is not None,
"media_count": len(versions),
"media_version": max(versions) if versions else 0}}
def produced_media_metadata(media, reference: dict, check: dict, text: str) -> None:
"""把「这次到底用了谁的嗓子、念了什么、音色对不对」写进素材,界面才说得清楚。"""
metadata = dict(media.metadata_json or {})
metadata.update({
"text": text,
"reference_audio": reference.get("path") or reference.get("attached") or "",
"reference_source": reference.get("source") or "",
"reference_note": reference.get("note") or "使用本声线已绑定的参考音频",
"timbre_check": check,
})
media.metadata_json = metadata
note = str(media.note or "")
tail = check.get("note") or ""
if tail and "音色自检" not in note:
media.note = f"{note}|音色自检:{tail}".strip("|")
def _owner_prompt(project: Project | None, owner_type: str, owner, kind: str, custom: str) -> str:
if custom.strip():
return custom.strip()
bits = []
if owner_type == "human":
bits = [f"{owner.name},电影级角色设定图", owner.appearance, owner.persona_prompt,
"五官、发型、体型、服装与配饰保持一致,正面清晰,材质细节可信"]
elif owner_type == "scene":
bits = [f"{owner.name},电影级场景概念图", owner.prompt or owner.description,
owner.lighting, owner.time_of_day, owner.palette,
"空间结构清晰,透视准确,建筑与陈设保持一致"]
if project:
bits.append(project.style_guide)
return "。".join(str(part).strip() for part in bits if part)
@router.get("/api/platform/{owner_type}/{asset_id}/reference-status")
def platform_reference_status(owner_type: str, asset_id: int, db: Session = Depends(get_db)):
"""开「生成试听」抽屉时先问一句:现在这点生成会拿到谁的嗓子。
音色由参考音频决定,而表单里的性别/描述对 zero-shot 克隆模型只是元数据,
所以这个预告必须在**点生成之前**就给出来,而不是等试听样本出来才发现是女声。
"""
owner_type = _normalize_type(owner_type)
if owner_type != "voice":
raise HTTPException(422, "只有声线需要参考音频状态")
owner = _owner(db, owner_type, asset_id)
spec, _adapter = model_registry.resolve_voice(db, None, None)
return {"ok": True, "result": timbre.reference_status(
db, owner, clone=timbre.clone_model(spec))}
@router.post("/api/platform/{owner_type}/{asset_id}/generate")
def generate_platform_asset(owner_type: str, asset_id: int, data: schemas.PlatformGenerate,
db: Session = Depends(get_db)):
owner_type = _normalize_type(owner_type)
owner = _owner(db, owner_type, asset_id)
if owner_type == "voice":
spec, _adapter = model_registry.resolve_voice(db, data.provider or None, data.model or None)
if not spec.enabled:
raise HTTPException(409, f"模型 {spec.provider}/{spec.model} 已停用")
reference_ids = list(data.reference_media_ids)
if data.reference_media_id:
reference_ids.append(data.reference_media_id)
references = _reference_media(db, "voice", owner.id, reference_ids, kind="audio")
reference_media = references[0] if references else None
# 试听文本是「模型要念出来的台词」,不是声线设定。旧实现拿 owner.description 兜底,
# 于是用户填的「温和、语速中等,沉稳」被当成台词念了出来,试听样本一听就是设定说明文。
text = (data.sample_text or "").strip()
if not text:
raise HTTPException(422, "请先填写试听文本:它是模型要朗读的台词,"
"系统不会用「音色描述」代替")
out = storage.platform_dir("voice", owner.id, "generated") / f"sample-{now_utc().timestamp():.0f}.wav"
# 参考音频决定音色,而表单里的「性别」对 IndexTTS2 只是元数据。
# 旧实现缺参考时直接回退到模型默认参考(女声),男声声线就这样被生成成了女声。
reference = timbre.resolve_reference(
db, owner,
explicit=str(storage.to_absolute(reference_media.path)) if reference_media else "",
clone=timbre.clone_model(spec))
if not reference["path"] and reference.get("source") == "none":
raise HTTPException(409, reference["note"])
# 交给模型的是绝对路径(供应商要直接读文件);声线列里存 media_root 相对路径,
# 换机器 / 进容器时不会因为写死绝对路径而失效。与任务链路 handlers 保持一致。
reference_path = reference["path"] or owner.provider_voice_id
request = VoiceGenRequest(
text=text, voice_id=owner.name, provider_voice_id=reference_path,
language=owner.language, speed=float((owner.tuning or {}).get("speed", 1.0)),
seed=data.seed, params=(owner.tuning or {}).get("params") or {},
idempotency_key=f"platform-voice:{owner.id}:{asset_id}",
label=f"PLATFORM-VOICE-{owner.id}", output_path=out,
)
try:
result = model_registry.generate_voice(db, request, spec.provider, spec.model)
except ProviderError as exc:
raise HTTPException(502, f"{exc.message}({exc.code})") from exc
kind = "voice_sample"
owner.provider = spec.provider
if reference["path"] and timbre.clone_model(spec):
# 把真正参与克隆的那段参考音频绑回声线:否则试听样本是 A 的嗓子,
# 之后项目配音又落回模型默认参考,变成另一种嗓子。
owner.provider_voice_id = str(storage.to_relative(reference["path"]))
else:
spec, _adapter = model_registry.resolve_asset(db, "image", data.provider or None, data.model or None)
if not spec.enabled:
raise HTTPException(409, f"模型 {spec.provider}/{spec.model} 已停用")
references = _reference_media(db, owner_type, owner.id, data.reference_media_ids, kind="image")
prompt = _owner_prompt(None, owner_type, owner, data.kind, data.prompt)
out = storage.platform_dir(owner_type, owner.id, "generated") / f"{data.kind}.png"
request = AssetGenRequest(
kind=data.kind, prompt=prompt, negative_prompt=data.negative_prompt,
width=data.width, height=data.height, seed=data.seed,
reference_images=[str(storage.to_absolute(row.path)) for row in references],
params={"model": spec.model},
idempotency_key=(
f"platform-image:{owner.id}:{data.kind}:"
f"{now_utc().isoformat()}:{data.prompt}:{','.join(map(str, data.reference_media_ids))}"
),
label=f"PLATFORM-{owner_type.upper()}-{owner.id}", output_path=out,
)
try:
result = model_registry.generate_asset(db, request, "image", spec.provider, spec.model)
except ProviderError as exc:
raise HTTPException(502, f"{exc.message}({exc.code})") from exc
kind = data.kind
produced = Path(result.file_path)
if not produced.exists() or produced.stat().st_size < 128:
raise HTTPException(502, "模型未返回有效文件")
info = storage.save_platform_local_file(
produced, owner_type=owner_type, owner_id=owner.id, kind=kind,
stem=f"{owner_type}-{owner.id}-{kind}-ai",
)
if getattr(result, "duration_ms", 0):
info["duration_ms"] = int(result.duration_ms)
media = storage.register_platform_media(
db, owner_type=owner_type, owner_id=owner.id, kind=kind, path=info["path"],
name=f"{owner.name} · AI {AssetKind.LABELS.get(kind, kind)}",
note=f"平台资产生成:{spec.provider}/{spec.model}", source="provider", info=info,
metadata={"generated": True, "provider": spec.provider, "model": spec.model,
"prompt": getattr(request, "prompt", None) or getattr(request, "text", ""),
"reference_media_ids": ([reference_media.id] if owner_type == "voice" and reference_media
else [row.id for row in references]),
"task_id": None},
)
if owner_type == "voice":
# 音色自检:量一遍实际基频,与设定性别比对,结果进素材元数据供界面标注。
check = timbre.check_voice(produced, owner.gender)
produced_media_metadata(media, reference, check, text)
db.commit()
events.log_event(db, "platform.asset.generated",
message=f"平台{_type_label(owner_type)}「{owner.name}」已生成"
f"{AssetKind.LABELS.get(kind, kind)}({spec.provider}/{spec.model})",
entity_type=f"platform_{owner_type}", entity_id=owner.id)
return {"ok": True, "result": storage.platform_media_dict(media)}
@router.post("/api/platform/{owner_type}/{asset_id}/text-assist")
def platform_text_assist(owner_type: str, asset_id: int, data: schemas.PlatformTextAssist,
db: Session = Depends(get_db)):
owner_type = _normalize_type(owner_type)
owner = _owner(db, owner_type, asset_id)
if owner_type == "voice":
subject = f"{owner.name},{owner.gender},{owner.description}"
asset_label = "声线"
elif owner_type == "scene":
subject = ",".join(str(x) for x in (
owner.name, owner.description, owner.prompt, owner.lighting, owner.time_of_day, owner.palette
) if x)
asset_label = "场景"
else:
subject = ",".join(str(x) for x in (
owner.name, owner.role, owner.appearance, owner.personality
) if x)
asset_label = "人物"
spec, _adapter = model_registry.resolve_llm(db, data.provider or None, data.model or None)
prompt = (f"请补全以下{asset_label}设定的一致性提示词。保留事实,不添加冲突内容。"
f"输出一段可直接用于图像/视频模型的中文提示词。\n当前设定:{subject}\n"
f"补充要求:{data.instruction or '强化外观、材质、光线和跨镜头一致性'}")
result = model_registry.generate_asset(
db, AssetGenRequest(kind="text", prompt=prompt,
params={"subject": subject, "asset_type": owner_type, "model": spec.model},
idempotency_key=f"platform-text:{owner_type}:{owner.id}",
label=f"PLATFORM-TEXT-{owner.id}"),
"llm", spec.provider, spec.model,
)
if not result.text.strip():
raise HTTPException(502, "大模型未返回有效内容")
return {"ok": True, "result": {"text": result.text.strip(), "provider": spec.provider,
"model": spec.model}}
def _type_label(owner_type: str) -> str:
return {"human": "人物", "scene": "场景", "voice": "声线"}.get(owner_type, owner_type)