feat: blackbox call

This commit is contained in:
Dan Chen
2024-03-21 15:59:35 +08:00
parent 726b926d98
commit 11e1659e22
12 changed files with 49 additions and 13 deletions

View File

@ -18,3 +18,7 @@ async def blackbox(blackbox_name: Union[str, None] = None, request: Request = No
except ValueError:
return await JSONResponse(content={"error": "value error"}, status_code=status.HTTP_400_BAD_REQUEST)
return await box.fast_api_handler(request)
@app.post("/workflows")
async def workflows(reqest: Request):
print("workflows")

View File

@ -4,7 +4,8 @@ class Runtime:
self.parent = parent
self.records = records
def run(self, ast):
def run(self, ast, records={}):
self.records.update(records)
if ast["type"] == "Program":
return self.program(ast)
@ -44,6 +45,8 @@ class Runtime:
for arg in args:
unquoted_args.append(self.unquote(arg))
fu = self.records.get(id)
if fu == None:
raise Exception("Function not found: " + id)
return fu(*unquoted_args)
def unquote(self, ast):

View File

@ -8,6 +8,9 @@ from .blackbox import Blackbox
class AudioToText(Blackbox):
def __call__(self, *args, **kwargs):
return self.processing(*args, **kwargs)
def valid(self, *args, **kwargs) -> bool :
data = args[0]
kind = filetype.guess(data)
@ -15,7 +18,8 @@ class AudioToText(Blackbox):
return False
return kind.extension == "wav"
async def processing(self, data: bytes | io.BytesIO):
def processing(self, *args, **kwargs):
data = args[0]
if data is None:
raise ValueError("Data is required")
if isinstance(data, bytes):

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, *args, **kwargs) -> None:
def __init__(self, *args, **kwargs):
pass
"""
@ -18,7 +18,7 @@ class Blackbox(ABC):
Output same as above.
"""
@abstractmethod
async def processing(self, *args, **kwargs) -> any:
async def processing(self, *args, **kwargs):
pass
"""
@ -34,5 +34,9 @@ class Blackbox(ABC):
to the method.
"""
@abstractmethod
def fast_api_handler(self, request: Request) -> Response:
async def fast_api_handler(self, request: Request) -> Response:
pass
@abstractmethod
def __call__(self, *args, **kwargs):
pass

View File

@ -14,14 +14,21 @@ class BlackboxFactory:
self.tts = TTS()
self.asr = ASR("./.env.yaml")
self.sentiment = Sentiment()
self.sum = SUM()
self.calculator = Calculator()
self.audio_to_text = AudioToText()
self.text_to_audio = TextToAudio()
def create_blackbox(self, blackbox_name: str, blackbox_config: dict) -> Blackbox:
def __call__(self, *args, **kwargs):
return self.processing(*args, **kwargs)
def create_blackbox(self, blackbox_name: str) -> Blackbox:
if blackbox_name == "audio_to_text":
return AudioToText(blackbox_config)
return self.audio_to_text
if blackbox_name == "text_to_audio":
return TextToAudio(blackbox_config)
return self.text_to_audio
if blackbox_name == "calculator":
return Calculator(blackbox_config)
return self.calculator
if blackbox_name == "asr":
return self.asr
if blackbox_name == "tts":
@ -29,5 +36,5 @@ class BlackboxFactory:
if blackbox_name == "sentiment_engine":
return self.sentiment
if blackbox_name == "sum":
return SUM()
return self.sum
raise ValueError("Invalid blockbox type")

View File

@ -7,6 +7,9 @@ class Calculator(Blackbox):
"""This class just for example, it show how to implement Blackbox interface."""
def __call__(self, *args, **kwargs):
return self.processing(*args, **kwargs)
def valid(self, *args, **kwargs) -> bool:
data = args[0]
return isinstance(data, dict) and "op" in data and "left" in data and "right" in data

View File

@ -12,6 +12,9 @@ class Sentiment(Blackbox):
def __init__(self) -> None:
self.engine = SentimentEngine('resources/sentiment_engine/models/paimon_sentiment.onnx')
def __call__(self, *args, **kwargs):
return self.processing(*args, **kwargs)
def valid(self, *args, **kwargs) -> bool:
data = args[0]
return isinstance(data, str)

View File

@ -7,13 +7,15 @@ from .blackbox import Blackbox
class SUM(Blackbox):
def __call__(self, *args, **kwargs):
return self.processing(*args, **kwargs)
def valid(self, *args, **kwargs) -> bool:
data = args[0]
return isinstance(data, list)
def processing(self, *args, **kwargs) -> Coroutine[Any, Any, Any]:
data = args[0]
return sum(data)
def processing(self, *args, **kwargs):
return sum(args)
async def fast_api_handler(self, request: Request) -> Response:
try:

View File

@ -6,6 +6,9 @@ from io import BytesIO
class TextToAudio(Blackbox):
def __call__(self, *args, **kwargs):
return self.processing(*args, **kwargs)
def valid(self, data: any) -> bool:
return isinstance(data, str)

View File

@ -18,6 +18,9 @@ class TTS(Blackbox):
self.tts_service = TTService(*config['catmaid'])
super().__init__(config)
def __call__(self, *args, **kwargs):
return self.processing(*args, **kwargs)
def processing(self, *args, **kwargs) -> io.BytesIO:
text = args[0]
audio = self.tts_service.read(text)

BIN
test_data/test.mp3 Normal file

Binary file not shown.

BIN
test_data/testone.wav Normal file

Binary file not shown.