2024-01-25 17:26:27 -07:00
|
|
|
import concurrent.futures
|
2023-08-30 01:22:01 -07:00
|
|
|
import logging
|
2023-08-24 21:28:51 -07:00
|
|
|
import os
|
2023-12-14 12:51:24 -07:00
|
|
|
import sys
|
2023-06-24 20:18:09 -07:00
|
|
|
from pathlib import Path
|
2023-12-14 12:51:24 -07:00
|
|
|
from socket import socket
|
2023-06-24 20:18:09 -07:00
|
|
|
|
2023-12-14 12:51:24 -07:00
|
|
|
from gunicorn.arbiter import Arbiter
|
2023-06-17 20:49:19 -07:00
|
|
|
from pydantic import BaseSettings
|
2023-08-30 01:22:01 -07:00
|
|
|
from rich.console import Console
|
|
|
|
from rich.logging import RichHandler
|
2023-12-14 12:51:24 -07:00
|
|
|
from uvicorn import Server
|
|
|
|
from uvicorn.workers import UvicornWorker
|
2023-06-17 20:49:19 -07:00
|
|
|
|
2023-06-24 20:18:09 -07:00
|
|
|
from .schemas import ModelType
|
|
|
|
|
|
|
|
|
2023-06-17 20:49:19 -07:00
|
|
|
class Settings(BaseSettings):
|
|
|
|
cache_folder: str = "/cache"
|
2023-11-16 19:42:44 -07:00
|
|
|
model_ttl: int = 300
|
|
|
|
model_ttl_poll_s: int = 10
|
2023-06-17 20:49:19 -07:00
|
|
|
host: str = "0.0.0.0"
|
|
|
|
port: int = 3003
|
|
|
|
workers: int = 1
|
2023-06-27 16:21:33 -07:00
|
|
|
test_full: bool = False
|
2023-08-24 21:28:51 -07:00
|
|
|
request_threads: int = os.cpu_count() or 4
|
2024-01-21 16:22:39 -07:00
|
|
|
model_inter_op_threads: int = 0
|
|
|
|
model_intra_op_threads: int = 0
|
2024-01-11 10:26:46 -07:00
|
|
|
ann: bool = True
|
2023-06-17 20:49:19 -07:00
|
|
|
|
2023-08-05 19:45:13 -07:00
|
|
|
class Config:
|
2023-06-24 20:18:09 -07:00
|
|
|
env_prefix = "MACHINE_LEARNING_"
|
2023-06-17 20:49:19 -07:00
|
|
|
case_sensitive = False
|
|
|
|
|
|
|
|
|
2023-08-30 01:22:01 -07:00
|
|
|
class LogSettings(BaseSettings):
|
|
|
|
log_level: str = "info"
|
|
|
|
no_color: bool = False
|
|
|
|
|
|
|
|
class Config:
|
|
|
|
case_sensitive = False
|
|
|
|
|
|
|
|
|
2023-08-24 21:28:51 -07:00
|
|
|
_clean_name = str.maketrans(":\\/", "___", ".")
|
|
|
|
|
|
|
|
|
2023-11-11 18:04:49 -07:00
|
|
|
def clean_name(model_name: str) -> str:
|
|
|
|
return model_name.split("/")[-1].translate(_clean_name)
|
|
|
|
|
|
|
|
|
2023-06-24 20:18:09 -07:00
|
|
|
def get_cache_dir(model_name: str, model_type: ModelType) -> Path:
|
2023-11-11 18:04:49 -07: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-24 20:18:09 -07:00
|
|
|
|
|
|
|
|
2023-08-30 01:22:01 -07: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-17 20:49:19 -07:00
|
|
|
settings = Settings()
|
2023-08-30 01:22:01 -07:00
|
|
|
log_settings = LogSettings()
|
|
|
|
|
2024-01-27 17:50:50 -07:00
|
|
|
LOG_LEVEL = LOG_LEVELS.get(log_settings.log_level.lower(), logging.INFO)
|
|
|
|
|
2023-08-31 16:30:53 -07:00
|
|
|
|
|
|
|
class CustomRichHandler(RichHandler):
|
|
|
|
def __init__(self) -> None:
|
|
|
|
console = Console(color_system="standard", no_color=log_settings.no_color)
|
2024-01-25 17:26:27 -07:00
|
|
|
self.excluded = ["uvicorn", "starlette", "fastapi"]
|
|
|
|
super().__init__(
|
|
|
|
show_path=False,
|
|
|
|
omit_repeated_times=False,
|
|
|
|
console=console,
|
|
|
|
rich_tracebacks=True,
|
|
|
|
tracebacks_suppress=[*self.excluded, concurrent.futures],
|
2024-01-27 17:50:50 -07:00
|
|
|
tracebacks_show_locals=LOG_LEVEL == logging.DEBUG,
|
2024-01-25 17:26:27 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
# hack to exclude certain modules from rich tracebacks
|
|
|
|
def emit(self, record: logging.LogRecord) -> None:
|
|
|
|
if record.exc_info is not None:
|
|
|
|
tb = record.exc_info[2]
|
|
|
|
while tb is not None:
|
|
|
|
if any(excluded in tb.tb_frame.f_code.co_filename for excluded in self.excluded):
|
|
|
|
tb.tb_frame.f_locals["_rich_traceback_omit"] = True
|
|
|
|
tb = tb.tb_next
|
|
|
|
|
|
|
|
return super().emit(record)
|
|
|
|
|
|
|
|
|
|
|
|
log = logging.getLogger("ml.log")
|
2024-01-27 17:50:50 -07:00
|
|
|
log.setLevel(LOG_LEVEL)
|
2023-12-14 12:51:24 -07:00
|
|
|
|
|
|
|
|
|
|
|
# patches this issue https://github.com/encode/uvicorn/discussions/1803
|
|
|
|
class CustomUvicornServer(Server):
|
|
|
|
async def shutdown(self, sockets: list[socket] | None = None) -> None:
|
|
|
|
for sock in sockets or []:
|
|
|
|
sock.close()
|
|
|
|
await super().shutdown()
|
|
|
|
|
|
|
|
|
|
|
|
class CustomUvicornWorker(UvicornWorker):
|
|
|
|
async def _serve(self) -> None:
|
|
|
|
self.config.app = self.wsgi
|
|
|
|
server = CustomUvicornServer(config=self.config)
|
|
|
|
self._install_sigquit_handler()
|
|
|
|
await server.serve(sockets=self.sockets)
|
|
|
|
if not server.started:
|
|
|
|
sys.exit(Arbiter.WORKER_BOOT_ERROR)
|