mirror of
https://github.com/snailyp/gemini-balance.git
synced 2026-09-04 23:19:17 +08:00
feat: 添加TTS相关配置和功能
- 在.env.example中添加TTS模型、语音名称和语速的配置选项 - 更新README文件,增加TTS相关配置的说明 - 在配置类中添加TTS相关设置 - 新增TTS请求模型以支持文本转语音功能 - 更新智能路由中间件以支持音频请求 - 在路由中添加处理TTS请求的API接口 - 更新前端配置编辑器以支持TTS配置选项
This commit is contained in:
@@ -77,6 +77,11 @@ class Settings(BaseSettings):
|
||||
THINKING_MODELS: List[str] = []
|
||||
THINKING_BUDGET_MAP: Dict[str, float] = {}
|
||||
|
||||
# TTS相关配置
|
||||
TTS_MODEL: str = "gemini-2.5-flash-preview-tts"
|
||||
TTS_VOICE_NAME: str = "Zephyr"
|
||||
TTS_SPEED: str = "normal"
|
||||
|
||||
# 图像生成相关配置
|
||||
PAID_KEY: str = ""
|
||||
CREATE_IMAGE_MODEL: str = DEFAULT_CREATE_IMAGE_MODEL
|
||||
|
||||
@@ -33,3 +33,10 @@ class ImageGenerationRequest(BaseModel):
|
||||
quality: Optional[str] = None
|
||||
style: Optional[str] = None
|
||||
response_format: Optional[str] = "url"
|
||||
|
||||
|
||||
class TTSRequest(BaseModel):
|
||||
model: str = "gemini-2.5-flash-preview-tts"
|
||||
input: str
|
||||
voice: str = "Kore"
|
||||
response_format: Optional[str] = "wav"
|
||||
|
||||
@@ -67,9 +67,9 @@ class SmartRoutingMiddleware(BaseHTTPMiddleware):
|
||||
r"^/gemini/v1beta/models/[^/:]+:(generate|streamGenerate)Content$", # Gemini带前缀
|
||||
r"^/v1beta/models$", # Gemini模型列表
|
||||
r"^/gemini/v1beta/models$", # Gemini带前缀的模型列表
|
||||
r"^/v1/(chat/completions|models|embeddings|images/generations)$", # v1格式
|
||||
r"^/openai/v1/(chat/completions|models|embeddings|images/generations)$", # OpenAI格式
|
||||
r"^/hf/v1/(chat/completions|models|embeddings|images/generations)$", # HF格式
|
||||
r"^/v1/(chat/completions|models|embeddings|images/generations|audio/speech)$", # v1格式
|
||||
r"^/openai/v1/(chat/completions|models|embeddings|images/generations|audio/speech)$", # OpenAI格式
|
||||
r"^/hf/v1/(chat/completions|models|embeddings|images/generations|audio/speech)$", # HF格式
|
||||
r"^/vertex-express/v1beta/models/[^/:]+:(generate|streamGenerate)Content$", # Vertex Express Gemini格式
|
||||
r"^/vertex-express/v1beta/models$", # Vertex Express模型列表
|
||||
r"^/vertex-express/v1/(chat/completions|models|embeddings|images/generations)$", # Vertex Express OpenAI格式
|
||||
@@ -146,6 +146,8 @@ class SmartRoutingMiddleware(BaseHTTPMiddleware):
|
||||
return "/openai/v1/embeddings", {"type": "openai_embeddings"}
|
||||
elif "image" in path.lower():
|
||||
return "/openai/v1/images/generations", {"type": "openai_images"}
|
||||
elif "audio" in path.lower():
|
||||
return "/openai/v1/audio/speech", {"type": "openai_audio"}
|
||||
elif method == "GET":
|
||||
if "model" in path.lower():
|
||||
return "/openai/v1/models", {"type": "openai_models"}
|
||||
@@ -161,6 +163,8 @@ class SmartRoutingMiddleware(BaseHTTPMiddleware):
|
||||
return "/v1/embeddings", {"type": "v1_embeddings"}
|
||||
elif "image" in path.lower():
|
||||
return "/v1/images/generations", {"type": "v1_images"}
|
||||
elif "audio" in path.lower():
|
||||
return "/v1/audio/speech", {"type": "v1_audio"}
|
||||
elif method == "GET":
|
||||
if "model" in path.lower():
|
||||
return "/v1/models", {"type": "v1_models"}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from fastapi import APIRouter, Depends, HTTPException, Response
|
||||
from fastapi.responses import StreamingResponse
|
||||
|
||||
from app.config.config import settings
|
||||
@@ -7,6 +7,7 @@ from app.domain.openai_models import (
|
||||
ChatRequest,
|
||||
EmbeddingRequest,
|
||||
ImageGenerationRequest,
|
||||
TTSRequest,
|
||||
)
|
||||
from app.handler.retry_handler import RetryHandler
|
||||
from app.handler.error_handler import handle_route_errors
|
||||
@@ -14,6 +15,7 @@ from app.log.logger import get_openai_logger
|
||||
from app.service.chat.openai_chat_service import OpenAIChatService
|
||||
from app.service.embedding.embedding_service import EmbeddingService
|
||||
from app.service.image.image_create_service import ImageCreateService
|
||||
from app.service.tts.tts_service import TTSService
|
||||
from app.service.key.key_manager import KeyManager, get_key_manager_instance
|
||||
from app.service.model.model_service import ModelService
|
||||
|
||||
@@ -24,6 +26,7 @@ security_service = SecurityService()
|
||||
model_service = ModelService()
|
||||
embedding_service = EmbeddingService()
|
||||
image_create_service = ImageCreateService()
|
||||
tts_service = TTSService()
|
||||
|
||||
|
||||
async def get_key_manager():
|
||||
@@ -41,6 +44,11 @@ async def get_openai_chat_service(key_manager: KeyManager = Depends(get_key_mana
|
||||
return OpenAIChatService(settings.BASE_URL, key_manager)
|
||||
|
||||
|
||||
async def get_tts_service():
|
||||
"""获取TTS服务实例"""
|
||||
return tts_service
|
||||
|
||||
|
||||
@router.get("/v1/models")
|
||||
@router.get("/hf/v1/models")
|
||||
async def list_models(
|
||||
@@ -147,3 +155,21 @@ async def get_keys_list(
|
||||
},
|
||||
"total": len(keys_status["valid_keys"]) + len(keys_status["invalid_keys"]),
|
||||
}
|
||||
|
||||
|
||||
@router.post("/v1/audio/speech")
|
||||
@router.post("/hf/v1/audio/speech")
|
||||
async def text_to_speech(
|
||||
request: TTSRequest,
|
||||
_=Depends(security_service.verify_authorization),
|
||||
api_key: str = Depends(get_next_working_key_wrapper),
|
||||
tts_service: TTSService = Depends(get_tts_service),
|
||||
):
|
||||
"""处理 OpenAI TTS 请求。"""
|
||||
operation_name = "text_to_speech"
|
||||
async with handle_route_errors(logger, operation_name):
|
||||
logger.info(f"Handling TTS request for model: {request.model}")
|
||||
logger.debug(f"Request: \n{request.model_dump_json(indent=2)}")
|
||||
logger.info(f"Using API key: {api_key}")
|
||||
audio_data = await tts_service.create_tts(request, api_key)
|
||||
return Response(content=audio_data, media_type="audio/wav")
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
import datetime
|
||||
import io
|
||||
import re
|
||||
import time
|
||||
import wave
|
||||
from typing import Optional
|
||||
|
||||
from google import genai
|
||||
|
||||
from app.config.config import settings
|
||||
from app.database.services import add_error_log, add_request_log
|
||||
from app.domain.openai_models import TTSRequest
|
||||
from app.log.logger import get_openai_logger
|
||||
|
||||
logger = get_openai_logger()
|
||||
|
||||
|
||||
def _create_wav_file(audio_data: bytes) -> bytes:
|
||||
"""Creates a WAV file in memory from raw audio data."""
|
||||
with io.BytesIO() as wav_file:
|
||||
with wave.open(wav_file, "wb") as wf:
|
||||
wf.setnchannels(1) # Mono
|
||||
wf.setsampwidth(2) # 16-bit
|
||||
wf.setframerate(24000) # 24kHz sample rate
|
||||
wf.writeframes(audio_data)
|
||||
return wav_file.getvalue()
|
||||
|
||||
|
||||
class TTSService:
|
||||
async def create_tts(self, request: TTSRequest, api_key: str) -> Optional[bytes]:
|
||||
"""
|
||||
使用 Google Gemini SDK 创建音频。
|
||||
"""
|
||||
start_time = time.perf_counter()
|
||||
request_datetime = datetime.datetime.now()
|
||||
is_success = False
|
||||
status_code = None
|
||||
response = None
|
||||
error_log_msg = ""
|
||||
try:
|
||||
client = genai.Client(api_key=api_key)
|
||||
response =await client.aio.models.generate_content(
|
||||
model=settings.TTS_MODEL,
|
||||
contents=f"Speak in a {settings.TTS_SPEED} speed voice: {request.input}",
|
||||
config={
|
||||
"response_modalities": ["Audio"],
|
||||
"speech_config": {
|
||||
"voice_config": {
|
||||
"prebuilt_voice_config": {
|
||||
"voice_name": settings.TTS_VOICE_NAME
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
)
|
||||
if (
|
||||
response.candidates
|
||||
and response.candidates[0].content.parts
|
||||
and response.candidates[0].content.parts[0].inline_data
|
||||
):
|
||||
raw_audio_data = response.candidates[0].content.parts[0].inline_data.data
|
||||
is_success = True
|
||||
status_code = 200
|
||||
return _create_wav_file(raw_audio_data)
|
||||
except Exception as e:
|
||||
is_success = False
|
||||
error_log_msg = f"Generic error: {e}"
|
||||
logger.error(f"An error occurred in TTSService: {error_log_msg}")
|
||||
match = re.search(r"status code (\d+)", str(e))
|
||||
if match:
|
||||
status_code = int(match.group(1))
|
||||
else:
|
||||
status_code = 500
|
||||
raise
|
||||
finally:
|
||||
end_time = time.perf_counter()
|
||||
latency_ms = int((end_time - start_time) * 1000)
|
||||
if not is_success:
|
||||
await add_error_log(
|
||||
gemini_key=api_key,
|
||||
model_name=settings.TTS_MODEL,
|
||||
error_type="google-tts",
|
||||
error_log=error_log_msg,
|
||||
error_code=status_code,
|
||||
request_msg=request.input
|
||||
)
|
||||
await add_request_log(
|
||||
model_name=settings.TTS_MODEL,
|
||||
api_key=api_key,
|
||||
is_success=is_success,
|
||||
status_code=status_code,
|
||||
latency_ms=latency_ms,
|
||||
request_time=request_datetime
|
||||
)
|
||||
@@ -745,6 +745,13 @@ endblock %} {% block head_extra_styles %}
|
||||
>
|
||||
模型配置
|
||||
</button>
|
||||
<button
|
||||
class="tab-btn px-5 py-2 rounded-full font-medium text-sm transition-all duration-200"
|
||||
data-tab="tts"
|
||||
style="background-color: #f8fafc !important; color: #64748b !important; border: 2px solid #e2e8f0 !important; box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1) !important; font-weight: 500 !important;"
|
||||
>
|
||||
TTS 配置
|
||||
</button>
|
||||
<button
|
||||
class="tab-btn px-5 py-2 rounded-full font-medium text-sm transition-all duration-200"
|
||||
data-tab="image"
|
||||
@@ -1370,11 +1377,97 @@ endblock %} {% block head_extra_styles %}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 图像生成相关配置 -->
|
||||
<div class="config-section" id="image-section">
|
||||
|
||||
<!-- TTS配置 -->
|
||||
<div class="config-section" id="tts-section">
|
||||
<h2
|
||||
class="text-xl font-bold mb-6 pb-3 border-b flex items-center gap-2 text-gray-800 border-violet-300 border-opacity-30"
|
||||
>
|
||||
<i class="fas fa-volume-up text-violet-400"></i> TTS 相关配置
|
||||
</h2>
|
||||
|
||||
<!-- TTS 模型 -->
|
||||
<div class="mb-6">
|
||||
<label for="TTS_MODEL" class="block font-semibold mb-2 text-gray-700"
|
||||
>TTS 模型</label
|
||||
>
|
||||
<select
|
||||
id="TTS_MODEL"
|
||||
name="TTS_MODEL"
|
||||
class="w-full px-4 py-3 rounded-lg form-select-themed"
|
||||
>
|
||||
<option value="gemini-2.5-flash-preview-tts">gemini-2.5-flash-preview-tts</option>
|
||||
<option value="gemini-2.5-pro-preview-tts">gemini-2.5-pro-preview-tts</option>
|
||||
</select>
|
||||
<small class="text-gray-500 mt-1 block">用于TTS的模型</small>
|
||||
</div>
|
||||
|
||||
<!-- TTS 语音名称 -->
|
||||
<div class="mb-6">
|
||||
<label for="TTS_VOICE_NAME" class="block font-semibold mb-2 text-gray-700"
|
||||
>TTS 语音名称</label
|
||||
>
|
||||
<select
|
||||
id="TTS_VOICE_NAME"
|
||||
name="TTS_VOICE_NAME"
|
||||
class="w-full px-4 py-3 rounded-lg form-select-themed"
|
||||
>
|
||||
<option value="Zephyr">Zephyr (明亮)</option>
|
||||
<option value="Puck">Puck (欢快)</option>
|
||||
<option value="Charon">Charon (信息丰富)</option>
|
||||
<option value="Kore">Kore (坚定)</option>
|
||||
<option value="Fenrir">Fenrir (易激动)</option>
|
||||
<option value="Leda">Leda (年轻)</option>
|
||||
<option value="Orus">Orus (坚定)</option>
|
||||
<option value="Aoede">Aoede (轻松)</option>
|
||||
<option value="Callirhoe">Callirhoe (随和)</option>
|
||||
<option value="Autonoe">Autonoe (明亮)</option>
|
||||
<option value="Enceladus">Enceladus (呼吸感)</option>
|
||||
<option value="Iapetus">Iapetus (清晰)</option>
|
||||
<option value="Umbriel">Umbriel (随和)</option>
|
||||
<option value="Algieba">Algieba (平滑)</option>
|
||||
<option value="Despina">Despina (平滑)</option>
|
||||
<option value="Erinome">Erinome (清晰)</option>
|
||||
<option value="Algenib">Algenib (沙哑)</option>
|
||||
<option value="Rasalgethi">Rasalgethi (信息丰富)</option>
|
||||
<option value="Laomedeia">Laomedeia (欢快)</option>
|
||||
<option value="Achernar">Achernar (轻柔)</option>
|
||||
<option value="Alnilam">Alnilam (坚定)</option>
|
||||
<option value="Schedar">Schedar (平稳)</option>
|
||||
<option value="Gacrux">Gacrux (成熟)</option>
|
||||
<option value="Pulcherrima">Pulcherrima (向前)</option>
|
||||
<option value="Achird">Achird (友好)</option>
|
||||
<option value="Zubenelgenubi">Zubenelgenubi (休闲)</option>
|
||||
<option value="Vindemiatrix">Vindemiatrix (温柔)</option>
|
||||
<option value="Sadachbia">Sadachbia (活泼)</option>
|
||||
<option value="Sadaltager">Sadaltager (博学)</option>
|
||||
<option value="Sulafat">Sulafat (温暖)</option>
|
||||
</select>
|
||||
<small class="text-gray-500 mt-1 block">TTS 的语音名称,控制风格、语调、口音和节奏</small>
|
||||
</div>
|
||||
|
||||
<!-- TTS 语速 -->
|
||||
<div class="mb-6">
|
||||
<label for="TTS_SPEED" class="block font-semibold mb-2 text-gray-700"
|
||||
>TTS 语速</label
|
||||
>
|
||||
<select
|
||||
id="TTS_SPEED"
|
||||
name="TTS_SPEED"
|
||||
class="w-full px-4 py-3 rounded-lg form-select-themed"
|
||||
>
|
||||
<option value="slow">慢</option>
|
||||
<option value="normal">正常</option>
|
||||
<option value="fast">快</option>
|
||||
</select>
|
||||
<small class="text-gray-500 mt-1 block">选择 TTS 的语速</small>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 图像生成相关配置 -->
|
||||
<div class="config-section" id="image-section">
|
||||
<h2
|
||||
class="text-xl font-bold mb-6 pb-3 border-b flex items-center gap-2 text-gray-800 border-violet-300 border-opacity-30"
|
||||
>
|
||||
<i class="fas fa-image text-violet-400"></i> 图像生成配置
|
||||
</h2>
|
||||
@@ -1511,12 +1604,12 @@ endblock %} {% block head_extra_styles %}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 流式输出优化器配置 -->
|
||||
<!-- 流式输出优化配置 -->
|
||||
<div class="config-section" id="stream-section">
|
||||
<h2
|
||||
class="text-xl font-bold mb-6 pb-3 border-b flex items-center gap-2 text-gray-800 border-violet-300 border-opacity-30"
|
||||
>
|
||||
<i class="fas fa-stream text-violet-400"></i> 流式输出优化器
|
||||
<i class="fas fa-stream text-violet-400"></i> 流式输出相关配置
|
||||
</h2>
|
||||
|
||||
<!-- 启用流式输出优化 -->
|
||||
|
||||
Reference in New Issue
Block a user