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:
ResourceClamsHTTPApi 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"]) asrouteandadd_url_ruleby default.
- post() Response[source]¶
Maps HTTP POST verb to
annotate(). Note that for now HTTP PUT verbs is also mapped toannotate().- 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 toannotate().- Returns:
Returns MMIF output from a ClamsApp in a HTTP response.
- clams.restify.PRODUCTION_WORKERS_ATTR = 'RESTIFIER_PRODUCTION_WORKERS'¶
Names of the optional
ClamsAppclass attributes thatRestifier.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). SeeRestifier.serve_production()for their precedence.
- class clams.restify.Restifier(app_instance: ClamsApp, loopback: bool = False, port: int = 5000, debug: bool = True)[source]¶
Bases:
objectRestifier is a simple wrapper that takes a
ClamsAppobject and turn it into a flaks-based HTTP server. For mapping between Python API and HTTP API, it relies onClamsHTTPApiclass.Constructor takes a
ClamsAppinstance and some options for flask configuration.- Parameters:
app_instance – A
ClamsAppto 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):
workersthe
workerskeyword of this call; theCLAMS_GUNICORN_WORKERSenvironment variable; the app’sPRODUCTION_WORKERS_ATTRclass attribute; else the smaller of(CPU cores x 2) + 1andtotal VRAM / est_gpu_mem_typof the app metadata.max_requests(requests a worker serves before it is replaced)the
max_requestskeyword of this call; the app’sPRODUCTION_MAX_REQUESTS_ATTRclass 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.