mirror of
https://github.com/BoardWare-Genius/jarvis-models.git
synced 2025-12-13 16:53:24 +00:00
feat: workflow
This commit is contained in:
@ -39,7 +39,7 @@ class AudioToText(Blackbox):
|
|||||||
return JSONResponse(content={"error": "data is required"}, status_code=status.HTTP_400_BAD_REQUEST)
|
return JSONResponse(content={"error": "data is required"}, status_code=status.HTTP_400_BAD_REQUEST)
|
||||||
d = await data.read()
|
d = await data.read()
|
||||||
try:
|
try:
|
||||||
txt = await self.processing(d)
|
txt = self.processing(d)
|
||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
return JSONResponse(content={"error": str(e)}, status_code=status.HTTP_400_BAD_REQUEST)
|
return JSONResponse(content={"error": str(e)}, status_code=status.HTTP_400_BAD_REQUEST)
|
||||||
return JSONResponse(content={"txt": txt}, status_code=status.HTTP_200_OK)
|
return JSONResponse(content={"text": txt}, status_code=status.HTTP_200_OK)
|
||||||
@ -112,12 +112,24 @@ def sum_loader():
|
|||||||
from .sum import Sum
|
from .sum import Sum
|
||||||
return Injector().get(Sum)
|
return Injector().get(Sum)
|
||||||
|
|
||||||
|
@model_loader(lazy=blackboxConf.lazyloading)
|
||||||
|
def audio_to_text_loader():
|
||||||
|
from .audio_to_text import AudioToText
|
||||||
|
return Injector().get(AudioToText)
|
||||||
|
|
||||||
|
@model_loader(lazy=blackboxConf.lazyloading)
|
||||||
|
def text_to_audio_loader():
|
||||||
|
from .text_to_audio import TextToAudio
|
||||||
|
return Injector().get(TextToAudio)
|
||||||
|
|
||||||
@singleton
|
@singleton
|
||||||
class BlackboxFactory:
|
class BlackboxFactory:
|
||||||
models = {}
|
models = {}
|
||||||
|
|
||||||
@inject
|
@inject
|
||||||
def __init__(self,) -> None:
|
def __init__(self,) -> None:
|
||||||
|
self.models["text_to_audio"] = text_to_audio_loader
|
||||||
|
self.models["audio_to_text"] = audio_to_text_loader
|
||||||
self.models["asr"] = asr_loader
|
self.models["asr"] = asr_loader
|
||||||
self.models["tts"] = tts_loader
|
self.models["tts"] = tts_loader
|
||||||
self.models["sentiment_engine"] = sentiment_loader
|
self.models["sentiment_engine"] = sentiment_loader
|
||||||
|
|||||||
@ -4,6 +4,7 @@ from .blackbox import Blackbox
|
|||||||
from gtts import gTTS
|
from gtts import gTTS
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
from injector import singleton
|
from injector import singleton
|
||||||
|
|
||||||
@singleton
|
@singleton
|
||||||
class TextToAudio(Blackbox):
|
class TextToAudio(Blackbox):
|
||||||
|
|
||||||
@ -31,4 +32,4 @@ class TextToAudio(Blackbox):
|
|||||||
if text is None:
|
if text is None:
|
||||||
return JSONResponse(content={"error": "text is required"}, status_code=status.HTTP_400_BAD_REQUEST)
|
return JSONResponse(content={"error": "text is required"}, status_code=status.HTTP_400_BAD_REQUEST)
|
||||||
by = self.processing(text)
|
by = self.processing(text)
|
||||||
return Response(content=by.read(), media_type="audio/mp3", headers={"Content-Disposition": "attachment; filename=audio.mp3"})
|
return Response(content=by.read(), media_type="audio/mp3", headers={"Content-Disposition": "attachment; filename=audio.mp3"})
|
||||||
|
|||||||
@ -1,42 +1,72 @@
|
|||||||
|
|
||||||
|
from .audio_to_text import AudioToText
|
||||||
|
from .text_to_audio import TextToAudio
|
||||||
from .sum import Sum
|
from .sum import Sum
|
||||||
from fastapi import Request
|
from fastapi import Request, Response
|
||||||
from .blackbox import Blackbox
|
from .blackbox import Blackbox
|
||||||
from injector import singleton, inject
|
from injector import singleton, inject
|
||||||
from ..dotchain.runtime.interpreter import program_parser
|
from ..dotchain.runtime.interpreter import program_parser
|
||||||
from ..dotchain.runtime.runtime import Runtime
|
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
|
||||||
|
|
||||||
|
def read_binay(io):
|
||||||
|
return Literal(io.read())
|
||||||
|
|
||||||
|
def new_map():
|
||||||
|
return Literal({})
|
||||||
|
|
||||||
|
def set_map(map: dict, key, value):
|
||||||
|
map[key] = value
|
||||||
|
return map
|
||||||
|
|
||||||
|
def jsonfiy(obj):
|
||||||
|
return Literal(json.dumps(obj))
|
||||||
|
|
||||||
|
def get_value(d: dict, key):
|
||||||
|
value = d.get(key)
|
||||||
|
if value is dict:
|
||||||
|
return value
|
||||||
|
if value is list:
|
||||||
|
return value
|
||||||
|
return Literal(value)
|
||||||
|
|
||||||
@singleton
|
@singleton
|
||||||
class Workflow(Blackbox):
|
class Workflow(Blackbox):
|
||||||
|
|
||||||
@inject
|
@inject
|
||||||
def __init__(self, sum: Sum):
|
def __init__(self, sum: Sum,
|
||||||
|
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)
|
||||||
|
|
||||||
|
def text_to_audio(self, text):
|
||||||
|
return Literal(self.text_to_audio_blackbox.processing(text))
|
||||||
|
|
||||||
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))
|
||||||
|
|
||||||
async def processing(self, *args, **kwargs):
|
async def processing(self, *args, **kwargs):
|
||||||
request = args[0]
|
request = args[0]
|
||||||
json = await request.json()
|
json = await request.json()
|
||||||
result = None
|
content = None
|
||||||
|
mdeia_type = None
|
||||||
def set_result(r):
|
headers = {}
|
||||||
nonlocal result
|
def set_content(c):
|
||||||
result = r
|
nonlocal content
|
||||||
|
content = c
|
||||||
def get_value(d: dict, key):
|
def set_media_type(m):
|
||||||
value = d.get(key)
|
nonlocal mdeia_type
|
||||||
if value is dict:
|
mdeia_type = m
|
||||||
return value
|
def add_header(key, value):
|
||||||
if value is list:
|
nonlocal headers
|
||||||
return value
|
headers[key] = value
|
||||||
return Literal(value)
|
|
||||||
|
|
||||||
script = json["script"]
|
script = json["script"]
|
||||||
t = Tokenizer()
|
t = Tokenizer()
|
||||||
@ -44,18 +74,37 @@ class Workflow(Blackbox):
|
|||||||
runtime = Runtime(
|
runtime = Runtime(
|
||||||
context={"json": json},
|
context={"json": json},
|
||||||
exteral_fun={
|
exteral_fun={
|
||||||
|
"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,
|
"get_value": get_value,
|
||||||
"print": print,
|
"print": print,
|
||||||
"set_result": set_result,
|
"set_content": set_content,
|
||||||
|
"set_media_type": set_media_type,
|
||||||
|
"add_header": add_header,
|
||||||
"sum": self.sum,
|
"sum": self.sum,
|
||||||
|
"read_binay": read_binay,
|
||||||
|
"jsonfiy": jsonfiy,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
ast = program_parser(t)
|
ast = program_parser(t)
|
||||||
ast.exec(runtime)
|
ast.exec(runtime)
|
||||||
return result
|
return Response(content=content, media_type=mdeia_type, headers=headers)
|
||||||
|
|
||||||
def valid(self, *args, **kwargs) -> bool:
|
def valid(self, *args, **kwargs) -> bool:
|
||||||
return super().valid(*args, **kwargs)
|
return super().valid(*args, **kwargs)
|
||||||
|
|
||||||
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();
|
||||||
|
set_map(b,"man","good");
|
||||||
|
let a = new_map();
|
||||||
|
set_map(a, "hello", "world");
|
||||||
|
set_map(a, "hello2", b);
|
||||||
|
set_media_type("application/json");
|
||||||
|
set_content(jsonfiy(a));
|
||||||
|
"""
|
||||||
Reference in New Issue
Block a user