1
0
Fork 0
onnx-web/api/onnx_web/server/utils.py

41 lines
1.1 KiB
Python
Raw Normal View History

2023-02-26 20:15:30 +00:00
from functools import partial, update_wrapper
from os import makedirs, path
from typing import Callable, Dict, List, Tuple
from flask import Flask
2023-02-27 02:09:42 +00:00
from ..utils import base_join
from ..worker.pool import DevicePoolExecutor
from .context import ServerContext
def check_paths(server: ServerContext) -> None:
if not path.exists(server.model_path):
raise RuntimeError("model path must exist")
if not path.exists(server.output_path):
makedirs(server.output_path)
def get_model_path(server: ServerContext, model: str):
return base_join(server.model_path, model)
2023-02-26 20:15:30 +00:00
def register_routes(
app: Flask,
server: ServerContext,
2023-02-26 20:15:30 +00:00
pool: DevicePoolExecutor,
routes: List[Tuple[str, Dict, Callable]],
):
2023-02-27 02:09:42 +00:00
for route, kwargs, method in routes:
app.route(route, **kwargs)(wrap_route(method, server, pool=pool))
def wrap_route(func, *args, **kwargs):
2023-03-17 03:29:07 +00:00
"""
From http://louistiao.me/posts/adding-__name__-and-__doc__-attributes-to-functoolspartial-objects/
"""
partial_func = partial(func, *args, **kwargs)
update_wrapper(partial_func, func)
return partial_func