-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
30 lines (24 loc) · 854 Bytes
/
app.py
File metadata and controls
30 lines (24 loc) · 854 Bytes
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
from flask import Flask, render_template, request, jsonify
import pickle
import numpy as np
# Load trained model and scaler
with open("kmeans_model.pkl", "rb") as model_file:
kmeans = pickle.load(model_file)
with open("scaler.pkl", "rb") as scaler_file:
scaler = pickle.load(scaler_file)
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/predict', methods=['POST'])
def predict():
try:
data = request.json
features = np.array(data['features']).reshape(1, -1)
scaled_features = scaler.transform(features)
cluster = kmeans.predict(scaled_features)[0]
return jsonify({'Cluster': int(cluster)})
except Exception as e:
return jsonify({'error': str(e)})
if __name__ == '__main__':
app.run(debug=True)