OneStarDao commited on
Commit
994e7d4
Β·
verified Β·
1 Parent(s): e12152f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -66
app.py CHANGED
@@ -1,3 +1,9 @@
 
 
 
 
 
 
1
  import io, numpy as np, matplotlib
2
  matplotlib.use("Agg")
3
 
@@ -8,77 +14,84 @@ from transformers import AutoTokenizer, AutoModelForCausalLM
8
  from wfgy_sdk import get_engine
9
  from wfgy_sdk.evaluator import compare_logits, plot_histogram
10
 
11
- # tiny model (CPU-friendly demo)
12
- tok = AutoTokenizer.from_pretrained("sshleifer/tiny-gpt2")
13
- mdl = AutoModelForCausalLM.from_pretrained("sshleifer/tiny-gpt2")
14
- eng = get_engine()
15
 
16
- # history buffer
17
- history = {"step": [0], "var": [0.0], "kl": [0.0]}
18
-
19
- # paper table
20
- paper_df = pd.DataFrame({
21
  "Benchmark": ["MMLU","GSM8K","BBH","MathBench","TruthfulQA",
22
  "XNLI","MLQA","LongBench","VQAv2","OK-VQA"],
23
  "Baseline": [61.0,78.0,79.3,72.2,62.4,59.5,78.1,51.4,69.1,65.7],
24
  "WFGY": [89.8,98.7,100.7,87.4,90.4,77.3,106.6,69.6,86.6,86.8]
25
  })
26
- paper_df["Abs_gain"] = (paper_df["WFGY"]-paper_df["Baseline"]).round(1)
27
- paper_df["Rel_gain%"] = ((paper_df["Abs_gain"]/paper_df["Baseline"])*100).round(0)
28
-
29
- styled_df = (
30
- paper_df.style
31
  .background_gradient(cmap="Greens", subset=["Abs_gain","Rel_gain%"])
32
  .format({"Abs_gain":"{:.1f}","Rel_gain%":"{:.0f}"})
33
  )
34
 
35
- paper_bar = px.bar(
36
- paper_df, x="Benchmark", y="Rel_gain%",
37
- title="Relative gain (%)", color="Rel_gain%",
38
- color_continuous_scale="Greens", height=300
39
- )
40
-
41
- # helpers
42
- def top5(logits: np.ndarray):
43
- p = torch.softmax(torch.tensor(logits), dim=0).numpy()
44
- idx = p.argsort()[-5:][::-1]
45
- return "\n".join([f"{tok.decode(int(i))!r}: {p[i]:.2e}" for i in idx])
46
-
47
- def hist_plot():
48
- df = pd.DataFrame(history)
49
- return px.line(df, x="step", y=["var","kl"],
50
- labels={"value":"metric","step":"call"},
51
- title="history (var% ↓ & KL)").update_layout(height=260)
52
-
53
- def clear_hist():
54
- history["step"][:] = [0]; history["var"][:]=[0.0]; history["kl"][:]=[0.0]
55
- return hist_plot()
56
-
57
- def run(prompt: str):
58
- p = prompt.strip()
59
- if not p:
60
- return "", "", "", "", None, hist_plot()
61
-
62
- ids = tok(p, return_tensors="pt").input_ids
63
- rawL = mdl(ids).logits[0,-1].detach().cpu().numpy()
64
- G = np.random.randn(256).astype(np.float32)
65
- I = G + np.random.normal(scale=0.05,size=256).astype(np.float32)
66
- modL = eng.run(I,G,rawL)
67
-
68
- m = compare_logits(rawL,modL)
69
- n = len(history["step"])
70
- history["step"].append(n); history["var"].append(m["var_drop"]*100); history["kl"].append(m["kl"])
71
-
72
- fig = plot_histogram(rawL,modL); buf=io.BytesIO(); fig.savefig(buf,format="png"); buf.seek(0)
 
 
 
 
 
 
 
 
 
 
 
 
 
73
 
