mirror of
https://github.com/BoardWare-Genius/jarvis-models.git
synced 2025-12-13 16:53:24 +00:00
feat: log
This commit is contained in:
25
main.py
25
main.py
@ -1,4 +1,27 @@
|
||||
import uvicorn
|
||||
import logging
|
||||
from injector import Injector,inject
|
||||
from src.log.handler import LogHandler
|
||||
from src.configuration import EnvConf, LogConf, singleton
|
||||
|
||||
@singleton
|
||||
class Main():
|
||||
env_conf: EnvConf
|
||||
@inject
|
||||
def __init__(self, log_config: LogConf, env_conf: EnvConf, log_handler: LogHandler) -> None:
|
||||
self.env_conf=env_conf
|
||||
logging.basicConfig(
|
||||
handlers=[log_handler],
|
||||
level=log_config.level,
|
||||
datefmt=log_config.time_format,
|
||||
format='%(asctime)s %(message)s')
|
||||
|
||||
def run(self):
|
||||
logger = logging.getLogger(__name__)
|
||||
logger.info("jarvis-models start", "version", "0.0.1")
|
||||
uvicorn.run("server:app", host="0.0.0.0", port=8000, log_level="info")
|
||||
|
||||
if __name__ == "__main__":
|
||||
uvicorn.run("router:app", host="0.0.0.0", port=8000, log_level="info")
|
||||
injector = Injector()
|
||||
main = injector.get(Main)
|
||||
main.run()
|
||||
@ -25,7 +25,7 @@ async def blackbox(blackbox_name: Union[str, None] = None, request: Request = No
|
||||
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)
|
||||
box = blackbox_factory.get_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)
|
||||
@ -4,8 +4,6 @@ from typing import Any, Coroutine
|
||||
from fastapi import Request, Response, status
|
||||
from fastapi.responses import JSONResponse
|
||||
|
||||
from ..log.log import Logger
|
||||
|
||||
from ..asr.rapid_paraformer.utils import read_yaml
|
||||
from ..asr.rapid_paraformer import RapidParaformer
|
||||
from .blackbox import Blackbox
|
||||
@ -15,8 +13,7 @@ from injector import singleton, inject
|
||||
class ASR(Blackbox):
|
||||
|
||||
@inject
|
||||
def __init__(self,logger: Logger,path = ".env.yaml") -> None:
|
||||
logger.info("ASR init")
|
||||
def __init__(self,path = ".env.yaml") -> None:
|
||||
config = read_yaml(path)
|
||||
self.paraformer = RapidParaformer(config)
|
||||
|
||||
@ -38,6 +35,7 @@ class ASR(Blackbox):
|
||||
async def fast_api_handler(self, request: Request) -> Response:
|
||||
data = (await request.form()).get("audio")
|
||||
if data is None:
|
||||
self.logger.warn("asr bag request","type", "fast_api_handler", "api", "asr")
|
||||
return JSONResponse(content={"error": "data is required"}, status_code=status.HTTP_400_BAD_REQUEST)
|
||||
d = await data.read()
|
||||
try:
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
import logging
|
||||
from fastapi import Request, Response,status
|
||||
from fastapi.responses import JSONResponse
|
||||
from injector import inject, singleton
|
||||
@ -8,11 +9,14 @@ from .tts import TTS
|
||||
|
||||
from .blackbox import Blackbox
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@singleton
|
||||
class AudioChat(Blackbox):
|
||||
|
||||
@inject
|
||||
def __init__(self, asr: ASR, gpt: Tesou, tts: TTS):
|
||||
logger.info("audio chat initint")
|
||||
self.asr = asr
|
||||
self.gpt = gpt
|
||||
self.tts = tts
|
||||
|
||||
@ -41,7 +41,7 @@ class BlackboxFactory:
|
||||
def __call__(self, *args, **kwargs):
|
||||
return self.processing(*args, **kwargs)
|
||||
|
||||
def call_blackbox(self, blackbox_name: str) -> Blackbox:
|
||||
def get_blackbox(self, blackbox_name: str) -> Blackbox:
|
||||
model = self.models.get(blackbox_name)
|
||||
if model is None:
|
||||
raise ValueError("Invalid blockbox type")
|
||||
|
||||
@ -1,9 +1,10 @@
|
||||
|
||||
from dataclasses import dataclass
|
||||
from injector import Injector, inject
|
||||
from injector import inject,singleton
|
||||
import yaml
|
||||
import sys
|
||||
import logging
|
||||
|
||||
@singleton
|
||||
class Configuration():
|
||||
|
||||
@inject
|
||||
@ -26,18 +27,19 @@ class Configuration():
|
||||
bbb:
|
||||
ccc: "hello world"
|
||||
"""
|
||||
def get(self, path: str | list[str], cfg: dict = None):
|
||||
def get(self, path: str | list[str], cfg: dict = None, default = None):
|
||||
if isinstance(path, str):
|
||||
if cfg is None:
|
||||
cfg = self.cfg
|
||||
return self.get(path.split("."), cfg)
|
||||
return self.get(path.split("."), cfg, default=default)
|
||||
lenght = len(path)
|
||||
if lenght == 0 or not isinstance(cfg, dict):
|
||||
if default is None:
|
||||
return None
|
||||
return default
|
||||
if lenght == 1:
|
||||
return cfg.get(path[0])
|
||||
return self.get(path[1:], cfg.get(path[0]))
|
||||
|
||||
return self.get(path[1:], cfg.get(path[0]), default=default)
|
||||
|
||||
class TesouConf():
|
||||
url: str
|
||||
@ -46,9 +48,36 @@ class TesouConf():
|
||||
def __init__(self,config: Configuration) -> None:
|
||||
self.url = config.get("tesou.url")
|
||||
|
||||
# 'CRITICAL': CRITICAL,
|
||||
# 'FATAL': FATAL,
|
||||
# 'ERROR': ERROR,
|
||||
# 'WARN': WARNING,
|
||||
# 'WARNING': WARNING,
|
||||
# 'INFO': INFO,
|
||||
# 'DEBUG': DEBUG,
|
||||
# 'NOTSET': NOTSET,
|
||||
DEFAULT_LEVEL="WARNING"
|
||||
DEFAULT_TIME_FORMAT="%Y-%m-%d %H:%M:%S"
|
||||
|
||||
@singleton
|
||||
class LogConf():
|
||||
level: str
|
||||
level: int
|
||||
time_format = "%Y-%m-%d %H:%M:%S"
|
||||
|
||||
@inject
|
||||
def __init__(self,config: Configuration) -> None:
|
||||
self.level = config.get("log.level")
|
||||
c = config.get("log.level", default=DEFAULT_LEVEL).upper()
|
||||
level=logging._nameToLevel.get(c)
|
||||
if level is None:
|
||||
self.level = logging.WARNING
|
||||
else:
|
||||
self.level = level
|
||||
self.time_format=config.get("log.time_format", default=DEFAULT_TIME_FORMAT)
|
||||
|
||||
@singleton
|
||||
class EnvConf():
|
||||
version: str
|
||||
|
||||
@inject
|
||||
def __init__(self, config: Configuration) -> None:
|
||||
self.version = config.get("env.version")
|
||||
13
src/log/handler.py
Normal file
13
src/log/handler.py
Normal file
@ -0,0 +1,13 @@
|
||||
import logging
|
||||
|
||||
class LogHandler(logging.Handler):
|
||||
|
||||
def __init__(self)-> None:
|
||||
logging.Handler.__init__(self=self)
|
||||
|
||||
def emit(self, record) -> None:
|
||||
print("emit", record)
|
||||
|
||||
def handle(self, record) -> None:
|
||||
print("@ handle",record)
|
||||
|
||||
@ -1,15 +0,0 @@
|
||||
import logging
|
||||
from injector import singleton, inject
|
||||
|
||||
from ..configuration import LogConf
|
||||
|
||||
@singleton
|
||||
class Logger(logging.Logger):
|
||||
|
||||
@inject
|
||||
def __init__(self, config: LogConf) -> None:
|
||||
super().__init__(__name__)
|
||||
self.setLevel(config.level)
|
||||
self.addHandler(logging.StreamHandler())
|
||||
self.addHandler(logging.FileHandler('log.log'))
|
||||
|
||||
@ -16,7 +16,6 @@ from src.tts.vits.text.symbols import symbols
|
||||
from src.tts.vits.text import text_to_sequence
|
||||
|
||||
import logging
|
||||
logging.getLogger().setLevel(logging.INFO)
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
|
||||
dirbaspath = __file__.split("\\")[1:-1]
|
||||
|
||||
Reference in New Issue
Block a user