| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- """
- 系统音色服务
- 提供系统预置音色的查询功能,支持按模型和场景分类筛选
- 所有系统音色数据均从数据库获取,不包含任何硬编码
- 需求: 4.1, 4.2, 4.3, 4.4
- """
- import logging
- from typing import List, Optional
- from sqlalchemy.orm import Session
- from app.models.audio import SystemVoice
- from app.schemas.audio_schema import SystemVoiceResponse, SystemVoiceFeatures
- logger = logging.getLogger(__name__)
- class SystemVoiceService:
- """
- 系统音色服务类
-
- 所有系统音色数据均从数据库 system_voice 表获取,不包含任何硬编码。
- 系统音色数据通过初始化脚本 scripts/init_system_voices.py 初始化到数据库。
- """
-
- def __init__(self, db: Session):
- """
- 初始化系统音色服务
-
- Args:
- db: 数据库会话
- """
- self.db = db
-
- def get_system_voices(
- self,
- model: Optional[str] = None,
- category: Optional[str] = None
- ) -> List[SystemVoiceResponse]:
- """
- 获取系统音色列表
-
- 从数据库 system_voice 表查询所有启用的系统音色,支持按模型和场景分类筛选。
- 所有数据均从数据库获取,不包含任何硬编码。
-
- Args:
- model: 按模型筛选,返回支持该模型的音色(如:cosyvoice-v3-flash、cosyvoice-v3-plus)
- category: 按场景分类筛选,返回该分类下的音色(如:社交陪伴、客服、新闻播报等)
-
- Returns:
- 系统音色响应列表
-
- 需求:
- 4.1 - 返回所有启用的系统音色
- 4.2 - 按模型筛选
- 4.3 - 按场景分类筛选
- 4.4 - 返回完整的音色信息
- """
- # 从数据库查询:只查询启用的音色
- query = self.db.query(SystemVoice).filter(SystemVoice.is_active == True)
-
- # 按模型筛选:检查models JSONB数组是否包含指定模型
- if model:
- # PostgreSQL JSONB数组包含查询
- query = query.filter(SystemVoice.models.contains([model]))
-
- # 按场景分类筛选
- if category:
- query = query.filter(SystemVoice.category == category)
-
- # 执行数据库查询
- voices = query.all()
-
- # 将数据库记录转换为响应对象
- result = []
- for voice in voices:
- # 构建功能特性对象
- features = SystemVoiceFeatures(
- ssml=voice.ssml_support or False,
- instruct=voice.instruct_support or False,
- timestamp=voice.timestamp_support or False
- )
-
- # 构建响应对象
- response = SystemVoiceResponse(
- voice_id=voice.voice_id,
- name=voice.name,
- trait=voice.trait or "",
- age=voice.age or "",
- category=voice.category or "",
- languages=voice.languages or [],
- models=voice.models or [],
- features=features
- )
- result.append(response)
-
- logger.debug(
- f"从数据库查询系统音色: model={model}, category={category}, "
- f"返回{len(result)}条记录"
- )
-
- return result
|