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, label='More Chance AH') plt.scatter(u_pca[:, 0], u_pca[:, 1], c = 'g', cmap = plt.cm.Spectral, s = 40) plt.annotate('You', (u_pca[:, 0]+10, u_pca[:, 1]+5), bbox = dict(boxstyle="round",ec='g',fc='lightgreen')) plt.title(f"PCA, Exp. Variance: {np.round(np.sum(pca.explained_variance_ratio_), 4)}") plt.legend() 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
Project Coeur ❤", description = "

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.

"+ "Models: Logistic Regression, K-means, Decision Trees, Naive Bayes and Random Forest"+ "
Metrics: Accuracy: 0.82, Precision: 0.775, Recall: 0.939, F1 Score: 0.849

Please provide the requested data:", article='Step-by-step on GitHub notebook
~ 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)