Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,25 +1,23 @@
|
|
1 |
"""
|
2 |
-
WFGY Space β
|
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 |
|
10 |
from PIL import Image
|
11 |
-
import pandas as pd, plotly.express as px, gradio as gr
|
12 |
-
import torch
|
13 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
14 |
from wfgy_sdk import get_engine
|
15 |
from wfgy_sdk.evaluator import compare_logits, plot_histogram
|
16 |
|
17 |
-
#
|
18 |
TOKENIZER = AutoTokenizer.from_pretrained("sshleifer/tiny-gpt2")
|
19 |
MODEL = AutoModelForCausalLM.from_pretrained("sshleifer/tiny-gpt2")
|
20 |
ENGINE = get_engine()
|
21 |
|
22 |
-
#
|
23 |
paper = pd.DataFrame({
|
24 |
"Benchmark": ["MMLU","GSM8K","BBH","MathBench","TruthfulQA",
|
25 |
"XNLI","MLQA","LongBench","VQAv2","OK-VQA"],
|
@@ -27,35 +25,39 @@ paper = pd.DataFrame({
|
|
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"])
|
|
|
|
|
31 |
paper_style = (
|
32 |
paper.style
|
33 |
-
.
|
34 |
.format({"Abs_gain":"{:.1f}","Rel_gain%":"{:.0f}"})
|
|
|
35 |
)
|
36 |
|
37 |
-
#
|
38 |
-
def top5(
|
39 |
-
|
40 |
-
idx
|
41 |
-
rows
|
42 |
return "\n".join(rows)
|
43 |
|
44 |
-
def
|
45 |
-
if not
|
46 |
return gr.Plot()
|
47 |
-
df = pd.DataFrame(
|
48 |
-
return px.line(
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
)
|
|
|
54 |
|
55 |
-
def run(prompt,
|
56 |
prompt = prompt.strip()
|
57 |
if not prompt:
|
58 |
-
return "", "", " ", None,
|
59 |
|
60 |
ids = TOKENIZER(prompt, return_tensors="pt").input_ids
|
61 |
rawL = MODEL(ids).logits[0, -1].detach().cpu().numpy()
|
@@ -63,30 +65,25 @@ def run(prompt, hist):
|
|
63 |
I = G + np.random.normal(scale=0.05, size=256).astype(np.float32)
|
64 |
modL = ENGINE.run(I, G, rawL)
|
65 |
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
hist["kl"].append(met["kl_divergence"])
|
71 |
|
72 |
-
|
73 |
-
|
74 |
-
head
|
75 |
|
76 |
-
return top5(rawL), top5(modL), head, Image.open(buf),
|
77 |
|
78 |
-
|
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 |
-
|
87 |
-
|
88 |
|
89 |
-
[β Star the repo](https://github.com/onestardao/WFGY) β 10 k β before
|
90 |
[π PDF](https://doi.org/10.5281/zenodo.15630970) β’ [GitHub](https://github.com/onestardao/WFGY)
|
91 |
""")
|
92 |
|
@@ -97,26 +94,32 @@ Tiny GPT-2 + variance-gate. Type any prompt and watch logits collapse.
|
|
97 |
raw_box = gr.Textbox(label="Raw top-5 tokens", lines=6)
|
98 |
mod_box = gr.Textbox(label="WFGY top-5 tokens", lines=6)
|
99 |
|
100 |
-
headline
|
101 |
-
hist_img
|
102 |
-
|
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 |
-
"
|
109 |
-
"`
|
|
|
110 |
|
111 |
-
gr.Markdown(
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
|
117 |
run_btn.click(run, [prompt, hist_state],
|
118 |
-
[raw_box, mod_box, headline, hist_img,
|
119 |
-
clear_btn.click(clear, hist_state,
|
|
|
|
|
|
|
|
|
120 |
|
121 |
if __name__ == "__main__":
|
122 |
demo.queue().launch()
|
|
|
1 |
"""
|
2 |
+
WFGY Space β tiny-GPT-2 variance-gate demo
|
3 |
+
β
10 k GitHub β before 2025-08-01 unlocks WFGY 2.0 β
|
|
|
4 |
"""
|
5 |
|
6 |
import io, numpy as np, matplotlib
|
7 |
matplotlib.use("Agg")
|
8 |
|
9 |
from PIL import Image
|
10 |
+
import pandas as pd, plotly.express as px, gradio as gr, torch
|
|
|
11 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
12 |
from wfgy_sdk import get_engine
|
13 |
from wfgy_sdk.evaluator import compare_logits, plot_histogram
|
14 |
|
15 |
+
# tiny free-CPU model
|
16 |
TOKENIZER = AutoTokenizer.from_pretrained("sshleifer/tiny-gpt2")
|
17 |
MODEL = AutoModelForCausalLM.from_pretrained("sshleifer/tiny-gpt2")
|
18 |
ENGINE = get_engine()
|
19 |
|
20 |
+
# paper numbers
|
21 |
paper = pd.DataFrame({
|
22 |
"Benchmark": ["MMLU","GSM8K","BBH","MathBench","TruthfulQA",
|
23 |
"XNLI","MLQA","LongBench","VQAv2","OK-VQA"],
|
|
|
25 |
"WFGY": [89.8,98.7,100.7,87.4,90.4,77.3,106.6,69.6,86.6,86.8]
|
26 |
})
|
27 |
paper["Abs_gain"] = (paper["WFGY"] - paper["Baseline"]).round(1)
|
28 |
+
paper["Rel_gain%"] = ((paper["Abs_gain"] / paper["Baseline"])*100).round(0)
|
29 |
+
|
30 |
+
# color bars on gains
|
31 |
paper_style = (
|
32 |
paper.style
|
33 |
+
.format({"Baseline":"{:.1f}","WFGY":"{:.1f}"})
|
34 |
.format({"Abs_gain":"{:.1f}","Rel_gain%":"{:.0f}"})
|
35 |
+
.background_gradient(cmap="Greens", subset=["Abs_gain","Rel_gain%"])
|
36 |
)
|
37 |
|
38 |
+
# helpers βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
39 |
+
def top5(logits: np.ndarray) -> str:
|
40 |
+
p = torch.softmax(torch.tensor(logits), 0).numpy()
|
41 |
+
idx = p.argsort()[-5:][::-1]
|
42 |
+
rows = [f"{TOKENIZER.decode(int(i)).strip()!r}: {p[i]:.2e}" for i in idx]
|
43 |
return "\n".join(rows)
|
44 |
|
45 |
+
def hist_plot(state):
|
46 |
+
if not state["step"]:
|
47 |
return gr.Plot()
|
48 |
+
df = pd.DataFrame(state)
|
49 |
+
return px.line(df, x="step", y=["var","kl"],
|
50 |
+
labels={"value":"metric","step":"call"},
|
51 |
+
height=270)
|
52 |
+
|
53 |
+
def clear(state): # reset history
|
54 |
+
state["step"].clear(); state["var"].clear(); state["kl"].clear()
|
55 |
+
return hist_plot(state)
|
56 |
|
57 |
+
def run(prompt: str, state):
|
58 |
prompt = prompt.strip()
|
59 |
if not prompt:
|
60 |
+
return "", "", " ", None, hist_plot(state)
|
61 |
|
62 |
ids = TOKENIZER(prompt, return_tensors="pt").input_ids
|
63 |
rawL = MODEL(ids).logits[0, -1].detach().cpu().numpy()
|
|
|
65 |
I = G + np.random.normal(scale=0.05, size=256).astype(np.float32)
|
66 |
modL = ENGINE.run(I, G, rawL)
|
67 |
|
68 |
+
m = compare_logits(rawL, modL)
|
69 |
+
step = len(state["step"])
|
70 |
+
state["step"].append(step); state["var"].append(m["var_drop"]*100)
|
71 |
+
state["kl"].append(m["kl_divergence"])
|
|
|
72 |
|
73 |
+
buf = io.BytesIO()
|
74 |
+
plot_histogram(rawL, modL).savefig(buf, format="png"); buf.seek(0)
|
75 |
+
head = f"βΌ var {m['var_drop']*100:4.1f}% | KL {m['kl_divergence']:.3f} | top-1 {'kept' if m['top1'] else 'changed'}"
|
76 |
|
77 |
+
return top5(rawL), top5(modL), head, Image.open(buf), hist_plot(state)
|
78 |
|
79 |
+
# UI ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
|
|
|
|
|
80 |
with gr.Blocks(title="WFGY variance gate demo") as demo:
|
81 |
gr.Markdown(
|
82 |
"""
|
83 |
+
> π **θ¬ζ³ζΈδΈοΌδΈι΅εε AI ε€ͺζ₯΅εΎͺη°**
|
84 |
+
> π *Semantic accuracy β 22 %β Reasoning pass β 42 %β Stability Γ 3.6*
|
85 |
|
86 |
+
[β Star the repo](https://github.com/onestardao/WFGY) β **10 k β before 2025-08-01 unlocks WFGY 2.0**
|
87 |
[π PDF](https://doi.org/10.5281/zenodo.15630970) β’ [GitHub](https://github.com/onestardao/WFGY)
|
88 |
""")
|
89 |
|
|
|
94 |
raw_box = gr.Textbox(label="Raw top-5 tokens", lines=6)
|
95 |
mod_box = gr.Textbox(label="WFGY top-5 tokens", lines=6)
|
96 |
|
97 |
+
headline = gr.Markdown()
|
98 |
+
hist_img = gr.Image(type="pil", label="Logit histogram")
|
99 |
+
hist_line = gr.Plot()
|
100 |
clear_btn = gr.Button("Clear history")
|
101 |
+
hist_state = gr.State({"step": [], "var": [], "kl": []})
|
102 |
|
103 |
with gr.Accordion("Paper benchmarks (fixed values from WFGY 1.0)", open=False):
|
104 |
gr.DataFrame(paper_style, interactive=False, wrap=True)
|
105 |
gr.Markdown(
|
106 |
+
"Numbers are taken from the paper. Run your own benchmarks with "
|
107 |
+
"`examples/example_08_big_model.py` on a larger model."
|
108 |
+
)
|
109 |
|
110 |
+
gr.Markdown(
|
111 |
+
"---\n"
|
112 |
+
"Free-CPU demo uses a tiny GPT-2, so KL is small and top-1 often kept. "
|
113 |
+
"Run WFGY on a β₯ 7 B LLM for dramatic variance drop & token shifts."
|
114 |
+
)
|
115 |
|
116 |
run_btn.click(run, [prompt, hist_state],
|
117 |
+
[raw_box, mod_box, headline, hist_img, hist_line])
|
118 |
+
clear_btn.click(clear, hist_state, hist_line)
|
119 |
+
|
120 |
+
gr.Markdown(
|
121 |
+
"\n\nβ **Remember to star the repo β every click pushes WFGY 2.0 closer!**"
|
122 |
+
)
|
123 |
|
124 |
if __name__ == "__main__":
|
125 |
demo.queue().launch()
|