Update app.py
Browse files
app.py
CHANGED
|
@@ -64,8 +64,8 @@ def load_csv(file):
|
|
| 64 |
return df, gr.update(choices=numeric_cols, value=numeric_cols[:5])
|
| 65 |
|
| 66 |
# ---------- Main Analysis ----------
|
| 67 |
-
def analyze(df, selected_kpis, progress=gr.Progress(
|
| 68 |
-
|
| 69 |
print("β
Analyze function triggered")
|
| 70 |
|
| 71 |
if df is None or not selected_kpis:
|
|
@@ -77,7 +77,6 @@ def analyze(df, selected_kpis, progress=gr.Progress(track_tqdm=False)):
|
|
| 77 |
explanations = []
|
| 78 |
total = len(selected_kpis)
|
| 79 |
|
| 80 |
-
# ---- KPI Analysis ----
|
| 81 |
for i, kpi in enumerate(selected_kpis):
|
| 82 |
progress((i + 1) / total, desc=f"Analyzing {kpi}...")
|
| 83 |
|
|
@@ -87,27 +86,25 @@ def analyze(df, selected_kpis, progress=gr.Progress(track_tqdm=False)):
|
|
| 87 |
|
| 88 |
results.append((kpi, trend, score, vals))
|
| 89 |
|
| 90 |
-
# ---- Sort by score ----
|
| 91 |
ranked = sorted(results, key=lambda x: x[2], reverse=True)
|
| 92 |
|
| 93 |
-
#
|
| 94 |
progress(0.9, desc="Generating LLM explanations...")
|
| 95 |
|
| 96 |
for kpi, trend, score, vals in ranked[:5]:
|
|
|
|
| 97 |
try:
|
| 98 |
-
exp = explain(kpi,
|
| 99 |
except Exception as e:
|
| 100 |
exp = f"β οΈ LLM Error: {str(e)}"
|
| 101 |
explanations.append(f"**{kpi}** β {exp}")
|
| 102 |
|
| 103 |
progress(1.0, desc="Done β
")
|
| 104 |
|
| 105 |
-
|
| 106 |
-
elapsed = round(time.time() - start_time, 2)
|
| 107 |
|
| 108 |
-
|
| 109 |
-
text =
|
| 110 |
-
text += "## π₯ Top 5 KPIs with Biggest Change\n"
|
| 111 |
|
| 112 |
for kpi, trend, score, _ in ranked[:5]:
|
| 113 |
text += f"**{kpi}** β {trend} (Score: {score})\n\n"
|
|
@@ -116,10 +113,6 @@ def analyze(df, selected_kpis, progress=gr.Progress(track_tqdm=False)):
|
|
| 116 |
for e in explanations:
|
| 117 |
text += f"{e}\n\n"
|
| 118 |
|
| 119 |
-
text += "## π Full Ranking\n"
|
| 120 |
-
for kpi, trend, score, _ in ranked:
|
| 121 |
-
text += f"- {kpi}: {trend} | Score {score}\n"
|
| 122 |
-
|
| 123 |
return text
|
| 124 |
|
| 125 |
# ---------- UI ----------
|
|
|
|
| 64 |
return df, gr.update(choices=numeric_cols, value=numeric_cols[:5])
|
| 65 |
|
| 66 |
# ---------- Main Analysis ----------
|
| 67 |
+
def analyze(df, selected_kpis, progress=gr.Progress()):
|
| 68 |
+
start = time.time()
|
| 69 |
print("β
Analyze function triggered")
|
| 70 |
|
| 71 |
if df is None or not selected_kpis:
|
|
|
|
| 77 |
explanations = []
|
| 78 |
total = len(selected_kpis)
|
| 79 |
|
|
|
|
| 80 |
for i, kpi in enumerate(selected_kpis):
|
| 81 |
progress((i + 1) / total, desc=f"Analyzing {kpi}...")
|
| 82 |
|
|
|
|
| 86 |
|
| 87 |
results.append((kpi, trend, score, vals))
|
| 88 |
|
|
|
|
| 89 |
ranked = sorted(results, key=lambda x: x[2], reverse=True)
|
| 90 |
|
| 91 |
+
# LLM step
|
| 92 |
progress(0.9, desc="Generating LLM explanations...")
|
| 93 |
|
| 94 |
for kpi, trend, score, vals in ranked[:5]:
|
| 95 |
+
short_vals = vals[:50] # β
FIX TOKEN OVERFLOW
|
| 96 |
try:
|
| 97 |
+
exp = explain(kpi, short_vals, trend)
|
| 98 |
except Exception as e:
|
| 99 |
exp = f"β οΈ LLM Error: {str(e)}"
|
| 100 |
explanations.append(f"**{kpi}** β {exp}")
|
| 101 |
|
| 102 |
progress(1.0, desc="Done β
")
|
| 103 |
|
| 104 |
+
elapsed = round(time.time() - start, 2)
|
|
|
|
| 105 |
|
| 106 |
+
text = f"β± Time taken: {elapsed}s\n\n"
|
| 107 |
+
text += "## π₯ Top 5 KPIs\n"
|
|
|
|
| 108 |
|
| 109 |
for kpi, trend, score, _ in ranked[:5]:
|
| 110 |
text += f"**{kpi}** β {trend} (Score: {score})\n\n"
|
|
|
|
| 113 |
for e in explanations:
|
| 114 |
text += f"{e}\n\n"
|
| 115 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
return text
|
| 117 |
|
| 118 |
# ---------- UI ----------
|