机器学习模型实用程序
bavard-ml-common的Python项目详细描述
巴瓦德ml普通
Web服务示例
importtypingastfromsklearn.datasetsimportload_irisfromsklearn.treeimportDecisionTreeClassifierimportuvicornfrombavard_ml_common.mlops.web_serviceimportendpoint,WebServiceclassDTModel(WebService):""" A machine learning model that can turn into a web service. """def__init__(self)->None:self._dt=DecisionTreeClassifier()self._fitted=Falsedeffit(self,X,y)->None:self._dt.fit(X,y)self._fitted=True# Type annotations are required for all `@endpoint` method arguments.@endpointdefpredict(self,X:t.List[t.List[float]]):assertself._fittedreturnself._dt.predict(X).tolist()@endpointdeffeature_importances(self)->list:assertself._fittedreturnself._dt.feature_importances_.tolist()# Fit a ML model on a dataset.iris=load_iris()X=iris.datay=iris.targetmodel=DTModel()model.fit(X,y)# Convert the model into a `fastapi` web service.api=model.to_app()# Run the web service.uvicorn.run(api,host="0.0.0.0",use_colors=True,log_level="debug")# The fitted model is now available at 0.0.0.0:8000/,# with a `/predict` endpoint and a `/feature_importances` endpoint.# API documentation for the fitted model is at a `/docs` endpoint.
在当地发展
安装依赖项:
^{pr2}$然后,使用pytest运行测试:
python3 -m unittest
释放程序包
发布包由CI自动处理,但必须采取三个步骤才能触发成功的释放:
- 将
setup.py
中的VERSION
变量增加到所需的新版本(例如VERSION="1.1.1"
) - 使用提交并标记repo,该值与您在
VERSION
变量中填充的值完全相同(例如git tag 1.1.1
) - 将commit和tag推送到remote。这些可以一起使用:
git push --atomic origin <branch name> <tag>
一旦推送commit和标记,CI将使用该版本将包发布给pypi。在
- 项目
标签: