2023-08-30 08:22:01 +00:00
|
|
|
import logging
|
2023-08-25 04:28:51 +00:00
|
|
|
import os
|
2023-06-25 03:18:09 +00:00
|
|
|
from pathlib import Path
|
|
|
|
|
2023-08-31 23:30:53 +00:00
|
|
|
import gunicorn
|
2023-08-30 08:22:01 +00:00
|
|
|
import starlette
|
2023-06-18 03:49:19 +00:00
|
|
|
from pydantic import BaseSettings
|
2023-08-30 08:22:01 +00:00
|
|
|
from rich.console import Console
|
|
|
|
from rich.logging import RichHandler
|
2023-06-18 03:49:19 +00:00
|
|
|
|
2023-06-25 03:18:09 +00:00
|
|
|
from .schemas import ModelType
|
|
|
|
|
|
|
|
|
2023-06-18 03:49:19 +00:00
|
|
|
class Settings(BaseSettings):
|
|
|
|
cache_folder: str = "/cache"
|
2023-11-17 02:42:44 +00:00
|
|
|
model_ttl: int = 300
|
|
|
|
model_ttl_poll_s: int = 10
|
2023-06-18 03:49:19 +00:00
|
|
|
host: str = "0.0.0.0"
|
|
|
|
port: int = 3003
|
|
|
|
workers: int = 1
|
2023-06-27 23:21:33 +00:00
|
|
|
test_full: bool = False
|
2023-08-25 04:28:51 +00:00
|
|
|
request_threads: int = os.cpu_count() or 4
|
|
|
|
model_inter_op_threads: int = 1
|
|
|
|
model_intra_op_threads: int = 2
|
2023-06-18 03:49:19 +00:00
|
|
|
|
2023-08-06 02:45:13 +00:00
|
|
|
class Config:
|
2023-06-25 03:18:09 +00:00
|
|
|
env_prefix = "MACHINE_LEARNING_"
|
2023-06-18 03:49:19 +00:00
|
|
|
case_sensitive = False
|
|
|
|
|
|
|
|
|
2023-08-30 08:22:01 +00:00
|
|
|
class LogSettings(BaseSettings):
|
|
|
|
log_level: str = "info"
|
|
|
|
no_color: bool = False
|
|
|
|
|
|
|
|
class Config:
|
|
|
|
case_sensitive = False
|
|
|
|
|
|
|
|
|
2023-08-25 04:28:51 +00:00
|
|
|
_clean_name = str.maketrans(":\\/", "___", ".")
|
|
|
|
|
|
|
|
|
2023-11-12 01:04:49 +00:00
|
|
|
def clean_name(model_name: str) -> str:
|
|
|
|
return model_name.split("/")[-1].translate(_clean_name)
|
|
|
|
|
|
|
|
|
2023-06-25 03:18:09 +00:00
|
|
|
def get_cache_dir(model_name: str, model_type: ModelType) -> Path:
|
2023-11-12 01:04:49 +00:00
|
|
|
return Path(settings.cache_folder) / model_type.value / clean_name(model_name)
|
|
|
|
|
|
|
|
|
|
|
|
def get_hf_model_name(model_name: str) -> str:
|
|
|
|
return f"immich-app/{clean_name(model_name)}"
|
2023-06-25 03:18:09 +00:00
|
|
|
|
|
|
|
|
2023-08-30 08:22:01 +00:00
|
|
|
LOG_LEVELS: dict[str, int] = {
|
|
|
|
"critical": logging.ERROR,
|
|
|
|
"error": logging.ERROR,
|
|
|
|
"warning": logging.WARNING,
|
|
|
|
"warn": logging.WARNING,
|
|
|
|
"info": logging.INFO,
|
|
|
|
"log": logging.INFO,
|
|
|
|
"debug": logging.DEBUG,
|
|
|
|
"verbose": logging.DEBUG,
|
|
|
|
}
|
|
|
|
|
2023-06-18 03:49:19 +00:00
|
|
|
settings = Settings()
|
2023-08-30 08:22:01 +00:00
|
|
|
log_settings = LogSettings()
|
|
|
|
|
2023-08-31 23:30:53 +00:00
|
|
|
|
|
|
|
class CustomRichHandler(RichHandler):
|
|
|
|
def __init__(self) -> None:
|
|
|
|
console = Console(color_system="standard", no_color=log_settings.no_color)
|
|
|
|
super().__init__(
|
|
|
|
show_path=False, omit_repeated_times=False, console=console, tracebacks_suppress=[gunicorn, starlette]
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
log = logging.getLogger("gunicorn.access")
|
2023-08-30 08:22:01 +00:00
|
|
|
log.setLevel(LOG_LEVELS.get(log_settings.log_level.lower(), logging.INFO))
|