add blackbox fastchat

This commit is contained in:
ACBBZ
2024-04-10 09:31:16 +00:00
107 changed files with 29145 additions and 50 deletions

41
src/blackbox/asr.py Normal file
View File

@ -0,0 +1,41 @@
from io import BytesIO
from typing import Any, Coroutine
from fastapi import Request, Response, status
from fastapi.responses import JSONResponse
from ..asr.rapid_paraformer.utils import read_yaml
from ..asr.rapid_paraformer import RapidParaformer
from .blackbox import Blackbox
class ASR(Blackbox):
def __init__(self, *args, **kwargs) -> None:
config = read_yaml(args[0])
self.paraformer = RapidParaformer(config)
def __call__(self, *args, **kwargs):
return self.processing(*args, **kwargs)
async def processing(self, *args, **kwargs):
data = args[0]
results = self.paraformer([BytesIO(data)])
if len(results) == 0:
return None
return results[0]
def valid(self, data: any) -> bool:
if isinstance(data, bytes):
return True
return False
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()
try:
txt = await self.processing(d)
except ValueError as e:
return JSONResponse(content={"error": str(e)}, status_code=status.HTTP_400_BAD_REQUEST)
return JSONResponse(content={"text": txt}, status_code=status.HTTP_200_OK)

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/x-wav", headers={"Content-Disposition": "attachment; filename=audio.wav"})

View File

@ -1,7 +1,8 @@
from .audio_chat import AudioChat
from .sum import SUM
from .sentiment import Sentiment
from .tts import TTS
from ..asr.asr import ASR
from .asr import ASR
from .audio_to_text import AudioToText
from .blackbox import Blackbox
from .calculator import Calculator
@ -13,14 +14,18 @@ class BlackboxFactor:
def __init__(self) -> None:
self.tts = TTS()
self.asr = ASR("./.env.yaml")
self.asr = ASR(".env.yaml")
self.sentiment = Sentiment()
self.sum = SUM()
self.calculator = Calculator()
self.audio_to_text = AudioToText()
self.text_to_audio = TextToAudio()
self.tesou = Tesou()
<<<<<<< HEAD
self.fastchat = Fastchat()
=======
self.audio_chat = AudioChat(self.asr, self.tesou, self.tts)
>>>>>>> refs/remotes/origin/main
def __call__(self, *args, **kwargs):
return self.processing(*args, **kwargs)
@ -42,6 +47,11 @@ class BlackboxFactor:
return self.sum
if blackbox_name == "tesou":
return self.tesou
<<<<<<< HEAD
if blackbox_name == "fastchat":
return self.fastchat
=======
if blackbox_name == "audio_chat":
return self.audio_chat
>>>>>>> refs/remotes/origin/main
raise ValueError("Invalid blockbox type")

View File

@ -3,14 +3,14 @@ from typing import Any, Coroutine
from fastapi import Request, Response, status
from fastapi.responses import JSONResponse
from sentiment_engine.sentiment_engine import SentimentEngine
from ..sentiment_engine.sentiment_engine import SentimentEngine
from .blackbox import Blackbox
class Sentiment(Blackbox):
def __init__(self) -> None:
self.engine = SentimentEngine('resources/sentiment_engine/models/paimon_sentiment.onnx')
self.engine = SentimentEngine()
def __call__(self, *args, **kwargs):
return self.processing(*args, **kwargs)

View File

@ -17,23 +17,21 @@ class Tesou(Blackbox):
# 用户输入的数据格式为:[{"id": "123", "prompt": "叉烧饭,帮我查询叉烧饭的介绍"}]
def processing(self, id, prompt) -> str:
url = 'http://120.196.116.194:48891/'
url = 'http://120.196.116.194:48891/chat/'
message = {
"user_id": id,
"prompt": prompt,
}
response = requests.post(url, json=message)
return response
return response.json()
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)
user_id = data.get("id")
user_prompt = data.get("prompt")
user_id = data.get("user_id")
user_prompt = data.get("prompt")
if user_prompt is None:
return JSONResponse(content={"error": "question is required"}, status_code=status.HTTP_400_BAD_REQUEST)
return JSONResponse(content={"Response": self.processing(user_id, user_prompt)}, status_code=status.HTTP_200_OK)

View File

@ -1,27 +1,25 @@
import io
import time
from ntpath import join
from fastapi import Request, Response, status
from fastapi.responses import JSONResponse
from .blackbox import Blackbox
from tts.tts_service import TTService
from ..tts.tts_service import TTService
class TTS(Blackbox):
def __init__(self, *args, **kwargs) -> 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)
self.tts_service = TTService("catmaid")
def __call__(self, *args, **kwargs):
return self.processing(*args, **kwargs)
def processing(self, *args, **kwargs) -> io.BytesIO:
text = args[0]
current_time = time.time()
audio = self.tts_service.read(text)
print("#### TTS Service consume : ", (time.time()-current_time))
return audio
def valid(self, *args, **kwargs) -> bool: