Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 41 additions & 13 deletions src/cnlpt/_cli/rest.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,43 @@
from typing import Union

import click

from ..api import MODEL_TYPES, get_rest_app

def parse_models(
ctx: click.Context,
param: click.Parameter,
value: Union[tuple[str, ...], None],
):
if value is None:
return None

models: list[tuple[str, str]] = []
for item in value:
if "=" in item:
prefix, path = item.split("=", 1)
if not prefix.startswith("/"):
raise click.BadParameter(
f"route prefix must start with '/': {prefix}", param=param
)
elif len(value) > 1:
raise click.BadParameter(
"route prefixes are required when serving more than one model",
param=param,
)
else:
path = item
prefix = ""
models.append((prefix, path))
return models


@click.command("rest", context_settings={"show_default": True})
@click.option(
"--model-type",
type=click.Choice(MODEL_TYPES),
required=True,
"--model",
"models",
multiple=True,
callback=parse_models,
help="Model definition as [ROUTER_PREFIX=]PATH_TO_MODEL. Prefix must start with '/'.",
)
@click.option(
"-h",
Expand All @@ -19,15 +49,13 @@
@click.option(
"-p", "--port", type=int, default=8000, help="Port to serve the REST app."
)
@click.option(
"--reload",
type=bool,
is_flag=True,
default=False,
help="Auto-reload the REST app.",
)
def rest_command(model_type: str, host: str, port: int, reload: bool):
def rest_command(models: list[tuple[str, str]], host: str, port: int):
"""Start a REST application from a model."""
import uvicorn

uvicorn.run(get_rest_app(model_type), host=host, port=port, reload=reload)
from ..rest import CnlpRestApp

app = CnlpRestApp.multi_app(
[(CnlpRestApp(model_path=path), prefix) for prefix, path in models]
)
uvicorn.run(app, host=host, port=port)
71 changes: 0 additions & 71 deletions src/cnlpt/api/__init__.py

This file was deleted.

102 changes: 0 additions & 102 deletions src/cnlpt/api/cnn_rest.py

This file was deleted.

106 changes: 0 additions & 106 deletions src/cnlpt/api/current_rest.py

This file was deleted.

Loading
Loading