74
- head = f"β–Ό var {m['var_drop']*100:4.1f}% | KL {m['kl']:.3f} | top-1 {'kept' if m['top1'] else 'changed'}"
75
- return top5(rawL), top5(modL), head, Image.open(buf), hist_plot()
 
76
 
77
- # UI
78
- with gr.Blocks(title="WFGY variance gate demo") as demo:
79
- gr.Markdown("# 🧠 WFGY simulation demo")
80
  prompt = gr.Textbox(label="Prompt", value="Explain SchrΓΆdinger's cat")
81
- run_b = gr.Button("πŸš€ Run")
82
 
83
  with gr.Row():
84
  raw_box = gr.Textbox(label="Raw top-5 tokens", lines=6)
@@ -86,17 +99,24 @@ with gr.Blocks(title="WFGY variance gate demo") as demo:
86
 
87
  headline = gr.Markdown()
88
  hist_img = gr.Image(type="pil", label="Logit histogram")
89
- hist_p = gr.Plot()
90
- clr_b = gr.Button("Clear history")
 
 
 
 
 
 
91
 
92
- with gr.Accordion("Paper benchmarks", open=False):
93
- gr.DataFrame(styled_df, interactive=False, wrap=True)
94
- gr.Plot(paper_bar)
95
 
96
- gr.Markdown("---\n⭐ **10 k GitHub stars before 2025-08-01 unlock WFGY 2.0**")
 
97
 
98
- run_b.click(run, prompt, [raw_box,mod_box,headline,hist_img,hist_p])
99
- clr_b.click(clear_hist, None, hist_p)
 
100
 
101
  if __name__ == "__main__":
102
  demo.queue().launch()
 
1
+ """
2
+ WFGY Space – interactive variance-gate demo (tiny-GPT-2 version)
3
+
4
+ β˜… Help us reach 10 k GitHub stars before 2025-08-01 to unlock WFGY 2.0 β˜…
5
+ """
6
+
7
  import io, numpy as np, matplotlib
8
  matplotlib.use("Agg")
9
 
 
14
  from wfgy_sdk import get_engine
15
  from wfgy_sdk.evaluator import compare_logits, plot_histogram
16
 
17
+ # ── tiny model keeps HF Space free-CPU build fast ──
18
+ TOKENIZER = AutoTokenizer.from_pretrained("sshleifer/tiny-gpt2")
19
+ MODEL = AutoModelForCausalLM.from_pretrained("sshleifer/tiny-gpt2")
20
+ ENGINE = get_engine()
21
 
22
+ # ── benchmark table from the WFGY 1.0 paper ──
23
+ paper = pd.DataFrame({
 
 
 
24
  "Benchmark": ["MMLU","GSM8K","BBH","MathBench","TruthfulQA",
25
  "XNLI","MLQA","LongBench","VQAv2","OK-VQA"],
26
  "Baseline": [61.0,78.0,79.3,72.2,62.4,59.5,78.1,51.4,69.1,65.7],
27
  "WFGY": [89.8,98.7,100.7,87.4,90.4,77.3,106.6,69.6,86.6,86.8]
28
  })
29
+ paper["Abs_gain"] = (paper["WFGY"] - paper["Baseline"]).round(1)
30
+ paper["Rel_gain%"] = ((paper["Abs_gain"] / paper["Baseline"]) * 100).round(0)
31
+ paper_style = (
32
+ paper.style
 
33
  .background_gradient(cmap="Greens", subset=["Abs_gain","Rel_gain%"])
34
  .format({"Abs_gain":"{:.1f}","Rel_gain%":"{:.0f}"})
35
  )
36
 
