chatterbox-ui/backend/app/models/speaker_models.py

48 lines
1.8 KiB
Python

from pydantic import BaseModel, validator, field_validator, model_validator
from typing import Optional
class SpeakerBase(BaseModel):
name: str
reference_text: Optional[str] = None # Temporarily optional for migration
class SpeakerCreate(SpeakerBase):
"""Model for speaker creation requests"""
reference_text: str # Required for new speakers
@validator('reference_text')
def validate_new_speaker_reference_text(cls, v):
"""Validate reference text for new speakers (stricter than legacy)"""
if not v or not v.strip():
raise ValueError("Reference text is required for new speakers")
if len(v.strip()) > 500:
raise ValueError("Reference text should be under 500 characters")
return v.strip()
class Speaker(SpeakerBase):
"""Complete speaker model with ID and sample path"""
id: str
sample_path: Optional[str] = None
@validator('reference_text')
def validate_reference_text_length(cls, v):
"""Validate reference text length and provide defaults for migration"""
if not v or v is None:
# Provide a default for legacy speakers during migration
return "This is a sample voice for text-to-speech generation."
if not v.strip():
return "This is a sample voice for text-to-speech generation."
if len(v.strip()) > 500:
raise ValueError("reference_text should be under 500 characters for optimal performance")
return v.strip()
class Config:
from_attributes = True # Replaces orm_mode = True in Pydantic v2
class SpeakerResponse(SpeakerBase):
"""Response model for speaker operations"""
id: str
message: Optional[str] = None
class Config:
from_attributes = True