init!
Browse files
app.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pickle
|
| 3 |
+
import numpy as np
|
| 4 |
+
from sklearn.preprocessing import StandardScaler
|
| 5 |
+
|
| 6 |
+
# Load the trained models
|
| 7 |
+
models = {
|
| 8 |
+
"Logistic": pickle.load(open("logistic_model_best.pkl", "rb")),
|
| 9 |
+
"SVC": pickle.load(open("svc_model_best.pkl", "rb")),
|
| 10 |
+
"Decision Trees": pickle.load(open("decisiontree_model_best.pkl", "rb")),
|
| 11 |
+
"Random Forest": pickle.load(open("rfc_model_best.pkl", "rb")),
|
| 12 |
+
"KNN": pickle.load(open("knn_model_best.pkl", "rb")),
|
| 13 |
+
"LDA": pickle.load(open("lda_model_best.pkl", "rb"))
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
# Define the input and output dropdowns
|
| 17 |
+
age_dropdown = gr.inputs.Number(label='Age')
|
| 18 |
+
sex_dropdown = gr.inputs.Dropdown(["Male", "Female"], label="Sex")
|
| 19 |
+
cp_dropdown = gr.inputs.Dropdown(["1", "2", "3", "4"], label="Chest Pain Type")
|
| 20 |
+
restbp_dropdown = gr.inputs.Number(label='Resting Blood pressure')
|
| 21 |
+
chol_dropdown = gr.inputs.Number(label='Serum cholestoral')
|
| 22 |
+
fbs_dropdown = gr.inputs.Dropdown(["0", "1"], label="Fasting Blood Sugar")
|
| 23 |
+
ecg_dropdown = gr.inputs.Dropdown(["0", "1", "2"], label="Electrocardiographic Results")
|
| 24 |
+
max_rt_dropdown = gr.inputs.Number(label="Maximum Heart Rate")
|
| 25 |
+
exang_dropdown = gr.inputs.Dropdown(["0", "1"], label="Exercise-Induced Angina")
|
| 26 |
+
oldpeak_dropdown = gr.inputs.Number(label="oldpeak =ST Depression Induced by Exercise")
|
| 27 |
+
slope_dropdown = gr.inputs.Dropdown(["0", "1", "2"], label="Slope of the ST Segment")
|
| 28 |
+
ca_dropdown = gr.inputs.Dropdown(["0", "1", "2", "3"], label="Number of Major Vessels")
|
| 29 |
+
thal_dropdown = gr.inputs.Dropdown(["0", "1", "2"], label="Thalassemia")
|
| 30 |
+
model_dropdown = gr.inputs.Dropdown(
|
| 31 |
+
["Logistic", "SVC", "Decision Trees", "Random Forest", "KNN", "LDA"],
|
| 32 |
+
label="Model"
|
| 33 |
+
)
|
| 34 |
+
xoutputs = [
|
| 35 |
+
gr.outputs.Textbox(label="Probability of Heart Disease"),
|
| 36 |
+
]
|
| 37 |
+
# Define the function to make predictions
|
| 38 |
+
import numpy as np
|
| 39 |
+
|
| 40 |
+
def predict(age, sex, cp, restbp, chol, fbs, ecg, max_rt, exang, oldpeak, slope, ca, thal, model):
|
| 41 |
+
# Convert input values to their respective data types
|
| 42 |
+
age = int(age)
|
| 43 |
+
sex = 1 if sex == "Male" else 0
|
| 44 |
+
cp = int(cp)
|
| 45 |
+
restbp = int(restbp)
|
| 46 |
+
chol = int(chol)
|
| 47 |
+
fbs = int(fbs)
|
| 48 |
+
ecg = int(ecg)
|
| 49 |
+
max_rt = int(max_rt)
|
| 50 |
+
exang = int(exang)
|
| 51 |
+
oldpeak = float(oldpeak)
|
| 52 |
+
slope = int(slope)
|
| 53 |
+
ca = int(ca)
|
| 54 |
+
thal = int(thal)
|
| 55 |
+
|
| 56 |
+
# Create the input array
|
| 57 |
+
input_array = np.array([age, sex, cp, restbp, chol, fbs, ecg, max_rt, exang, oldpeak, slope, ca, thal]).reshape(1,-1)
|
| 58 |
+
|
| 59 |
+
# Get the selected model
|
| 60 |
+
selected_model = models[model]
|
| 61 |
+
|
| 62 |
+
scaler = StandardScaler()
|
| 63 |
+
normalized_input_array = scaler.fit_transform(input_array)
|
| 64 |
+
|
| 65 |
+
predi = selected_model.predict(normalized_input_array)
|
| 66 |
+
# Convert the prediction to a string
|
| 67 |
+
if predi[0] == 1:
|
| 68 |
+
return "Absent"
|
| 69 |
+
elif predi[0] == 2:
|
| 70 |
+
return "Present"
|
| 71 |
+
else:
|
| 72 |
+
return "Unknown"
|
| 73 |
+
|
| 74 |
+
# Create the interface
|
| 75 |
+
interface = gr.Interface(
|
| 76 |
+
fn=predict,
|
| 77 |
+
inputs=[
|
| 78 |
+
age_dropdown, sex_dropdown, cp_dropdown, restbp_dropdown, chol_dropdown,
|
| 79 |
+
fbs_dropdown, ecg_dropdown,max_rt_dropdown, exang_dropdown, oldpeak_dropdown,
|
| 80 |
+
slope_dropdown, ca_dropdown, thal_dropdown, model_dropdown
|
| 81 |
+
],
|
| 82 |
+
outputs = xoutputs,
|
| 83 |
+
examples=[[70, 1.0,4.0,130.0,322.0,0.0,2.0,109.0,0.0,2.4,2.0,3.0,2.0,"SVC"]],
|
| 84 |
+
|
| 85 |
+
title="Heart Disease Prediction",
|
| 86 |
+
description="ML HomeWork 04"
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
interface.launch()
|