-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
45 lines (37 loc) · 1.23 KB
/
app.py
File metadata and controls
45 lines (37 loc) · 1.23 KB
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
43
44
45
from flask import Flask, request, jsonify
import numpy as np
import pickle
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
# Load the trained model
with open('GREYLIFE/model.pkl', 'rb') as model_file:
your_trained_model = pickle.load(model_file)
# Load the PCA object
with open('GREYLIFE/pca_object.pkl', 'rb') as pca_file:
pca_object = pickle.load(pca_file)
# Load the StandardScaler
with open('GREYLIFE/scaler.pkl', 'rb') as scaler_file:
sc = pickle.load(scaler_file)
# Function to preprocess input data
def preprocess_input(data):
features = data['features']
features_array = np.array(features).reshape(1, -1)
features_std = sc.transform(features_array)
features_pca = pca_object.transform(features_std)
return features_pca
@app.route('/')
def index():
return "Hello"
# Flask route to handle predictions
@app.route('/prediction', methods=['POST', 'GET'])
def predict():
try:
input_data = request.get_json()
features_pca = preprocess_input(input_data)
prediction = your_trained_model.predict(features_pca)
return jsonify({'prediction': int(prediction[0])})
except Exception as e:
return jsonify({'error': str(e)})
if __name__ == '__main__':
app.run()