-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp1.py
More file actions
65 lines (52 loc) · 1.96 KB
/
Copy pathapp1.py
File metadata and controls
65 lines (52 loc) · 1.96 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from flask import Flask, render_template, request, jsonify
import pickle
import numpy as np
# Initialize Flask App
app = Flask(__name__)
# Load Pretrained Model & Scaler
try:
model = pickle.load(open("static/model/cardiac_model.sav", "rb")) # CNN + LSTM + SVM + RF
scaler = pickle.load(open("static/model/scaler.pkl", "rb")) # Feature Scaler
except Exception as e:
print(f"Error loading model or scaler: {e}")
@app.route("/")
def home():
return render_template("home.html")
@app.route("/about")
def about():
return render_template("about.html")
@app.route("/predict", methods=["POST", "GET"])
def predict():
if request.method == "POST":
try:
# Extract form data
form_data = request.form.to_dict()
features = [
float(form_data["ecg_signal"]),
float(form_data["respiratory_rate"]),
float(form_data["oxygen_level"]),
float(form_data["heart_rate"]),
float(form_data["blood_pressure"]),
float(form_data["temperature"]),
float(form_data["st_depression"]),
]
# Preprocess Data
scaled_features = scaler.transform([features])
# Model Prediction
prediction_prob = model.predict_proba(scaled_features).max() * 100
prediction = int(model.predict(scaled_features)[0])
# Prepare Result
result = {
"prediction": "High Risk" if prediction == 1 else "Low Risk",
"prediction_prob": round(prediction_prob, 2),
"features": form_data
}
return render_template("result.html", results=result)
except Exception as e:
return jsonify({"error": f"Prediction failed: {e}"})
return render_template("cardiac_form.html")
@app.route("/contact")
def contact():
return render_template("contact.html")
if __name__ == "__main__":
app.run(debug=True)