-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
40 lines (30 loc) · 1.1 KB
/
app.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
from flask import Flask, request, jsonify
import numpy as np
from joblib import load
app = Flask(__name__)
# Load the model from file
model = load('models/model_xgb.joblib')
@app.route('/predict', methods=['POST'])
def predict():
"""
Receive input data as JSON, make predictions using the loaded model,
and return the predictions as JSON.
"""
try:
# Get the request data
data = request.json
# Validate the input data format
if 'data' not in data or not isinstance(data['data'], list):
raise ValueError("Invalid input data format")
# Convert the input data to a NumPy array
new_data = np.array(data['data'])
# Make predictions using the loaded model
new_data_predictions = model.predict(new_data)
# Return the predictions as JSON
return jsonify(predictions=new_data_predictions.tolist())
except ValueError as e:
return jsonify(error=str(e)), 400
except Exception as e:
return jsonify(error=str(e)), 500
if __name__ == '__main__':
app.run()