refactor: args

This commit is contained in:
Dan Chen
2024-03-21 15:20:09 +08:00
parent 49659dcc13
commit 726b926d98
6 changed files with 22 additions and 13 deletions

View File

@ -10,12 +10,13 @@ from ..blackbox.blackbox import Blackbox
class ASR(Blackbox): class ASR(Blackbox):
def __init__(self, config: any) -> None: def __init__(self, *args, **kwargs) -> None:
config = read_yaml(config) config = read_yaml(args[0])
self.paraformer = RapidParaformer(config) self.paraformer = RapidParaformer(config)
super().__init__(config) super().__init__(config)
async def processing(self, data: bytes): async def processing(self, *args, **kwargs):
data = args[0]
results = self.paraformer([BytesIO(data)]) results = self.paraformer([BytesIO(data)])
if len(results) == 0: if len(results) == 0:
return None return None

View File

@ -8,7 +8,8 @@ from .blackbox import Blackbox
class AudioToText(Blackbox): class AudioToText(Blackbox):
def valid(self, data: any) -> bool : def valid(self, *args, **kwargs) -> bool :
data = args[0]
kind = filetype.guess(data) kind = filetype.guess(data)
if kind is None: if kind is None:
return False return False

View File

@ -7,7 +7,8 @@ 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 valid(self, data: any) -> bool: 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 return isinstance(data, dict) and "op" in data and "left" in data and "right" in data
def processing(self, data: dict) -> int | float: def processing(self, data: dict) -> int | float:

View File

@ -12,10 +12,12 @@ 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 valid(self, data: any) -> bool: def valid(self, *args, **kwargs) -> bool:
data = args[0]
return isinstance(data, str) return isinstance(data, str)
def processing(self, text: any) -> int: def processing(self, *args, **kwargs) -> int:
text = args[0]
return int(self.engine.infer(text)) return int(self.engine.infer(text))
async def fast_api_handler(self, request) -> Response: async def fast_api_handler(self, request) -> Response:

View File

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

View File

@ -9,7 +9,7 @@ import time
class TTS(Blackbox): class TTS(Blackbox):
def __init__(self) -> None: def __init__(self, *args, **kwargs) -> None:
config = { config = {
'paimon': ['resources/tts/models/paimon6k.json', 'resources/tts/models/paimon6k_390k.pth', 'character_paimon', 1], '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], 'yunfei': ['resources/tts/models/yunfeimix2.json', 'resources/tts/models/yunfeimix2_53k.pth', 'character_yunfei', 1.1],
@ -18,12 +18,14 @@ class TTS(Blackbox):
self.tts_service = TTService(*config['catmaid']) self.tts_service = TTService(*config['catmaid'])
super().__init__(config) super().__init__(config)
def processing(self, text: str) -> io.BytesIO: def processing(self, *args, **kwargs) -> io.BytesIO:
text = args[0]
audio = self.tts_service.read(text) audio = self.tts_service.read(text)
return audio return audio
def valid(self, txt: any) -> bool: def valid(self, *args, **kwargs) -> bool:
return isinstance(txt, str) text = args[0]
return isinstance(text, str)
async def fast_api_handler(self, request: Request) -> Response: async def fast_api_handler(self, request: Request) -> Response:
try: try: