CoolFace
Apppublic

Ivann043/MHR_Clasification

sourceHugging Faceunknownupdated 1y agoView on Hugging Face
0likes
app.py48 linesDownload Raw Back to root
1from flask import Flask, request, render_template
2import joblib
3import numpy as np
4
5app = Flask(__name__)
6
7# Load model KNN dan scaler MinMax/Standard
8model = joblib.load('knn_model.pkl')
9scaler = joblib.load('scaler.pkl')
10
11# Mapping label prediksi (ubah sesuai kebutuhanmu)
12label_mapping = {
13    0: 'Low',
14    1: 'Medium',
15    2: 'High'
16}
17
18@app.route('/')
19def home():
20    return render_template('index.html')
21
22@app.route('/predict', methods=['POST'])
23def predict():
24    try:
25        # Ambil nilai input dari form
26        age = float(request.form['Age'])
27        systolic = float(request.form['SystolicBP'])
28        diastolic = float(request.form['DiastolicBP'])
29        bs = float(request.form['BS'])
30        temp = float(request.form['BodyTemp'])
31        heart_rate = float(request.form['HeartRate'])
32
33        # Gabungkan jadi array dan scaling
34        features = np.array([[age, systolic, diastolic, bs, temp, heart_rate]])
35        features_scaled = scaler.transform(features)
36
37        # Prediksi menggunakan model
38        prediction = model.predict(features_scaled)[0]
39        risk_label = label_mapping.get(int(prediction), 'Unknown')
40
41        return render_template('index.html', result=risk_label)
42
43    except Exception as e:
44        return render_template('index.html', result=f"Terjadi kesalahan: {str(e)}")
45
46if __name__ == '__main__':
47    app.run(host='0.0.0.0', port=7860)
48