mirror of
https://github.com/BoardWare-Genius/jarvis-models.git
synced 2025-12-14 00:53:25 +00:00
feat: tts
This commit is contained in:
36
src/blackbox/tts.py
Normal file
36
src/blackbox/tts.py
Normal file
@ -0,0 +1,36 @@
|
||||
import io
|
||||
from typing import Any, Coroutine
|
||||
|
||||
from fastapi import Request, Response, status
|
||||
from fastapi.responses import JSONResponse
|
||||
from .blackbox import Blackbox
|
||||
from tts.tts_service import TTService
|
||||
|
||||
class TTS(Blackbox):
|
||||
|
||||
def __init__(self) -> None:
|
||||
config = {
|
||||
'paimon': ['resources/tts/models/paimon6k.json', 'resources/tts/models/paimon6k_390k.pth', 'character_paimon', 1],
|
||||
'yunfei': ['resources/tts/models/yunfeimix2.json', 'resources/tts/models/yunfeimix2_53k.pth', 'character_yunfei', 1.1],
|
||||
'catmaid': ['resources/tts/models/catmix.json', 'resources/tts/models/catmix_107k.pth', 'character_catmaid', 1.2]
|
||||
}
|
||||
self.tts_service = TTService(*config['catmaid'])
|
||||
super().__init__(config)
|
||||
|
||||
def processing(self, text: str) -> io.BytesIO:
|
||||
audio = self.tts_service.read(text)
|
||||
return audio
|
||||
|
||||
def valid(self, txt: any) -> bool:
|
||||
return isinstance(txt, str)
|
||||
|
||||
async def fast_api_handler(self, request: Request) -> Response:
|
||||
try:
|
||||
data = await request.json()
|
||||
except:
|
||||
return JSONResponse(content={"error": "json parse error"}, status_code=status.HTTP_400_BAD_REQUEST)
|
||||
text = data.get("text")
|
||||
if text is None:
|
||||
return JSONResponse(content={"error": "text is required"}, status_code=status.HTTP_400_BAD_REQUEST)
|
||||
by = self.processing(text)
|
||||
return Response(content=by.read(), media_type="audio/wav", headers={"Content-Disposition": "attachment; filename=audio.wav"})
|
||||
Reference in New Issue
Block a user