Files
gemini-balance/app/service/tts/native/tts_response_handler.py
2025-07-15 18:04:16 +09:00

54 lines
2.0 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
原生Gemini TTS响应处理器扩展
继承自原始响应处理器添加原生Gemini TTS支持保持向后兼容
"""
from typing import Any, Dict, Optional
from app.handler.response_handler import GeminiResponseHandler
from app.log.logger import get_gemini_logger
logger = get_gemini_logger()
class TTSResponseHandler(GeminiResponseHandler):
"""
支持TTS的响应处理器
继承自原始的GeminiResponseHandler添加TTS响应处理
"""
def handle_response(
self, response: Dict[str, Any], model: str, stream: bool = False, usage_metadata: Optional[Dict[str, Any]] = None
) -> Dict[str, Any]:
"""
处理响应支持TTS音频数据
"""
# 检查是否是TTS响应包含音频数据
if self._is_tts_response(response):
logger.info("Detected TTS response with audio data, returning original response")
return response
# 对于非TTS响应使用父类的处理方法
return super().handle_response(response, model, stream, usage_metadata)
def _is_tts_response(self, response: Dict[str, Any]) -> bool:
"""
检查是否是TTS响应
"""
try:
if (response.get("candidates") and
len(response["candidates"]) > 0 and
response["candidates"][0].get("content") and
response["candidates"][0]["content"].get("parts") and
len(response["candidates"][0]["content"]["parts"]) > 0):
parts = response["candidates"][0]["content"]["parts"]
for part in parts:
if "inlineData" in part:
mime_type = part["inlineData"].get("mimeType", "")
if mime_type.startswith("audio/"):
return True
return False
except Exception as e:
logger.warning(f"Error checking TTS response: {e}")
return False