Update app.py
Browse files
app.py
CHANGED
|
@@ -57,14 +57,20 @@ def load_csv(file):
|
|
| 57 |
return df, gr.update(choices=numeric_cols, value=numeric_cols[:5])
|
| 58 |
|
| 59 |
# ---------- Main Analysis ----------
|
| 60 |
-
def analyze(df, selected_kpis):
|
| 61 |
if df is None or not selected_kpis:
|
| 62 |
return "β Upload CSV and select KPI columns"
|
| 63 |
|
|
|
|
|
|
|
| 64 |
results = []
|
| 65 |
explanations = []
|
| 66 |
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
vals = df[kpi].dropna().values.tolist()
|
| 69 |
trend = detect_trend(vals)
|
| 70 |
score = change_score(vals)
|
|
@@ -73,12 +79,16 @@ def analyze(df, selected_kpis):
|
|
| 73 |
|
| 74 |
ranked = sorted(results, key=lambda x: x[2], reverse=True)
|
| 75 |
|
| 76 |
-
|
|
|
|
|
|
|
| 77 |
for kpi, trend, score, vals in ranked[:5]:
|
| 78 |
exp = explain(kpi, vals, trend)
|
| 79 |
explanations.append(f"**{kpi}** β {exp}")
|
| 80 |
|
| 81 |
-
|
|
|
|
|
|
|
| 82 |
text = "## π₯ Top 5 KPIs with Biggest Change\n"
|
| 83 |
for kpi, trend, score, _ in ranked[:5]:
|
| 84 |
text += f"**{kpi}** β {trend} (Score: {score})\n\n"
|
|
@@ -93,6 +103,7 @@ def analyze(df, selected_kpis):
|
|
| 93 |
|
| 94 |
return text
|
| 95 |
|
|
|
|
| 96 |
# ---------- UI ----------
|
| 97 |
with gr.Blocks() as demo:
|
| 98 |
gr.Markdown("# π€ KPI Trend Analyzer with LLM Explanation")
|
|
@@ -107,6 +118,12 @@ with gr.Blocks() as demo:
|
|
| 107 |
analyze_btn = gr.Button("Analyze")
|
| 108 |
output = gr.Markdown()
|
| 109 |
|
| 110 |
-
analyze_btn.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
|
|
|
| 112 |
demo.launch()
|
|
|
|
| 57 |
return df, gr.update(choices=numeric_cols, value=numeric_cols[:5])
|
| 58 |
|
| 59 |
# ---------- Main Analysis ----------
|
| 60 |
+
def analyze(df, selected_kpis, progress=gr.Progress(track_tqdm=False)):
|
| 61 |
if df is None or not selected_kpis:
|
| 62 |
return "β Upload CSV and select KPI columns"
|
| 63 |
|
| 64 |
+
progress(0, desc="Starting analysis...")
|
| 65 |
+
|
| 66 |
results = []
|
| 67 |
explanations = []
|
| 68 |
|
| 69 |
+
total = len(selected_kpis)
|
| 70 |
+
|
| 71 |
+
for i, kpi in enumerate(selected_kpis):
|
| 72 |
+
progress((i + 1) / total, desc=f"Analyzing {kpi}...")
|
| 73 |
+
|
| 74 |
vals = df[kpi].dropna().values.tolist()
|
| 75 |
trend = detect_trend(vals)
|
| 76 |
score = change_score(vals)
|
|
|
|
| 79 |
|
| 80 |
ranked = sorted(results, key=lambda x: x[2], reverse=True)
|
| 81 |
|
| 82 |
+
progress(0.85, desc="Generating LLM explanations...")
|
| 83 |
+
|
| 84 |
+
# Explain only top 5
|
| 85 |
for kpi, trend, score, vals in ranked[:5]:
|
| 86 |
exp = explain(kpi, vals, trend)
|
| 87 |
explanations.append(f"**{kpi}** β {exp}")
|
| 88 |
|
| 89 |
+
progress(1.0, desc="Done β
")
|
| 90 |
+
|
| 91 |
+
# ----- Output -----
|
| 92 |
text = "## π₯ Top 5 KPIs with Biggest Change\n"
|
| 93 |
for kpi, trend, score, _ in ranked[:5]:
|
| 94 |
text += f"**{kpi}** β {trend} (Score: {score})\n\n"
|
|
|
|
| 103 |
|
| 104 |
return text
|
| 105 |
|
| 106 |
+
|
| 107 |
# ---------- UI ----------
|
| 108 |
with gr.Blocks() as demo:
|
| 109 |
gr.Markdown("# π€ KPI Trend Analyzer with LLM Explanation")
|
|
|
|
| 118 |
analyze_btn = gr.Button("Analyze")
|
| 119 |
output = gr.Markdown()
|
| 120 |
|
| 121 |
+
analyze_btn.click(
|
| 122 |
+
fn=analyze,
|
| 123 |
+
inputs=[df_state, kpi_select],
|
| 124 |
+
outputs=output,
|
| 125 |
+
show_progress=True
|
| 126 |
+
)
|
| 127 |
|
| 128 |
+
# analyze_btn.click(fn=analyze, inputs=[df_state, kpi_select], outputs=output)
|
| 129 |
demo.launch()
|