mirror of
https://github.com/BoardWare-Genius/jarvis-models.git
synced 2025-12-13 16:53:24 +00:00
feat: blackbox call
This commit is contained in:
4
main.py
4
main.py
@ -18,3 +18,7 @@ async def blackbox(blackbox_name: Union[str, None] = None, request: Request = No
|
|||||||
except ValueError:
|
except ValueError:
|
||||||
return await JSONResponse(content={"error": "value error"}, status_code=status.HTTP_400_BAD_REQUEST)
|
return await JSONResponse(content={"error": "value error"}, status_code=status.HTTP_400_BAD_REQUEST)
|
||||||
return await box.fast_api_handler(request)
|
return await box.fast_api_handler(request)
|
||||||
|
|
||||||
|
@app.post("/workflows")
|
||||||
|
async def workflows(reqest: Request):
|
||||||
|
print("workflows")
|
||||||
@ -4,7 +4,8 @@ class Runtime:
|
|||||||
self.parent = parent
|
self.parent = parent
|
||||||
self.records = records
|
self.records = records
|
||||||
|
|
||||||
def run(self, ast):
|
def run(self, ast, records={}):
|
||||||
|
self.records.update(records)
|
||||||
if ast["type"] == "Program":
|
if ast["type"] == "Program":
|
||||||
return self.program(ast)
|
return self.program(ast)
|
||||||
|
|
||||||
@ -44,6 +45,8 @@ class Runtime:
|
|||||||
for arg in args:
|
for arg in args:
|
||||||
unquoted_args.append(self.unquote(arg))
|
unquoted_args.append(self.unquote(arg))
|
||||||
fu = self.records.get(id)
|
fu = self.records.get(id)
|
||||||
|
if fu == None:
|
||||||
|
raise Exception("Function not found: " + id)
|
||||||
return fu(*unquoted_args)
|
return fu(*unquoted_args)
|
||||||
|
|
||||||
def unquote(self, ast):
|
def unquote(self, ast):
|
||||||
|
|||||||
@ -8,6 +8,9 @@ from .blackbox import Blackbox
|
|||||||
|
|
||||||
class AudioToText(Blackbox):
|
class AudioToText(Blackbox):
|
||||||
|
|
||||||
|
def __call__(self, *args, **kwargs):
|
||||||
|
return self.processing(*args, **kwargs)
|
||||||
|
|
||||||
def valid(self, *args, **kwargs) -> bool :
|
def valid(self, *args, **kwargs) -> bool :
|
||||||
data = args[0]
|
data = args[0]
|
||||||
kind = filetype.guess(data)
|
kind = filetype.guess(data)
|
||||||
@ -15,7 +18,8 @@ class AudioToText(Blackbox):
|
|||||||
return False
|
return False
|
||||||
return kind.extension == "wav"
|
return kind.extension == "wav"
|
||||||
|
|
||||||
async def processing(self, data: bytes | io.BytesIO):
|
def processing(self, *args, **kwargs):
|
||||||
|
data = args[0]
|
||||||
if data is None:
|
if data is None:
|
||||||
raise ValueError("Data is required")
|
raise ValueError("Data is required")
|
||||||
if isinstance(data, bytes):
|
if isinstance(data, bytes):
|
||||||
|
|||||||
@ -8,7 +8,7 @@ class Blackbox(ABC):
|
|||||||
the methods processing, valid and fast_api_handler.
|
the methods processing, valid and fast_api_handler.
|
||||||
If implemented correctly, the blackbox class can be used in the main.py file
|
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
|
pass
|
||||||
|
|
||||||
"""
|
"""
|
||||||
@ -18,7 +18,7 @@ class Blackbox(ABC):
|
|||||||
Output same as above.
|
Output same as above.
|
||||||
"""
|
"""
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
async def processing(self, *args, **kwargs) -> any:
|
async def processing(self, *args, **kwargs):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
"""
|
"""
|
||||||
@ -34,5 +34,9 @@ class Blackbox(ABC):
|
|||||||
to the method.
|
to the method.
|
||||||
"""
|
"""
|
||||||
@abstractmethod
|
@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
|
pass
|
||||||
@ -14,14 +14,21 @@ class BlackboxFactory:
|
|||||||
self.tts = TTS()
|
self.tts = TTS()
|
||||||
self.asr = ASR("./.env.yaml")
|
self.asr = ASR("./.env.yaml")
|
||||||
self.sentiment = Sentiment()
|
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":
|
if blackbox_name == "audio_to_text":
|
||||||
return AudioToText(blackbox_config)
|
return self.audio_to_text
|
||||||
if blackbox_name == "text_to_audio":
|
if blackbox_name == "text_to_audio":
|
||||||
return TextToAudio(blackbox_config)
|
return self.text_to_audio
|
||||||
if blackbox_name == "calculator":
|
if blackbox_name == "calculator":
|
||||||
return Calculator(blackbox_config)
|
return self.calculator
|
||||||
if blackbox_name == "asr":
|
if blackbox_name == "asr":
|
||||||
return self.asr
|
return self.asr
|
||||||
if blackbox_name == "tts":
|
if blackbox_name == "tts":
|
||||||
@ -29,5 +36,5 @@ class BlackboxFactory:
|
|||||||
if blackbox_name == "sentiment_engine":
|
if blackbox_name == "sentiment_engine":
|
||||||
return self.sentiment
|
return self.sentiment
|
||||||
if blackbox_name == "sum":
|
if blackbox_name == "sum":
|
||||||
return SUM()
|
return self.sum
|
||||||
raise ValueError("Invalid blockbox type")
|
raise ValueError("Invalid blockbox type")
|
||||||
@ -7,6 +7,9 @@ class Calculator(Blackbox):
|
|||||||
|
|
||||||
"""This class just for example, it show how to implement Blackbox interface."""
|
"""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:
|
def valid(self, *args, **kwargs) -> bool:
|
||||||
data = args[0]
|
data = args[0]
|
||||||
return isinstance(data, dict) and "op" in data and "left" in data and "right" in data
|
return isinstance(data, dict) and "op" in data and "left" in data and "right" in data
|
||||||
|
|||||||
@ -12,6 +12,9 @@ class Sentiment(Blackbox):
|
|||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
self.engine = SentimentEngine('resources/sentiment_engine/models/paimon_sentiment.onnx')
|
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:
|
def valid(self, *args, **kwargs) -> bool:
|
||||||
data = args[0]
|
data = args[0]
|
||||||
return isinstance(data, str)
|
return isinstance(data, str)
|
||||||
|
|||||||
@ -7,13 +7,15 @@ from .blackbox import Blackbox
|
|||||||
|
|
||||||
class SUM(Blackbox):
|
class SUM(Blackbox):
|
||||||
|
|
||||||
|
def __call__(self, *args, **kwargs):
|
||||||
|
return self.processing(*args, **kwargs)
|
||||||
|
|
||||||
def valid(self, *args, **kwargs) -> bool:
|
def valid(self, *args, **kwargs) -> bool:
|
||||||
data = args[0]
|
data = args[0]
|
||||||
return isinstance(data, list)
|
return isinstance(data, list)
|
||||||
|
|
||||||
def processing(self, *args, **kwargs) -> Coroutine[Any, Any, Any]:
|
def processing(self, *args, **kwargs):
|
||||||
data = args[0]
|
return sum(args)
|
||||||
return sum(data)
|
|
||||||
|
|
||||||
async def fast_api_handler(self, request: Request) -> Response:
|
async def fast_api_handler(self, request: Request) -> Response:
|
||||||
try:
|
try:
|
||||||
|
|||||||
@ -6,6 +6,9 @@ from io import BytesIO
|
|||||||
|
|
||||||
class TextToAudio(Blackbox):
|
class TextToAudio(Blackbox):
|
||||||
|
|
||||||
|
def __call__(self, *args, **kwargs):
|
||||||
|
return self.processing(*args, **kwargs)
|
||||||
|
|
||||||
def valid(self, data: any) -> bool:
|
def valid(self, data: any) -> bool:
|
||||||
return isinstance(data, str)
|
return isinstance(data, str)
|
||||||
|
|
||||||
|
|||||||
@ -18,6 +18,9 @@ class TTS(Blackbox):
|
|||||||
self.tts_service = TTService(*config['catmaid'])
|
self.tts_service = TTService(*config['catmaid'])
|
||||||
super().__init__(config)
|
super().__init__(config)
|
||||||
|
|
||||||
|
def __call__(self, *args, **kwargs):
|
||||||
|
return self.processing(*args, **kwargs)
|
||||||
|
|
||||||
def processing(self, *args, **kwargs) -> io.BytesIO:
|
def processing(self, *args, **kwargs) -> io.BytesIO:
|
||||||
text = args[0]
|
text = args[0]
|
||||||
audio = self.tts_service.read(text)
|
audio = self.tts_service.read(text)
|
||||||
|
|||||||
BIN
test_data/test.mp3
Normal file
BIN
test_data/test.mp3
Normal file
Binary file not shown.
BIN
test_data/testone.wav
Normal file
BIN
test_data/testone.wav
Normal file
Binary file not shown.
Reference in New Issue
Block a user