使用Flask将机器学习模型部署为REST API。错误:RuntimeError:在请求上下文之外工作

问题描述 投票:2回答:1

我开发了一种用于测试分类的分类器。并尝试使用REST API访问它。这是代码:

clf_model = joblib.load('MNB_Clf.pkl','r')

app = Flask(__name__)

@app.route('/spend_api',methods=['POST'])
def make_predict():
    data = request.get_json(force=True)

    test_data = pd.read_csv(data)

    pred_proba_class = clf_model.predict_proba(test_data['ColumnName1'])


    final_pred_file = pd.DataFrame(pred_proba_class)

    sub_file = 'output_'+str(datetime.datetime.now().strftime("%Y-%m-%d-%H-%M")) + '.csv'
    return jsonify(results=final_pred_file.to_csv(sub_file))
if __name__ == '__main__':
  app.run(port = 9000,debug=True)

我正在尝试使用以下代码将CSV文件发送到api:

url = 'http://localhost:9000/spend_api'
files = {'file': ('Test_data_final.csv')}
r = request.post(url,files=files)

我收到运行时错误。你能帮忙解决一下这个问题。

这是错误:

  RuntimeError                              Traceback (most recent call 
  last)
  <ipython-input-15-4b8522aa1eb0> in <module>()
         3 url = 'http://localhost:9000/spend_api'
         4 files = {'file': ('Test_data_final.csv')}
   ----> 5 r = request.post(url,files=files)
         6 
         7 

      C:\Users\pavansubhash_t\AppData\Local\Continuum\Anaconda2\lib\site -
    packages\werkzeug\local.pyc in __getattr__(self, name)
         345         if name == '__members__':
    346             return dir(self._get_current_object())
--> 347         return getattr(self._get_current_object(), name)
    348 
    349     def __setitem__(self, key, value):

C:\Users\pavansubhash_t\AppData\Local\Continuum\Anaconda2\lib\site-packages\werkzeug\local.pyc in _get_current_object(self)
    304         """
    305         if not hasattr(self.__local, '__release_local__'):
--> 306             return self.__local()
    307         try:
    308             return getattr(self.__local, self.__name__)

C:\Users\pavansubhash_t\AppData\Local\Continuum\Anaconda2\lib\site-packages\flask\globals.pyc in _lookup_req_object(name)
     35     top = _request_ctx_stack.top
     36     if top is None:
---> 37         raise RuntimeError(_request_ctx_err_msg)
     38     return getattr(top, name)
     39 

RuntimeError: Working outside of request context.

这通常意味着您尝试使用需要活动HTTP请求的功能。有关如何避免此问题的信息,请参阅有关测试的文档。

python rest api machine-learning flask
1个回答
1
投票

如果我很了解您的要求,您就拥有预先训练好的分类器,并且您希望通过API进行分类。您的API接收CSV文件作为输入,并发回另一个CSV文件。要重构代码以执行此操作,您需要添加以下更改。

Fix how you are sending the file in a request:

修复将csv文件加载到文件字典中的方式,如下所示:

url = 'http://localhost:9000/spend_api'
files = {'file': open('Test_data_final.csv','rb')}
r = request.post(url,files=files)

你可以在这个SO主题中找到更多细节。

Fix how to send the json response

clf_model = joblib.load('MNB_Clf.pkl','r')

app = Flask(__name__)

@app.route('/spend_api',methods=['POST'])
def make_predict():
    data = request.get_json(force=True)

    test_data = pd.read_csv(data)
    pred_proba_class = clf_model.predict_proba(test_data['ColumnName1'])
    final_pred_file = pd.DataFrame(pred_proba_class)
    return jsonify(results=final_pred_file.to_dict(orient="records"))
if __name__ == '__main__':
  app.run(port = 9000,debug=True)

If you want to send a CSV file instead

这是一个独立的工作示例:

from flask import Flask 
from flask import send_file 
from StringIO import StringIO 
import pandas as pd 

app = Flask("csv")

@app.route("/get_csv")
def hello():

    st = """col1|col2
    1|2
    3|4 
    """
    df = pd.read_csv(StringIO(st), sep="|")
    df.to_csv("/tmp/my_test_csv.csv", index=False, sep="|")
    return send_file("/tmp/my_test_csv.csv")


if __name__ == '__main__':
    app.run(port=5000)

作为旁注,我建议您重新考虑您的设计以使用JSON。

© www.soinside.com 2019 - 2024. All rights reserved.