File size: 5,545 Bytes
8aa976d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ba616de
8aa976d
 
 
 
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
import gradio as gr
import joblib
import pandas as pd
import numpy as np
import pickle
import tensorflow as tf

# Load the saved models
best_models = [
    ("Linear Regression", joblib.load('best_Linear_Regression_model.pkl')),
    ("Random Forest", joblib.load('best_Random_Forest_model.pkl')),
    ("Ridge Regression", joblib.load('best_Ridge_Regression_model.pkl')),
    ("Decision Tree", joblib.load('best_Decision_Tree_model.pkl')),
    ("MLP", tf.keras.models.load_model('best_mlp_model.h5')),
]

# Load the saved scaler, PCA, and target scaler
scaler = joblib.load('scaler.pkl')
pca = joblib.load('pca.pkl')
#----------------------------------------
#with open("target_scaler.pkl", "rb") as f:
   # target_scaler = pickle.load(f)
#---------------------------------------   uncomment this statement to get scaled predidcted values


attribute_names = [
    'Age', 'Height', 'Weight', 'Diabetes', 'Simvastatin', 'Amiodarone',
    'INR', 'Gender', 'Race', 'VKORC1_genotype'
]

race_columns = [
    'Race_Asian', 'Race_Black', 'Race_Black African', 'Race_Black Caribbean',
    'Race_Black or African American', 'Race_Black other', 'Race_Caucasian',
    'Race_Chinese', 'Race_Han Chinese', 'Race_Hispanic', 'Race_Indian',
    'Race_Intermediate', 'Race_Japanese', 'Race_Korean', 'Race_Malay',
    'Race_Other', 'Race_Other (Black British)', 'Race_Other (Hungarian)',
    'Race_Other Mixed Race', 'Race_White', 'Race_other'
]

def predict(*args):
    user_features = list(args[:-1])
    model_name = args[-1]
    model = dict(best_models)[model_name]
    
    # Mapping categorical inputs to numerical values
    user_features[3] = 1 if user_features[3] == 'Yes' else 0  # Diabetes
    user_features[4] = 1 if user_features[4] == 'Yes' else 0  # Simvastatin
    user_features[5] = 1 if user_features[5] == 'Yes' else 0  # Amiodarone
    user_features[7] = 1 if user_features[7] == 'Male' else 0  # Gender
    
    # Handling one-hot encoding for 'Race'
    race = user_features.pop(8)
    race_encoded = [1 if col == race else 0 for col in race_columns]
    user_features = user_features[:8] + race_encoded + user_features[8:]
    
    # Handling one-hot encoding for 'VKORC1_genotype'
    vkorc1_genotype = user_features.pop(-1)
    vkorc1_genotype_encoded = [1 if vkorc1_genotype == 'A/G' else 0, 1 if vkorc1_genotype == 'G/G' else 0]
    user_features += vkorc1_genotype_encoded
    
    input_data = np.array(user_features).reshape(1, -1)
    input_data_scaled = scaler.transform(input_data)
    input_data_pca = pca.transform(input_data_scaled)

    print("Input data PCA:", input_data_pca)  # Add this print statement

    
    if model_name == "MLP":
        prediction = model.predict(input_data_pca).reshape(-1)
    else:
        prediction = model.predict(input_data_pca)
    
    #print("Raw prediction:", prediction)  # Add this print statement just check this line to debug whether it is giving correct prediction or not
#------------------------------------------------------------------------
    # Inverse transform the prediction
    #prediction_rescaled = target_scaler.inverse_transform(prediction.reshape(-1, 1))[0][0]

   # return {"Predicted Value": (prediction)} 

   # prediction_rescaled = target_scaler.inverse_transform(prediction.reshape(-1, 1))[0][0]
   # print("Prediction rescaled:", prediction_rescaled)
   # return {"Therapeutic Dosage": f"{prediction_rescaled:.2f} mg"}
#-----------------------------------------------------------------------------------------------   the above staments used for practice on how it will work on rescaled prediction values
    return {"Therapeutic Dosage": f"{prediction[0]:.2f} mg"}


    #previously it return function was not float I changed it because FastAPI can't serialize the NumPy float32 object to JSON
    #I added float(prediction[0]) to convert the NumPy float32 value to a Python float.prediction_rescaled


# List of model names for the dropdown menu
model_names = [name for name, _ in best_models]

# Define inputs
age = gr.inputs.Slider(minimum=10, maximum=90, label='Age')
height = gr.inputs.Slider(minimum=124.968, maximum=202.0, label='Height')
weight = gr.inputs.Slider(minimum=30.0, maximum=237.7, label='Weight')
diabetes = gr.inputs.Dropdown(choices=['Yes', 'No'], label='Diabetes')
simvastatin = gr.inputs.Dropdown(choices=['Yes', 'No'], label='Simvastatin')
amiodarone = gr.inputs.Dropdown(choices=['Yes', 'No'], label='Amiodarone')
inr = gr.inputs.Slider(minimum=0.8, maximum=6.1, label='INR')
gender = gr.inputs.Dropdown(choices=['Male', 'Female'], label='Gender')
race = gr.inputs.Dropdown(choices=[
    'Asian', 'Black', 'Black African',
    'Black Caribbean', 'Black or African American',
    'Black other', 'Caucasian', 'Chinese',
    'Han Chinese', 'Hispanic', 'Indian', 'Intermediate',
    'Japanese', 'Korean', 'Malay', 'Other',
    'Other (Black British)', 'Other (Hungarian)',
    'Other Mixed Race', 'White', 'other'
], label='Race')
vkorc1_genotype = gr.inputs.Dropdown(choices=['A/G', 'G/G'], label='VKORC1_genotype')
model_name = gr.inputs.Dropdown(choices=model_names, label="Model")

# Define output
output = gr.outputs.Textbox(label="Therapeutic Dosage")

# Create the Gradio interface
iface = gr.Interface(
    fn=predict,
    inputs=[
        age, height, weight, diabetes, simvastatin, amiodarone,
        inr, gender, race, vkorc1_genotype, model_name
    ],
    outputs=output ,title="Warafarin_dose_Predicition",
    description="Select a model and enter user features to predict the therapeutic dose.",
).launch(debug=True)