CoolFace
Apppublic

austinjhoward00/FinalProject

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
app.py135 linesDownload Raw Back to root
1import joblib2from sklearn.preprocessing import StandardScaler3import gradio as gr4import pandas as pd5import numpy as np6 7# All of the columns listed here, for use later8column_names = ['Gender_impute', 'Height (cm)_impute', 'Weight (kg)_impute',9       'Diabetes_impute', 'Simvastatin (Zocor)_impute',10       'Amiodarone (Cordarone)_impute',11       'INR on Reported Therapeutic Dose of Warfarin_impute',12       'VKORC1 genotype: -1639 G>A (3673); chr16:31015190; rs9923231; C/T_impute',13       'Race (Reported)_impute_0.0',14       'Race (Reported)_impute_1.0', 'Race (Reported)_impute_2.0',15       'Race (Reported)_impute_3.0', 'Race (Reported)_impute_4.0',16       'Race (Reported)_impute_5.0', 'Race (Reported)_impute_6.0',17       'Race (Reported)_impute_7.0', 'Race (Reported)_impute_8.0',18       'Race (Reported)_impute_9.0', 'Race (Reported)_impute_10.0',19       'Race (Reported)_impute_11.0', 'Race (Reported)_impute_12.0',20       'Race (Reported)_impute_13.0', 'Race (Reported)_impute_14.0',21       'Race (Reported)_impute_15.0', 'Race (Reported)_impute_16.0',22       'Race (Reported)_impute_17.0', 'Race (Reported)_impute_18.0',23       'Race (Reported)_impute_19.0', 'Race (Reported)_impute_20.0',24       'Age_impute_0.0', 'Age_impute_1.0', 'Age_impute_2.0', 'Age_impute_3.0',25       'Age_impute_4.0', 'Age_impute_4.904855059576739', 'Age_impute_5.0',26       'Age_impute_6.0', 'Age_impute_7.0']27 28# Creating all of the inputs for this application29input_sex = gr.inputs.Dropdown(default = 'Male', choices = ['Male', 'Female'], label = "Gender: ")30input_race = gr.inputs.Dropdown(default = 'White', choices = ['White', 'Japanese', 'Caucasian', 'Korean', 'Han Chinese', 'Black', 'Black or African American', 'Chinese', 'Malay', 'Intermediate', 31            'Indian', 'African-American', 'Asian', 'Other Mixed Race', 'Hispanic', 'Other', 'Black other', 'Black Caribbean', 32            'Other (Hungarian)', 'Other (Black British)', 'Black African'], label = "Race (Reported): ")33input_age = gr.inputs.Dropdown(default = '10 - 19', choices = ['10 - 19', '20 - 29', '30 - 39', '40 - 49', '50 - 59', '60 - 69', '70 - 79', '80 - 89', '90+'], label = "Age: ")34input_height = gr.inputs.Slider(default = 150, minimum = 125, maximum = 202, label = "Height(cm): ")35input_weight = gr.inputs.Slider(default = 125, minimum = 30, maximum = 237, label = "Weight(kg): ")36input_diabetes = gr.inputs.Dropdown(default = 'No', choices = ['Yes', 'No'], label = "Diabetes: ")37input_simvastatin = gr.inputs.Dropdown(default = 'No', choices = ['Yes', 'No'], label = "Simvastatin (Zocor): ")38input_amiodarone = gr.inputs.Dropdown(default = 'No', choices = ['Yes', 'No'], label = "Amiodarone (Cordarone): ")39input_inr_therapeutic = gr.inputs.Slider(default = '3.0', minimum = 0.8, maximum = 6.1, label = "INR on Reported Therapeutic Dose of Warfarin: ")40input_genotype = gr.inputs.Dropdown(default = 'A/A', choices = ['A/A', 'A/G', 'G/G'], label = "VKORC1 genotype: -1639 G>A (3673); chr16:31015190; rs9923231; C/T: ")41input_model = gr.inputs.Dropdown(default = 'Linear Regression', choices=['Linear Regression', 'Polynomial Regression', 'Ridge Regression', 'Decision Tree', 'Artificial Neural Network'], label = "ML Models Dropdown")42 43# creating the output for this application44output_module = gr.outputs.Textbox(label = "Therapeutic Dose of Warfarin")45 46# Creating the mapping so that we can turn the dropdown text into values47gender_map = {'Male': 0, 'Female': 1}48race_map = {'White': 0, 'Japanese': 1, 'Caucasian': 2, 'Korean': 3, 'Han Chinese': 4, 'Black': 5, 'Black or African American': 6, 'Chinese': 7, 'Malay': 8, 'Intermediate': 9, 49            'Indian' : 10, 'African-American' : 11, 'Asian' : 12, 'Other Mixed Race' : 13, 'Hispanic' : 14, 'Other' : 15, 'Black other' : 16, 'Black Caribbean' : 17, 50            'Other (Hungarian)' : 18, 'Other (Black British)' : 19, 'Black African' : 20}51age_map = {'10 - 19': 0, '20 - 29': 1, '30 - 39': 2, '40 - 49': 3, '50 - 59': 3, '60 - 69': 4, '70 - 79': 5, '80 - 89': 6, '90+': 7}52genotype_map = {'A/A': 0, 'A/G': 1, 'G/G': 2}53diabetes_map = {'No': 0, 'Yes': 1}54simvastatin_map = {'No': 0, 'Yes': 1}55amiodarone_map = {'No': 0, 'Yes': 1}56 57 58# loading our models59lr_model = joblib.load('lr_model.joblib') #linear regression 60poly_reg_model = joblib.load('poly_reg_model.joblib') # Polynomial Regression61rr_model = joblib.load('ridge_model.joblib') # ridge regression62dt_model = joblib.load('dt_model.joblib') # decision tree63ann_model = joblib.load('ann_model.joblib') # artificial neural networks64 65scaler = joblib.load('minmax_scaler.joblib')66poly_scaler = joblib.load("poly_scaler.joblib")67 68# rf_model = joblib.load('rf_model.joblib') # random forest69 70# defining our function to make the predictions71def pred(gender, race, age, height, weight, diabetes, simvastatin, amiodarone, inr_therapeutic, genotype, model):72 73  gender = gender_map[gender] # getting gender value74  #-----------------------------75  race_index = race_map[race] # getting race value76  race = [0] * 2177  race[race_index] = 178  print(race)79  #-----------------------------80  age_index = age_map[age] # getting age value81  age = [0] * 982  age[age_index] = 183  print(age)84  #-----------------------------85  height = height # getting height value86  weight = weight # getting weight value87  diabetes = diabetes_map[diabetes] # getting diabetes value88  simvastatin = simvastatin_map[simvastatin] # getting simvastatin value89  amiodarone = amiodarone_map[amiodarone] # getting amiodarone value90  inr_therapeutic = inr_therapeutic # gwtting inr_therapeutic value91  genotype = genotype_map[genotype] # getting genotype value92 93 94  input_list = [gender, height, weight, diabetes, simvastatin, amiodarone, inr_therapeutic, genotype]95  96  for i in race:97    input_list.append(i)98  for j in age:99    input_list.append(j)100 101  import numpy as np102 103  # Create a 1D array104  # input_list = pd.DataFrame(input_list, columns= column_names)105  # print(input_list)106 107  # Create a 2D array108  my_array = np.array(input_list).reshape(1, -1)109  df = pd.DataFrame(my_array, columns=[f"{i}" for i in column_names])110  111  # Reshape the 1D array into a 2D array with 1 rows and 36 columns112  # new_input = input_list.reshape((1, 38))113  114  if model == "Polynomial Regression":115    poly_input = poly_scaler.transform(df)116  else:117    scaled_input = scaler.transform(df)118 119  if model == "Linear Regression":120    preds = lr_model.predict(scaled_input)121  elif model == "Ridge Regression":122    preds = rr_model.predict(scaled_input)123  elif model == "Decision Tree":124    preds = dt_model.predict(scaled_input)125  elif model == "Polynomial Regression":126    preds = poly_reg_model.predict(poly_input)127  elif model == "Artificial Neural Network":128    preds = ann_model.predict(scaled_input)129    return preds[0][0]130 131  return preds[0]132 133gr.Interface(fn=pred, 134             inputs=[input_sex,input_race,input_age, input_height, input_weight,input_diabetes,input_simvastatin,input_amiodarone,input_inr_therapeutic,input_genotype,input_model],135             outputs=output_module).launch(debug = True)