mirror of
https://github.com/BoardWare-Genius/jarvis-models.git
synced 2025-12-14 00:53:25 +00:00
feat: blackbox call
This commit is contained in:
@ -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):
|
||||
|
||||
@ -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
|
||||
@ -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")
|
||||
@ -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
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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:
|
||||
|
||||
@ -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)
|
||||
|
||||
|
||||
@ -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)
|
||||
|
||||
Reference in New Issue
Block a user