Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,7 @@ import pandas as pd
|
|
| 3 |
from transformers import pipeline
|
| 4 |
|
| 5 |
# ------------------------------------------------
|
| 6 |
-
# Load Qwen 3B
|
| 7 |
# ------------------------------------------------
|
| 8 |
generator = pipeline(
|
| 9 |
"text-generation",
|
|
@@ -13,7 +13,7 @@ generator = pipeline(
|
|
| 13 |
)
|
| 14 |
|
| 15 |
# ------------------------------------------------
|
| 16 |
-
#
|
| 17 |
# ------------------------------------------------
|
| 18 |
def magnitude_bucket(x):
|
| 19 |
if x < 0.05:
|
|
@@ -43,68 +43,77 @@ def analyze_kpi(csv_file, top_n):
|
|
| 43 |
df["diff"] = df[curr_date] - df[prev_date]
|
| 44 |
df["abs_diff"] = df["diff"].abs()
|
| 45 |
|
| 46 |
-
ranked = df.sort_values("abs_diff", ascending=False)
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
# -------------------------------
|
| 49 |
-
#
|
| 50 |
# -------------------------------
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
|
| 61 |
# -------------------------------
|
| 62 |
-
#
|
| 63 |
# -------------------------------
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
|
|
|
| 76 |
|
| 77 |
# -------------------------------
|
| 78 |
-
# Model input =
|
| 79 |
# -------------------------------
|
| 80 |
model_input = (
|
| 81 |
-
"
|
| 82 |
-
|
| 83 |
-
f"{
|
|
|
|
|
|
|
| 84 |
)
|
| 85 |
|
| 86 |
output = generator(
|
| 87 |
model_input,
|
| 88 |
-
max_new_tokens=
|
| 89 |
do_sample=False
|
| 90 |
)[0]["generated_text"]
|
| 91 |
|
| 92 |
-
return ranked[["Kpi", "abs_diff"]], output
|
| 93 |
|
| 94 |
# ------------------------------------------------
|
| 95 |
-
# UI
|
| 96 |
# ------------------------------------------------
|
| 97 |
-
with gr.Blocks(title="
|
| 98 |
-
gr.Markdown("##
|
| 99 |
-
gr.Markdown(
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
csv_input = gr.File(file_types=[".csv"])
|
| 102 |
-
top_n_input = gr.Slider(3,
|
| 103 |
|
| 104 |
-
btn = gr.Button("Generate")
|
| 105 |
|
| 106 |
-
table = gr.Dataframe()
|
| 107 |
-
summary = gr.Textbox(lines=
|
| 108 |
|
| 109 |
btn.click(
|
| 110 |
analyze_kpi,
|
|
|
|
| 3 |
from transformers import pipeline
|
| 4 |
|
| 5 |
# ------------------------------------------------
|
| 6 |
+
# Load Qwen 3B (instruction-following, concise)
|
| 7 |
# ------------------------------------------------
|
| 8 |
generator = pipeline(
|
| 9 |
"text-generation",
|
|
|
|
| 13 |
)
|
| 14 |
|
| 15 |
# ------------------------------------------------
|
| 16 |
+
# Fact quantization (GENERIC)
|
| 17 |
# ------------------------------------------------
|
| 18 |
def magnitude_bucket(x):
|
| 19 |
if x < 0.05:
|
|
|
|
| 43 |
df["diff"] = df[curr_date] - df[prev_date]
|
| 44 |
df["abs_diff"] = df["diff"].abs()
|
| 45 |
|
| 46 |
+
ranked = df.sort_values("abs_diff", ascending=False)
|
| 47 |
+
|
| 48 |
+
top_kpis = ranked.head(3)
|
| 49 |
+
other_kpis = ranked.iloc[3:]
|
| 50 |
|
| 51 |
# -------------------------------
|
| 52 |
+
# Top 3 KPI FACT BLOCKS
|
| 53 |
# -------------------------------
|
| 54 |
+
top_facts = []
|
| 55 |
+
for _, row in top_kpis.iterrows():
|
| 56 |
+
top_facts.append({
|
| 57 |
+
"KPI": row["Kpi"],
|
| 58 |
+
"DIRECTION": direction_bucket(row["diff"]),
|
| 59 |
+
"CHANGE_VALUE": round(row["abs_diff"], 2),
|
| 60 |
+
"MAGNITUDE": magnitude_bucket(row["abs_diff"]),
|
| 61 |
+
"UNIT": "percentage points" if "%" in row["Kpi"] else "units"
|
| 62 |
+
})
|
| 63 |
|
| 64 |
# -------------------------------
|
| 65 |
+
# Remaining KPI AGGREGATED FACTS
|
| 66 |
# -------------------------------
|
| 67 |
+
if len(other_kpis) > 0:
|
| 68 |
+
other_facts = {
|
| 69 |
+
"KPI_COUNT": len(other_kpis),
|
| 70 |
+
"AVG_CHANGE": round(other_kpis["abs_diff"].mean(), 2),
|
| 71 |
+
"MAGNITUDE": magnitude_bucket(other_kpis["abs_diff"].mean()),
|
| 72 |
+
"DIRECTION_BALANCE": (
|
| 73 |
+
"mostly_increase" if (other_kpis["diff"] > 0).mean() > 0.7
|
| 74 |
+
else "mostly_decrease" if (other_kpis["diff"] > 0).mean() < 0.3
|
| 75 |
+
else "mixed"
|
| 76 |
+
)
|
| 77 |
+
}
|
| 78 |
+
else:
|
| 79 |
+
other_facts = None
|
| 80 |
|
| 81 |
# -------------------------------
|
| 82 |
+
# Model input = PURE FACTS
|
| 83 |
# -------------------------------
|
| 84 |
model_input = (
|
| 85 |
+
"Write a short insight-style summary using only the facts below.\n\n"
|
| 86 |
+
"TOP_KPI_FACTS:\n"
|
| 87 |
+
f"{top_facts}\n\n"
|
| 88 |
+
"OTHER_KPI_FACTS:\n"
|
| 89 |
+
f"{other_facts}"
|
| 90 |
)
|
| 91 |
|
| 92 |
output = generator(
|
| 93 |
model_input,
|
| 94 |
+
max_new_tokens=120,
|
| 95 |
do_sample=False
|
| 96 |
)[0]["generated_text"]
|
| 97 |
|
| 98 |
+
return ranked.head(top_n)[["Kpi", "abs_diff"]], output
|
| 99 |
|
| 100 |
# ------------------------------------------------
|
| 101 |
+
# UI (HF Space)
|
| 102 |
# ------------------------------------------------
|
| 103 |
+
with gr.Blocks(title="Network Insight Summary") as demo:
|
| 104 |
+
gr.Markdown("## 📊 Network Insight Summary")
|
| 105 |
+
gr.Markdown(
|
| 106 |
+
"Upload KPI CSV to generate a concise, insight-style summary "
|
| 107 |
+
"focused on the most significant KPI changes."
|
| 108 |
+
)
|
| 109 |
|
| 110 |
csv_input = gr.File(file_types=[".csv"])
|
| 111 |
+
top_n_input = gr.Slider(3, 6, value=5, step=1, label="KPIs to Display")
|
| 112 |
|
| 113 |
+
btn = gr.Button("Generate Insight")
|
| 114 |
|
| 115 |
+
table = gr.Dataframe(label="Top KPI Changes")
|
| 116 |
+
summary = gr.Textbox(label="Insight Summary", lines=4)
|
| 117 |
|
| 118 |
btn.click(
|
| 119 |
analyze_kpi,
|