Blackbox MeloTTS created

This commit is contained in:
gdw6463
2024-05-13 19:17:30 +08:00
parent 4c51fa24da
commit 68d5088552
6 changed files with 95 additions and 18 deletions

47
src/blackbox/melotts.py Normal file
View File

@ -0,0 +1,47 @@
import io
import time
import requests
from fastapi import Request, Response, status
from fastapi.responses import JSONResponse
from injector import inject
from injector import singleton
from ..configuration import MeloConf
from .blackbox import Blackbox
@singleton
class MeloTTS(Blackbox):
melotts: str
@inject
def __init__(self, melo_config: MeloConf) -> None:
self.melotts = melo_config.melotts
def __call__(self, *args, **kwargs):
return self.processing(*args, **kwargs)
def valid(self, *args, **kwargs) -> bool:
text = args[0]
return isinstance(text, str)
def processing(self, *args, **kwargs) -> io.BytesIO:
text = args[0]
current_time = time.time()
audio = self.tts_service.read(text)
print("#### MeloTTS Service consume : ", (time.time()-current_time))
return audio
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/mp3", headers={"Content-Disposition": "attachment; filename=audio.mp3"})