liumaolin commited on
Commit
226effc
·
1 Parent(s): 5258538

添加获取TTS模型参考音频的API路由,支持通过模型ID获取音频文件。

Browse files
src/voice_dialogue/api/routes/tts_routes.py CHANGED
@@ -1,6 +1,7 @@
1
  from typing import Optional
2
 
3
  from fastapi import APIRouter, HTTPException, BackgroundTasks, Request
 
4
 
5
  from voice_dialogue.services.audio.generators import tts_config_registry
6
  from voice_dialogue.utils.logger import logger
@@ -397,3 +398,41 @@ def _find_config_by_id(model_id: str) -> Optional:
397
  if config_id == model_id:
398
  return config
399
  return None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  from typing import Optional
2
 
3
  from fastapi import APIRouter, HTTPException, BackgroundTasks, Request
4
+ from fastapi.responses import FileResponse
5
 
6
  from voice_dialogue.services.audio.generators import tts_config_registry
7
  from voice_dialogue.utils.logger import logger
 
398
  if config_id == model_id:
399
  return config
400
  return None
401
+
402
+
403
+ @router.get("/models/{model_id}/reference-audio", summary="获取TTS模型参考音频")
404
+ async def get_tts_model_reference_audio(model_id: str):
405
+ """
406
+ 通过模型ID获取TTS模型的参考音频文件
407
+ """
408
+ try:
409
+ # 通过ID找到对应的配置
410
+ config = _find_config_by_id(model_id)
411
+ if not config:
412
+ raise HTTPException(status_code=404, detail="模型ID不存在")
413
+
414
+ # 检查模型是否已经下载
415
+ if not config.is_model_complete():
416
+ raise HTTPException(status_code=400, detail="模型尚未下载,无法获取参考音频")
417
+
418
+ # 获取参考音频文件路径
419
+ reference_audio_path = ''
420
+ if hasattr(config, "reference_audio_path"):
421
+ reference_audio_path = config.reference_audio_path
422
+
423
+ # 检查文件是否存在
424
+ if reference_audio_path and not reference_audio_path.exists():
425
+ raise HTTPException(status_code=404, detail="参考音频文件不存在")
426
+
427
+ # 返回音频文件
428
+ return FileResponse(
429
+ path=str(reference_audio_path),
430
+ media_type="audio/wav",
431
+ filename=f"{config.character_name}_reference.wav"
432
+ )
433
+
434
+ except HTTPException:
435
+ raise
436
+ except Exception as e:
437
+ logger.error(f"获取TTS模型参考音频失败: {e}", exc_info=True)
438
+ raise HTTPException(status_code=500, detail=f"获取参考音频失败: {str(e)}")