feat: tts

This commit is contained in:
superobk
2024-03-19 17:33:09 +08:00
parent 2dccf5e78d
commit f2d6b9e526
90 changed files with 533580 additions and 5 deletions

View File

@ -6,11 +6,10 @@ from fastapi.responses import JSONResponse
from .rapid_paraformer.utils import read_yaml
from .rapid_paraformer import RapidParaformer
from .asr_service import ASRService
from ..blackbox.blackbox import Blackbox
class ASR(Blackbox):
def __init__(self, config: any) -> None:
config = read_yaml(config)
self.paraformer = RapidParaformer(config)

View File

@ -8,7 +8,7 @@ class Blackbox(ABC):
the methods processing, valid and fast_api_handler.
If implemented correctly, the blackbox class can be used in the main.py file
"""
def __init__(self, config: any) -> None:
def __init__(self, *args, **kwargs) -> None:
pass
"""

View File

@ -1,3 +1,4 @@
from .tts import TTS
from ..asr.asr import ASR
from .audio_to_text import AudioToText
from .blackbox import Blackbox
@ -8,8 +9,8 @@ from .text_to_audio import TextToAudio
class BlackboxFactory:
def __init__(self) -> None:
self.tts = TTS()
self.asr = ASR("./.env.yaml")
pass
def create_blackbox(self, blackbox_name: str, blackbox_config: dict) -> Blackbox:
if blackbox_name == "audio_to_text":
@ -20,4 +21,6 @@ class BlackboxFactory:
return Calculator(blackbox_config)
if blackbox_name == "asr":
return self.asr
if blackbox_name == "tts":
return self.tts
raise ValueError("Invalid blockbox type")

36
src/blackbox/tts.py Normal file
View 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"})