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 logging
from injector import Injector,inject
from src.log.handler import LogHandler
from src.configuration import EnvConf, LogConf, singleton
from src.configuration import LogConf, singleton
@singleton
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 fastapi import Request, Response
from .blackbox import Blackbox
@ -10,6 +8,8 @@ from ..dotchain.runtime.runtime import Runtime
from ..dotchain.runtime.tokenizer import Tokenizer
from ..dotchain.runtime.ast import Literal
import json
import re
def read_binay(io):
return Literal(io.read())
@ -24,7 +24,7 @@ def set_map(map: dict, key, value):
def jsonfiy(obj):
return Literal(json.dumps(obj))
def get_value(d: dict, key):
def get_map_value(d: dict, key):
value = d.get(key)
if value is dict:
return value
@ -36,12 +36,8 @@ def get_value(d: dict, key):
class Workflow(Blackbox):
@inject
def __init__(self, sum: Sum,
audio_to_text: AudioToText,
text_to_audio: TextToAudio) -> None:
def __init__(self, sum: Sum) -> None:
self.sum_blackbox = sum
self.audio_to_text = audio_to_text
self.text_to_audio_blackbox = text_to_audio
def __call__(self, *args, **kwargs):
return self.processing(*args, **kwargs)
@ -54,6 +50,18 @@ class Workflow(Blackbox):
async def processing(self, *args, **kwargs):
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()
content = None
mdeia_type = None
@ -67,19 +75,16 @@ class Workflow(Blackbox):
def add_header(key, value):
nonlocal headers
headers[key] = value
script = json["script"]
t = Tokenizer()
t.init(script)
runtime = Runtime(
context={"json": json},
exteral_fun={
"print": print,
"new_map": new_map,
"set_map": set_map,
"audio_to_text": self.audio_to_text,
"text_to_audio": self.text_to_audio,
"get_value": get_value,
"print": print,
"get_map_value": get_map_value,
"set_content": set_content,
"set_media_type": set_media_type,
"add_header": add_header,
@ -91,13 +96,13 @@ class Workflow(Blackbox):
ast = program_parser(t)
ast.exec(runtime)
return Response(content=content, media_type=mdeia_type, headers=headers)
def valid(self, *args, **kwargs) -> bool:
return super().valid(*args, **kwargs)
# formdata mode 保留 script 字段給 runtime 解析
async def handler_with_formdata_mode(self,request: Request):
json = await request.json()
print("form")
async def fast_api_handler(self, request: 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();