This commit is contained in:
Chris 2025-02-18 11:25:06 +08:00 committed by GitHub
commit a2ad5466f5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 67 additions and 0 deletions

View file

@ -217,3 +217,11 @@ TTS:
parallel_infer: true
repetition_penalty: 1.35
aux_ref_audio_paths: []
fish_audio_tts:
#当今世界最自然的AI语音
#api申请地址https://fish.audio/zh-CN/go-api/billing/
type: fish_audio
api_key: "你的fishaudio—api_key"
api_url: "https://api.fish.audio/v1/tts"
voice: "你的音频模型ID"
output_file: "tmp/"

59
fish_audio.py Normal file
View file

@ -0,0 +1,59 @@
import os
import uuid
import ormsgpack
import httpx
from datetime import datetime
from typing import Literal
from pydantic import BaseModel, conint
from typing import Annotated
from core.providers.tts.base import TTSProviderBase
class ServeReferenceAudio(BaseModel):
audio: bytes
text: str
class ServeTTSRequest(BaseModel):
text: str
chunk_length: Annotated[int, conint(ge=100, le=300, strict=True)] = 200
format: Literal["wav", "pcm", "mp3"] = "mp3"
mp3_bitrate: Literal[64, 128, 192] = 128
references: list[ServeReferenceAudio] = []
reference_id: str | None = None
normalize: bool = True
latency: Literal["normal", "balanced"] = "normal"
class TTSProvider(TTSProviderBase):
def __init__(self, config, delete_audio_file):
super().__init__(config, delete_audio_file)
self.api_key = config.get("api_key")
self.api_url = config.get("api_url")
self.voice = config.get("voice")
self.output_file = config.get("output_file")
def generate_filename(self, extension=".mp3"):
return os.path.join(self.output_file, f"tts-{datetime.now().date()}@{uuid.uuid4().hex}{extension}")
async def text_to_speak(self, text, output_file):
request = ServeTTSRequest(
text=text,
references=[], # 如果需要参考音频可以在这里添加
reference_id=self.voice # 使用配置的voice作为reference_id
)
async with httpx.AsyncClient() as client:
async with client.stream(
"POST",
self.api_url,
content=ormsgpack.packb(request, option=ormsgpack.OPT_SERIALIZE_PYDANTIC),
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/msgpack",
},
timeout=None,
) as response:
if response.status_code == 200:
with open(output_file, "wb") as f:
async for chunk in response.aiter_bytes():
f.write(chunk)
else:
raise ValueError(f"TTS请求失败: {response.status_code} - {response.text}")