mirror of
https://github.com/BoardWare-Genius/jarvis-models.git
synced 2025-12-13 16:53:24 +00:00
fix: rename
This commit is contained in:
17
src/blackbox/blackbox_factory.py
Normal file
17
src/blackbox/blackbox_factory.py
Normal file
@ -0,0 +1,17 @@
|
||||
from blackbox.audio_to_text import AudioToText
|
||||
from blackbox.blackbox import Blackbox
|
||||
from blackbox.calculator import Calculator
|
||||
from blackbox.text_to_audio import TextToAudio
|
||||
|
||||
|
||||
class BlackboxFactory:
|
||||
|
||||
def create_blackbox(self, blackbox_type: str, blackbox_config: dict) -> Blackbox:
|
||||
|
||||
if blackbox_type == "audio_to_text":
|
||||
return AudioToText(blackbox_config)
|
||||
if blackbox_type == "text_to_audio":
|
||||
return TextToAudio(blackbox_config)
|
||||
if blackbox_type == "calculator":
|
||||
return Calculator(blackbox_config)
|
||||
raise ValueError("Invalid blockbox type")
|
||||
@ -1,14 +0,0 @@
|
||||
from blackbox.audio_to_text import AudioToText
|
||||
from blackbox.blackbox import Blackbox
|
||||
from blackbox.text_to_audio import TextToAudio
|
||||
|
||||
|
||||
class BlockboxFactory:
|
||||
|
||||
def create_blockbox(self, blockbox_type: str, blockbox_config: dict) -> Blackbox:
|
||||
|
||||
if blockbox_type == "audio_to_text":
|
||||
return AudioToText(blockbox_config)
|
||||
if blockbox_type == "text_to_audio":
|
||||
return TextToAudio(blockbox_config)
|
||||
raise ValueError("Invalid blockbox type")
|
||||
38
src/blackbox/calculator.py
Normal file
38
src/blackbox/calculator.py
Normal file
@ -0,0 +1,38 @@
|
||||
from fastapi import status
|
||||
from fastapi.responses import JSONResponse
|
||||
from blackbox.blackbox import Blackbox
|
||||
|
||||
|
||||
class Calculator(Blackbox):
|
||||
|
||||
"""This class just for example, it show how to implement Blackbox interface."""
|
||||
|
||||
def valid(self, data: any) -> bool:
|
||||
return isinstance(data, dict) and "operation" in data and "a" in data and "b" in data
|
||||
|
||||
def processing(self, data: dict) -> any:
|
||||
if not self.valid(data):
|
||||
raise ValueError("Invalid data")
|
||||
a = data["a"]
|
||||
b = data["b"]
|
||||
op = data["operation"]
|
||||
if op == "add":
|
||||
return a + b
|
||||
if op == "sub":
|
||||
return a - b
|
||||
if op == "mul":
|
||||
return a * b
|
||||
if op == "div":
|
||||
return a / b
|
||||
raise ValueError("Invalid operation")
|
||||
|
||||
async def fast_api_handler(self, request) -> any:
|
||||
try:
|
||||
data = await request.json()
|
||||
except:
|
||||
return JSONResponse(content={"error": "json parse error"}, status_code=status.HTTP_400_BAD_REQUEST)
|
||||
try:
|
||||
result = self.processing(data)
|
||||
except ValueError as e:
|
||||
return JSONResponse(content={"error": str(e)}, status_code=status.HTTP_400_BAD_REQUEST)
|
||||
return JSONResponse(content={"result": result}, status_code=status.HTTP_200_OK)
|
||||
@ -1,19 +1,19 @@
|
||||
from typing import Union
|
||||
|
||||
from fastapi import FastAPI, Request, UploadFile, status
|
||||
from fastapi import FastAPI, Request, status
|
||||
from fastapi.responses import JSONResponse
|
||||
|
||||
from blackbox.blockbox_factory import BlockboxFactory
|
||||
from blackbox.blackbox_factory import BlackboxFactory
|
||||
app = FastAPI()
|
||||
|
||||
blackbox_factory = BlockboxFactory()
|
||||
blackbox_factory = BlackboxFactory()
|
||||
|
||||
@app.post("/")
|
||||
async def blackbox(blackbox_name: Union[str, None] = None, request: Request = None):
|
||||
if not blackbox_name:
|
||||
return await JSONResponse(content={"error": "blackbox_name is required"}, status_code=status.HTTP_400_BAD_REQUEST)
|
||||
try:
|
||||
box = blackbox_factory.create_blockbox(blackbox_name, {})
|
||||
box = blackbox_factory.create_blackbox(blackbox_name, {})
|
||||
except ValueError:
|
||||
return await JSONResponse(content={"error": "value error"}, status_code=status.HTTP_400_BAD_REQUEST)
|
||||
return await box.fast_api_handler(request)
|
||||
|
||||
Reference in New Issue
Block a user