我们为Python模型铸造模型
acumos-model-runner的Python项目详细描述
acumos python model runner用户指南
acumos_model_runner包安装一个命令行工具acumos_model_runner,用于运行由Acumos Python client library创建的模型。
模型运行器提供了用于调用模型方法的http api,以及用于文档的Swagger UI。有关用法的详细信息,请参见教程。
安装
您需要一个python 3.4+环境来安装acumos_model_runner。 您可以使用Anaconda (首选)或pyenv安装和 管理python环境。
acumos_model_runner包可以与pip一起安装:
$ pip install acumos_model_runner
命令行用法
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模型运行程序教程
本教程演示如何将acumos python模型运行器与示例模型一起使用。
创建模型
必须首先使用Acumos Python client library定义acumos模型。为了便于说明,下面定义了一个具有确定性方法的简单模型。
# example_model.pyfromcollectionsimportCounterfromacumos.sessionimportAcumosSessionfromacumos.modelingimportModel,List,Dictdefadd(x:int,y:int)->int:'''Adds two numbers'''returnx+ydefcount(strings:List[str])->Dict[str,int]:'''Counts the occurrences of words in `strings`'''returnCounter(strings)model=Model(add=add,count=count)session=AcumosSession()session.dump(model,'example-model','.')
在以下目录中执行example_model.py将得到结果:
.├──example_model.py└──example-model
运行模型
现在可以使用acumos_model_runner命令行工具运行保存的模型。
$ 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
使用模型
模型http api可以通过其生成的swagger ui进行探索。通过导航到web浏览器中的http://localhost:3330,可以访问上面example-model的招摇用户界面。
下面是example-model的招摇用户界面的一些屏幕截图。
模型API
swagger ui列举了模型方法api,以及用于访问模型工件的api。下面,add和count方法对应的api列在methods标记下。
计数方法API
展开count方法的文档将显示有关如何调用api的更多信息。
计数方法请求
swagger ui提供了一个输入表单,可用于使用示例数据尝试countapi。
计数方法响应
来自countapi的响应表明一切都按预期工作!
acumos python model runner发行说明
v0.2.2
- 修复了由相对模型目录引起的模型工件资源的404错误
- 修复了protobuf资源的错误媒体类型
v0.2.1
- 将Swagger UI从V2升级到V3
v0.2.0
- 模型转轮api大修
- 通过Content-Type和Accept头添加了对application/json的支持
- 添加了自动生成OpenAPI Specification和Swagger UI
- 增加了对CORS的支持
v0.1.0
- 模型运行器实现与Acumos Python client项目分离
acumos python model runner开发人员指南
测试
我们使用tox、pytest和flake8的组合来测试 acumos_model_runner。不符合PEP8的代码(E501除外)将是 被认为是失败的测试。您可以使用诸如autopep8之类的工具 按如下方式“清除”代码:
$ pip install autopep8
$ cd python-model-runner
$ autopep8 -r --in-place --ignore E501 acumos_model_runner/ testing/ examples/
直接运行tox:
$ cd python-model-runner
$ tox
您还可以指定要测试的特定毒物环境:
$ tox -e py34 # only test against Python 3.4 $ tox -e flake8 # only lint code
最后,您可以在环境中直接运行pytest (推荐的起始位置):
$ pytest
$ pytest -s # verbose output