2023-02-19 02:28:21 +00:00
|
|
|
from logging import getLogger
|
|
|
|
from os import environ, path
|
2023-04-20 12:36:31 +00:00
|
|
|
from secrets import token_urlsafe
|
2023-03-01 03:44:52 +00:00
|
|
|
from typing import List, Optional
|
2023-02-19 02:28:21 +00:00
|
|
|
|
2023-03-22 02:45:27 +00:00
|
|
|
import torch
|
|
|
|
|
2023-02-19 02:28:21 +00:00
|
|
|
from ..utils import get_boolean
|
|
|
|
from .model_cache import ModelCache
|
|
|
|
|
|
|
|
logger = getLogger(__name__)
|
|
|
|
|
2023-03-18 18:47:39 +00:00
|
|
|
DEFAULT_CACHE_LIMIT = 5
|
2023-03-18 18:40:15 +00:00
|
|
|
DEFAULT_JOB_LIMIT = 10
|
|
|
|
DEFAULT_IMAGE_FORMAT = "png"
|
|
|
|
|
2023-02-19 02:28:21 +00:00
|
|
|
|
|
|
|
class ServerContext:
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
bundle_path: str = ".",
|
|
|
|
model_path: str = ".",
|
|
|
|
output_path: str = ".",
|
|
|
|
params_path: str = ".",
|
|
|
|
cors_origin: str = "*",
|
|
|
|
any_platform: bool = True,
|
2023-03-01 03:44:52 +00:00
|
|
|
block_platforms: Optional[List[str]] = None,
|
|
|
|
default_platform: Optional[str] = None,
|
2023-03-18 18:40:15 +00:00
|
|
|
image_format: str = DEFAULT_IMAGE_FORMAT,
|
|
|
|
cache_limit: int = DEFAULT_CACHE_LIMIT,
|
2023-03-01 03:44:52 +00:00
|
|
|
cache_path: Optional[str] = None,
|
2023-02-19 02:28:21 +00:00
|
|
|
show_progress: bool = True,
|
2023-03-01 03:44:52 +00:00
|
|
|
optimizations: Optional[List[str]] = None,
|
2023-03-05 04:57:31 +00:00
|
|
|
extra_models: Optional[List[str]] = None,
|
2023-03-18 18:40:15 +00:00
|
|
|
job_limit: int = DEFAULT_JOB_LIMIT,
|
|
|
|
memory_limit: Optional[int] = None,
|
2023-04-20 12:36:31 +00:00
|
|
|
admin_token: Optional[str] = None,
|
2023-02-19 02:28:21 +00:00
|
|
|
) -> None:
|
|
|
|
self.bundle_path = bundle_path
|
|
|
|
self.model_path = model_path
|
|
|
|
self.output_path = output_path
|
|
|
|
self.params_path = params_path
|
|
|
|
self.cors_origin = cors_origin
|
|
|
|
self.any_platform = any_platform
|
2023-02-19 13:37:29 +00:00
|
|
|
self.block_platforms = block_platforms or []
|
2023-02-19 02:28:21 +00:00
|
|
|
self.default_platform = default_platform
|
|
|
|
self.image_format = image_format
|
2023-03-11 14:17:27 +00:00
|
|
|
self.cache_limit = cache_limit
|
2023-02-19 02:28:21 +00:00
|
|
|
self.cache_path = cache_path or path.join(model_path, ".cache")
|
|
|
|
self.show_progress = show_progress
|
2023-02-19 13:37:29 +00:00
|
|
|
self.optimizations = optimizations or []
|
2023-03-05 04:57:31 +00:00
|
|
|
self.extra_models = extra_models or []
|
2023-03-18 18:40:15 +00:00
|
|
|
self.job_limit = job_limit
|
|
|
|
self.memory_limit = memory_limit
|
2023-04-20 12:36:31 +00:00
|
|
|
self.admin_token = admin_token or token_urlsafe()
|
2023-03-18 18:40:15 +00:00
|
|
|
|
|
|
|
self.cache = ModelCache(self.cache_limit)
|
2023-02-19 02:28:21 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_environ(cls):
|
2023-03-18 18:40:15 +00:00
|
|
|
memory_limit = environ.get("ONNX_WEB_MEMORY_LIMIT", None)
|
|
|
|
if memory_limit is not None:
|
|
|
|
memory_limit = int(memory_limit)
|
|
|
|
|
2023-02-19 02:28:21 +00:00
|
|
|
return cls(
|
|
|
|
bundle_path=environ.get(
|
|
|
|
"ONNX_WEB_BUNDLE_PATH", path.join("..", "gui", "out")
|
|
|
|
),
|
|
|
|
model_path=environ.get("ONNX_WEB_MODEL_PATH", path.join("..", "models")),
|
|
|
|
output_path=environ.get("ONNX_WEB_OUTPUT_PATH", path.join("..", "outputs")),
|
|
|
|
params_path=environ.get("ONNX_WEB_PARAMS_PATH", "."),
|
|
|
|
# others
|
|
|
|
cors_origin=environ.get("ONNX_WEB_CORS_ORIGIN", "*").split(","),
|
|
|
|
any_platform=get_boolean(environ, "ONNX_WEB_ANY_PLATFORM", True),
|
|
|
|
block_platforms=environ.get("ONNX_WEB_BLOCK_PLATFORMS", "").split(","),
|
|
|
|
default_platform=environ.get("ONNX_WEB_DEFAULT_PLATFORM", None),
|
|
|
|
image_format=environ.get("ONNX_WEB_IMAGE_FORMAT", "png"),
|
2023-03-18 18:40:15 +00:00
|
|
|
cache_limit=int(environ.get("ONNX_WEB_CACHE_MODELS", DEFAULT_CACHE_LIMIT)),
|
2023-02-19 02:28:21 +00:00
|
|
|
show_progress=get_boolean(environ, "ONNX_WEB_SHOW_PROGRESS", True),
|
|
|
|
optimizations=environ.get("ONNX_WEB_OPTIMIZATIONS", "").split(","),
|
2023-03-05 04:57:31 +00:00
|
|
|
extra_models=environ.get("ONNX_WEB_EXTRA_MODELS", "").split(","),
|
2023-03-18 18:40:15 +00:00
|
|
|
job_limit=int(environ.get("ONNX_WEB_JOB_LIMIT", DEFAULT_JOB_LIMIT)),
|
|
|
|
memory_limit=memory_limit,
|
2023-02-19 02:28:21 +00:00
|
|
|
)
|
2023-03-22 02:45:27 +00:00
|
|
|
|
|
|
|
def torch_dtype(self):
|
|
|
|
if "torch-fp16" in self.optimizations:
|
|
|
|
return torch.float16
|
|
|
|
else:
|
|
|
|
return torch.float32
|