37
+ # ── helpers ────────────────────────────────────────────────────────────
38
+ def top5(txt_logits: np.ndarray) -> str:
39
+ probs = torch.softmax(torch.tensor(txt_logits), dim=0).numpy()
40
+ idx = probs.argsort()[-5:][::-1]
41
+ rows = [f"{TOKENIZER.decode(int(i)).strip()!r}: {probs[i]:.2e}" for i in idx]
42
+ return "\n".join(rows)
43
+
44
+ def history_plot(hist):
45
+ if not hist["step"]:
46
+ return gr.Plot()
47
+ df = pd.DataFrame(hist)
48
+ return px.line(
49
+ df, x="step", y=["var","kl"],
50
+ labels={"value":"metric","step":"call"},
51
+ title="History (variance ↓ & KL)",
52
+ height=270
53
+ )
54
+
55
+ def run(prompt, hist):
56
+ prompt = prompt.strip()
57
+ if not prompt:
58
+ return "", "", " ", None, history_plot(hist)
59
+
60
+ ids = TOKENIZER(prompt, return_tensors="pt").input_ids
61
+ rawL = MODEL(ids).logits[0, -1].detach().cpu().numpy()
62
+ G = np.random.randn(256).astype(np.float32)
63
+ I = G + np.random.normal(scale=0.05, size=256).astype(np.float32)
64
+ modL = ENGINE.run(I, G, rawL)
65
+
66
+ met = compare_logits(rawL, modL)
67
+ n = len(hist["step"])
68
+ hist["step"].append(n)
69
+ hist["var"].append(met["var_drop"]*100)
70
+ hist["kl"].append(met["kl_divergence"])
71
+
72
+ fig = plot_histogram(rawL, modL)
73
+ buf = io.BytesIO(); fig.savefig(buf, format="png"); buf.seek(0)
74
+ head = f"β–Ό var {met['var_drop']*100:4.1f}% | KL {met['kl_divergence']:.3f} | top-1 {'kept' if met['top1'] else 'changed'}"
75
+
76
+ return top5(rawL), top5(modL), head, Image.open(buf), history_plot(hist)
77
+
78
+ def clear(hist):
79
+ hist["step"].clear(); hist["var"].clear(); hist["kl"].clear()
80
+ return history_plot(hist)
81
+
82
+ # ── UI ─────────────────────────────────────────────────────────────────
83
+ with gr.Blocks(title="WFGY variance gate demo") as demo:
84
+ gr.Markdown(
85
+ """
86
+ # 🧠 **WFGY simulation demo**
87
+ Tiny GPT-2 + variance-gate. Type any prompt and watch logits collapse.
88
 
89
+ [⭐ Star the repo](https://github.com/onestardao/WFGY) – 10 k ⭐ before **2025-08-01** unlocks WFGY 2.0.
90
+ [πŸ“„ PDF](https://doi.org/10.5281/zenodo.15630970) β€’ [GitHub](https://github.com/onestardao/WFGY)
91
+ """)
92
 
 
 
 
93
  prompt = gr.Textbox(label="Prompt", value="Explain SchrΓΆdinger's cat")
94
+ run_btn = gr.Button("πŸš€ Run")
95
 
96
  with gr.Row():
97
  raw_box = gr.Textbox(label="Raw top-5 tokens", lines=6)
 
99
 
100
  headline = gr.Markdown()
101
  hist_img = gr.Image(type="pil", label="Logit histogram")
102
+ hist_plot = gr.Plot()
103
+ clear_btn = gr.Button("Clear history")
104
+
105
+ with gr.Accordion("Paper benchmarks (fixed values from WFGY 1.0)", open=False):
106
+ gr.DataFrame(paper_style, interactive=False, wrap=True)
107
+ gr.Markdown(
108
+ "These numbers come from the paper; run your own benchmarks with "
109
+ "`WFGY/examples/example_08_big_model.py` on a larger LLM.")
110
 
111
+ gr.Markdown("---\n*Free CPU demo = modest effect.* "
112
+ "Try a bigger model locally for dramatic variance drop & KL gain.")
 
113
 
114
+ # state for history
115
+ hist_state = gr.State({"step": [], "var": [], "kl": []})
116
 
117
+ run_btn.click(run, [prompt, hist_state],
118
+ [raw_box, mod_box, headline, hist_img, hist_plot])
119
+ clear_btn.click(clear, hist_state, hist_plot)
120
 
121
  if __name__ == "__main__":
122
  demo.queue().launch()