Merge pull request #15 from BoardWare-Genius/workflow

Workflow
This commit is contained in:
Dan218
2024-08-21 10:15:06 +08:00
committed by GitHub
3 changed files with 29 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())
@ -17,31 +17,31 @@ def read_binay(io):
def new_map(): def new_map():
return Literal({}) return Literal({})
def set_map(map: dict, key, value): def set_map_value(map: dict, key, value):
map[key] = value map[key] = value
return map return map
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
if value is list: if value is list:
return value return value
return Literal(value) return Literal(value)
def get_map_int(d: dict, key):
value = d.get(key)
return Literal(int(value))
@singleton @singleton
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.cost = 0
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)
@ -52,8 +52,19 @@ class Workflow(Blackbox):
def sum(self, *args, **kwargs): def sum(self, *args, **kwargs):
return Literal(self.sum_blackbox.processing(*args, **kwargs)) return Literal(self.sum_blackbox.processing(*args, **kwargs))
def get_cost(self):
return self.cost
def blackbox_example(self):
self.cost_increase(10);
return Literal("Blackbox result")
def cost_increase(self, cost):
self.cost+=cost
return self.cost
async def processing(self, *args, **kwargs): async def processing(self, *args, **kwargs):
request = args[0] request: Request = args[0]
json = await request.json() json = await request.json()
content = None content = None
mdeia_type = None mdeia_type = None
@ -67,25 +78,25 @@ class Workflow(Blackbox):
def add_header(key, value): def add_header(key, value):
nonlocal headers nonlocal headers
headers[key] = value headers[key] = value
script = request.query_params["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_value": set_map_value,
"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,
"sum": self.sum, "sum": self.sum,
"read_binay": read_binay, "read_binay": read_binay,
"jsonfiy": jsonfiy, "jsonfiy": jsonfiy,
"get_map_int": get_map_int,
"blackbox_example": self.blackbox_example,
"get_cost": self.get_cost,
} }
) )
ast = program_parser(t) ast = program_parser(t)
@ -97,7 +108,6 @@ class Workflow(Blackbox):
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();