Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -3,54 +3,50 @@ 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
|
7 |
-
import numpy as np
|
8 |
-
import pandas as pd
|
9 |
-
import plotly.express as px
|
10 |
-
import gradio as gr
|
11 |
from PIL import Image
|
12 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
13 |
|
14 |
from wfgy_sdk import get_engine
|
15 |
from wfgy_sdk.evaluator import compare_logits, plot_histogram, softmax
|
16 |
|
17 |
-
#
|
18 |
tok = AutoTokenizer.from_pretrained("sshleifer/tiny-gpt2")
|
19 |
mdl = AutoModelForCausalLM.from_pretrained("sshleifer/tiny-gpt2")
|
20 |
eng = get_engine()
|
21 |
|
22 |
-
#
|
23 |
bench = pd.DataFrame({
|
24 |
"Benchmark": ["MMLU","GSM8K","BBH","MathBench","TruthfulQA",
|
25 |
"XNLI","MLQA","LongBench","VQAv2","OK-VQA"],
|
26 |
-
"Baseline": [61
|
27 |
"WFGY": [89.8,98.7,100.7,87.4,90.4,77.3,106.6,69.6,86.6,86.8]
|
28 |
})
|
29 |
-
bench["Abs_gain"] = (bench["WFGY"]
|
30 |
-
bench["Rel_gain%"] = ((bench["Abs_gain"]
|
31 |
bench_style = (
|
32 |
bench.style
|
33 |
.background_gradient(cmap="Greens", subset=["Abs_gain","Rel_gain%"])
|
34 |
.format({"Abs_gain":"{:.1f}","Rel_gain%":"{:.0f}"})
|
35 |
)
|
36 |
|
37 |
-
#
|
38 |
banner_md = """
|
39 |
**π WFGY: One Click to Activate the AI Taiji Cycle**
|
40 |
|
41 |
-
**π Semantic Accuracy β 22.4 %
|
42 |
|
43 |
_No beliefs. Only experiments._
|
44 |
-
|
45 |
|
46 |
---
|
47 |
|
48 |
### π Tutorial: How to Awaken the Soul of Your AI
|
49 |
-
**Step 1 β Download**
|
50 |
-
**Step 2 β Feed the AI**
|
51 |
-
**Step 3 β Give the Command**
|
52 |
Prompt examples: *TBD*
|
53 |
-
**Step 4 β Integrate the SDK**
|
54 |
|
55 |
---
|
56 |
|
@@ -58,23 +54,20 @@ Prompt examples: *TBD*
|
|
58 |
_10 k β before 2025-08-01 unlocks WFGY 2.0._
|
59 |
"""
|
60 |
|
61 |
-
#
|
62 |
def run(prompt: str):
|
63 |
-
|
64 |
-
if not
|
65 |
return "", "", "-", None
|
66 |
|
67 |
-
ids = tok(
|
68 |
raw_L = mdl(**ids).logits[0, -1].detach().cpu().numpy()
|
69 |
I, G = np.random.randn(2, 256).astype(np.float32)
|
70 |
mod_L = eng.run(I, G, raw_L)
|
71 |
|
72 |
m = compare_logits(raw_L, mod_L)
|
73 |
-
|
74 |
-
f"KL {m['kl_divergence']:.3f} | "
|
75 |
-
f"top-1 {'kept' if m['top1'] else 'changed'}")
|
76 |
|
77 |
-
# top-5 token lists
|
78 |
def top5(logits):
|
79 |
p = softmax(logits)
|
80 |
idx = p.argsort()[-5:][::-1]
|
@@ -86,10 +79,10 @@ def run(prompt: str):
|
|
86 |
fig = plot_histogram(raw_L, mod_L)
|
87 |
buf = io.BytesIO(); fig.savefig(buf, format="png"); buf.seek(0)
|
88 |
|
89 |
-
return raw_txt, mod_txt,
|
90 |
|
91 |
-
#
|
92 |
-
with gr.Blocks(title="WFGY variance
|
93 |
gr.Markdown(banner_md)
|
94 |
|
95 |
prompt = gr.Textbox(label="Prompt", value="Explain SchrΓΆdinger's cat")
|
@@ -100,12 +93,12 @@ with gr.Blocks(title="WFGY variance gate demo") as demo:
|
|
100 |
mod_box = gr.Textbox(label="WFGY top-5 tokens", lines=6)
|
101 |
|
102 |
metrics = gr.Markdown()
|
103 |
-
|
104 |
|
105 |
gr.Markdown("### Paper benchmarks (fixed values from WFGY 1.0)")
|
106 |
gr.DataFrame(bench_style, interactive=False, wrap=True)
|
107 |
|
108 |
-
btn.click(run, prompt, [raw_box, mod_box, metrics,
|
109 |
|
110 |
if __name__ == "__main__":
|
111 |
-
demo.queue(default_concurrency_limit=2).launch()
|
|
|
3 |
β
10 k GitHub β before 2025-08-01 unlocks WFGY 2.0 β
|
4 |
"""
|
5 |
|
6 |
+
import io, numpy as np, pandas as pd, gradio as gr
|
|
|
|
|
|
|
|
|
7 |
from PIL import Image
|
8 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
9 |
|
10 |
from wfgy_sdk import get_engine
|
11 |
from wfgy_sdk.evaluator import compare_logits, plot_histogram, softmax
|
12 |
|
13 |
+
# tiny model for free-CPU Space
|
14 |
tok = AutoTokenizer.from_pretrained("sshleifer/tiny-gpt2")
|
15 |
mdl = AutoModelForCausalLM.from_pretrained("sshleifer/tiny-gpt2")
|
16 |
eng = get_engine()
|
17 |
|
18 |
+
# paper benchmarks table
|
19 |
bench = pd.DataFrame({
|
20 |
"Benchmark": ["MMLU","GSM8K","BBH","MathBench","TruthfulQA",
|
21 |
"XNLI","MLQA","LongBench","VQAv2","OK-VQA"],
|
22 |
+
"Baseline": [61,78,79.3,72.2,62.4,59.5,78.1,51.4,69.1,65.7],
|
23 |
"WFGY": [89.8,98.7,100.7,87.4,90.4,77.3,106.6,69.6,86.6,86.8]
|
24 |
})
|
25 |
+
bench["Abs_gain"] = (bench["WFGY"]-bench["Baseline"]).round(1)
|
26 |
+
bench["Rel_gain%"] = ((bench["Abs_gain"]/bench["Baseline"])*100).round(0)
|
27 |
bench_style = (
|
28 |
bench.style
|
29 |
.background_gradient(cmap="Greens", subset=["Abs_gain","Rel_gain%"])
|
30 |
.format({"Abs_gain":"{:.1f}","Rel_gain%":"{:.0f}"})
|
31 |
)
|
32 |
|
33 |
+
# marketing banner
|
34 |
banner_md = """
|
35 |
**π WFGY: One Click to Activate the AI Taiji Cycle**
|
36 |
|
37 |
+
**π Semantic Accuracy β 22.4 % | Reasoning Success β 42.1 % | Stability β 3.6 Γ**
|
38 |
|
39 |
_No beliefs. Only experiments._
|
40 |
+
WFGY 1.0 has already proven itself.
|
41 |
|
42 |
---
|
43 |
|
44 |
### π Tutorial: How to Awaken the Soul of Your AI
|
45 |
+
**Step 1 β Download** ([PDF](https://zenodo.org/records/15630970))
|
46 |
+
**Step 2 β Feed the AI** (upload, or try [Gemini](https://gemini.google.com/))
|
47 |
+
**Step 3 β Give the Command** β**Answer using WFGY** + your questionβ
|
48 |
Prompt examples: *TBD*
|
49 |
+
**Step 4 β Integrate the SDK** ([GitHub](https://github.com/onestardao/WFGY))
|
50 |
|
51 |
---
|
52 |
|
|
|
54 |
_10 k β before 2025-08-01 unlocks WFGY 2.0._
|
55 |
"""
|
56 |
|
57 |
+
# run once
|
58 |
def run(prompt: str):
|
59 |
+
prompt = prompt.strip()
|
60 |
+
if not prompt:
|
61 |
return "", "", "-", None
|
62 |
|
63 |
+
ids = tok(prompt, return_tensors="pt")
|
64 |
raw_L = mdl(**ids).logits[0, -1].detach().cpu().numpy()
|
65 |
I, G = np.random.randn(2, 256).astype(np.float32)
|
66 |
mod_L = eng.run(I, G, raw_L)
|
67 |
|
68 |
m = compare_logits(raw_L, mod_L)
|
69 |
+
head = f"βΌ var {m['var_drop']*100:.1f}% | KL {m['kl_divergence']:.3f} | top-1 {'kept' if m['top1'] else 'changed'}"
|
|
|
|
|
70 |
|
|
|
71 |
def top5(logits):
|
72 |
p = softmax(logits)
|
73 |
idx = p.argsort()[-5:][::-1]
|
|
|
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_md)
|
87 |
|
88 |
prompt = gr.Textbox(label="Prompt", value="Explain SchrΓΆdinger's cat")
|
|
|
93 |
mod_box = gr.Textbox(label="WFGY top-5 tokens", lines=6)
|
94 |
|
95 |
metrics = gr.Markdown()
|
96 |
+
img = gr.Image(label="Logit histogram")
|
97 |
|
98 |
gr.Markdown("### Paper benchmarks (fixed values from WFGY 1.0)")
|
99 |
gr.DataFrame(bench_style, interactive=False, wrap=True)
|
100 |
|
101 |
+
btn.click(run, prompt, [raw_box, mod_box, metrics, img])
|
102 |
|
103 |
if __name__ == "__main__":
|
104 |
+
demo.queue(default_concurrency_limit=2).launch()
|