-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
42 lines (33 loc) · 1.2 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# Dependencies
from flask import Flask, request, jsonify
from sklearn.externals import joblib
import traceback
import pandas as pd
import numpy as np
# Your API definition
app = Flask(__name__)
@app.route('/predict', methods=['GET','POST'])
def predict():
if model:
try:
json_ = request.get_json(force=True)
print(json_)
query = pd.DataFrame(json_)
#query = query.reindex(columns=model_columns, fill_value=0)
prediction = list(model.predict(query))
return jsonify({'prediction': str(prediction)})
except:
return jsonify({'trace': traceback.format_exc()})
else:
print ('Train the model first')
return ('No model here to use')
if __name__ == '__main__':
try:
port = int(sys.argv[1]) # This is for a command-line input
except:
port = 12345 # If you don't provide any port the port will be set to 12345
model = joblib.load("model.pkl") # Load "model.pkl"
print ('Model loaded')
#model_columns = joblib.load("model_columns.pkl") # Load "model_columns.pkl"
#print ('Model columns loaded')
app.run(port=port, debug=True)