Spaces:
Build error
Build error
File size: 4,527 Bytes
4a6e29c |
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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
from statistics import mode
from cProfile import label
from joblib import load
import matplotlib.pyplot as plt
import gradio as gr
import numpy as np
def getdata(Age,Sex,CP,Trtbps,Chol,Fbs,Restecg,Thalachh,Oldpeak,Slp,Caa,Thall,Exng):
if Sex == "Male":
Sex = 1
else:
Sex = 0
if CP == "Typical Angina":
CP = 0
elif CP == "Atypical Angina":
CP = 1
elif CP == "Non-anginal Pain":
CP = 2
else:
CP = 3
if Fbs == "True":
Fbs = 1
else:
Fbs = 0
if Restecg == "Normal":
Restecg = 0
elif Restecg == "ST-T wave normality":
Restecg = 1
else:
Restecg = 2
if Exng == "Yes":
Exng = 1
else:
Exng = 0
a = [Age,Sex,CP,Trtbps,Chol,Fbs,Restecg,Thalachh,Exng,Oldpeak,Slp,Caa,Thall]
arr = np.array([a])
return arr
def getfig(X_test):
X_pca = load('X_pca.data')
y = load('y.data')
pca = load('pca.dim')
u_pca = pca.transform(X_test)
fig = plt.figure(figsize=(5,4))
plt.scatter(X_pca[:, 0], X_pca[:, 1], c = y, cmap = plt.cm.Spectral, s = 10)
plt.scatter(u_pca[:, 0], u_pca[:, 1], c = 'g', cmap = plt.cm.Spectral, s = 40)
plt.title(f"PCA, Exp. Variance: {np.round(np.sum(pca.explained_variance_ratio_), 4)}")
plt.xlabel("PC 1")
plt.ylabel("PC 2")
return fig
def greet(Age,Sex,CP,Trtbps,Chol,Fbs,Restecg,Thalachh,Oldpeak,Slp,Caa,Thall,Exng):
X_test = getdata(Age,Sex,CP,Trtbps,Chol,Fbs,Restecg,Thalachh,Oldpeak,Slp,Caa,Thall,Exng)
scaler = load('stdscaler.model')
x_std = scaler.transform(X_test)
log_reg = load('log_reg.model')
y_lr = log_reg.predict(x_std)
kmeans = load('kmeans.model')
y_km = kmeans.predict(x_std)
tree = load('tree.model')
y_tree = tree.predict(x_std)
nb = load('nb.model')
y_bayes = nb.predict(X_test)
forest = load('forest.model')
y_forest = forest.predict(X_test)
r = [y_lr[0], y_km[0], y_tree[0], y_bayes[0], y_forest[0]]
f = mode(r)
if f == 0:
x = "You have less chance of heart attack"
else:
x = "You have more chance of heart attack"
fig = load('dime.fig')
fig2 = getfig(X_test)
return x, fig, fig2
interface = gr.Interface(
title = "HeartAttack prediction - UMG <br> Project Coeur ❤",
description = "<h3>The idea is to classify between 0 = less chance of heart attack and 1 = more chance of heart attack, according to the data provided by the user.</h3>"+
"<b>Models:</b> Logistic Regression, K-means, Decision Trees, Naive Bayes and Random Forest"+
"<br><b>Metrics:</b> Accuracy: 0.82, Precision: 0.775, Recall: 0.939, F1 Score: 0.849 <br> <br><b>Please provide the requested data:</b>",
article='Step-by-step on GitHub <a href="https://github.com/Adrian8aS/Machine-Learning-App-Gradio/blob/21246d9ba87859e9068369b89d48b4c6ee13dfe5/Proyecto%20integrador.ipynb"> notebook </a> '+
'<br>Dashboard of our train data <a href="https://1drv.ms/x/s!At7E16oDTBiKktUagvJHHpF5CCoITA?e=fOLjUq"> here! </a> '+
'<br>Privacy Policy <a href="https://raw.githubusercontent.com/rulasvrdz/DataMining/main/Texto.txt"> here! </a> '+
"<br><br> ~ Project Coeur",
allow_flagging = "never",
fn = greet,
inputs = [
gr.Number(label="Age of the patient"),
gr.Radio(["Male", "Female"], label="Sex of the patient"),
gr.Radio(["Typical Angina", "Atypical Angina", "Non-anginal Pain", "Asymptomatic"], label="Chest pain type"),
gr.Number(label="Resting blood pressure (in mm Hg)"),
gr.Number(label="Cholestoral in mg/dl fetched via BMI sensor"),
gr.Radio(["True", "False"], label="Fasting blood sugar > 120 mg/dl"),
gr.Radio(["Normal", "ST-T wave normality", "Left ventricular hypertrophy"], label="Resting electrocardiographic results"),
gr.Number(label="Maximum heart rate achieved"),
gr.Number(label="Previous peak"),
gr.Radio([0, 1, 2], label="Slope"),
gr.Radio([0, 1, 2, 3, 4], label="Number of major vessels"),
gr.Radio([0, 1, 2, 3], label="Thalium Stress Test result"),
gr.Radio(["Yes", "No"], label="Exercise induced angina")
],
outputs = [gr.Text(label="Prediction"), 'plot', 'plot'],
examples = [[41,"Female","Typical Angina",130,204,"False","Normal",172,1.4,2,0,2,"No"],
[45,"Male","Non-anginal Pain",110,264,"False","ST-T wave normality",132,0.2,1,0,3,"No"]]
)
interface.launch(share = False) |