pavanmutha commited on
Commit
5eb4ccd
Β·
verified Β·
1 Parent(s): 4b463c1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -27
app.py CHANGED
@@ -40,15 +40,31 @@ def clean_data(df):
40
  df = df.fillna(df.mean(numeric_only=True))
41
  return df
42
 
43
- def upload_file(file):
 
 
 
 
44
  global df_global
45
- if file is None:
46
- return pd.DataFrame({"Error": ["No file uploaded."]})
47
- ext = os.path.splitext(file.name)[-1]
48
- df = pd.read_csv(file.name) if ext == ".csv" else pd.read_excel(file.name)
49
- df = clean_data(df)
50
- df_global = df
51
- return df.head()
 
 
 
 
 
 
 
 
 
 
 
 
52
 
53
  def format_analysis_report(raw_output, visuals):
54
  try:
@@ -391,37 +407,40 @@ def explainability(_):
391
  with gr.Blocks() as demo:
392
  gr.Markdown("## πŸ“Š AI-Powered Data Analysis with Hyperparameter Optimization")
393
 
 
394
  with gr.Row():
395
  with gr.Column():
396
  file_input = gr.File(label="Upload CSV or Excel", type="filepath")
397
- df_output = gr.DataFrame(label="Cleaned Data Preview")
398
- file_input.change(fn=upload_file, inputs=file_input, outputs=df_output)
399
 
400
  with gr.Column():
401
- insights_output = gr.HTML(label="Insights from SmolAgent")
402
- visual_output = gr.Gallery(label="Visualizations (Auto-generated by Agent)", columns=2)
403
- agent_btn = gr.Button("Run AI Agent (5 Insights + 5 Visualizations)")
404
 
 
405
  with gr.Row():
406
- train_btn = gr.Button("Train Model with Optuna + WandB")
407
- metrics_output = gr.JSON(label="Performance Metrics")
408
- trials_output = gr.DataFrame(label="Top 7 Hyperparameter Trials")
409
 
 
410
  with gr.Row():
411
  explain_btn = gr.Button("SHAP + LIME Explainability")
412
- shap_img = gr.Image(label="SHAP Summary Plot")
413
- lime_img = gr.Image(label="LIME Explanation")
414
-
 
415
  with gr.Row():
416
  ab_test_button = gr.Button("Run A/B Testing")
417
- ab_summary = gr.HTML()
418
- ab_results = gr.DataFrame()
419
-
420
-
421
 
422
- agent_btn.click(fn=analyze_data, inputs=[file_input], outputs=[insights_output, visual_output])
423
- train_btn.click(fn=train_model, inputs=[file_input], outputs=[metrics_output, trials_output])
424
- explain_btn.click(fn=explainability, inputs=[file_input], outputs=[shap_img, lime_img])
 
425
  ab_test_button.click(fn=ab_test_models, inputs=[], outputs=[ab_summary, ab_results])
426
 
427
- demo.launch(debug=True)
 
40
  df = df.fillna(df.mean(numeric_only=True))
41
  return df
42
 
43
+ def upload_file(file_path):
44
+ """
45
+ file_path comes in as a str because type="filepath".
46
+ We catch any error and return a tiny DataFrame with the message.
47
+ """
48
  global df_global
49
+ try:
50
+ if not file_path:
51
+ raise ValueError("No file uploaded.")
52
+ ext = os.path.splitext(file_path)[-1].lower()
53
+ if ext == ".csv":
54
+ df = pd.read_csv(file_path)
55
+ elif ext in {".xls", ".xlsx"}:
56
+ df = pd.read_excel(file_path)
57
+ else:
58
+ raise ValueError(f"Unsupported extension: {ext}")
59
+
60
+ df = clean_data(df)
61
+ df_global = df
62
+ return df.head()
63
+
64
+ except Exception as e:
65
+ # Return a 1Γ—1 DataFrame so Gradio won’t crash
66
+ return pd.DataFrame({"Error": [str(e)]})
67
+
68
 
69
  def format_analysis_report(raw_output, visuals):
70
  try:
 
407
  with gr.Blocks() as demo:
408
  gr.Markdown("## πŸ“Š AI-Powered Data Analysis with Hyperparameter Optimization")
409
 
410
+ # ─────────────────────── Upload & Preview ───────────────────────
411
  with gr.Row():
412
  with gr.Column():
413
  file_input = gr.File(label="Upload CSV or Excel", type="filepath")
414
+ df_output = gr.DataFrame(label="Cleaned Data Preview")
415
+ file_input.change(fn=upload_file, inputs=[file_input], outputs=[dff := df_output])
416
 
417
  with gr.Column():
418
+ insights_output = gr.HTML(label="Insights")
419
+ visual_output = gr.Gallery(columns=2, label="Visuals")
420
+ agent_btn = gr.Button("Run AI Agent")
421
 
422
+ # ─────────────────── Hyperopt + Trials ───────────────────
423
  with gr.Row():
424
+ train_btn = gr.Button("Train Model")
425
+ metrics_output = gr.JSON(label="Metrics")
426
+ trials_output = gr.DataFrame(label="Top Trials")
427
 
428
+ # ─────────────────── Explainability ───────────────────
429
  with gr.Row():
430
  explain_btn = gr.Button("SHAP + LIME Explainability")
431
+ shap_img = gr.Image(label="SHAP Summary")
432
+ lime_img = gr.Image(label="LIME Explanation")
433
+
434
+ # ─────────────────── A/B Testing ───────────────────
435
  with gr.Row():
436
  ab_test_button = gr.Button("Run A/B Testing")
437
+ ab_summary = gr.HTML(label="A/B Test Summary")
438
+ ab_results = gr.DataFrame(label="A/B Test Results")
 
 
439
 
440
+ # ─────────────────── Hook callbacks ───────────────────
441
+ agent_btn.click(fn=analyze_data, inputs=[file_input], outputs=[insights_output, visual_output])
442
+ train_btn.click(fn=train_model, inputs=[file_input], outputs=[metrics_output, trials_output])
443
+ explain_btn.click(fn=explainability, inputs=[], outputs=[shap_img, lime_img])
444
  ab_test_button.click(fn=ab_test_models, inputs=[], outputs=[ab_summary, ab_results])
445
 
446
+ demo.launch(debug=True)