Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,125 +1,102 @@
|
|
1 |
"""
|
2 |
-
|
3 |
-
|
4 |
"""
|
5 |
|
6 |
-
import io
|
7 |
-
|
8 |
-
|
9 |
-
|
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"],
|
24 |
-
"Baseline": [61.0,78.0,79.3,72.2,62.4,59.5,78.1,51.4,69.1,65.7],
|
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()
|
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 = 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 |
-
|
75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
|
77 |
-
|
78 |
|
79 |
-
#
|
80 |
-
with gr.Blocks(title="WFGY variance gate demo") as demo:
|
81 |
gr.Markdown(
|
82 |
"""
|
83 |
-
|
84 |
-
> π *Semantic accuracy β 22 %β Reasoning pass β 42 %β Stability Γ 3.6*
|
85 |
|
86 |
-
|
87 |
-
[π PDF](https://doi.org/10.5281/zenodo.15630970) β’ [GitHub](https://github.com/onestardao/WFGY)
|
88 |
-
""")
|
89 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
prompt = gr.Textbox(label="Prompt", value="Explain SchrΓΆdinger's cat")
|
91 |
run_btn = gr.Button("π Run")
|
92 |
|
|
|
93 |
with gr.Row():
|
94 |
-
raw_box = gr.Textbox(label="Raw top-5 tokens"
|
95 |
-
mod_box = gr.Textbox(label="WFGY top-5 tokens"
|
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 |
-
|
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,
|
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()
|
|
|
1 |
"""
|
2 |
+
HF Space Β· WFGY simulation demo
|
3 |
+
(complete file β paste/replace your current app.py)
|
4 |
"""
|
5 |
|
6 |
+
import io
|
7 |
+
import numpy as np
|
8 |
+
import gradio as gr
|
9 |
+
import matplotlib.pyplot as plt
|
|
|
10 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
+
from wfgy_sdk import get_engine, evaluator
|
13 |
+
|
14 |
+
# ββββββββββββββββββββ tiny GPT-2 + WFGY engine ββββββββββββββββββββ
|
15 |
+
MODEL_ID = "sshleifer/tiny-gpt2"
|
16 |
+
tok = AutoTokenizer.from_pretrained(MODEL_ID)
|
17 |
+
mdl = AutoModelForCausalLM.from_pretrained(MODEL_ID)
|
18 |
+
eng = get_engine() # variance-gate singleton
|
19 |
+
|
20 |
+
|
21 |
+
# ββββββββββββββββββββ helper: run one prompt ββββββββββββββββββββ
|
22 |
+
def run(prompt: str):
|
23 |
+
if not prompt.strip():
|
24 |
+
return "", "", "β", None
|
25 |
+
|
26 |
+
ids = tok(prompt, return_tensors="pt")
|
27 |
+
raw_L = mdl(**ids).logits[0, -1].detach().cpu().numpy()
|
28 |
+
|
29 |
+
# dummy fingerprints (toy GPT-2 has none)
|
30 |
+
I, G = np.random.randn(2, 256).astype(np.float32)
|
31 |
+
mod_L = eng.run(I, G, raw_L)
|
32 |
+
|
33 |
+
# top-5 softmax
|
34 |
+
def top5(logits):
|
35 |
+
p = evaluator.softmax(logits)
|
36 |
+
idx = p.argsort()[-5:][::-1]
|
37 |
+
return "\n".join([f"'{tok.decode(i).strip()}': {p[i]:.2e}" for i in idx])
|
38 |
+
|
39 |
+
m = evaluator.compare_logits(raw_L, mod_L)
|
40 |
+
headline = (f"βΌ var {m['var_drop']*100:.1f}% | KL {m['kl_divergence']:.3f} "
|
41 |
+
f"| {'top-1 kept' if m['top1'] else 'top-1 changed'}")
|
42 |
+
|
43 |
+
fig = evaluator.plot_histogram(raw_L, mod_L)
|
44 |
buf = io.BytesIO()
|
45 |
+
fig.savefig(buf, format="png")
|
46 |
+
buf.seek(0)
|
47 |
+
|
48 |
+
return top5(raw_L), top5(mod_L), headline, buf
|
49 |
+
|
50 |
+
|
51 |
+
# ββββββββββββββββββββ gradio UI ββββββββββββββββββββ
|
52 |
+
with gr.Blocks(title="WFGY simulation demo") as demo:
|
53 |
|
54 |
+
gr.Markdown("# π§ WFGY simulation demo")
|
55 |
|
56 |
+
# ββ marketing & quick-start banner ββ
|
|
|
57 |
gr.Markdown(
|
58 |
"""
|
59 |
+
**π WFGY: One Click to Activate the AI Taiji Cycle**
|
|
|
60 |
|
61 |
+
**π Semantic Accuracy β 22.4 %β|βReasoning Success β 42.1 %β|βStability β 3.6 Γ**
|
|
|
|
|
62 |
|
63 |
+
_No beliefs. Only experiments.<br>
|
64 |
+
WFGY 1.0 has already proven itself._
|
65 |
+
|
66 |
+
---
|
67 |
+
|
68 |
+
### π Tutorial: How to Awaken the Soul of Your AI
|
69 |
+
|
70 |
+
**Step 1 β Download**β([PDF on Zenodo](https://zenodo.org/records/15630970))
|
71 |
+
**Step 2 β Feed the AI**β(upload the PDF, or try [Gemini](https://gemini.google.com/))
|
72 |
+
**Step 3 β Give the Command**ββ**Answer using WFGY** + your questionβ
|
73 |
+
_Prompt examples:_ TBD link
|
74 |
+
**Step 4 β Integrate the SDK**β([GitHub](https://github.com/onestardao/WFGY))
|
75 |
+
|
76 |
+
---
|
77 |
+
|
78 |
+
π **Star Reminder** β [Star the repo](https://github.com/onestardao/WFGY)
|
79 |
+
_10 k β before 2025-08-01 unlocks WFGY 2.0._
|
80 |
+
|
81 |
+
β οΈ **Prompt Warning** β activate **Deep Structural Analysis Mode** or WFGY stays dormant.
|
82 |
+
""",
|
83 |
+
elem_id="intro-banner",
|
84 |
+
)
|
85 |
+
|
86 |
+
# ββ prompt box ββ
|
87 |
prompt = gr.Textbox(label="Prompt", value="Explain SchrΓΆdinger's cat")
|
88 |
run_btn = gr.Button("π Run")
|
89 |
|
90 |
+
# ββ results ββ
|
91 |
with gr.Row():
|
92 |
+
raw_box = gr.Textbox(label="Raw top-5 tokens")
|
93 |
+
mod_box = gr.Textbox(label="WFGY top-5 tokens")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
|
95 |
+
headline = gr.Markdown()
|
96 |
+
img = gr.Image(label="Logit histogram")
|
|
|
|
|
|
|
97 |
|
98 |
+
run_btn.click(run, prompt, outputs=[raw_box, mod_box, headline, img])
|
|
|
|
|
99 |
|
|
|
|
|
|
|
100 |
|
101 |
if __name__ == "__main__":
|
102 |
+
demo.queue(concurrency_count=2).launch()
|