feat: audio chat

This commit is contained in:
superobk
2024-04-08 17:21:44 +08:00
parent 703bdaec5a
commit 41621b44f7
4 changed files with 42 additions and 3 deletions

View File

@ -0,0 +1,36 @@
from fastapi import Request, Response,status
from fastapi.responses import JSONResponse
from .blackbox import Blackbox
class AudioChat(Blackbox):
def __init__(self, asr, gpt, tts):
self.asr = asr
self.gpt = gpt
self.tts = tts
def __call__(self, *args, **kwargs):
return self.processing(*args, **kwargs)
def valid(self, *args, **kwargs) -> bool :
data = args[0]
if isinstance(data, bytes):
return True
return False
async def processing(self, *args, **kwargs):
data = args[0]
text = await self.asr(data)
# TODO: ID
text = self.gpt("123", " " + text)
audio = self.tts(text)
return audio
async def fast_api_handler(self, request: Request) -> Response:
data = (await request.form()).get("audio")
if data is None:
return JSONResponse(content={"error": "data is required"}, status_code=status.HTTP_400_BAD_REQUEST)
d = await data.read()
by = await self.processing(d)
return Response(content=by.read(), media_type="audio/wav", headers={"Content-Disposition": "attachment; filename=audio.wav"})