Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -179,6 +179,20 @@ questions = {
|
|
| 179 |
def predict_ncd(*inputs):
|
| 180 |
# Convert inputs to appropriate format
|
| 181 |
input_dict = {name: value for name, value in zip(questions.keys(), inputs)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
df = pd.DataFrame([input_dict])
|
| 183 |
|
| 184 |
# Load the model and scaler
|
|
@@ -187,6 +201,15 @@ def predict_ncd(*inputs):
|
|
| 187 |
with open('scaler.pkl', 'rb') as f:
|
| 188 |
scaler = pickle.load(f)
|
| 189 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
# Preprocess the input
|
| 191 |
X = scaler.transform(df)
|
| 192 |
|
|
@@ -206,7 +229,7 @@ for name, config in questions.items():
|
|
| 206 |
elif config['type'] == 'radio':
|
| 207 |
inputs.append(gr.Radio(choices=config['choices'], label=config['label']))
|
| 208 |
|
| 209 |
-
output = gr.Label(label="
|
| 210 |
|
| 211 |
iface = gr.Interface(
|
| 212 |
fn=predict_ncd,
|
|
|
|
| 179 |
def predict_ncd(*inputs):
|
| 180 |
# Convert inputs to appropriate format
|
| 181 |
input_dict = {name: value for name, value in zip(questions.keys(), inputs)}
|
| 182 |
+
|
| 183 |
+
# Encode categorical variables
|
| 184 |
+
for col, value in input_dict.items():
|
| 185 |
+
if col in questions and questions[col]['type'] == 'radio':
|
| 186 |
+
# Find the index of the selected choice
|
| 187 |
+
try:
|
| 188 |
+
choice_index = questions[col]['choices'].index(value)
|
| 189 |
+
# Get the corresponding encoded value
|
| 190 |
+
input_dict[col] = questions[col]['values'][choice_index]
|
| 191 |
+
except ValueError:
|
| 192 |
+
# Handle case where value is not in choices
|
| 193 |
+
input_dict[col] = -1 # default value for unknown/invalid choices
|
| 194 |
+
|
| 195 |
+
# Create DataFrame with encoded values
|
| 196 |
df = pd.DataFrame([input_dict])
|
| 197 |
|
| 198 |
# Load the model and scaler
|
|
|
|
| 201 |
with open('scaler.pkl', 'rb') as f:
|
| 202 |
scaler = pickle.load(f)
|
| 203 |
|
| 204 |
+
# Ensure column order matches training data
|
| 205 |
+
expected_columns = ['age', 'A2', 'A1', 'M6', 'M7', 'E5_a', 'education', 'F10', 'E3',
|
| 206 |
+
'B3', 'B4', 'E1', 'E2', 'B1', 'B2', 'E11', 'B5', 'F15', 'B6',
|
| 207 |
+
'J14', 'J15', 'J17', 'K2', 'J1', 'J2', 'J4', 'E12', 'E6', 'B8', 'B9']
|
| 208 |
+
df = df[expected_columns]
|
| 209 |
+
|
| 210 |
+
# Convert all values to float
|
| 211 |
+
df = df.astype(float)
|
| 212 |
+
|
| 213 |
# Preprocess the input
|
| 214 |
X = scaler.transform(df)
|
| 215 |
|
|
|
|
| 229 |
elif config['type'] == 'radio':
|
| 230 |
inputs.append(gr.Radio(choices=config['choices'], label=config['label']))
|
| 231 |
|
| 232 |
+
output = gr.Label(label="Model Prediction:")
|
| 233 |
|
| 234 |
iface = gr.Interface(
|
| 235 |
fn=predict_ncd,
|