Dusit-P commited on
Commit
d741bd6
·
verified ·
1 Parent(s): 3d347ae

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -16
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
- s = _summarize_df(df)
108
- # Bar รวม
109
- fig_bar = go.Figure([go.Bar(x=["negative","positive"], y=[s["neg"], s["pos"]])])
110
- fig_bar.update_layout(title="Label counts", xaxis_title="label", yaxis_title="count")
111
- # Pie รวม
112
- fig_pie = go.Figure(go.Pie(labels=["negative","positive"], values=[s["neg"], s["pos"]], hole=0.35))
113
- fig_pie.update_layout(title="Label share")
114
- return fig_bar, fig_pie, s["md"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
115
 
116
  def _shop_summary(out_df: pd.DataFrame, max_shops=15):
117
- """สรุปต่อร้าน: table + stacked bar (pos/neg counts) — ถ้ามีคอลัมน์ shop"""
118
  if "shop" not in out_df.columns:
119
- return go.Figure(), pd.DataFrame(columns=["shop","total","positive","negative","positive_rate(%)","negative_rate(%)"])
 
 
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(barmode="stack", title=f"Per-shop counts (top {len(top)})",
139
- xaxis_title="shop", yaxis_title="count", legend_title="label")
 
 
 
 
 
 
 
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 ----------