Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -9,12 +9,12 @@ from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
9 |
from wfgy_sdk import get_engine
|
10 |
from wfgy_sdk.evaluator import compare_logits, plot_histogram, softmax
|
11 |
|
12 |
-
#
|
13 |
tok = AutoTokenizer.from_pretrained("sshleifer/tiny-gpt2")
|
14 |
mdl = AutoModelForCausalLM.from_pretrained("sshleifer/tiny-gpt2")
|
15 |
eng = get_engine()
|
16 |
|
17 |
-
#
|
18 |
bench = pd.DataFrame({
|
19 |
"Benchmark": ["MMLU","GSM8K","BBH","MathBench","TruthfulQA",
|
20 |
"XNLI","MLQA","LongBench","VQAv2","OK-VQA"],
|
@@ -23,13 +23,13 @@ bench = pd.DataFrame({
|
|
23 |
})
|
24 |
bench["Abs_gain"] = (bench["WFGY"] - bench["Baseline"]).round(1)
|
25 |
bench["Rel_gain%"] = ((bench["Abs_gain"] / bench["Baseline"]) * 100).round(0)
|
26 |
-
|
27 |
bench.style
|
28 |
.background_gradient(cmap="Greens", subset=["Abs_gain","Rel_gain%"])
|
29 |
.format({"Abs_gain":"{:.1f}","Rel_gain%":"{:.0f}"})
|
30 |
)
|
31 |
|
32 |
-
#
|
33 |
banner = """
|
34 |
**π WFGY: One Click to Activate the AI Taiji Cycle**
|
35 |
|
@@ -41,11 +41,11 @@ WFGY 1.0 has already proven itself.
|
|
41 |
---
|
42 |
|
43 |
### π Tutorial: How to Awaken the Soul of Your AI
|
44 |
-
**Step 1 β Download**
|
45 |
-
**Step 2 β Feed the AI**
|
46 |
-
**Step 3 β Give the Command**
|
47 |
Prompt examples: *TBD*
|
48 |
-
**Step 4 β Integrate the SDK**
|
49 |
|
50 |
---
|
51 |
|
@@ -53,24 +53,25 @@ Prompt examples: *TBD*
|
|
53 |
_10 k β before 2025-08-01 unlocks **WFGY 2.0**._
|
54 |
"""
|
55 |
|
56 |
-
#
|
57 |
def run(prompt: str):
|
58 |
-
|
59 |
-
if not
|
60 |
return "", "", "-", None
|
61 |
|
62 |
-
ids = tok(
|
63 |
raw_L = mdl(**ids).logits[0, -1].detach().cpu().numpy()
|
64 |
I, G = np.random.randn(2, 256).astype(np.float32)
|
65 |
mod_L = eng.run(I, G, raw_L)
|
66 |
|
67 |
m = compare_logits(raw_L, mod_L)
|
68 |
-
|
69 |
|
70 |
def top5(logits):
|
71 |
p = softmax(logits)
|
72 |
idx = p.argsort()[-5:][::-1]
|
73 |
-
|
|
|
74 |
|
75 |
raw_txt = top5(raw_L)
|
76 |
mod_txt = top5(mod_L)
|
@@ -78,9 +79,9 @@ def run(prompt: str):
|
|
78 |
fig = plot_histogram(raw_L, mod_L)
|
79 |
buf = io.BytesIO(); fig.savefig(buf, format="png"); buf.seek(0)
|
80 |
|
81 |
-
return raw_txt, mod_txt,
|
82 |
|
83 |
-
#
|
84 |
with gr.Blocks(title="WFGY variance-gate demo") as demo:
|
85 |
gr.Markdown(banner)
|
86 |
|
@@ -95,7 +96,7 @@ with gr.Blocks(title="WFGY variance-gate demo") as demo:
|
|
95 |
img = gr.Image(label="Logit histogram")
|
96 |
|
97 |
gr.Markdown("### Paper benchmarks (fixed values from WFGY 1.0)")
|
98 |
-
gr.DataFrame(
|
99 |
|
100 |
btn.click(run, prompt, [raw_box, mod_box, metrics, img])
|
101 |
|
|
|
9 |
from wfgy_sdk import get_engine
|
10 |
from wfgy_sdk.evaluator import compare_logits, plot_histogram, softmax
|
11 |
|
12 |
+
# tiny model (CPU)
|
13 |
tok = AutoTokenizer.from_pretrained("sshleifer/tiny-gpt2")
|
14 |
mdl = AutoModelForCausalLM.from_pretrained("sshleifer/tiny-gpt2")
|
15 |
eng = get_engine()
|
16 |
|
17 |
+
# paper benchmarks
|
18 |
bench = pd.DataFrame({
|
19 |
"Benchmark": ["MMLU","GSM8K","BBH","MathBench","TruthfulQA",
|
20 |
"XNLI","MLQA","LongBench","VQAv2","OK-VQA"],
|
|
|
23 |
})
|
24 |
bench["Abs_gain"] = (bench["WFGY"] - bench["Baseline"]).round(1)
|
25 |
bench["Rel_gain%"] = ((bench["Abs_gain"] / bench["Baseline"]) * 100).round(0)
|
26 |
+
bench_sty = (
|
27 |
bench.style
|
28 |
.background_gradient(cmap="Greens", subset=["Abs_gain","Rel_gain%"])
|
29 |
.format({"Abs_gain":"{:.1f}","Rel_gain%":"{:.0f}"})
|
30 |
)
|
31 |
|
32 |
+
# banner markdown
|
33 |
banner = """
|
34 |
**π WFGY: One Click to Activate the AI Taiji Cycle**
|
35 |
|
|
|
41 |
---
|
42 |
|
43 |
### π Tutorial: How to Awaken the Soul of Your AI
|
44 |
+
**Step 1 β Download**β([PDF](https://zenodo.org/records/15630970))
|
45 |
+
**Step 2 β Feed the AI**β(upload, or try [Gemini](https://gemini.google.com/))
|
46 |
+
**Step 3 β Give the Command**ββ**Answer using WFGY** + your questionβ
|
47 |
Prompt examples: *TBD*
|
48 |
+
**Step 4 β Integrate the SDK**β([GitHub](https://github.com/onestardao/WFGY))
|
49 |
|
50 |
---
|
51 |
|
|
|
53 |
_10 k β before 2025-08-01 unlocks **WFGY 2.0**._
|
54 |
"""
|
55 |
|
56 |
+
# inference
|
57 |
def run(prompt: str):
|
58 |
+
p = prompt.strip()
|
59 |
+
if not p:
|
60 |
return "", "", "-", None
|
61 |
|
62 |
+
ids = tok(p, return_tensors="pt")
|
63 |
raw_L = mdl(**ids).logits[0, -1].detach().cpu().numpy()
|
64 |
I, G = np.random.randn(2, 256).astype(np.float32)
|
65 |
mod_L = eng.run(I, G, raw_L)
|
66 |
|
67 |
m = compare_logits(raw_L, mod_L)
|
68 |
+
head = f"βΌ var {m['var_drop']*100:.1f}% | KL {m['kl_divergence']:.3f} | top-1 {'kept' if m['top1'] else 'changed'}"
|
69 |
|
70 |
def top5(logits):
|
71 |
p = softmax(logits)
|
72 |
idx = p.argsort()[-5:][::-1]
|
73 |
+
lines = [f\"'{tok.decode(int(i)).strip()}': {p[i]:.2e}\" for i in idx]
|
74 |
+
return "\\n".join(lines)
|
75 |
|
76 |
raw_txt = top5(raw_L)
|
77 |
mod_txt = top5(mod_L)
|
|
|
79 |
fig = plot_histogram(raw_L, mod_L)
|
80 |
buf = io.BytesIO(); fig.savefig(buf, format="png"); buf.seek(0)
|
81 |
|
82 |
+
return raw_txt, mod_txt, head, Image.open(buf)
|
83 |
|
84 |
+
# UI
|
85 |
with gr.Blocks(title="WFGY variance-gate demo") as demo:
|
86 |
gr.Markdown(banner)
|
87 |
|
|
|
96 |
img = gr.Image(label="Logit histogram")
|
97 |
|
98 |
gr.Markdown("### Paper benchmarks (fixed values from WFGY 1.0)")
|
99 |
+
gr.DataFrame(bench_sty, interactive=False, wrap=True)
|
100 |
|
101 |
btn.click(run, prompt, [raw_box, mod_box, metrics, img])
|
102 |
|