import urllib.request import pandas as pd import numpy as np import matplotlib.pyplot as plt import seaborn as sns from sklearn.model_selection import train_test_split, RandomizedSearchCV, cross_val_score from sklearn.preprocessing import StandardScaler, MinMaxScaler from sklearn.linear_model import LogisticRegression from sklearn.svm import SVC from sklearn.tree import DecisionTreeClassifier from sklearn.ensemble import RandomForestClassifier from sklearn.neighbors import KNeighborsClassifier from sklearn.discriminant_analysis import LinearDiscriminantAnalysis, QuadraticDiscriminantAnalysis from sklearn.naive_bayes import GaussianNB from sklearn.neural_network import MLPClassifier from sklearn.model_selection import GridSearchCV from sklearn.model_selection import cross_val_score, cross_val_predict from sklearn.metrics import accuracy_score, recall_score, precision_score, f1_score import joblib import os from sklearn.metrics import confusion_matrix import seaborn as sns from sklearn.metrics import roc_curve, roc_auc_score from mlxtend.evaluate import bias_variance_decomp import gradio as gr my_model_dict = {} model_path = '.' for model_file in os.listdir(model_path): tmp_text = os.path.splitext(model_file) if len(tmp_text) < 2 or tmp_text[1] != '.joblib': continue model_filename = os.path.join(model_path, model_file) model = joblib.load(model_filename) my_model_dict[model_file.split('_')[2]] = model # 1 example case examples = [['Logistic Regression', 41, 1, 2, 135.0, 203.0, 0, 1, 132.0, 0, 3.5, 2, 3, 6], ['K-Nearest Neighbors', 51, 1, 2, 135.0, 203.0, 0, 1, 132.0, 0, 3.5, 2, 3, 6], ['Random Forest', 51, 1, 2, 135.0, 203.0, 0, 1, 132.0, 0, 3.5, 2, 3, 6]] c_model_name = gr.inputs.Dropdown(choices=['Logistic Regression', 'SVM', 'Decision Tree', 'Random Forest', 'K-Nearest Neighbors', 'Linear Discriminant Analysis', 'Quadratic Discriminant Analysis', 'Naive Bayes', 'Multi-layer Perceptron'], label='model_name') c_age = gr.inputs.Slider(minimum=1, maximum=100, step=1, label="age") # 41 c_sex = gr.inputs.Radio(choices=[0, 1], label="sex") # 1 c_chest_pain_type = gr.inputs.Dropdown(choices=[1, 2, 3, 4], label="chest_pain_type") # 2 c_resting_blood_pressure = gr.inputs.Slider(minimum=50, maximum=300, step=0.1, label="resting_blood_pressure") # 135.0 c_serum_cholesterol = gr.inputs.Slider(minimum=100, maximum=1000, step=0.1, label="serum_cholesterol") # 203.0 c_fasting_blood_sugar = gr.inputs.Slider(minimum=0, maximum=2, step=1, label="fasting_blood_sugar") # 0 c_resting_ecg = gr.inputs.Slider(minimum=0, maximum=2, step=1, label="resting_ecg") # 1 c_max_heart_rate_achieved = gr.inputs.Slider(minimum=50, maximum=300, step=0.01, label="max_heart_rate_achieved") # 132 c_exercise_induced_angina = gr.inputs.Slider(minimum=0, maximum=1, step=1, label="exercise_induced_angina") # 0 c_oldpeak = gr.inputs.Slider(minimum=0, maximum=10, step=0.01, label="oldpeak") # 3.5 c_slope = gr.inputs.Dropdown(choices=[1, 2, 3], label="slope") # 2 c_num_major_vessels = gr.inputs.Slider(minimum=0, maximum=3, step=1, label="num_major_vessels") # 3 c_thalassemia = gr.inputs.Dropdown(choices=[3, 6, 7], label="thalassemia") # 6 outputs = gr.outputs.Textbox() # returns the top prediction and confidence score # Define the function for prediction def predict(model_name, age, sex, chest_pain_type, resting_blood_pressure, serum_cholesterol, fasting_blood_sugar, resting_ecg, max_heart_rate_achieved, exercise_induced_angina, oldpeak, slope, num_major_vessels, thalassemia): # Create the input features pred_data = np.array([[age, sex, chest_pain_type, resting_blood_pressure, serum_cholesterol, fasting_blood_sugar, resting_ecg, max_heart_rate_achieved, exercise_induced_angina, oldpeak, slope, num_major_vessels, thalassemia]]) print(pred_data) # Scale the input features scaler = StandardScaler() scaler.fit(pred_data) pred_data_transform = scaler.transform(pred_data) # Make the prediction model = my_model_dict[model_name] setattr(model, 'probability', True) prediction = model.predict_proba(pred_data_transform) # Return the predicted class probabilities return f"Positive: {prediction[0][0]:.2%}\n Negative:{prediction[0][1]:.2%}" gr.Interface(fn=predict, inputs= [c_model_name, c_age, c_sex, c_chest_pain_type, c_resting_blood_pressure,c_serum_cholesterol, c_fasting_blood_sugar, c_resting_ecg,c_max_heart_rate_achieved, c_exercise_induced_angina, c_oldpeak,c_slope, c_num_major_vessels, c_thalassemia], outputs=outputs, examples=examples, description='Please click one line at examples table.').launch(debug=True)