feat: workflow

This commit is contained in:
2024-08-20 16:26:20 +08:00
parent 30dedc935f
commit 96d964361e
3 changed files with 24 additions and 20 deletions

View File

@ -1,8 +1,7 @@
import uvicorn import uvicorn
import logging import logging
from injector import Injector,inject from injector import Injector,inject
from src.log.handler import LogHandler from src.configuration import LogConf, singleton
from src.configuration import EnvConf, LogConf, singleton
@singleton @singleton
class Main(): class Main():

0
src/blackbox/__init__.py Normal file
View File

View File

@ -1,6 +1,4 @@
from .audio_to_text import AudioToText
from .text_to_audio import TextToAudio
from .sum import Sum from .sum import Sum
from fastapi import Request, Response from fastapi import Request, Response
from .blackbox import Blackbox from .blackbox import Blackbox
@ -10,6 +8,8 @@ from ..dotchain.runtime.runtime import Runtime
from ..dotchain.runtime.tokenizer import Tokenizer from ..dotchain.runtime.tokenizer import Tokenizer
from ..dotchain.runtime.ast import Literal from ..dotchain.runtime.ast import Literal
import json import json
import re
def read_binay(io): def read_binay(io):
return Literal(io.read()) return Literal(io.read())
@ -24,7 +24,7 @@ def set_map(map: dict, key, value):
def jsonfiy(obj): def jsonfiy(obj):
return Literal(json.dumps(obj)) return Literal(json.dumps(obj))
def get_value(d: dict, key): def get_map_value(d: dict, key):
value = d.get(key) value = d.get(key)
if value is dict: if value is dict:
return value return value
@ -36,12 +36,8 @@ def get_value(d: dict, key):
class Workflow(Blackbox): class Workflow(Blackbox):
@inject @inject
def __init__(self, sum: Sum, def __init__(self, sum: Sum) -> None:
audio_to_text: AudioToText,
text_to_audio: TextToAudio) -> None:
self.sum_blackbox = sum self.sum_blackbox = sum
self.audio_to_text = audio_to_text
self.text_to_audio_blackbox = text_to_audio
def __call__(self, *args, **kwargs): def __call__(self, *args, **kwargs):
return self.processing(*args, **kwargs) return self.processing(*args, **kwargs)
@ -54,6 +50,18 @@ class Workflow(Blackbox):
async def processing(self, *args, **kwargs): async def processing(self, *args, **kwargs):
request = args[0] request = args[0]
content_type = request.headers.get("Content-Type")
if re.match(r"application/json", content_type) is not None:
return await self.handler_with_json_mode(request)
elif re.match(r"multipart/form-data", content_type) is not None:
return await self.handler_with_formdata_mode(request)
return Response(content="Invalid content type", status_code=400)
def valid(self, *args, **kwargs) -> bool:
return super().valid(*args, **kwargs)
# json mode 保留 script 字段給 runtime 解析
async def handler_with_json_mode(self,request: Request):
json = await request.json() json = await request.json()
content = None content = None
mdeia_type = None mdeia_type = None
@ -67,19 +75,16 @@ class Workflow(Blackbox):
def add_header(key, value): def add_header(key, value):
nonlocal headers nonlocal headers
headers[key] = value headers[key] = value
script = json["script"] script = json["script"]
t = Tokenizer() t = Tokenizer()
t.init(script) t.init(script)
runtime = Runtime( runtime = Runtime(
context={"json": json}, context={"json": json},
exteral_fun={ exteral_fun={
"print": print,
"new_map": new_map, "new_map": new_map,
"set_map": set_map, "set_map": set_map,
"audio_to_text": self.audio_to_text, "get_map_value": get_map_value,
"text_to_audio": self.text_to_audio,
"get_value": get_value,
"print": print,
"set_content": set_content, "set_content": set_content,
"set_media_type": set_media_type, "set_media_type": set_media_type,
"add_header": add_header, "add_header": add_header,
@ -91,13 +96,13 @@ class Workflow(Blackbox):
ast = program_parser(t) ast = program_parser(t)
ast.exec(runtime) ast.exec(runtime)
return Response(content=content, media_type=mdeia_type, headers=headers) return Response(content=content, media_type=mdeia_type, headers=headers)
# formdata mode 保留 script 字段給 runtime 解析
def valid(self, *args, **kwargs) -> bool: async def handler_with_formdata_mode(self,request: Request):
return super().valid(*args, **kwargs) json = await request.json()
print("form")
async def fast_api_handler(self, request: Request): async def fast_api_handler(self, request: Request):
return await self.processing(request) return await self.processing(request)
# return Response(content=b.read(), media_type="audio/mp3", headers={"Content-Disposition": "attachment; filename=audio.mp3"})
""" """
let b = new_map(); let b = new_map();