mirror of
https://github.com/BoardWare-Genius/jarvis-models.git
synced 2025-12-13 16:53:24 +00:00
fix: singleton
This commit is contained in:
58
main.py
58
main.py
@ -1,60 +1,4 @@
|
|||||||
from typing import Annotated, Union
|
|
||||||
|
|
||||||
from fastapi import FastAPI, Request, status, Form
|
|
||||||
from fastapi.responses import JSONResponse
|
|
||||||
|
|
||||||
from src.dotchain.runtime.interpreter import program_parser
|
|
||||||
from src.dotchain.runtime.tokenizer import Tokenizer
|
|
||||||
from src.dotchain.runtime.runtime import Runtime
|
|
||||||
from src.blackbox.blackbox_factory import BlackboxFactory
|
|
||||||
import uvicorn
|
import uvicorn
|
||||||
from fastapi.middleware.cors import CORSMiddleware
|
|
||||||
from injector import Injector
|
|
||||||
|
|
||||||
app = FastAPI()
|
|
||||||
|
|
||||||
app.add_middleware(
|
|
||||||
CORSMiddleware,
|
|
||||||
allow_origins=["*"],
|
|
||||||
allow_credentials=True,
|
|
||||||
allow_methods=["*"],
|
|
||||||
allow_headers=["*"],
|
|
||||||
)
|
|
||||||
|
|
||||||
injector = Injector()
|
|
||||||
blackbox_factory = injector.get(BlackboxFactory)
|
|
||||||
|
|
||||||
@app.post("/")
|
|
||||||
async def blackbox(blackbox_name: Union[str, None] = None, request: Request = None):
|
|
||||||
if not blackbox_name:
|
|
||||||
return await JSONResponse(content={"error": "blackbox_name is required"}, status_code=status.HTTP_400_BAD_REQUEST)
|
|
||||||
try:
|
|
||||||
box = blackbox_factory.create_blackbox(blackbox_name)
|
|
||||||
except ValueError:
|
|
||||||
return await JSONResponse(content={"error": "value error"}, status_code=status.HTTP_400_BAD_REQUEST)
|
|
||||||
return await box.fast_api_handler(request)
|
|
||||||
|
|
||||||
def read_form_image(request: Request):
|
|
||||||
async def inner(field: str):
|
|
||||||
print(field)
|
|
||||||
return "image"
|
|
||||||
return inner
|
|
||||||
|
|
||||||
def read_form_text(request: Request):
|
|
||||||
def inner(field: str):
|
|
||||||
print(field)
|
|
||||||
return "text"
|
|
||||||
return inner
|
|
||||||
|
|
||||||
@app.post("/workflows")
|
|
||||||
async def workflows(script: Annotated[str, Form()], request: Request=None):
|
|
||||||
dsl_runtime = Runtime(exteral_fun={"print": print,
|
|
||||||
'read_form_image': read_form_image(request),
|
|
||||||
"read_form_text": read_form_text(request)})
|
|
||||||
t = Tokenizer()
|
|
||||||
t.init(script)
|
|
||||||
ast = program_parser(t)
|
|
||||||
ast.exec(dsl_runtime)
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
uvicorn.run("main:app", host="0.0.0.0", port=8000, log_level="info")
|
uvicorn.run("router:app", host="0.0.0.0", port=8000, log_level="info")
|
||||||
|
|||||||
35
router.py
Normal file
35
router.py
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
from typing import Annotated, Union
|
||||||
|
|
||||||
|
from fastapi import FastAPI, Request, status, Form
|
||||||
|
from fastapi.responses import JSONResponse
|
||||||
|
|
||||||
|
from src.dotchain.runtime.interpreter import program_parser
|
||||||
|
from src.dotchain.runtime.tokenizer import Tokenizer
|
||||||
|
from src.dotchain.runtime.runtime import Runtime
|
||||||
|
from src.blackbox.blackbox_factory import BlackboxFactory
|
||||||
|
import uvicorn
|
||||||
|
from fastapi.middleware.cors import CORSMiddleware
|
||||||
|
from injector import Injector
|
||||||
|
|
||||||
|
app = FastAPI()
|
||||||
|
|
||||||
|
app.add_middleware(
|
||||||
|
CORSMiddleware,
|
||||||
|
allow_origins=["*"],
|
||||||
|
allow_credentials=True,
|
||||||
|
allow_methods=["*"],
|
||||||
|
allow_headers=["*"],
|
||||||
|
)
|
||||||
|
|
||||||
|
injector = Injector()
|
||||||
|
blackbox_factory = injector.get(BlackboxFactory)
|
||||||
|
|
||||||
|
@app.post("/")
|
||||||
|
async def blackbox(blackbox_name: Union[str, None] = None, request: Request = None):
|
||||||
|
if not blackbox_name:
|
||||||
|
return await JSONResponse(content={"error": "blackbox_name is required"}, status_code=status.HTTP_400_BAD_REQUEST)
|
||||||
|
try:
|
||||||
|
box = blackbox_factory.call_blackbox(blackbox_name)
|
||||||
|
except ValueError:
|
||||||
|
return await JSONResponse(content={"error": "value error"}, status_code=status.HTTP_400_BAD_REQUEST)
|
||||||
|
return await box.fast_api_handler(request)
|
||||||
@ -7,7 +7,9 @@ from fastapi.responses import JSONResponse
|
|||||||
from ..asr.rapid_paraformer.utils import read_yaml
|
from ..asr.rapid_paraformer.utils import read_yaml
|
||||||
from ..asr.rapid_paraformer import RapidParaformer
|
from ..asr.rapid_paraformer import RapidParaformer
|
||||||
from .blackbox import Blackbox
|
from .blackbox import Blackbox
|
||||||
|
from injector import singleton
|
||||||
|
|
||||||
|
@singleton
|
||||||
class ASR(Blackbox):
|
class ASR(Blackbox):
|
||||||
|
|
||||||
def __init__(self, path = ".env.yaml") -> None:
|
def __init__(self, path = ".env.yaml") -> None:
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
from fastapi import Request, Response,status
|
from fastapi import Request, Response,status
|
||||||
from fastapi.responses import JSONResponse
|
from fastapi.responses import JSONResponse
|
||||||
from injector import inject
|
from injector import inject, singleton
|
||||||
|
|
||||||
from .asr import ASR
|
from .asr import ASR
|
||||||
from .tesou import Tesou
|
from .tesou import Tesou
|
||||||
@ -8,6 +8,7 @@ from .tts import TTS
|
|||||||
|
|
||||||
from .blackbox import Blackbox
|
from .blackbox import Blackbox
|
||||||
|
|
||||||
|
@singleton
|
||||||
class AudioChat(Blackbox):
|
class AudioChat(Blackbox):
|
||||||
|
|
||||||
@inject
|
@inject
|
||||||
|
|||||||
@ -3,9 +3,10 @@ from fastapi.responses import JSONResponse
|
|||||||
import speech_recognition as sr
|
import speech_recognition as sr
|
||||||
import filetype
|
import filetype
|
||||||
import io
|
import io
|
||||||
|
from injector import singleton
|
||||||
from .blackbox import Blackbox
|
from .blackbox import Blackbox
|
||||||
|
|
||||||
|
@singleton
|
||||||
class AudioToText(Blackbox):
|
class AudioToText(Blackbox):
|
||||||
|
|
||||||
def __call__(self, *args, **kwargs):
|
def __call__(self, *args, **kwargs):
|
||||||
|
|||||||
@ -9,8 +9,9 @@ from .tesou import Tesou
|
|||||||
from .fastchat import Fastchat
|
from .fastchat import Fastchat
|
||||||
from .g2e import G2E
|
from .g2e import G2E
|
||||||
from .text_and_image import TextAndImage
|
from .text_and_image import TextAndImage
|
||||||
from injector import inject
|
from injector import inject, singleton
|
||||||
|
|
||||||
|
@singleton
|
||||||
class BlackboxFactory:
|
class BlackboxFactory:
|
||||||
models = {}
|
models = {}
|
||||||
|
|
||||||
@ -40,7 +41,7 @@ class BlackboxFactory:
|
|||||||
def __call__(self, *args, **kwargs):
|
def __call__(self, *args, **kwargs):
|
||||||
return self.processing(*args, **kwargs)
|
return self.processing(*args, **kwargs)
|
||||||
|
|
||||||
def create_blackbox(self, blackbox_name: str) -> Blackbox:
|
def call_blackbox(self, blackbox_name: str) -> Blackbox:
|
||||||
model = self.models.get(blackbox_name)
|
model = self.models.get(blackbox_name)
|
||||||
if model is None:
|
if model is None:
|
||||||
raise ValueError("Invalid blockbox type")
|
raise ValueError("Invalid blockbox type")
|
||||||
|
|||||||
@ -6,8 +6,10 @@ from fastapi.responses import JSONResponse
|
|||||||
from .blackbox import Blackbox
|
from .blackbox import Blackbox
|
||||||
from lagent.llms.lmdepoly_wrapper import LMDeployClient
|
from lagent.llms.lmdepoly_wrapper import LMDeployClient
|
||||||
from lagent.llms.meta_template import INTERNLM2_META as META
|
from lagent.llms.meta_template import INTERNLM2_META as META
|
||||||
|
from injector import singleton
|
||||||
|
|
||||||
class Sentiment(Blackbox):
|
@singleton
|
||||||
|
class Emotion(Blackbox):
|
||||||
|
|
||||||
def __init__(self, model_name, model_url) -> None:
|
def __init__(self, model_name, model_url) -> None:
|
||||||
self.model = LMDeployClient(
|
self.model = LMDeployClient(
|
||||||
@ -7,6 +7,8 @@ from .blackbox import Blackbox
|
|||||||
import requests
|
import requests
|
||||||
import json
|
import json
|
||||||
|
|
||||||
|
from injector import singleton
|
||||||
|
@singleton
|
||||||
class Fastchat(Blackbox):
|
class Fastchat(Blackbox):
|
||||||
|
|
||||||
def __call__(self, *args, **kwargs):
|
def __call__(self, *args, **kwargs):
|
||||||
|
|||||||
@ -7,7 +7,8 @@ from .blackbox import Blackbox
|
|||||||
import requests
|
import requests
|
||||||
import json
|
import json
|
||||||
from openai import OpenAI
|
from openai import OpenAI
|
||||||
|
from injector import singleton
|
||||||
|
@singleton
|
||||||
class G2E(Blackbox):
|
class G2E(Blackbox):
|
||||||
|
|
||||||
def __call__(self, *args, **kwargs):
|
def __call__(self, *args, **kwargs):
|
||||||
|
|||||||
@ -6,7 +6,8 @@ from fastapi.responses import JSONResponse
|
|||||||
from ..sentiment_engine.sentiment_engine import SentimentEngine
|
from ..sentiment_engine.sentiment_engine import SentimentEngine
|
||||||
from .blackbox import Blackbox
|
from .blackbox import Blackbox
|
||||||
|
|
||||||
|
from injector import singleton
|
||||||
|
@singleton
|
||||||
class Sentiment(Blackbox):
|
class Sentiment(Blackbox):
|
||||||
|
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
|
|||||||
@ -7,7 +7,9 @@ from ..configuration import TesouConf
|
|||||||
from .blackbox import Blackbox
|
from .blackbox import Blackbox
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
from injector import singleton
|
||||||
|
|
||||||
|
@singleton
|
||||||
class Tesou(Blackbox):
|
class Tesou(Blackbox):
|
||||||
url: str
|
url: str
|
||||||
|
|
||||||
|
|||||||
@ -4,7 +4,8 @@ from fastapi import Request, Response, status
|
|||||||
from fastapi.responses import JSONResponse
|
from fastapi.responses import JSONResponse
|
||||||
from .blackbox import Blackbox
|
from .blackbox import Blackbox
|
||||||
|
|
||||||
|
from injector import singleton
|
||||||
|
@singleton
|
||||||
class TextAndImage(Blackbox):
|
class TextAndImage(Blackbox):
|
||||||
|
|
||||||
def __call__(self, *args, **kwargs):
|
def __call__(self, *args, **kwargs):
|
||||||
|
|||||||
@ -3,7 +3,8 @@ from fastapi.responses import JSONResponse
|
|||||||
from .blackbox import Blackbox
|
from .blackbox import Blackbox
|
||||||
from gtts import gTTS
|
from gtts import gTTS
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
|
from injector import singleton
|
||||||
|
@singleton
|
||||||
class TextToAudio(Blackbox):
|
class TextToAudio(Blackbox):
|
||||||
|
|
||||||
def __call__(self, *args, **kwargs):
|
def __call__(self, *args, **kwargs):
|
||||||
|
|||||||
@ -6,7 +6,9 @@ from fastapi import Request, Response, status
|
|||||||
from fastapi.responses import JSONResponse
|
from fastapi.responses import JSONResponse
|
||||||
from .blackbox import Blackbox
|
from .blackbox import Blackbox
|
||||||
from ..tts.tts_service import TTService
|
from ..tts.tts_service import TTService
|
||||||
|
from injector import singleton
|
||||||
|
|
||||||
|
@singleton
|
||||||
class TTS(Blackbox):
|
class TTS(Blackbox):
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs) -> None:
|
def __init__(self, *args, **kwargs) -> None:
|
||||||
|
|||||||
Reference in New Issue
Block a user