Update app.py
Browse files
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(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
global df_global
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 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
|
| 402 |
-
visual_output
|
| 403 |
-
agent_btn
|
| 404 |
|
|
|
|
| 405 |
with gr.Row():
|
| 406 |
-
train_btn
|
| 407 |
-
metrics_output = gr.JSON(label="
|
| 408 |
-
trials_output
|
| 409 |
|
|
|
|
| 410 |
with gr.Row():
|
| 411 |
explain_btn = gr.Button("SHAP + LIME Explainability")
|
| 412 |
-
shap_img
|
| 413 |
-
lime_img
|
| 414 |
-
|
|
|
|
| 415 |
with gr.Row():
|
| 416 |
ab_test_button = gr.Button("Run A/B Testing")
|
| 417 |
-
ab_summary
|
| 418 |
-
ab_results
|
| 419 |
-
|
| 420 |
-
|
| 421 |
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
|
|
|
| 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)
|