我正在尝试构建通过Flask + gunicorn提供的通用机器学习API。结构是:
Pipeline
类,它具有函数(tabulate
,preprocess
,train
等)并围绕它们提供一些通用结构(例如,计时,诊断)。 / li>
Blueprint
,它提供通用端点(/health
,/about_pipeline
,/train
,/classify
),并使用带有{{ 1}}对象作为参数。说明性的应用程序脚本(可重现的示例太大,但这应该显示结构):
Pipeline
# filename: example_app.py
import json
import pandas as pd
from sklearn.linear_model import LogisticRegression
from flask import Flask, jsonify, Response, request
# Importing our Pipeline class and Flask Blueprint constructor
from pipeline import Pipeline
from flask_blueprints import construct_ml_blueprint
def run_app():
with open('./user_config.json', 'r') as f:
user_config = json.load(f)
my_pipeline = Pipeline(name='Example ML API',
user_config=user_config)
@my_pipeline.step('tabulate')
def tabulate(json_data):
return pd.DataFrame(json_data['data'])
@my_pipeline.step('train')
def train(df):
lr = LogisticRegression()
lr.fit(df[['x_1', 'x_2', 'x_3']], df['y'])
return lr
ml_api = construct_ml_blueprint(my_pipeline)
app = Flask(__name__)
app.register_blueprint(ml_api)
print('App loaded')
return app
def main()
run_app()
if __name__ == '__main__':
main()
的说明性版本:
flask_blueprints.py
在命令行中使用gunicorn交付应用程序:
from flask import Blueprint, Flask, jsonify, Response, request
def construct_ml_blueprint(pipeline):
ml_api = Blueprint('ml_api', __name__)
# Health check for Kubernetes
@ml_api.route('/health', methods=['GET'])
def health():
return jsonify(success=True)
return ml_api
然后我尝试执行最简单的请求:
gunicorn example_app:run_app
...但是我收到此错误消息:
curl --request GET http://127.0.0.1:8000/health
这些位置参数来自哪里,如何解决?
(如果需要更多信息来理解问题,请在评论中让我知道,我将编辑问题。)