Acumos model runner for Python models
Project description
Acumos Python Model Runner User Guide
The acumos_model_runner package installs a command line tool acumos_model_runner for running models created by the Acumos Python client library.
The model runner provides an HTTP API for invoking model methods, as well as a Swagger UI for documentation. See the tutorial for more information on usage.
Installation
You will need a Python 3.4+ environment in order to install acumos_model_runner. You can use Anaconda (preferred) or pyenv to install and manage Python environments.
The acumos_model_runner package can be installed with pip:
$ pip install acumos_model_runner
Command Line Usage
usage: acumos_model_runner [-h] [--host HOST] [--port PORT]
[--workers WORKERS] [--timeout TIMEOUT]
[--cors CORS]
model_dir
positional arguments:
model_dir Directory containing a dumped Acumos Python model
optional arguments:
-h, --help show this help message and exit
--host HOST The interface to bind to
--port PORT The port to bind to
--workers WORKERS The number of gunicorn workers to spawn
--timeout TIMEOUT Time to wait (seconds) before a frozen worker is
restarted
--cors CORS Enables CORS if provided. Can be a domain, comma-
separated list of domains, or *
Acumos Python Model Runner Tutorial
This tutorial demonstrates how to use the Acumos Python model runner with an example model.
Creating A Model
An Acumos model must first be defined using the Acumos Python client library. For illustrative purposes, a simple model with deterministic methods is defined below.
# example_model.py
from collections import Counter
from acumos.session import AcumosSession
from acumos.modeling import Model, List, Dict
def add(x: int, y: int) -> int:
'''Adds two numbers'''
return x + y
def count(strings: List[str]) -> Dict[str, int]:
'''Counts the occurrences of words in `strings`'''
return Counter(strings)
model = Model(add=add, count=count)
session = AcumosSession()
session.dump(model, 'example-model', '.')
Executing example_model.py results in the following directory:
.
├── example_model.py
└── example-model
Running A Model
Now the acumos_model_runner command line tool can be used to run the saved model.
$ acumos_model_runner example-model/
[2018-08-08 12:16:57 -0400] [61113] [INFO] Starting gunicorn 19.9.0
[2018-08-08 12:16:57 -0400] [61113] [INFO] Listening at: http://0.0.0.0:3330 (61113)
[2018-08-08 12:16:57 -0400] [61113] [INFO] Using worker: sync
[2018-08-08 12:16:57 -0400] [61151] [INFO] Booting worker with pid: 61151
Using A Model
The model HTTP API can be explored via its generated Swagger UI. The Swagger UI of example-model above can be accessed by navigating to http://localhost:3330 in your web browser.
Below are some screenshots of the Swagger UI for example-model.
Model APIs
The Swagger UI enumerates model method APIs, as well as APIs for accessing model artifacts. Below, the APIs corresponding to the add and count methods are listed under the methods tag.
Count Method API
Expanding the documentation for the count method reveals more information on how to invoke the API. Below, the HTML form is used to input sample data.
Count Method Response
The response from the count method shows that everything is working as expected!
Acumos Python Model Runner Release Notes
v0.2.0
Overhaul of model runner API
Added support for application/json via Content-Type and Accept headers
Added automatic generation of OpenAPI Specification and Swagger UI
Added support for CORS
v0.1.0
Model runner implementation split off from Acumos Python client project
Acumos Python Model Runner Developer Guide
Testing
We use a combination of tox, pytest, and flake8 to test acumos_model_runner. Code which is not PEP8 compliant (aside from E501) will be considered a failing test. You can use tools like autopep8 to “clean” your code as follows:
$ pip install autopep8
$ cd python-model-runner
$ autopep8 -r --in-place --ignore E501 acumos_model_runner/ testing/ examples/
Run tox directly:
$ cd python-model-runner
$ tox
You can also specify certain tox environments to test:
$ tox -e py34 # only test against Python 3.4
$ tox -e flake8 # only lint code
And finally, you can run pytest directly in your environment (recommended starting place):
$ pytest
$ pytest -s # verbose output
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for acumos_model_runner-0.2.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4bd9650c229127a3aff17f019394f20aecd3da58e545b42ac85373e70d9ddd3e |
|
MD5 | 12fa36108992a6a913588d310004485a |
|
BLAKE2b-256 | 7882d3fbd453f7f58c14fc0cadb24a8c504a24c1e4a0169f5cd64bf8a7025f36 |
Hashes for acumos_model_runner-0.2.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2e38559004f52f9adfed4afbff8200db61a85e9f2d301aca6c940eb251995820 |
|
MD5 | cb6a6e2c7396f62289b291e17f7d578b |
|
BLAKE2b-256 | 81c2c0b5dfc50dd581d2d6c0b8872a96c87ce3ac2fb3fd6537d8209ceaaa7da6 |