sajinpgupta commited on
Commit
986040e
·
1 Parent(s): 95f4d62

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -16
app.py CHANGED
@@ -8,17 +8,16 @@ import torch.optim as optim
8
  from torchvision import transforms
9
  from PIL import Image
10
  import pandas as pd
11
- import joblib
12
- import os
13
 
14
  from sklearn.preprocessing import MinMaxScaler,LabelEncoder
15
 
16
- path = os.getcwd()
17
  n_tab_features = 8
18
  num_classes = 11
19
  n_pain_cls = 4
20
  n_hidden = 128
21
 
 
22
  scaler = MinMaxScaler(feature_range=(0,1))
23
  encoder = LabelEncoder()
24
 
@@ -89,11 +88,15 @@ dense_features = ['Weight_in_Kgs','Heart_rate','pulse_rate','Systolic_BP','Diast
89
  sparse_features = ['Pain_Position']
90
 
91
  # Define a function to get the medicine name from the model output
92
- def get_medicine_name(pred_medicine_class):
93
- med_encoder = joblib.load(path+'/cat_encoder.joblib')
94
  pred_medicine_name = med_encoder.inverse_transform(pred_medicine_class)
 
 
 
 
95
  # Return the corresponding medicine name
96
- return pred_medicine_name
97
 
98
  # Define a function to get the model input from the user input
99
  def get_model_input(Weight_in_Kgs, Heart_rate, pulse_rate, Systolic_BP, Diastolic_BP, BIS_Value, SPO2, Pain_Position):
@@ -103,17 +106,17 @@ def get_model_input(Weight_in_Kgs, Heart_rate, pulse_rate, Systolic_BP, Diastoli
103
  temp = pd.DataFrame(all_values, columns= dense_features + sparse_features)
104
  # print(temp.dtypes)
105
  # Normalize the dense feature values to be between 0 and 1
106
- scaler = joblib.load(path+'/scaler.joblib')
107
  temp[dense_features] = scaler.transform( temp[dense_features] )
108
- print(temp)
109
 
110
  # Encode the categorical features 0, 1 etc.
111
- painpos_encoder = joblib.load(path+'/cat_pain_level_encoder.joblib')
112
  temp[sparse_features] = painpos_encoder.transform( cat_values[0] )
113
 
114
  # Create a tensor from the normalized values
115
  input = torch.tensor(temp.to_numpy()).float()
116
- print(input)
117
  # Return the input tensor
118
  return input
119
 
@@ -121,13 +124,13 @@ def get_model_input(Weight_in_Kgs, Heart_rate, pulse_rate, Systolic_BP, Diastoli
121
  def predict(Weight_in_Kgs, Heart_rate, pulse_rate, Systolic_BP, Diastolic_BP, BIS_Value, SPO2, Pain_Position):
122
  # Get the model input from the user input
123
  X = get_model_input(Weight_in_Kgs, Heart_rate, pulse_rate, Systolic_BP, Diastolic_BP, BIS_Value, SPO2, Pain_Position)
124
- print(X.dtype)
125
  # Get the model output from the model input
126
  _, _, _, _, pred_dosage, pred_medicine_class, pred_pain_cls = model(X)
127
  # Get the medicine name from the model output
128
- medicine = get_medicine_name(pred_medicine_class)
129
  # Return the predicted medicine name as a string
130
- return "The predicted medicine for you is: " + medicine
131
 
132
  Weight_in_Kgs, Heart_rate, pulse_rate, Systolic_BP = 75.3, 85.7, 84, 141
133
  Diastolic_BP, BIS_Value, SPO2, Pain_Position = 92, 80, 90, 'Lower Back'
@@ -144,8 +147,7 @@ interface = gr.Interface(
144
  gr.inputs.Slider(minimum=71,maximum=100,step=1,label="SPO2"),
145
  gr.inputs.Dropdown(['Lower Back', 'Shoulder ', 'Abdomine', 'Muscle', 'Neck',
146
  'Fybromialgia', 'Knee Joint', 'Joint pain', 'Shoulder', 'No Pain'], label="Pain Position", default="Lower Back") ],
147
- outputs=gr.outputs.Textbox(label="Medicine") )
148
 
149
  # Launch the interface on Hugging Face Spaces (assuming you have an account)
150
- interface.launch()
151
-
 
8
  from torchvision import transforms
9
  from PIL import Image
10
  import pandas as pd
11
+ import joblib,os
 
12
 
13
  from sklearn.preprocessing import MinMaxScaler,LabelEncoder
14
 
 
15
  n_tab_features = 8
16
  num_classes = 11
17
  n_pain_cls = 4
18
  n_hidden = 128
19
 
20
+ project = os.getcwd()
21
  scaler = MinMaxScaler(feature_range=(0,1))
22
  encoder = LabelEncoder()
23
 
 
88
  sparse_features = ['Pain_Position']
89
 
90
  # Define a function to get the medicine name from the model output
91
+ def get_medicine_name(pred_medicine_class, pred_pain_cls):
92
+ med_encoder = joblib.load(project+'/cat_encoder.joblib')
93
  pred_medicine_name = med_encoder.inverse_transform(pred_medicine_class)
94
+
95
+ pain_level_enc= joblib.load(project+'/cat_dos_level_encoder.joblib')
96
+ pred_dos_level_nm = pain_level_enc.inverse_transform(pred_pain_cls)
97
+
98
  # Return the corresponding medicine name
99
+ return pred_medicine_name, pred_dos_level_nm
100
 
101
  # Define a function to get the model input from the user input
102
  def get_model_input(Weight_in_Kgs, Heart_rate, pulse_rate, Systolic_BP, Diastolic_BP, BIS_Value, SPO2, Pain_Position):
 
106
  temp = pd.DataFrame(all_values, columns= dense_features + sparse_features)
107
  # print(temp.dtypes)
108
  # Normalize the dense feature values to be between 0 and 1
109
+ scaler = joblib.load(project+'/scaler.joblib')
110
  temp[dense_features] = scaler.transform( temp[dense_features] )
111
+ #print(temp)
112
 
113
  # Encode the categorical features 0, 1 etc.
114
+ painpos_encoder = joblib.load(project+'/cat_pain_level_encoder.joblib')
115
  temp[sparse_features] = painpos_encoder.transform( cat_values[0] )
116
 
117
  # Create a tensor from the normalized values
118
  input = torch.tensor(temp.to_numpy()).float()
119
+ #print(input)
120
  # Return the input tensor
121
  return input
122
 
 
124
  def predict(Weight_in_Kgs, Heart_rate, pulse_rate, Systolic_BP, Diastolic_BP, BIS_Value, SPO2, Pain_Position):
125
  # Get the model input from the user input
126
  X = get_model_input(Weight_in_Kgs, Heart_rate, pulse_rate, Systolic_BP, Diastolic_BP, BIS_Value, SPO2, Pain_Position)
127
+ #print(X.dtype)
128
  # Get the model output from the model input
129
  _, _, _, _, pred_dosage, pred_medicine_class, pred_pain_cls = model(X)
130
  # Get the medicine name from the model output
131
+ medicine, dosage_level_nm = get_medicine_name(pred_medicine_class, pred_pain_cls)
132
  # Return the predicted medicine name as a string
133
+ return "The predicted medicine for you is: " + medicine[0], dosage_level_nm[0]
134
 
135
  Weight_in_Kgs, Heart_rate, pulse_rate, Systolic_BP = 75.3, 85.7, 84, 141
136
  Diastolic_BP, BIS_Value, SPO2, Pain_Position = 92, 80, 90, 'Lower Back'
 
147
  gr.inputs.Slider(minimum=71,maximum=100,step=1,label="SPO2"),
148
  gr.inputs.Dropdown(['Lower Back', 'Shoulder ', 'Abdomine', 'Muscle', 'Neck',
149
  'Fybromialgia', 'Knee Joint', 'Joint pain', 'Shoulder', 'No Pain'], label="Pain Position", default="Lower Back") ],
150
+ outputs=[gr.outputs.Textbox(label="Medicine"),gr.outputs.Textbox(label="Dosage Level")] )
151
 
152
  # Launch the interface on Hugging Face Spaces (assuming you have an account)
153
+ interface.launch()