EquilStable commited on
Commit
80af343
·
verified ·
1 Parent(s): fa5e396

Upload 4 files

Browse files
Files changed (4) hide show
  1. README.md +21 -6
  2. app.py +392 -0
  3. core.py +248 -0
  4. requirements.txt +3 -0
README.md CHANGED
@@ -1,15 +1,30 @@
1
  ---
2
  title: MarginGraph
3
- emoji: 💻
4
  colorFrom: green
5
- colorTo: yellow
6
  sdk: gradio
7
  sdk_version: 6.30.0
8
- python_version: '3.12'
9
  app_file: app.py
10
  pinned: false
11
- license: apache-2.0
12
- short_description: 'Margin-aware product recommendations: relevant add-ons that '
13
  ---
14
 
15
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
  title: MarginGraph
3
+ emoji: 🛒
4
  colorFrom: green
5
+ colorTo: blue
6
  sdk: gradio
7
  sdk_version: 6.30.0
8
+ python_version: "3.13"
9
  app_file: app.py
10
  pinned: false
11
+ license: mit
12
+ short_description: Checkout add-ons that sell and keep your margin
13
  ---
14
 
15
+ # MarginGraph demo
16
+
17
+ Profit-aware checkout add-on recommendations. Stage 1 (EASE co-purchase model)
18
+ finds what customers are likely to add; Stage 2 picks at most 2 add-ons that
19
+ maximize expected profit under hard rules (margin floor, in stock, overstock push).
20
+
21
+ * **Demo store:** 2,000 real products from UCI Online Retail II. Costs and stock are simulated.
22
+ * **Your store:** upload `products.csv` (item, description, price, unit_cost, stock, overstock)
23
+ and optionally `orders.csv` (order_id, item) to train on your own data in about 2 seconds.
24
+ * **Batch:** upload open baskets, download recommendations as CSV.
25
+ * **Backtest:** measured numbers on held-out Oct to Dec 2011 invoices.
26
+
27
+ Runs on the free CPU tier (about 250 to 350 MB RAM). Source:
28
+ https://github.com/Sanskar1724/MarginGraph (folder `space/`).
29
+
30
+ Rebuild the artifacts: `python space/build_artifacts.py --xlsx online_retail_II.xlsx`
app.py ADDED
@@ -0,0 +1,392 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """MarginGraph live demo (Gradio, CPU only, ~150 MB RAM).
2
+
3
+ Tab 1 Demo store: build a basket from 2,000 real Online Retail II products,
4
+ compare plain top-2 add-ons with MarginGraph's margin-safe pick.
5
+ Tab 2 Your store: upload your products (price, cost, stock) and optionally
6
+ your order history; get recommendations for one basket or a whole CSV.
7
+ Tab 3 Backtest: measured numbers on held-out Oct-Dec 2011 baskets.
8
+ """
9
+ from __future__ import annotations
10
+
11
+ import json
12
+ import tempfile
13
+ from pathlib import Path
14
+
15
+ import core
16
+ import gradio as gr
17
+ import numpy as np
18
+ import pandas as pd
19
+
20
+ ART = Path(__file__).resolve().parent / "artifacts"
21
+ META = json.loads((ART / "meta.json").read_text())
22
+ _npz = np.load(ART / "ease.npz")
23
+ DEMO = core.Model(
24
+ items=[str(s) for s in _npz["items"]],
25
+ B=_npz["B"].astype(np.float32),
26
+ calib=META["calibration"],
27
+ catalog=pd.read_csv(ART / "catalog.csv", dtype={"item": str}),
28
+ )
29
+ del _npz
30
+
31
+ PRODUCT_COLS = ["item", "description", "price", "unit_cost", "stock", "overstock"]
32
+ ALIASES = {
33
+ "item": ["item", "stock_code", "stockcode", "sku", "product_id", "product", "code", "id"],
34
+ "description": ["description", "name", "title", "product_name"],
35
+ "price": ["price", "unit_price", "sell_price", "retail_price"],
36
+ "unit_cost": ["unit_cost", "cost", "cogs", "buy_price", "purchase_price"],
37
+ "stock": ["stock", "qty", "quantity", "on_hand", "inventory", "stock_level"],
38
+ "overstock": ["overstock", "overstocked", "clearance", "excess"],
39
+ "basket": ["basket", "basket_id", "order", "order_id", "invoice", "invoice_no", "transaction_id", "cart_id"],
40
+ }
41
+ SIM_NOTE = ("Demo store costs and stock are **simulated** (Online Retail II publishes prices but not costs "
42
+ "or inventory). Upload your own numbers in *Your store* to use real ones.")
43
+
44
+
45
+ # ---------------------------------------------------------------- helpers
46
+
47
+ def label(model: core.Model, code: str) -> str:
48
+ i = model.pos.get(code)
49
+ return f"{code} · {model.catalog['description'].iat[i]}" if i is not None else code
50
+
51
+
52
+ def choices(model: core.Model) -> list[str]:
53
+ return [label(model, c) for c in model.items if model.catalog["stock"].iat[model.pos[c]] >= 0]
54
+
55
+
56
+ def codes_from(sel) -> list[str]:
57
+ if isinstance(sel, str):
58
+ sel = [s for s in sel.replace("\n", ",").split(",")]
59
+ return [s.split("·")[0].strip().upper() for s in (sel or []) if s and s.strip()]
60
+
61
+
62
+ def policy(floor, overstock_w, lam, discounts, k) -> core.Policy:
63
+ return core.Policy(lam=float(lam), overstock_weight=float(overstock_w), floor=float(floor),
64
+ discounts=(0.0, 0.10, 0.20) if discounts else (0.0,), max_addons=int(k))
65
+
66
+
67
+ def money(x: float) -> str:
68
+ return f"£{x:,.2f}"
69
+
70
+
71
+ def card(title: str, cand: pd.DataFrame, chosen, pol: core.Policy, accent: str) -> str:
72
+ st = core.offer_stats(cand, chosen, pol)
73
+ rows = []
74
+ for j, d in chosen:
75
+ r = cand.loc[j]
76
+ price = r.price * (1 - d)
77
+ m = (price - r.unit_cost) / price if price else 0
78
+ tags = []
79
+ if r.overstock:
80
+ tags.append('<span class="mg-tag mg-over">overstock</span>')
81
+ if d:
82
+ tags.append(f'<span class="mg-tag">-{int(d*100)}%</span>')
83
+ if m < pol.floor:
84
+ tags.append('<span class="mg-tag mg-bad">below margin floor</span>')
85
+ rows.append(
86
+ f'<div class="mg-item"><div class="mg-name">{r.description.title()} '
87
+ f'<span class="mg-code">{r["item"]}</span> {"".join(tags)}</div>'
88
+ f'<div class="mg-meta">{money(price)} · margin {m:.0%} · p(buy) {min(1, r.p_accept*(1+pol.elasticity*d)):.1%}'
89
+ f' · stock {int(r.stock)}</div>'
90
+ f'<div class="mg-why">bought with: {str(r.because_of).title()}</div></div>')
91
+ body = "".join(rows) or '<div class="mg-meta">No add-on passes the rules for this basket.</div>'
92
+ return (f'<div class="mg-card" style="border-top:4px solid {accent}"><div class="mg-h">{title}</div>{body}'
93
+ f'<div class="mg-foot">Expected profit <b>{money(st["exp_profit"])}</b> · expected margin '
94
+ f'{st["exp_margin"]:.0%} · chance of at least one sale ≈ {min(1, st["hit_prob"]):.0%}</div></div>')
95
+
96
+
97
+ def run(model: core.Model, codes: list[str], pol: core.Policy):
98
+ res = model.recommend(codes, pol)
99
+ if not res["known"]:
100
+ msg = "Add at least one product the model knows."
101
+ if res["unknown"]:
102
+ msg += f" Unknown codes: {', '.join(res['unknown'][:10])}"
103
+ return f'<div class="mg-note">{msg}</div>', "", "", None
104
+ cand = res["candidates"]
105
+ a = core.offer_stats(cand, res["top"], pol)
106
+ b = core.offer_stats(cand, res["optimized"], pol)
107
+ dp = (b["exp_profit"] / a["exp_profit"] - 1) * 100 if a["exp_profit"] > 0 else 0.0
108
+ dh = (min(1, b["hit_prob"]) - min(1, a["hit_prob"])) * 100
109
+ summary = (f'<div class="mg-sum"><div><span class="mg-big">{dp:+.0f}%</span><br>expected profit vs plain top-{pol.max_addons}</div>'
110
+ f'<div><span class="mg-big">{dh:+.1f} pts</span><br>chance of an add-on sale</div>'
111
+ f'<div><span class="mg-big">{a["floor_violations"]} → {b["floor_violations"]}</span><br>items below the {pol.floor:.0%} floor</div></div>')
112
+ if res["unknown"]:
113
+ summary += f'<div class="mg-note">Ignored codes not in the model: {", ".join(res["unknown"][:10])}</div>'
114
+ tbl = cand.assign(
115
+ p_accept=(cand.p_accept * 100).round(1), margin=(cand.margin * 100).round(0),
116
+ exp_profit=cand.exp_profit.round(3), price=cand.price.round(2), unit_cost=cand.unit_cost.round(2),
117
+ picked=["✓" if j in {x for x, _ in res["optimized"]} else "" for j in range(len(cand))],
118
+ ).rename(columns={"p_accept": "p(buy) %", "margin": "margin %", "exp_profit": "exp. profit £"})
119
+ return (summary, card(f"Plain top-{pol.max_addons} (relevance only)", cand, res["top"], pol, "#9aa0a6"),
120
+ card("MarginGraph pick", cand, res["optimized"], pol, "#0f9d58"), tbl)
121
+
122
+
123
+ # ---------------------------------------------------------------- merchant
124
+
125
+ def _rename(df: pd.DataFrame, wanted: list[str]) -> pd.DataFrame:
126
+ low = {c.lower().strip().replace(" ", "_"): c for c in df.columns}
127
+ m = {}
128
+ for w in wanted:
129
+ for a in ALIASES[w]:
130
+ if a in low:
131
+ m[low[a]] = w
132
+ break
133
+ return df.rename(columns=m)
134
+
135
+
136
+ def read_products(path) -> pd.DataFrame:
137
+ df = _rename(pd.read_csv(path, dtype=str), PRODUCT_COLS)
138
+ miss = [c for c in ["item", "price", "unit_cost", "stock"] if c not in df.columns]
139
+ if miss:
140
+ raise gr.Error(f"Products CSV is missing column(s): {', '.join(miss)}. Download the template.")
141
+ df["item"] = df["item"].astype(str).str.strip().str.upper()
142
+ if "description" not in df:
143
+ df["description"] = df["item"]
144
+ if "overstock" not in df:
145
+ df["overstock"] = "false"
146
+ df["overstock"] = df["overstock"].astype(str).str.strip().str.lower().isin(["1", "true", "yes", "y", "x"])
147
+ for c in ["price", "unit_cost", "stock"]:
148
+ df[c] = pd.to_numeric(df[c].astype(str).str.replace(r"[£$€,\s]", "", regex=True), errors="coerce")
149
+ bad = df[["price", "unit_cost", "stock"]].isna().any(axis=1).sum()
150
+ df = df.dropna(subset=["price", "unit_cost", "stock"])
151
+ df.attrs["dropped"] = int(bad)
152
+ return df[PRODUCT_COLS]
153
+
154
+
155
+ def read_baskets(path) -> list[list[str]]:
156
+ df = _rename(pd.read_csv(path, dtype=str), ["basket", "item"])
157
+ if not {"basket", "item"} <= set(df.columns):
158
+ raise gr.Error("Orders CSV needs two columns: order_id (or invoice/basket_id) and item (or sku/stock_code).")
159
+ return core.baskets_from_lines(df, "basket", "item")
160
+
161
+
162
+ def load_store(products_file, orders_file):
163
+ if products_file is None:
164
+ raise gr.Error("Upload a products CSV first (use the template).")
165
+ prods = read_products(products_file)
166
+ notes = [f"Loaded **{len(prods):,}** products"
167
+ + (f" ({prods.attrs['dropped']} rows skipped: missing price, cost or stock)." if prods.attrs["dropped"] else ".")]
168
+ if orders_file is not None:
169
+ baskets = read_baskets(orders_file)
170
+ try:
171
+ model = core.train_from_baskets(baskets, prods)
172
+ except ValueError as e:
173
+ raise gr.Error(str(e)) from e
174
+ h = model.calib["hit"]
175
+ notes.append(f"Trained on **{len(baskets):,}** of your orders ({len(model.items):,} products). "
176
+ f"Backtest on your most recent 20% of orders: the hidden item is in our top-2 "
177
+ f"**{h['hit@2']:.1%}** of the time, top-10 **{h['hit@10']:.1%}** "
178
+ f"({model.calib['n_events']:,} test baskets).")
179
+ else:
180
+ model = core.Model(DEMO.items, DEMO.B, DEMO.calib, prods)
181
+ n = int(model.catalog["price"].gt(0).sum())
182
+ notes.append(f"No order history uploaded, so the demo model (Online Retail II co-purchases) is used. "
183
+ f"**{n:,}** of your products match its item codes and can be recommended. "
184
+ f"Upload orders to train on your own data.")
185
+ if n == 0:
186
+ notes.append("⚠️ None of your codes match. Upload your order history so the model can learn your catalog.")
187
+ sold = model.catalog[model.catalog["price"] > 0]
188
+ return model, "\n\n".join(notes), gr.Dropdown(choices=[label(model, c) for c in sold["item"]], value=[])
189
+
190
+
191
+ def store_recommend(model, sel, floor, ow, lam, disc, k):
192
+ if model is None:
193
+ raise gr.Error("Load your store first.")
194
+ return run(model, codes_from(sel), policy(floor, ow, lam, disc, k))
195
+
196
+
197
+ def batch(model, baskets_file, floor, ow, lam, disc, k):
198
+ if model is None:
199
+ model = DEMO
200
+ if baskets_file is None:
201
+ raise gr.Error("Upload a CSV of open baskets (basket_id, item).")
202
+ df = _rename(pd.read_csv(baskets_file, dtype=str), ["basket", "item"])
203
+ if not {"basket", "item"} <= set(df.columns):
204
+ raise gr.Error("Baskets CSV needs columns basket_id and item.")
205
+ pol = policy(floor, ow, lam, disc, k)
206
+ out = []
207
+ for bid, g in df.groupby("basket", sort=False):
208
+ res = model.recommend(g["item"].tolist(), pol)
209
+ if not res["known"]:
210
+ out.append({"basket_id": bid, "slot": 0, "item": "", "description": "no known items"})
211
+ continue
212
+ cand = res["candidates"]
213
+ for slot, (j, d) in enumerate(res["optimized"], 1):
214
+ r = cand.loc[j]
215
+ price = r.price * (1 - d)
216
+ out.append({"basket_id": bid, "slot": slot, "item": r["item"], "description": r.description,
217
+ "price": round(price, 2), "discount": d, "margin": round((price - r.unit_cost) / price, 3),
218
+ "p_accept": round(r.p_accept * (1 + pol.elasticity * d), 4),
219
+ "expected_profit": round(r.p_accept * (1 + pol.elasticity * d) * (price - r.unit_cost), 4),
220
+ "overstock": bool(r.overstock), "because_of": r.because_of})
221
+ res = pd.DataFrame(out)
222
+ path = Path(tempfile.mkdtemp()) / "margingraph_recommendations.csv"
223
+ res.to_csv(path, index=False)
224
+ return res.head(50), str(path)
225
+
226
+
227
+ def template() -> str:
228
+ path = Path(tempfile.mkdtemp()) / "products_template.csv"
229
+ DEMO.catalog[PRODUCT_COLS].head(300).to_csv(path, index=False)
230
+ return str(path)
231
+
232
+
233
+ def orders_template() -> str:
234
+ path = Path(tempfile.mkdtemp()) / "orders_template.csv"
235
+ rows = [(f"ORD{i:05d}", c) for i, b in enumerate(META["examples"]) for c in b]
236
+ pd.DataFrame(rows, columns=["order_id", "item"]).to_csv(path, index=False)
237
+ return str(path)
238
+
239
+
240
+ # ---------------------------------------------------------------- backtest tab
241
+
242
+ def backtest_md() -> str:
243
+ bt, tr = META["backtest"], META["test_ranking"]
244
+ names = {"top2_relevance": "Plain top-2 (relevance only)",
245
+ "margingraph_lam0.5": "MarginGraph, balance 0.5",
246
+ "margingraph_lam1.0": "MarginGraph, balance 1.0 (profit)",
247
+ "margingraph_lam1.0_overstock2x": "MarginGraph, balance 1.0, overstock 2x"}
248
+ lines = ["| Strategy | Hit@2 | Realized profit / basket | vs top-2 (95% CI, £/basket) | Below 15% floor | Overstock share |",
249
+ "|---|---|---|---|---|---|"]
250
+ for k, v in bt.items():
251
+ ci = v["realized_profit_diff_ci95"]
252
+ lines.append(f"| {names.get(k, k)} | {v['hit@2']:.1%} | £{v['realized_profit_per_basket']:.4f} | "
253
+ f"{v['realized_profit_vs_top2_pct']:+.1f}% ([{ci[0]:+.4f}, {ci[1]:+.4f}]) | "
254
+ f"{v['margin_floor_violations_per_basket']:.3f} | {v['overstock_share_of_offers']:.1%} |")
255
+ return f"""
256
+ ### Measured on real held-out baskets (not targets)
257
+
258
+ **Protocol.** Model fit on Dec 2009 to Sep 2011, tested on {META['n_test_events']:,} invoices from Oct to Dec 2011.
259
+ In each test invoice one product is hidden; a strategy *hits* if one of its 2 add-ons is that product.
260
+ *Realized profit* only counts the hidden product, so it is grounded in what customers really bought.
261
+
262
+ **Stage 1 (EASE, {META['n_items']:,} products, {META['test_line_coverage']:.0%} of test lines covered):**
263
+ Hit@2 {tr['hit']['hit@2']:.1%} · Hit@5 {tr['hit']['hit@5']:.1%} · Hit@10 {tr['hit']['hit@10']:.1%}.
264
+ Picking at random from {META['n_items']:,} products would hit about {2/META['n_items']:.2%} at 2.
265
+
266
+ **Stage 2:**
267
+
268
+ {chr(10).join(lines)}
269
+
270
+ Costs and stock in this table are **simulated** (Beta(4,6) margins, mean 40%; 15% of items overstocked),
271
+ so profit numbers show how the optimizer behaves, not what a real store would earn.
272
+ The full research pipeline (LightGBM reranker, Hit@10 38.1%) lives in the main repo.
273
+ """
274
+
275
+
276
+ # ---------------------------------------------------------------- UI
277
+
278
+ CSS = """
279
+ .mg-card{background:var(--block-background-fill);border:1px solid var(--border-color-primary);border-radius:10px;padding:14px;min-height:210px}
280
+ .mg-h{font-weight:700;font-size:1.05em;margin-bottom:8px}
281
+ .mg-item{padding:8px 0;border-bottom:1px dashed var(--border-color-primary)}
282
+ .mg-name{font-weight:600}.mg-code{opacity:.6;font-size:.85em;font-weight:400}
283
+ .mg-meta,.mg-why{font-size:.9em;opacity:.85}.mg-why{font-style:italic}
284
+ .mg-foot{margin-top:10px;font-size:.95em}
285
+ .mg-tag{display:inline-block;font-size:.75em;padding:1px 6px;border-radius:8px;background:#e8eaed;color:#333;margin-left:4px}
286
+ .mg-over{background:#fde293}.mg-bad{background:#f6aea9}
287
+ .mg-sum{display:flex;gap:16px;flex-wrap:wrap;margin:6px 0}.mg-sum>div{flex:1;min-width:150px;text-align:center;padding:8px;border-radius:10px;background:var(--block-background-fill);border:1px solid var(--border-color-primary)}
288
+ .mg-big{font-size:1.8em;font-weight:700}
289
+ .mg-note{padding:8px;border-radius:8px;background:#fef7e0;color:#5f4b00;margin-top:6px}
290
+ """
291
+
292
+ DEMO_CHOICES = choices(DEMO)
293
+
294
+
295
+ def controls():
296
+ with gr.Row():
297
+ floor = gr.Slider(0, 0.5, 0.15, step=0.01, label="Margin floor (min margin per add-on)")
298
+ ow = gr.Slider(1.0, 2.0, 1.0, step=0.05, label="Overstock weight (push excess stock)")
299
+ lam = gr.Slider(0, 1, 1.0, step=0.05, label="Balance: 0 = most likely to sell, 1 = most profit")
300
+ with gr.Row():
301
+ disc = gr.Checkbox(False, label="Allow 10% / 20% discounts (assumed demand response)")
302
+ k = gr.Radio([1, 2], value=2, label="Add-ons to show")
303
+ return floor, ow, lam, disc, k
304
+
305
+
306
+ with gr.Blocks(title="MarginGraph: profit-aware add-on recommendations") as demo:
307
+ gr.Markdown("""# MarginGraph
308
+ **Checkout add-ons that customers want *and* that keep your margin.** Stage 1 learns what is bought together
309
+ from real orders. Stage 2 picks the best 2 add-ons under hard business rules: a minimum margin,
310
+ in-stock only, and an optional push for overstocked items.""")
311
+
312
+ with gr.Tab("Demo store"):
313
+ gr.Markdown(SIM_NOTE)
314
+ with gr.Row():
315
+ basket = gr.Dropdown(DEMO_CHOICES, value=[label(DEMO, c) for c in META["examples"][0]],
316
+ multiselect=True, label="Basket (type to search 2,000 products)", scale=4)
317
+ ex_btn = gr.Button("🎲 Real basket", scale=1)
318
+ floor, ow, lam, disc, k = controls()
319
+ go = gr.Button("Recommend add-ons", variant="primary")
320
+ summary = gr.HTML()
321
+ with gr.Row():
322
+ left, right = gr.HTML(), gr.HTML()
323
+ with gr.Accordion("All 20 candidates the optimizer looked at", open=False):
324
+ table = gr.Dataframe(wrap=True)
325
+ ins = [basket, floor, ow, lam, disc, k]
326
+ outs = [summary, left, right, table]
327
+ fn = lambda sel, *a: run(DEMO, codes_from(sel), policy(*a)) # noqa: E731
328
+ go.click(fn, ins, outs)
329
+ for c in [floor, ow, lam, disc, k]:
330
+ c.release(fn, ins, outs) if isinstance(c, gr.Slider) else c.change(fn, ins, outs)
331
+
332
+ def random_basket():
333
+ b = META["examples"][np.random.randint(len(META["examples"]))]
334
+ return [label(DEMO, c) for c in b]
335
+ ex_btn.click(random_basket, None, basket).then(fn, ins, outs)
336
+ demo.load(fn, ins, outs)
337
+
338
+ with gr.Tab("Your store"):
339
+ gr.Markdown("""**Use your own catalog.** 1) Upload products with `item, description, price, unit_cost, stock`
340
+ (optional `overstock` = yes/no). 2) Optionally upload order history (`order_id, item`, one row per item, oldest first)
341
+ so the model learns *your* customers; without it the demo model is used for matching item codes.
342
+ Files stay in this session only and are not stored.""")
343
+ store = gr.State(None)
344
+ with gr.Row():
345
+ with gr.Column():
346
+ pf = gr.File(label="Products CSV", file_types=[".csv"], elem_id="products-file")
347
+ gr.DownloadButton("Products template", value=template)
348
+ with gr.Column():
349
+ of = gr.File(label="Order history CSV (optional)", file_types=[".csv"], elem_id="orders-file")
350
+ gr.DownloadButton("Orders template", value=orders_template)
351
+ load_btn = gr.Button("Load my store", variant="primary")
352
+ status = gr.Markdown()
353
+ s_basket = gr.Dropdown([], multiselect=True, label="Basket from your catalog")
354
+ sfloor, sow, slam, sdisc, sk = controls()
355
+ s_go = gr.Button("Recommend", variant="primary")
356
+ s_sum = gr.HTML()
357
+ with gr.Row():
358
+ s_left, s_right = gr.HTML(), gr.HTML()
359
+ with gr.Accordion("Candidates", open=False):
360
+ s_tbl = gr.Dataframe(wrap=True)
361
+ load_btn.click(load_store, [pf, of], [store, status, s_basket])
362
+ s_go.click(store_recommend, [store, s_basket, sfloor, sow, slam, sdisc, sk], [s_sum, s_left, s_right, s_tbl])
363
+
364
+ gr.Markdown("### Batch: recommendations for many open baskets\nUpload `basket_id, item` rows; "
365
+ "uses your store if loaded, otherwise the demo store.")
366
+ bf = gr.File(label="Open baskets CSV", file_types=[".csv"], elem_id="baskets-file")
367
+ b_go = gr.Button("Run batch")
368
+ b_tbl = gr.Dataframe(label="First 50 rows")
369
+ b_dl = gr.File(label="Download all recommendations")
370
+ b_go.click(batch, [store, bf, sfloor, sow, slam, sdisc, sk], [b_tbl, b_dl])
371
+
372
+ with gr.Tab("Backtest"):
373
+ gr.Markdown(backtest_md())
374
+
375
+ with gr.Tab("How it works / API"):
376
+ gr.Markdown("""
377
+ **Pipeline.** Basket → EASE co-purchase model scores every product → top 20 candidates →
378
+ p(buy) calibrated on held-out baskets → exact optimizer picks ≤ 2 add-ons that maximize
379
+ `p(buy) × ((1 − balance) × avg p + balance × overstock_weight × unit profit)`
380
+ subject to margin ≥ floor, stock > 0, one discount per item. Rules are enforced in code; nothing is guessed by an LLM.
381
+
382
+ **Call it from code** (every button is also an API endpoint):
383
+ ```python
384
+ from gradio_client import Client
385
+ c = Client("EquilStable/MarginGraph")
386
+ print(c.view_api())
387
+ ```
388
+ Source and research pipeline: [github.com/Sanskar1724/MarginGraph](https://github.com/Sanskar1724/MarginGraph)
389
+ """)
390
+
391
+ if __name__ == "__main__":
392
+ demo.launch(css=CSS, theme=gr.themes.Soft())
core.py ADDED
@@ -0,0 +1,248 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """MarginGraph demo core: basket -> relevant add-ons -> margin-safe pick of 2.
2
+
3
+ Self-contained (numpy + pandas only) so it runs on a free CPU Space.
4
+
5
+ Stage 1 EASE item-item model (Steck 2019). On Online Retail II it is the
6
+ strongest single baseline in the main repo (test Hit@10 31.1%); the
7
+ repo's LightGBM reranker beats it (38.1%) but needs the full feature
8
+ pipeline, so the demo ships EASE only.
9
+ Stage 1b Logistic calibration of the EASE score into p(accept), fitted on
10
+ held-out baskets (leave-one-out: hide one item, can we find it?).
11
+ Stage 2 Exact solver for <= 2 add-ons: enumerate every feasible single and
12
+ pair, maximize sum p * ((1 - lam) * p_bar + lam * w * profit)
13
+ subject to margin floor, stock > 0, one discount per item.
14
+ Same objective and constraints as margingraph.optimize.solve_enum.
15
+ """
16
+ from __future__ import annotations
17
+
18
+ from dataclasses import dataclass, field
19
+
20
+ import numpy as np
21
+ import pandas as pd
22
+
23
+ # --------------------------------------------------------------------------
24
+ # Stage 1: EASE
25
+ # --------------------------------------------------------------------------
26
+
27
+ def fit_ease(baskets: list[list[int]], n_items: int, lam: float = 250.0) -> np.ndarray:
28
+ """Closed-form EASE weights B (n_items x n_items, zero diagonal)."""
29
+ rows = np.concatenate([np.full(len(b), i) for i, b in enumerate(baskets)])
30
+ cols = np.concatenate([np.asarray(b, dtype=int) for b in baskets])
31
+ X = np.zeros((len(baskets), n_items), dtype=np.float32)
32
+ X[rows, cols] = 1.0
33
+ G = (X.T @ X).astype(np.float64)
34
+ G[np.diag_indices(n_items)] += lam
35
+ P = np.linalg.inv(G)
36
+ B = -P / np.diag(P)[None, :]
37
+ B[np.diag_indices(n_items)] = 0.0
38
+ return B.astype(np.float32)
39
+
40
+
41
+ def scores_for(B: np.ndarray, basket: list[int]) -> np.ndarray:
42
+ s = B[basket].sum(axis=0).astype(np.float64)
43
+ s[basket] = -np.inf
44
+ return s
45
+
46
+
47
+ # --------------------------------------------------------------------------
48
+ # Stage 1b: calibration p(accept) = sigmoid(a * score + b * log1p(rank) + c)
49
+ # --------------------------------------------------------------------------
50
+
51
+ def _feat(score: np.ndarray, rank: np.ndarray) -> np.ndarray:
52
+ return np.column_stack([score, np.log1p(rank), np.ones_like(score)])
53
+
54
+
55
+ def fit_logistic(X: np.ndarray, y: np.ndarray, l2: float = 1e-3, iters: int = 50) -> np.ndarray:
56
+ w = np.zeros(X.shape[1])
57
+ for _ in range(iters): # Newton steps
58
+ p = 1 / (1 + np.exp(-np.clip(X @ w, -30, 30)))
59
+ g = X.T @ (p - y) + l2 * w
60
+ H = (X * (p * (1 - p))[:, None]).T @ X + l2 * np.eye(X.shape[1])
61
+ step = np.linalg.solve(H, g)
62
+ w -= step
63
+ if np.abs(step).max() < 1e-8:
64
+ break
65
+ return w
66
+
67
+
68
+ def loo_events(baskets: list[list[int]], rng: np.random.Generator, max_events: int = 4000):
69
+ """Leave-one-out events from held-out baskets: (context, hidden item)."""
70
+ ev = []
71
+ for b in baskets:
72
+ if len(b) < 2 or len(b) > 50:
73
+ continue
74
+ h = int(rng.integers(len(b)))
75
+ ev.append(([x for k, x in enumerate(b) if k != h], b[h]))
76
+ rng.shuffle(ev)
77
+ return ev[:max_events]
78
+
79
+
80
+ def calibrate(B: np.ndarray, events, top_n: int = 20) -> dict:
81
+ X, y, hits = [], [], {2: 0, 5: 0, 10: 0}
82
+ for ctx, hid in events:
83
+ s = scores_for(B, ctx)
84
+ top = np.argsort(-s)[:top_n]
85
+ top = top[np.isfinite(s[top])] # small catalogs: fewer than top_n candidates
86
+ X.append(_feat(s[top], np.arange(len(top))))
87
+ y.append((top == hid).astype(float))
88
+ for k in hits:
89
+ hits[k] += int(hid in top[:k])
90
+ X, y = np.vstack(X), np.concatenate(y)
91
+ w = fit_logistic(X, y, l2=1.0)
92
+ p = 1 / (1 + np.exp(-(X @ w)))
93
+ n = max(len(events), 1)
94
+ return {
95
+ "w": w.tolist(),
96
+ "p_bar": float(p.mean()),
97
+ "n_events": len(events),
98
+ "hit": {f"hit@{k}": hits[k] / n for k in hits},
99
+ "recall_top_n": float(y.sum() / n),
100
+ "top_n": top_n,
101
+ }
102
+
103
+
104
+ def proba(w, score: np.ndarray, rank: np.ndarray) -> np.ndarray:
105
+ return 1 / (1 + np.exp(-(_feat(score, rank) @ np.asarray(w))))
106
+
107
+
108
+ # --------------------------------------------------------------------------
109
+ # Stage 2: margin-aware selection (exact for max_addons <= 2)
110
+ # --------------------------------------------------------------------------
111
+
112
+ @dataclass
113
+ class Policy:
114
+ lam: float = 1.0 # 0 = pure relevance, 1 = profit-weighted
115
+ overstock_weight: float = 1.0 # 1.0-2.0x boost for overstocked items
116
+ floor: float = 0.15 # minimum post-discount margin per item
117
+ discounts: tuple = (0.0,)
118
+ elasticity: float = 1.5 # ASSUMED: p(accept|d) = p * (1 + e * d)
119
+ max_addons: int = 2
120
+
121
+
122
+ def solve(p, price, cost, overstock, stock, pol: Policy, p_bar: float) -> list[tuple[int, float]]:
123
+ """Return [(candidate index, discount), ...] maximizing the objective."""
124
+ n = len(p)
125
+ if n == 0:
126
+ return []
127
+ d = np.asarray(pol.discounts, dtype=float)
128
+ rev = price[:, None] * (1 - d[None, :])
129
+ profit = rev - cost[:, None]
130
+ pa = np.minimum(1.0, p[:, None] * (1 + pol.elasticity * d[None, :]))
131
+ w = np.where(overstock, pol.overstock_weight, 1.0)[:, None]
132
+ value = (pa * ((1 - pol.lam) * p_bar + pol.lam * w * profit)).ravel()
133
+ ok = (np.repeat((stock > 0)[:, None], len(d), axis=1) & (profit >= pol.floor * rev - 1e-9)).ravel()
134
+ item, disc = np.repeat(np.arange(n), len(d)), np.tile(d, n)
135
+ idx = np.flatnonzero(ok & (value > 0))
136
+ if len(idx) == 0:
137
+ return []
138
+ v, it = value[idx], item[idx]
139
+ best_val, best = v.max(), [int(np.argmax(v))]
140
+ if pol.max_addons >= 2 and len(idx) >= 2:
141
+ V = v[:, None] + v[None, :]
142
+ feas = (it[:, None] != it[None, :]) & np.triu(np.ones((len(idx),) * 2, bool), 1)
143
+ V = np.where(feas, V, -np.inf)
144
+ a, b = np.unravel_index(np.argmax(V), V.shape)
145
+ if V[a, b] > best_val + 1e-12:
146
+ best = [int(a), int(b)]
147
+ return [(int(it[j]), float(disc[idx][j])) for j in best]
148
+
149
+
150
+ # --------------------------------------------------------------------------
151
+ # Model bundle used by the app
152
+ # --------------------------------------------------------------------------
153
+
154
+ @dataclass
155
+ class Model:
156
+ items: list[str] # stock codes, index = model column
157
+ B: np.ndarray
158
+ calib: dict
159
+ catalog: pd.DataFrame # item, description, price, unit_cost, stock, overstock
160
+ pos: dict = field(default_factory=dict)
161
+
162
+ def __post_init__(self):
163
+ self.pos = {s: i for i, s in enumerate(self.items)}
164
+ c = self.catalog.drop_duplicates("item").set_index("item").reindex(self.items)
165
+ c.index.name = "item"
166
+ c["description"] = c["description"].fillna("").astype(str)
167
+ c.loc[c["description"] == "", "description"] = c.index[c["description"] == ""]
168
+ c["price"] = pd.to_numeric(c["price"], errors="coerce").fillna(0.0)
169
+ c["unit_cost"] = pd.to_numeric(c["unit_cost"], errors="coerce").fillna(c["price"])
170
+ # Items without a price or stock count are never offered.
171
+ c["stock"] = pd.to_numeric(c["stock"], errors="coerce").fillna(0).clip(lower=0)
172
+ c.loc[c["price"] <= 0, "stock"] = 0
173
+ c["overstock"] = c["overstock"].fillna(False).astype(bool)
174
+ self.catalog = c.reset_index()
175
+
176
+ def recommend(self, basket_codes: list[str], pol: Policy, top_n: int = 20) -> dict:
177
+ codes = [str(c).strip().upper() for c in basket_codes]
178
+ known = [self.pos[c] for c in dict.fromkeys(codes) if c in self.pos]
179
+ unknown = [c for c in codes if c not in self.pos]
180
+ if not known:
181
+ return {"known": [], "unknown": unknown, "candidates": pd.DataFrame(), "optimized": [], "top": []}
182
+ s = scores_for(self.B, known)
183
+ top = np.argsort(-s)[:top_n]
184
+ top = top[np.isfinite(s[top])]
185
+ p = proba(self.calib["w"], s[top], np.arange(len(top)))
186
+ c = self.catalog.iloc[top].reset_index(drop=True)
187
+ price, cost = c["price"].to_numpy(float), c["unit_cost"].to_numpy(float)
188
+ stock, over = c["stock"].to_numpy(float), c["overstock"].to_numpy(bool)
189
+ # Why: the basket item with the largest EASE weight toward each candidate.
190
+ contrib = self.B[np.ix_(known, top)]
191
+ because = [self.catalog["description"].iat[known[k]] for k in contrib.argmax(axis=0)]
192
+ cand = pd.DataFrame({
193
+ "rank": np.arange(1, len(top) + 1),
194
+ "item": c["item"], "description": c["description"],
195
+ "p_accept": p, "price": price, "unit_cost": cost,
196
+ "margin": np.divide(price - cost, price, out=np.zeros_like(price), where=price > 0), "stock": stock.astype(int), "overstock": over,
197
+ "exp_profit": p * (price - cost), "because_of": because,
198
+ })
199
+ in_stock = [j for j in range(len(top)) if stock[j] > 0]
200
+ topk = [(j, 0.0) for j in in_stock[: pol.max_addons]]
201
+ opt = solve(p, price, cost, over, stock, pol, self.calib["p_bar"])
202
+ return {"known": [self.items[i] for i in known], "unknown": unknown,
203
+ "candidates": cand, "optimized": opt, "top": topk}
204
+
205
+
206
+ def offer_stats(cand: pd.DataFrame, chosen, pol: Policy) -> dict:
207
+ """Expected profit / revenue / margin of an offer, plus floor violations."""
208
+ ep = er = 0.0
209
+ viol = 0
210
+ for j, d in chosen:
211
+ r = cand.at[j, "price"] * (1 - d)
212
+ pr = r - cand.at[j, "unit_cost"]
213
+ pa = min(1.0, cand.at[j, "p_accept"] * (1 + pol.elasticity * d))
214
+ ep += pa * pr
215
+ er += pa * r
216
+ viol += int(pr < pol.floor * r - 1e-9)
217
+ return {"exp_profit": ep, "exp_revenue": er, "exp_margin": ep / er if er else 0.0,
218
+ "hit_prob": sum(min(1.0, cand.at[j, "p_accept"] * (1 + pol.elasticity * d)) for j, d in chosen),
219
+ "floor_violations": viol}
220
+
221
+
222
+ def baskets_from_lines(df: pd.DataFrame, basket_col: str, item_col: str) -> list[list[str]]:
223
+ df = df[[basket_col, item_col]].dropna().astype(str)
224
+ df[item_col] = df[item_col].str.strip().str.upper()
225
+ return df.groupby(basket_col, sort=False)[item_col].apply(lambda s: list(dict.fromkeys(s))).tolist()
226
+
227
+
228
+ def train_from_baskets(baskets: list[list[str]], catalog: pd.DataFrame, max_items: int = 2500,
229
+ lam: float = 250.0, seed: int = 42, holdout: float = 0.2) -> Model:
230
+ """Fit EASE + calibration on a merchant's own order history.
231
+
232
+ The last `holdout` share of baskets (file order, so sort by date first)
233
+ calibrates p(accept) and gives an honest backtest; then EASE is refit on
234
+ all baskets for serving.
235
+ """
236
+ counts = pd.Series([i for b in baskets for i in b]).value_counts()
237
+ items = counts.index[:max_items].tolist()
238
+ pos = {s: i for i, s in enumerate(items)}
239
+ enc = [[pos[i] for i in b if i in pos] for b in baskets]
240
+ enc = [b for b in enc if len(b) >= 1]
241
+ cut = int(len(enc) * (1 - holdout))
242
+ train, held = enc[:cut], enc[cut:]
243
+ if len(train) < 20 or len(held) < 5:
244
+ raise ValueError("need at least ~30 orders with 2+ items to train")
245
+ B = fit_ease(train, len(items), lam)
246
+ calib = calibrate(B, loo_events(held, np.random.default_rng(seed)))
247
+ B = fit_ease(enc, len(items), lam)
248
+ return Model(items, B, calib, catalog)
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ gradio==6.30.0
2
+ pandas>=2.0
3
+ numpy>=1.26