doc: update swagger

This commit is contained in:
superobk
2024-04-25 15:07:18 +08:00
parent 201d0f374c
commit c062abbe19
4 changed files with 19 additions and 79 deletions

View File

@ -1,11 +1,9 @@
from .audio_chat import AudioChat
from .sum import SUM
from .sentiment import Sentiment
from .tts import TTS
from .asr import ASR
from .audio_to_text import AudioToText
from .blackbox import Blackbox
from .calculator import Calculator
from .text_to_audio import TextToAudio
from .tesou import Tesou
from .fastchat import Fastchat
@ -18,8 +16,6 @@ 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()
self.tesou = Tesou()
@ -36,16 +32,12 @@ class BlackboxFactory:
return self.audio_to_text
if blackbox_name == "text_to_audio":
return self.text_to_audio
if blackbox_name == "calculator":
return self.calculator
if blackbox_name == "asr":
return self.asr
if blackbox_name == "tts":
return self.tts
if blackbox_name == "sentiment_engine":
return self.sentiment
if blackbox_name == "sum":
return self.sum
if blackbox_name == "tesou":
return self.tesou
if blackbox_name == "fastchat":

View File

@ -1,42 +0,0 @@
from fastapi import status
from fastapi.responses import JSONResponse
from .blackbox import Blackbox
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
def processing(self, data: dict) -> int | float:
if not self.valid(data):
raise ValueError("Invalid data")
a = data["left"]
b = data["right"]
op = data["op"]
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)

View File

@ -1,27 +0,0 @@
from typing import Any, Coroutine
from fastapi import Request, Response, status
from fastapi.responses import JSONResponse
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):
return sum(args)
async def fast_api_handler(self, request: Request) -> Response:
try:
data = await request.json()
except:
return JSONResponse(content={"error": "json parse error"}, status_code=status.HTTP_400_BAD_REQUEST)
if not self.valid(data):
return JSONResponse(content={"error": "format error"}, status_code=status.HTTP_400_BAD_REQUEST)
return JSONResponse(content={"result": self.processing(data)}, status_code=status.HTTP_200_OK)

View File

@ -54,10 +54,19 @@ components:
audio:
type: string
format: binary
TextAndImageInput:
type: object
properties:
text:
type: string
image:
type: string
format: binary
Input:
oneOf:
- $ref: "#/components/schemas/TextToAudioInput"
- $ref: "#/components/schemas/AudioToTextInput"
- $ref: "#/components/schemas/TextAndImageInput"
TextResult:
type: object
properties:
@ -71,5 +80,13 @@ components:
type: string
description: "Blackbox name"
enum:
- "text_to_audio"
- "audio_to_text"
- text_to_audio
- audio_to_text
- asr
- tts
- sentiment_engine
- tesou
- fastchat
- audio_chat
- g2e
- text_and_image