Spaces:
Runtime error
Runtime error
import gradio as gr | |
import numpy as np | |
import pandas as pd | |
import pickle | |
from sklearn.preprocessing import MinMaxScaler | |
from sklearn.linear_model import LinearRegression | |
from sklearn.model_selection import train_test_split | |
# Classification Model Imports | |
with open('decisionTree.pkl', 'rb') as file: | |
loadedDecisionTree = pickle.load(file) | |
with open('linearReg.pkl', 'rb') as file: | |
loadedLinearReg = pickle.load(file) | |
with open('mlpRegression.pkl', 'rb') as file: | |
loadedmlpRegression = pickle.load(file) | |
# Regression Model Imports | |
with open('randForest.pkl', 'rb') as file: | |
loadedForest = pickle.load(file) | |
with open('knn.pkl', 'rb') as file: | |
loadedknn = pickle.load(file) | |
with open('mlpClassification.pkl', 'rb') as file: | |
loadedmlpClassification = pickle.load(file) | |
races = ['White', 'Black', 'Chinese', 'Other', 'Korean', 'Hispanic', 'Asian', 'Intermediate', 'Malay', 'Indian'] | |
modelChoice = gr.inputs.Dropdown(choices=["Decision Tree Regression", "Linear Regression", "MLP Neural Network Regression","Random Forest Classification", "K Nearest Neighbors Classification", "MLP Neural Network Classification"], label="Choose your Model") | |
# Define the input sliders for continuous features | |
age = gr.inputs.Slider(minimum=20, maximum=100, step=1, label="Age") | |
height = gr.inputs.Slider(minimum=140, maximum=200, step=1, label="Height (cm)") | |
weight = gr.inputs.Slider(minimum=30, maximum=150, step=1, label="Weight (kg)") | |
inr = gr.inputs.Slider(minimum=0, maximum=4, step=0.01, label="INR on Reported Therapeutic Dose of Warfarin") | |
# Define the input dropdowns for categorical features | |
gender = gr.inputs.Dropdown(choices=["Male", "Female"], label="Gender") | |
race = gr.inputs.Dropdown(choices=races, label="Race") | |
diabetes = gr.inputs.Dropdown(choices=["No", "Yes"], label="Diabetes") | |
simvastatin = gr.inputs.Dropdown(choices=["No", "Yes"], label="Simvastatin (Zocor)") | |
amiodarone = gr.inputs.Dropdown(choices=["No", "Yes"], label="Amiodarone (Cordarone)") | |
vkorc1 = gr.inputs.Dropdown(choices=["A/A", "A/G", "G/G"], label="VKORC1 genotype") | |
# Define the output textbox | |
therapeutic_dose = gr.outputs.Textbox(label="If Classification output = Yes or No (Above 50 mg/week) If Regression output = recomended dose (mg/week)") | |
# Define the function to make predictions | |
def predict_therapeutic_dose(modelChoice, gender, race, age, height, weight, diabetes, simvastatin, amiodarone, inr, vkorc1): | |
# Converting values to values that the models understand | |
gender = {"Male": 1, "Female": 0}[gender] | |
race = races.index(race) | |
diabetes = {"No": 0, "Yes": 1}[diabetes] | |
simvastatin = {"No": 0, "Yes": 1}[simvastatin] | |
amiodarone = {"No": 0, "Yes": 1}[amiodarone] | |
vkorc1 = {"A/A": 1, "A/G": 2, "G/G": 3}[vkorc1] | |
if modelChoice == "Decision Tree Regression": | |
predicted_dose = loadedDecisionTree.predict(np.array([[gender, race, age, height, weight, diabetes, simvastatin, amiodarone, inr, vkorc1]])) | |
elif modelChoice == "Linear Regression": | |
predicted_dose = loadedLinearReg.predict(np.array([[gender, race, age, height, weight, diabetes, simvastatin, amiodarone, inr, vkorc1]])) | |
elif modelChoice == "MLP Neural Network Regression": | |
predicted_dose = loadedmlpRegression.predict(np.array([[gender, race, age, height, weight, diabetes, simvastatin, amiodarone, inr, vkorc1]])) | |
elif modelChoice == "Random Forest Classification": | |
predicted_dose = loadedForest.predict(np.array([[gender, race, age, height, weight, diabetes, simvastatin, amiodarone, inr, vkorc1]])) | |
elif modelChoice == "K Nearest Neighbors Classification": | |
predicted_dose = loadedknn.predict(np.array([[gender, race, age, height, weight, diabetes, simvastatin, amiodarone, inr, vkorc1]])) | |
else: | |
predicted_dose = loadedmlpClassification.predict(np.array([[gender, race, age, height, weight, diabetes, simvastatin, amiodarone, inr, vkorc1]])) | |
if predicted_dose[0] == 0: | |
predicted_dose = "No" | |
elif predicted_dose[0] == 1: | |
predicted_dose = "Yes" | |
return predicted_dose | |
# Create the Gradio interface | |
gr.Interface( fn=predict_therapeutic_dose, inputs=[modelChoice, gender, race, age, height, weight, diabetes, simvastatin, amiodarone, inr, vkorc1], outputs=therapeutic_dose ).launch(debug=True) |