Praneeth383 commited on
Commit
8aa976d
·
1 Parent(s): d569d9e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +129 -0
app.py ADDED
@@ -0,0 +1,129 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import joblib
3
+ import pandas as pd
4
+ import numpy as np
5
+ import pickle
6
+ import tensorflow as tf
7
+
8
+ # Load the saved models
9
+ best_models = [
10
+ ("Linear Regression", joblib.load('best_Linear_Regression_model.pkl')),
11
+ ("Random Forest", joblib.load('best_Random_Forest_model.pkl')),
12
+ ("Ridge Regression", joblib.load('best_Ridge_Regression_model.pkl')),
13
+ ("Decision Tree", joblib.load('best_Decision_Tree_model.pkl')),
14
+ ("MLP", tf.keras.models.load_model('best_mlp_model.h5')),
15
+ ]
16
+
17
+ # Load the saved scaler, PCA, and target scaler
18
+ scaler = joblib.load('scaler.pkl')
19
+ pca = joblib.load('pca.pkl')
20
+ #----------------------------------------
21
+ #with open("target_scaler.pkl", "rb") as f:
22
+ # target_scaler = pickle.load(f)
23
+ #--------------------------------------- uncomment this statement to get scaled predidcted values
24
+
25
+
26
+ attribute_names = [
27
+ 'Age', 'Height', 'Weight', 'Diabetes', 'Simvastatin', 'Amiodarone',
28
+ 'INR', 'Gender', 'Race', 'VKORC1_genotype'
29
+ ]
30
+
31
+ race_columns = [
32
+ 'Race_Asian', 'Race_Black', 'Race_Black African', 'Race_Black Caribbean',
33
+ 'Race_Black or African American', 'Race_Black other', 'Race_Caucasian',
34
+ 'Race_Chinese', 'Race_Han Chinese', 'Race_Hispanic', 'Race_Indian',
35
+ 'Race_Intermediate', 'Race_Japanese', 'Race_Korean', 'Race_Malay',
36
+ 'Race_Other', 'Race_Other (Black British)', 'Race_Other (Hungarian)',
37
+ 'Race_Other Mixed Race', 'Race_White', 'Race_other'
38
+ ]
39
+
40
+ def predict(*args):
41
+ user_features = list(args[:-1])
42
+ model_name = args[-1]
43
+ model = dict(best_models)[model_name]
44
+
45
+ # Mapping categorical inputs to numerical values
46
+ user_features[3] = 1 if user_features[3] == 'Yes' else 0 # Diabetes
47
+ user_features[4] = 1 if user_features[4] == 'Yes' else 0 # Simvastatin
48
+ user_features[5] = 1 if user_features[5] == 'Yes' else 0 # Amiodarone
49
+ user_features[7] = 1 if user_features[7] == 'Male' else 0 # Gender
50
+
51
+ # Handling one-hot encoding for 'Race'
52
+ race = user_features.pop(8)
53
+ race_encoded = [1 if col == race else 0 for col in race_columns]
54
+ user_features = user_features[:8] + race_encoded + user_features[8:]
55
+
56
+ # Handling one-hot encoding for 'VKORC1_genotype'
57
+ vkorc1_genotype = user_features.pop(-1)
58
+ vkorc1_genotype_encoded = [1 if vkorc1_genotype == 'A/G' else 0, 1 if vkorc1_genotype == 'G/G' else 0]
59
+ user_features += vkorc1_genotype_encoded
60
+
61
+ input_data = np.array(user_features).reshape(1, -1)
62
+ input_data_scaled = scaler.transform(input_data)
63
+ input_data_pca = pca.transform(input_data_scaled)
64
+
65
+ print("Input data PCA:", input_data_pca) # Add this print statement
66
+
67
+
68
+ if model_name == "MLP":
69
+ prediction = model.predict(input_data_pca).reshape(-1)
70
+ else:
71
+ prediction = model.predict(input_data_pca)
72
+
73
+ #print("Raw prediction:", prediction) # Add this print statement just check this line to debug whether it is giving correct prediction or not
74
+ #------------------------------------------------------------------------
75
+ # Inverse transform the prediction
76
+ #prediction_rescaled = target_scaler.inverse_transform(prediction.reshape(-1, 1))[0][0]
77
+
78
+ # return {"Predicted Value": (prediction)}
79
+
80
+ # prediction_rescaled = target_scaler.inverse_transform(prediction.reshape(-1, 1))[0][0]
81
+ # print("Prediction rescaled:", prediction_rescaled)
82
+ # return {"Therapeutic Dosage": f"{prediction_rescaled:.2f} mg"}
83
+ #----------------------------------------------------------------------------------------------- the above staments used for practice on how it will work on rescaled prediction values
84
+ return {"Therapeutic Dosage": f"{prediction[0]:.2f} mg"}
85
+
86
+
87
+ #previously it return function was not float I changed it because FastAPI can't serialize the NumPy float32 object to JSON
88
+ #I added float(prediction[0]) to convert the NumPy float32 value to a Python float.prediction_rescaled
89
+
90
+
91
+ # List of model names for the dropdown menu
92
+ model_names = [name for name, _ in best_models]
93
+
94
+ # Define inputs
95
+ age = gr.inputs.Slider(minimum=10, maximum=90, label='Age')
96
+ height = gr.inputs.Slider(minimum=124.968, maximum=202.0, label='Height')
97
+ weight = gr.inputs.Slider(minimum=30.0, maximum=237.7, label='Weight')
98
+ diabetes = gr.inputs.Dropdown(choices=['Yes', 'No'], label='Diabetes')
99
+ simvastatin = gr.inputs.Dropdown(choices=['Yes', 'No'], label='Simvastatin')
100
+ amiodarone = gr.inputs.Dropdown(choices=['Yes', 'No'], label='Amiodarone')
101
+ inr = gr.inputs.Slider(minimum=0.8, maximum=6.1, label='INR')
102
+ gender = gr.inputs.Dropdown(choices=['Male', 'Female'], label='Gender')
103
+ race = gr.inputs.Dropdown(choices=[
104
+ 'Asian', 'Black', 'Black African',
105
+ 'Black Caribbean', 'Black or African American',
106
+ 'Black other', 'Caucasian', 'Chinese',
107
+ 'Han Chinese', 'Hispanic', 'Indian', 'Intermediate',
108
+ 'Japanese', 'Korean', 'Malay', 'Other',
109
+ 'Other (Black British)', 'Other (Hungarian)',
110
+ 'Other Mixed Race', 'White', 'other'
111
+ ], label='Race')
112
+ vkorc1_genotype = gr.inputs.Dropdown(choices=['A/G', 'G/G'], label='VKORC1_genotype')
113
+ model_name = gr.inputs.Dropdown(choices=model_names, label="Model")
114
+
115
+ # Define output
116
+ output = gr.outputs.Textbox(label="Therapeutic Dosage")
117
+
118
+ # Create the Gradio interface
119
+ iface = gr.Interface(
120
+ fn=predict,
121
+ inputs=[
122
+ age, height, weight, diabetes, simvastatin, amiodarone,
123
+ inr, gender, race, vkorc1_genotype, model_name
124
+ ],
125
+ outputs=output ,title="Machine Learning Model",
126
+ description="Select a model and enter user features to predict the therapeutic dose.",
127
+ ).launch(debug=True)
128
+
129
+