Barisha commited on
Commit
563f077
Β·
verified Β·
1 Parent(s): ee79e54

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -15
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(track_tqdm=False)):
68
- start_time = time.time()
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
- # ---- LLM Explanations for Top 5 ----
94
  progress(0.9, desc="Generating LLM explanations...")
95
 
96
  for kpi, trend, score, vals in ranked[:5]:
 
97
  try:
98
- exp = explain(kpi, vals, trend)
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
- # ---- Time Taken ----
106
- elapsed = round(time.time() - start_time, 2)
107
 
108
- # ---- Output ----
109
- text = f"⏱️ Time taken: {elapsed} seconds\n\n"
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 ----------