Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -104,21 +104,55 @@ def _summarize_df(df: pd.DataFrame):
|
|
| 104 |
return {"total": total, "neg": neg, "pos": pos, "neg_avg": neg_avg, "pos_avg": pos_avg, "md": info}
|
| 105 |
|
| 106 |
def _make_figures(df: pd.DataFrame):
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
def _shop_summary(out_df: pd.DataFrame, max_shops=15):
|
| 117 |
-
"""สรุปต่อร้าน:
|
| 118 |
if "shop" not in out_df.columns:
|
| 119 |
-
|
|
|
|
|
|
|
| 120 |
g = out_df.groupby("shop")["label"].value_counts().unstack(fill_value=0)
|
| 121 |
-
# ให้มีทั้งสองคอลัมน์เสมอ
|
| 122 |
for col in ["positive","negative"]:
|
| 123 |
if col not in g.columns:
|
| 124 |
g[col] = 0
|
|
@@ -130,13 +164,20 @@ def _shop_summary(out_df: pd.DataFrame, max_shops=15):
|
|
| 130 |
table["negative_rate(%)"] = (table["negative"] / table["total"] * 100).round(2)
|
| 131 |
table = table.reset_index().rename(columns={"index":"shop"})
|
| 132 |
|
| 133 |
-
# กราฟโชว์ top N
|
| 134 |
top = table.head(max_shops)
|
| 135 |
fig = go.Figure()
|
| 136 |
-
fig.add_bar(name="positive", x=top["shop"], y=top["positive"])
|
| 137 |
-
fig.add_bar(name="negative", x=top["shop"], y=top["negative"])
|
| 138 |
-
fig.update_layout(
|
| 139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
return fig, table
|
| 141 |
|
| 142 |
# ---------- API wrappers ----------
|
|
|
|
| 104 |
return {"total": total, "neg": neg, "pos": pos, "neg_avg": neg_avg, "pos_avg": pos_avg, "md": info}
|
| 105 |
|
| 106 |
def _make_figures(df: pd.DataFrame):
|
| 107 |
+
# สรุปเบื้องต้น
|
| 108 |
+
total = len(df)
|
| 109 |
+
neg = int((df["label"] == "negative").sum())
|
| 110 |
+
pos = int((df["label"] == "positive").sum())
|
| 111 |
+
neg_avg = pd.to_numeric(df["negative(%)"].str.rstrip("%"), errors="coerce").mean()
|
| 112 |
+
pos_avg = pd.to_numeric(df["positive(%)"].str.rstrip("%"), errors="coerce").mean()
|
| 113 |
+
info = (
|
| 114 |
+
f"**Summary** \n"
|
| 115 |
+
f"- Total: {total} \n"
|
| 116 |
+
f"- Negative: {neg} \n"
|
| 117 |
+
f"- Positive: {pos} \n"
|
| 118 |
+
f"- Avg negative: {neg_avg:.2f}% \n"
|
| 119 |
+
f"- Avg positive: {pos_avg:.2f}%"
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
# --- BAR: 2 trace, สีคงที่ ---
|
| 123 |
+
fig_bar = go.Figure()
|
| 124 |
+
fig_bar.add_bar(name="negative", x=["negative"], y=[neg], marker_color=NEG_COLOR)
|
| 125 |
+
fig_bar.add_bar(name="positive", x=["positive"], y=[pos], marker_color=POS_COLOR)
|
| 126 |
+
fig_bar.update_layout(
|
| 127 |
+
barmode="group",
|
| 128 |
+
title="Label counts",
|
| 129 |
+
xaxis_title="label",
|
| 130 |
+
yaxis_title="count",
|
| 131 |
+
template=TEMPLATE,
|
| 132 |
+
legend_title="label",
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
# --- PIE: สีสอดคล้องกับ bar ---
|
| 136 |
+
fig_pie = go.Figure(
|
| 137 |
+
go.Pie(
|
| 138 |
+
labels=["negative", "positive"],
|
| 139 |
+
values=[neg, pos],
|
| 140 |
+
hole=0.35,
|
| 141 |
+
sort=False,
|
| 142 |
+
marker=dict(colors=[NEG_COLOR, POS_COLOR]),
|
| 143 |
+
)
|
| 144 |
+
)
|
| 145 |
+
fig_pie.update_layout(title="Label share", template=TEMPLATE)
|
| 146 |
+
|
| 147 |
+
return fig_bar, fig_pie, info
|
| 148 |
|
| 149 |
def _shop_summary(out_df: pd.DataFrame, max_shops=15):
|
| 150 |
+
"""สรุปต่อร้าน: ตาราง + stacked bar (pos/neg) — ใช้สีคงที่"""
|
| 151 |
if "shop" not in out_df.columns:
|
| 152 |
+
empty_tbl = pd.DataFrame(columns=["shop","total","positive","negative","positive_rate(%)","negative_rate(%)"])
|
| 153 |
+
return go.Figure(), empty_tbl
|
| 154 |
+
|
| 155 |
g = out_df.groupby("shop")["label"].value_counts().unstack(fill_value=0)
|
|
|
|
| 156 |
for col in ["positive","negative"]:
|
| 157 |
if col not in g.columns:
|
| 158 |
g[col] = 0
|
|
|
|
| 164 |
table["negative_rate(%)"] = (table["negative"] / table["total"] * 100).round(2)
|
| 165 |
table = table.reset_index().rename(columns={"index":"shop"})
|
| 166 |
|
| 167 |
+
# กราฟโชว์ top N ร้าน
|
| 168 |
top = table.head(max_shops)
|
| 169 |
fig = go.Figure()
|
| 170 |
+
fig.add_bar(name="positive", x=top["shop"], y=top["positive"], marker_color=POS_COLOR)
|
| 171 |
+
fig.add_bar(name="negative", x=top["shop"], y=top["negative"], marker_color=NEG_COLOR)
|
| 172 |
+
fig.update_layout(
|
| 173 |
+
barmode="stack",
|
| 174 |
+
title=f"Per-shop counts (top {len(top)})",
|
| 175 |
+
xaxis_title="shop",
|
| 176 |
+
yaxis_title="count",
|
| 177 |
+
legend_title="label",
|
| 178 |
+
template=TEMPLATE,
|
| 179 |
+
xaxis=dict(tickangle=-30),
|
| 180 |
+
)
|
| 181 |
return fig, table
|
| 182 |
|
| 183 |
# ---------- API wrappers ----------
|