143 lines
8.1 KiB
Python
143 lines
8.1 KiB
Python
"""人物与声音资产库(定妆图 / 三视图 / 服装 / 声线 / 音色样本 / 角色设定 / 场景)。
|
||
|
||
资产本体(Asset)与版本历史(AssetVersion)以多态 owner 挂载到人物、场景、分镜与项目,
|
||
便于「资产版本」与「生成记录」追溯。
|
||
"""
|
||
from __future__ import annotations
|
||
|
||
from datetime import datetime
|
||
|
||
from sqlalchemy import JSON, Boolean, DateTime, ForeignKey, Integer, String, Text, UniqueConstraint, Index
|
||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||
|
||
from app.models.base import TimestampMixin, now_utc
|
||
from app.db import Base
|
||
|
||
|
||
class Character(TimestampMixin, Base):
|
||
"""角色设定 + 视觉资产 + 声线绑定。"""
|
||
|
||
__tablename__ = "characters"
|
||
|
||
id: Mapped[int] = mapped_column(primary_key=True)
|
||
project_id: Mapped[int] = mapped_column(ForeignKey("projects.id", ondelete="CASCADE"), index=True)
|
||
code: Mapped[str] = mapped_column(String(40), default="")
|
||
name: Mapped[str] = mapped_column(String(120))
|
||
role: Mapped[str] = mapped_column(String(60), default="配角")
|
||
gender: Mapped[str] = mapped_column(String(20), default="") # male / female / neutral / 空
|
||
personality: Mapped[str] = mapped_column(Text, default="") # 性格与动机
|
||
appearance: Mapped[str] = mapped_column(Text, default="") # 外观/发型/体型设定
|
||
persona_prompt: Mapped[str] = mapped_column(Text, default="") # 注入镜头提示词的一致性描述
|
||
voice_profile: Mapped[str] = mapped_column(Text, default="") # 声线描述(音色、语速、情绪)
|
||
default_voice_id: Mapped[int | None] = mapped_column(ForeignKey("voice_assets.id", ondelete="SET NULL"), nullable=True)
|
||
style_version: Mapped[str] = mapped_column(String(40), default="v1")
|
||
tags: Mapped[list] = mapped_column(JSON, default=list)
|
||
notes: Mapped[str] = mapped_column(Text, default="")
|
||
is_active: Mapped[bool] = mapped_column(Boolean, default=True)
|
||
platform_source_type: Mapped[str | None] = mapped_column(String(20), nullable=True, default="")
|
||
platform_source_id: Mapped[int | None] = mapped_column(Integer, nullable=True)
|
||
platform_source_version: Mapped[str | None] = mapped_column(String(40), nullable=True, default="")
|
||
platform_snapshot_json: Mapped[dict | None] = mapped_column(JSON, nullable=True, default=dict)
|
||
|
||
project: Mapped["Project"] = relationship(back_populates="characters") # noqa: F821
|
||
default_voice: Mapped["VoiceAsset | None"] = relationship(foreign_keys=[default_voice_id])
|
||
|
||
|
||
class Scene(TimestampMixin, Base):
|
||
"""场景资产:环境图、光线、时间、氛围提示词。"""
|
||
|
||
__tablename__ = "scenes"
|
||
|
||
id: Mapped[int] = mapped_column(primary_key=True)
|
||
project_id: Mapped[int] = mapped_column(ForeignKey("projects.id", ondelete="CASCADE"), index=True)
|
||
code: Mapped[str] = mapped_column(String(40), default="")
|
||
name: Mapped[str] = mapped_column(String(160))
|
||
description: Mapped[str] = mapped_column(Text, default="")
|
||
prompt: Mapped[str] = mapped_column(Text, default="")
|
||
lighting: Mapped[str] = mapped_column(String(120), default="")
|
||
time_of_day: Mapped[str] = mapped_column(String(60), default="")
|
||
palette: Mapped[str] = mapped_column(String(160), default="")
|
||
tags: Mapped[list] = mapped_column(JSON, default=list)
|
||
platform_source_type: Mapped[str | None] = mapped_column(String(20), nullable=True, default="")
|
||
platform_source_id: Mapped[int | None] = mapped_column(Integer, nullable=True)
|
||
platform_source_version: Mapped[str | None] = mapped_column(String(40), nullable=True, default="")
|
||
platform_snapshot_json: Mapped[dict | None] = mapped_column(JSON, nullable=True, default=dict)
|
||
|
||
project: Mapped["Project"] = relationship(back_populates="scenes")
|
||
|
||
|
||
class VoiceAsset(TimestampMixin, Base):
|
||
"""声线资产:供应商音色 ID + 调参 + 音色样本。"""
|
||
|
||
__tablename__ = "voice_assets"
|
||
|
||
id: Mapped[int] = mapped_column(primary_key=True)
|
||
project_id: Mapped[int] = mapped_column(ForeignKey("projects.id", ondelete="CASCADE"), index=True)
|
||
name: Mapped[str] = mapped_column(String(160))
|
||
provider: Mapped[str] = mapped_column(String(60), default="mock")
|
||
provider_voice_id: Mapped[str] = mapped_column(String(160), default="")
|
||
language: Mapped[str] = mapped_column(String(30), default="zh-CN")
|
||
gender: Mapped[str] = mapped_column(String(20), default="")
|
||
description: Mapped[str] = mapped_column(Text, default="")
|
||
tuning: Mapped[dict] = mapped_column(JSON, default=dict) # speed / pitch / volume / sample_rate
|
||
version: Mapped[str] = mapped_column(String(40), default="v1")
|
||
is_default: Mapped[bool] = mapped_column(Boolean, default=False)
|
||
platform_source_type: Mapped[str | None] = mapped_column(String(20), nullable=True, default="")
|
||
platform_source_id: Mapped[int | None] = mapped_column(Integer, nullable=True)
|
||
platform_source_version: Mapped[str | None] = mapped_column(String(40), nullable=True, default="")
|
||
platform_snapshot_json: Mapped[dict | None] = mapped_column(JSON, nullable=True, default=dict)
|
||
|
||
project: Mapped["Project"] = relationship(back_populates="voices")
|
||
|
||
|
||
class Asset(TimestampMixin, Base):
|
||
"""统一资产表(图片/音频/视频/文档),带版本历史。"""
|
||
|
||
__tablename__ = "assets"
|
||
__table_args__ = (Index("ix_assets_owner", "owner_type", "owner_id", "kind"),)
|
||
|
||
id: Mapped[int] = mapped_column(primary_key=True)
|
||
project_id: Mapped[int] = mapped_column(ForeignKey("projects.id", ondelete="CASCADE"), index=True)
|
||
owner_type: Mapped[str] = mapped_column(String(20)) # human / scene / shot / project / voice
|
||
owner_id: Mapped[int] = mapped_column(Integer)
|
||
kind: Mapped[str] = mapped_column(String(30)) # portrait / turnaround / costume ...
|
||
name: Mapped[str] = mapped_column(String(200), default="")
|
||
note: Mapped[str] = mapped_column(Text, default="")
|
||
path: Mapped[str] = mapped_column(String(600))
|
||
checksum: Mapped[str] = mapped_column(String(80), default="")
|
||
mime: Mapped[str] = mapped_column(String(120), default="")
|
||
size_bytes: Mapped[int] = mapped_column(Integer, default=0)
|
||
width: Mapped[int | None] = mapped_column(Integer, nullable=True)
|
||
height: Mapped[int | None] = mapped_column(Integer, nullable=True)
|
||
duration_ms: Mapped[int | None] = mapped_column(Integer, nullable=True)
|
||
current_version: Mapped[int] = mapped_column(Integer, default=1)
|
||
provider_uri: Mapped[str] = mapped_column(String(600), default="") # 供应商可访问地址(对象存储/签名 URL)
|
||
metadata_json: Mapped[dict] = mapped_column(JSON, default=dict)
|
||
|
||
versions: Mapped[list["AssetVersion"]] = relationship(
|
||
back_populates="asset", cascade="all, delete-orphan",
|
||
order_by="AssetVersion.version_no.desc()", lazy="selectin")
|
||
|
||
|
||
class AssetVersion(Base):
|
||
__tablename__ = "asset_versions"
|
||
__table_args__ = (UniqueConstraint("asset_id", "version_no", name="uq_asset_version"),)
|
||
|
||
id: Mapped[int] = mapped_column(primary_key=True)
|
||
asset_id: Mapped[int] = mapped_column(ForeignKey("assets.id", ondelete="CASCADE"), index=True)
|
||
version_no: Mapped[int] = mapped_column(Integer, default=1)
|
||
path: Mapped[str] = mapped_column(String(600))
|
||
checksum: Mapped[str] = mapped_column(String(80), default="")
|
||
source: Mapped[str] = mapped_column(String(30), default="upload") # upload / derive / provider
|
||
note: Mapped[str] = mapped_column(Text, default="")
|
||
size_bytes: Mapped[int] = mapped_column(Integer, default=0)
|
||
# 版本级元数据:切换「当前版本」时要把父行整体换成这一版的值,
|
||
# 缺了它们就只能回退到「保留上一版的宽高」,切版本后画面比例会是错的。
|
||
mime: Mapped[str] = mapped_column(String(120), default="", nullable=True)
|
||
width: Mapped[int | None] = mapped_column(Integer, nullable=True)
|
||
height: Mapped[int | None] = mapped_column(Integer, nullable=True)
|
||
duration_ms: Mapped[int | None] = mapped_column(Integer, nullable=True)
|
||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=now_utc)
|
||
|
||
asset: Mapped["Asset"] = relationship(back_populates="versions")
|