Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -34,6 +34,7 @@ def update_dropdown(file):
|
|
| 34 |
return gr.update(choices=[], value=None)
|
| 35 |
return gr.update(choices=list(df.columns), value=None)
|
| 36 |
except Exception as e:
|
|
|
|
| 37 |
return gr.update(choices=[], value=None)
|
| 38 |
|
| 39 |
def analyze_file(file, label_col, n_clusters):
|
|
@@ -48,6 +49,7 @@ def analyze_file(file, label_col, n_clusters):
|
|
| 48 |
else:
|
| 49 |
return ("Unsupported file type. Please upload a CSV or XLSX file.", None, None, None, None, None)
|
| 50 |
except Exception as e:
|
|
|
|
| 51 |
return (f"Error reading file: {e}", None, None, None, None, None)
|
| 52 |
|
| 53 |
if df.empty:
|
|
@@ -253,6 +255,7 @@ def predict_interactive(**kwargs):
|
|
| 253 |
|
| 254 |
def create_interactive_inputs(file, label_col):
|
| 255 |
if file is None or label_col is None:
|
|
|
|
| 256 |
return []
|
| 257 |
|
| 258 |
try:
|
|
@@ -261,9 +264,18 @@ def create_interactive_inputs(file, label_col):
|
|
| 261 |
elif file.name.endswith('.xlsx'):
|
| 262 |
df = pd.read_excel(file.name)
|
| 263 |
else:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 264 |
return []
|
| 265 |
|
| 266 |
X = df.drop(columns=[label_col])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 267 |
components = []
|
| 268 |
for col in X.columns:
|
| 269 |
examples = X[col].dropna().sample(min(3, len(X[col].dropna()))).tolist()
|
|
@@ -272,8 +284,10 @@ def create_interactive_inputs(file, label_col):
|
|
| 272 |
else:
|
| 273 |
unique_values = X[col].dropna().unique().tolist()
|
| 274 |
components.append(gr.Dropdown(label=f"{col} (e.g., {', '.join(map(str, examples))})", choices=unique_values, value=None))
|
|
|
|
| 275 |
return components
|
| 276 |
-
except Exception:
|
|
|
|
| 277 |
return []
|
| 278 |
|
| 279 |
with gr.Blocks() as demo:
|
|
@@ -328,13 +342,14 @@ with gr.Blocks() as demo:
|
|
| 328 |
gr.Markdown("Enter values for each feature to get a prediction based on the trained model.")
|
| 329 |
with gr.Column():
|
| 330 |
input_components = gr.State(value=[])
|
| 331 |
-
dynamic_inputs = gr.Column()
|
| 332 |
predict_btn = gr.Button("Predict")
|
| 333 |
prediction_output = gr.Textbox(label="Prediction Result")
|
| 334 |
|
| 335 |
def update_inputs(file, label_col):
|
|
|
|
| 336 |
components = create_interactive_inputs(file, label_col)
|
| 337 |
-
return components, gr.Column.update(components=components)
|
| 338 |
|
| 339 |
file_input.change(
|
| 340 |
fn=update_inputs,
|
|
@@ -355,4 +370,4 @@ with gr.Blocks() as demo:
|
|
| 355 |
analyze_btn.click(fn=analyze_file, inputs=[file_input, label_dropdown, clusters_slider],
|
| 356 |
outputs=[results_textbox, model_img_output, fi_output, kmeans_output, agg_output, diff_output])
|
| 357 |
|
| 358 |
-
demo.launch()
|
|
|
|
| 34 |
return gr.update(choices=[], value=None)
|
| 35 |
return gr.update(choices=list(df.columns), value=None)
|
| 36 |
except Exception as e:
|
| 37 |
+
print(f"Error in update_dropdown: {e}") # Debug logging
|
| 38 |
return gr.update(choices=[], value=None)
|
| 39 |
|
| 40 |
def analyze_file(file, label_col, n_clusters):
|
|
|
|
| 49 |
else:
|
| 50 |
return ("Unsupported file type. Please upload a CSV or XLSX file.", None, None, None, None, None)
|
| 51 |
except Exception as e:
|
| 52 |
+
print(f"Error reading file: {e}") # Debug logging
|
| 53 |
return (f"Error reading file: {e}", None, None, None, None, None)
|
| 54 |
|
| 55 |
if df.empty:
|
|
|
|
| 255 |
|
| 256 |
def create_interactive_inputs(file, label_col):
|
| 257 |
if file is None or label_col is None:
|
| 258 |
+
print("No file or label column provided") # Debug logging
|
| 259 |
return []
|
| 260 |
|
| 261 |
try:
|
|
|
|
| 264 |
elif file.name.endswith('.xlsx'):
|
| 265 |
df = pd.read_excel(file.name)
|
| 266 |
else:
|
| 267 |
+
print("Unsupported file type") # Debug logging
|
| 268 |
+
return []
|
| 269 |
+
|
| 270 |
+
if df.empty or label_col not in df.columns:
|
| 271 |
+
print(f"Empty DataFrame or invalid label column: {label_col}") # Debug logging
|
| 272 |
return []
|
| 273 |
|
| 274 |
X = df.drop(columns=[label_col])
|
| 275 |
+
if X.empty:
|
| 276 |
+
print("No features available after dropping label column") # Debug logging
|
| 277 |
+
return []
|
| 278 |
+
|
| 279 |
components = []
|
| 280 |
for col in X.columns:
|
| 281 |
examples = X[col].dropna().sample(min(3, len(X[col].dropna()))).tolist()
|
|
|
|
| 284 |
else:
|
| 285 |
unique_values = X[col].dropna().unique().tolist()
|
| 286 |
components.append(gr.Dropdown(label=f"{col} (e.g., {', '.join(map(str, examples))})", choices=unique_values, value=None))
|
| 287 |
+
print(f"Generated {len(components)} input components") # Debug logging
|
| 288 |
return components
|
| 289 |
+
except Exception as e:
|
| 290 |
+
print(f"Error in create_interactive_inputs: {e}") # Debug logging
|
| 291 |
return []
|
| 292 |
|
| 293 |
with gr.Blocks() as demo:
|
|
|
|
| 342 |
gr.Markdown("Enter values for each feature to get a prediction based on the trained model.")
|
| 343 |
with gr.Column():
|
| 344 |
input_components = gr.State(value=[])
|
| 345 |
+
dynamic_inputs = gr.Column(visible=True)
|
| 346 |
predict_btn = gr.Button("Predict")
|
| 347 |
prediction_output = gr.Textbox(label="Prediction Result")
|
| 348 |
|
| 349 |
def update_inputs(file, label_col):
|
| 350 |
+
print(f"Updating inputs with file: {file}, label_col: {label_col}") # Debug logging
|
| 351 |
components = create_interactive_inputs(file, label_col)
|
| 352 |
+
return components, gr.Column.update(components=components, visible=True)
|
| 353 |
|
| 354 |
file_input.change(
|
| 355 |
fn=update_inputs,
|
|
|
|
| 370 |
analyze_btn.click(fn=analyze_file, inputs=[file_input, label_dropdown, clusters_slider],
|
| 371 |
outputs=[results_textbox, model_img_output, fi_output, kmeans_output, agg_output, diff_output])
|
| 372 |
|
| 373 |
+
demo.launch(debug=True) # Enable debug mode for more detailed error logging
|