2023-02-26 20:15:30 +00:00
|
|
|
from functools import partial, update_wrapper
|
2023-02-26 16:15:12 +00:00
|
|
|
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
|
2023-02-26 16:15:12 +00:00
|
|
|
from .context import ServerContext
|
|
|
|
|
|
|
|
|
2023-04-10 01:33:03 +00:00
|
|
|
def check_paths(server: ServerContext) -> None:
|
|
|
|
if not path.exists(server.model_path):
|
2023-02-26 16:15:12 +00:00
|
|
|
raise RuntimeError("model path must exist")
|
|
|
|
|
2023-04-10 01:33:03 +00:00
|
|
|
if not path.exists(server.output_path):
|
|
|
|
makedirs(server.output_path)
|
2023-02-26 16:15:12 +00:00
|
|
|
|
|
|
|
|
2023-04-10 01:33:03 +00:00
|
|
|
def get_model_path(server: ServerContext, model: str):
|
|
|
|
return base_join(server.model_path, model)
|
2023-02-26 16:15:12 +00:00
|
|
|
|
|
|
|
|
2023-02-26 20:15:30 +00:00
|
|
|
def register_routes(
|
|
|
|
app: Flask,
|
2023-04-10 01:33:03 +00:00
|
|
|
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:
|
2023-04-10 01:33:03 +00:00
|
|
|
app.route(route, **kwargs)(wrap_route(method, server, pool=pool))
|
2023-02-26 16:15:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
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/
|
|
|
|
"""
|
2023-02-26 16:15:12 +00:00
|
|
|
partial_func = partial(func, *args, **kwargs)
|
|
|
|
update_wrapper(partial_func, func)
|
|
|
|
return partial_func
|