clams.restify package

Package providing wrapper class for running a CLAMS app as a HTTP app.

class clams.restify.ClamsHTTPApi(cla_instance: ClamsApp)[source]

Bases: Resource

ClamsHTTPApi provides mapping from HTTP verbs to Python API defined in ClamsApp.

Constructor takes an instance of ClamsApp.

get() → Response[source]

Maps HTTP GET verb to appmetadata().

Returns:

Returns app metadata in a HTTP response.

static json_to_response(json_str: str, status=200) → Response[source]

Helper method to convert JSON output from a ClamsApp to a HTTP response.

Parameters:
  • json_str – a serialized JSON .

  • status – a numerical HTTP code to respond.

Returns:

A HTTP response ready to send.

methods: t.ClassVar[t.Collection[str] | None] = {'GET', 'POST', 'PUT'}

The methods this view is registered for. Uses the same default (["GET", "HEAD", "OPTIONS"]) as route and add_url_rule by default.

post() → Response[source]

Maps HTTP POST verb to annotate(). Note that for now HTTP PUT verbs is also mapped to annotate().

Returns:

Returns MMIF output from a ClamsApp in a HTTP response.

put() → Response[source]

Maps HTTP POST verb to annotate(). Note that for now HTTP PUT verbs is also mapped to annotate().

Returns:

Returns MMIF output from a ClamsApp in a HTTP response.

clams.restify.PRODUCTION_WORKERS_ATTR = 'RESTIFIER_PRODUCTION_WORKERS'

Names of the optional ClamsApp class attributes that Restifier.serve_production() reads: the gunicorn worker count, and the number of requests a worker serves before it is replaced. An app declares them when it holds something that must not be forked or restarted with the worker, e.g. a model loaded in a child process (ClamsGGUFPromptableApp). See Restifier.serve_production() for their precedence.

class clams.restify.Restifier(app_instance: ClamsApp, loopback: bool = False, port: int = 5000, debug: bool = True)[source]

Bases: object

Restifier is a simple wrapper that takes a ClamsApp object and turn it into a flaks-based HTTP server. For mapping between Python API and HTTP API, it relies on ClamsHTTPApi class.

Constructor takes a ClamsApp instance and some options for flask configuration.

Parameters:
  • app_instance – A ClamsApp to wrap.

  • loopback – when True, the flask wrapper only listens to requests from localhost (used for debugging).

  • port – Port number for the flask app to listen (used for debugging).

  • debug – When True, the flask wrapper will run in debug mode.

run(**options)[source]

Starts a development server. See serve_development().

Parameters:

options – any additional options to pass to the web server.

serve_development(**options)[source]

Runs the CLAMS app as a flask webapp, using flask built-in development server (https://werkzeug.palletsprojects.com/en/2.0.x/).

Parameters:

options – any additional options to pass to the web server.

serve_production(**options)[source]

Runs the CLAMS app as a flask webapp, using a production-ready web server (gunicorn, https://docs.gunicorn.org/en/stable/#).

Two gunicorn settings are derived rather than fixed, in this order of precedence (first match wins):

workers

the workers keyword of this call; the CLAMS_GUNICORN_WORKERS environment variable; the app’s PRODUCTION_WORKERS_ATTR class attribute; else the smaller of (CPU cores x 2) + 1 and total VRAM / est_gpu_mem_typ of the app metadata.

max_requests (requests a worker serves before it is replaced)

the max_requests keyword of this call; the app’s PRODUCTION_MAX_REQUESTS_ATTR class attribute; else 1, so that a worker’s GPU memory is freed after each request.

Parameters:

options – any additional options to pass to the web server.

test_client()[source]

Returns flask test client.