I've developed a classifier for test classification. and trying to access it using REST API. Here is the code:
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)
I am trying to send the CSV file to the api using the below code:
url = 'http://localhost:9000/spend_api'
files = {'file': ('Test_data_final.csv')}
r = request.post(url,files=files)
I am getting a Runtime Error. Could you please help how to resolve the issue.
Here is the error:
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.
This typically means that you attempted to use functionality that needed an active HTTP request. Consult the documentation on testing for information about how to avoid this problem.