Spaces:
Sleeping
Sleeping
huahualuya
commited on
Commit
•
f43ceb2
1
Parent(s):
42a1e3a
Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pickle
|
3 |
+
import pandas as pd
|
4 |
+
|
5 |
+
# load the data
|
6 |
+
heart=pd.read_csv('heart.dat', header=None, sep=' ', names=['age', 'sex', 'cp', 'trestbps', 'chol',
|
7 |
+
'fbs', 'restecg', 'thalach', 'exang',
|
8 |
+
'oldpeak', 'slope', 'ca', 'thal', 'heart disease'])
|
9 |
+
|
10 |
+
# load the saved models
|
11 |
+
with open('/content/drive/MyDrive/Colab Notebooks/Homework04/Tree.pkl', 'rb') as f:
|
12 |
+
tree_model = pickle.load(f)
|
13 |
+
|
14 |
+
with open('/content/drive/MyDrive/Colab Notebooks/Homework04/svm.pkl', 'rb') as f:
|
15 |
+
svm_model = pickle.load(f)
|
16 |
+
|
17 |
+
with open('/content/drive/MyDrive/Colab Notebooks/Homework04/QDA.pkl', 'rb') as f:
|
18 |
+
qda_model = pickle.load(f)
|
19 |
+
|
20 |
+
with open('/content/drive/MyDrive/Colab Notebooks/Homework04/MLP.pkl', 'rb') as f:
|
21 |
+
mlp_model = pickle.load(f)
|
22 |
+
|
23 |
+
with open('/content/drive/MyDrive/Colab Notebooks/Homework04/Log.pkl', 'rb') as f:
|
24 |
+
log_model = pickle.load(f)
|
25 |
+
|
26 |
+
with open('/content/drive/MyDrive/Colab Notebooks/Homework04/LDA.pkl', 'rb') as f:
|
27 |
+
lda_model = pickle.load(f)
|
28 |
+
|
29 |
+
with open('/content/drive/MyDrive/Colab Notebooks/Homework04/For.pkl', 'rb') as f:
|
30 |
+
for_model = pickle.load(f)
|
31 |
+
|
32 |
+
# Define the function to make predictions
|
33 |
+
def make_prediction(age, sex, cp, trestbps, chol, fbs, restecg, thalach, exang, oldpeak, slope, ca, thal, model_name):
|
34 |
+
# Create a pandas DataFrame from the inputs
|
35 |
+
input_data = pd.DataFrame({
|
36 |
+
'age': [age],
|
37 |
+
'sex': [sex],
|
38 |
+
'cp': [cp],
|
39 |
+
'trestbps': [trestbps],
|
40 |
+
'chol': [chol],
|
41 |
+
'fbs': [fbs],
|
42 |
+
'restecg': [restecg],
|
43 |
+
'thalach': [thalach],
|
44 |
+
'exang': [exang],
|
45 |
+
'oldpeak': [oldpeak],
|
46 |
+
'slope': [slope],
|
47 |
+
'ca': [ca],
|
48 |
+
'thal': [thal]
|
49 |
+
})
|
50 |
+
|
51 |
+
# feature scaling
|
52 |
+
from sklearn.model_selection import train_test_split
|
53 |
+
X = heart.drop('heart disease', axis=1)
|
54 |
+
y = heart['heart disease']
|
55 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42, stratify=y)
|
56 |
+
from sklearn.preprocessing import StandardScaler
|
57 |
+
scaler = StandardScaler()
|
58 |
+
X_train_std = scaler.fit_transform(X_train)
|
59 |
+
|
60 |
+
# choose the model and make prediction
|
61 |
+
model_dict = {'Decision_Tree': tree_model,
|
62 |
+
'QDA': qda_model,
|
63 |
+
'Artificial_Neural_Networks': mlp_model,
|
64 |
+
'Logistic_Regression': log_model,
|
65 |
+
'LDA': lda_model,
|
66 |
+
'Random_Forest': for_model,
|
67 |
+
'SVM': svm_model}
|
68 |
+
model = model_dict[model_name]
|
69 |
+
input_data_std = scaler.transform(input_data)
|
70 |
+
probas = model.predict_proba(input_data_std)
|
71 |
+
outtext={1:'no heart_disease', 2:'heart disease'}
|
72 |
+
return {f"Probability of Class {i+1}": proba for i, proba in enumerate(probas[0])}
|
73 |
+
|
74 |
+
# Create the Gradio interface
|
75 |
+
inputs = [
|
76 |
+
gr.inputs.Number(label='age'),
|
77 |
+
gr.inputs.Radio(choices=[0,1], label='sex'),
|
78 |
+
gr.inputs.Dropdown(choices=[1,2,3,4], label='chest pain type'),
|
79 |
+
gr.inputs.Number(label='resting blood pressure'),
|
80 |
+
gr.inputs.Number(label='serum cholestoral'),
|
81 |
+
gr.inputs.Radio(choices=[0,1], label='fasting blood sugar'),
|
82 |
+
gr.inputs.Radio(choices=[0,1,2], label='resting electrocardiographic'),
|
83 |
+
gr.inputs.Number(label='maximum heart rate'),
|
84 |
+
gr.inputs.Radio(choices=[0,1], label='exercise induced angina'),
|
85 |
+
gr.inputs.Number(label='oldpeak'),
|
86 |
+
gr.inputs.Dropdown(choices=[1,2,3], label='slope ST'),
|
87 |
+
gr.inputs.Dropdown(choices=[0,1,2,3], label='major vessels'),
|
88 |
+
gr.inputs.Dropdown(choices=[3,6,7], label='thal'),
|
89 |
+
gr.inputs.Dropdown(choices=['Decision_Tree', 'QDA', 'Artificial_Neural_Networks', 'Logistic_Regression', 'LDA', 'Random_Forest', 'SVM'], label='Select the model')
|
90 |
+
]
|
91 |
+
|
92 |
+
outputs = gr.outputs.Label(label='Predicted class probabilities')
|
93 |
+
|
94 |
+
gr.Interface(fn=make_prediction, inputs=inputs, outputs=outputs).launch()
|