OneStarDao commited on
Commit
243324b
·
verified ·
1 Parent(s): e2ba1b1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -88
app.py CHANGED
@@ -1,12 +1,3 @@
1
- """
2
- WFGY HuggingFace Space — deluxe marketing demo
3
- ----------------------------------------------
4
- * Show before/after text
5
- * Display variance drop, KL, top-1 shift
6
- * Overlay histogram
7
- * Rich Markdown explaining every metric, PDF trick, star goal, secret papers
8
- """
9
-
10
  import base64, io, numpy as np, gradio as gr, wfgy_sdk as w
11
  from wfgy_sdk.evaluator import compare_logits
12
  from wfgy_sdk.visual import plot_histogram
@@ -14,119 +5,102 @@ from wfgy_sdk.visual import plot_histogram
14
  import torch
15
  from transformers import AutoModelForCausalLM, AutoTokenizer, set_seed
16
 
17
- MODEL = "sshleifer/tiny-gpt2" # fast CPU model
18
  tokenizer = AutoTokenizer.from_pretrained(MODEL)
19
- model = AutoModelForCausalLM.from_pretrained(MODEL)
20
  set_seed(42)
 
21
 
22
- ENGINE = w.get_engine() # singleton
23
 
24
-
25
- # ------------------------------------------------------------
26
- # helper: run WFGY or bypass, return text + metrics + img
27
- # ------------------------------------------------------------
28
- def wfgy_demo(prompt: str, enable_wfgy: bool):
29
  if not prompt.strip():
30
- return "", "", "", ""
31
-
32
- # ----- raw logits -----
33
- ids = tokenizer(prompt, return_tensors="pt").input_ids
34
- with torch.no_grad():
35
- output = model(ids)
36
- raw_logits = output.logits[0, -1].cpu().numpy()
37
-
38
- # ----- dummy semantic vectors (demo only) -----
39
- G = np.random.randn(256); G /= np.linalg.norm(G)
40
- I = G + np.random.normal(scale=0.05, size=256)
41
-
42
- # ----- run or skip WFGY -----
43
- if enable_wfgy:
44
- mod_logits = ENGINE.run(
45
- input_vec=I,
46
- ground_vec=G,
47
- logits=raw_logits
48
  )
49
- else:
50
- mod_logits = raw_logits.copy()
51
-
52
- # ----- decode 1-step continuation -----
53
- raw_next = tokenizer.decode(int(raw_logits.argmax()))
54
- mod_next = tokenizer.decode(int(mod_logits.argmax()))
55
- raw_txt = prompt + raw_next
56
- mod_txt = prompt + mod_next
57
-
58
- # ----- metrics -----
59
- m = compare_logits(raw_logits, mod_logits)
60
- top1_flag = "✔ changed" if m["top1_shift"] else "✘ unchanged"
61
- badge = (
62
- f"<b>variance ▼ {(1-m['std_ratio'])*100:.0f}%</b> "
63
- f"| <b>KL {m['kl_divergence']:.2f}</b> "
64
- f"| top-1 {top1_flag}"
65
- )
66
 
67
- # ----- histogram -----
68
- fig = plot_histogram(raw_logits, mod_logits, show=False)
69
- buffer = io.BytesIO(); fig.savefig(buffer, format="png"); fig.clf()
70
- hist_uri = "data:image/png;base64," + base64.b64encode(buffer.getvalue()).decode()
 
 
 
71
 
72
- return raw_txt, mod_txt, badge, hist_uri
 
 
73
 
 
 
 
74
 
75
- # ------------------------------------------------------------
76
- # Gradio UI
77
- # ------------------------------------------------------------
78
- with gr.Blocks(title="WFGY — Self-Healing Variance Gate") as demo:
79
 
 
 
 
 
 
 
 
80
  gr.Markdown(
81
  """
82
- ### 🧠 **WFGY 1-click Variance Gate**
83
 
84
- *Turn GPT-2 into a calmer thinker in seconds. Bigger LLMs show even stronger gains.*
 
85
 
86
  | Metric | Meaning |
87
  |--------|---------|
88
- | **variance ▼** | logits become less noisy (focus ↑) |
89
- | **KL** | distribution actually reshaped |
90
- | **top-1** | most-likely token swapped or not ✘ |
91
 
92
- **Benchmarks (WFGY 1.0 vs base):**
93
 
94
  | Task | Base % | WFGY % | Δ |
95
- |------|-------|-------|---|
96
  | MMLU | 61.0 | **89.8** | +47 % |
97
  | TruthfulQA | 62.4 | **90.4** | +45 % |
98
  | GSM8K | 78.0 | **98.7** | +27 % |
99
-
100
- > 🔖 *PDF workflow*: clone repo → feed `docs/WFGY_1.0.pdf` to <em>any</em> chat-LLM, prepend your prompt with **“use WFGY”** and watch the difference — no-code, cross-model magic.
101
- > ⭐ **10 000 GitHub Stars before 2025-08-01** unlocks **WFGY 2.0** (adaptive gamma, cross-modal). Miss it and v2 goes pay-walled & sealed.
102
- > 📂 *I_am_not_lizardman/* holds <b>8 + 1 “Challenge-Einstein” papers</b> — tweet a screenshot if you find them!
103
  """
104
  )
105
 
106
- with gr.Row():
107
- prompt = gr.Textbox(label="Prompt", placeholder="Ask anything…", lines=2)
108
- enable = gr.Checkbox(label="Enable WFGY", value=True)
109
- run_btn = gr.Button("Run")
110
 
111
  with gr.Row():
112
- raw_out = gr.Textbox(label="Raw GPT-2", lines=4)
113
- mod_out = gr.Textbox(label="After WFGY", lines=4)
114
 
115
  metrics = gr.HTML()
116
- hist = gr.Image(label="Logit distribution", width=440)
117
 
118
- run_btn.click(
119
- fn=wfgy_demo,
120
- inputs=[prompt, enable],
121
- outputs=[raw_out, mod_out, metrics, hist]
122
- )
123
 
124
  gr.Markdown(
125
  """
126
- <div align="center">
127
- Love the variance drop? <a href="https://github.com/onestardao/WFGY" target="_blank"><b>Star the repo</b></a> &nbsp;•&nbsp;
128
- <a href="https://doi.org/10.5281/zenodo.15630970" target="_blank">Read the paper</a>
129
- </div>
 
 
 
130
  """,
131
  elem_id="footer"
132
  )
 
 
 
 
 
 
 
 
 
 
1
  import base64, io, numpy as np, gradio as gr, wfgy_sdk as w
2
  from wfgy_sdk.evaluator import compare_logits
3
  from wfgy_sdk.visual import plot_histogram
 
5
  import torch
6
  from transformers import AutoModelForCausalLM, AutoTokenizer, set_seed
7
 
8
+ MODEL = "sshleifer/tiny-gpt2"
9
  tokenizer = AutoTokenizer.from_pretrained(MODEL)
10
+ model = AutoModelForCausalLM.from_pretrained(MODEL)
11
  set_seed(42)
12
+ ENGINE = w.get_engine()
13
 
 
14
 
15
+ def wfgy_pipeline(prompt: str, enable_wfgy: bool):
 
 
 
 
16
  if not prompt.strip():
17
+ return "", "", "<i>Please enter a prompt.</i>", None
18
+
19
+ try:
20
+ ids = tokenizer(prompt, return_tensors="pt").input_ids
21
+ raw_logits = model(ids).logits[0, -1].detach().numpy()
22
+
23
+ G = np.random.randn(256); G /= np.linalg.norm(G)
24
+ I = G + np.random.normal(scale=0.05, size=256)
25
+
26
+ mod_logits = (
27
+ ENGINE.run(input_vec=I, ground_vec=G, logits=raw_logits)
28
+ if enable_wfgy else raw_logits.copy()
 
 
 
 
 
 
29
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
+ m = compare_logits(raw_logits, mod_logits)
32
+ top1 = "✔" if m["top1_shift"] else "✘"
33
+ metrics_html = (
34
+ f"<b>variance {(1-m['std_ratio'])*100:.0f}%</b> "
35
+ f"| <b>KL {m['kl_divergence']:.2f}</b> "
36
+ f"| top-1 {top1}"
37
+ )
38
 
39
+ fig = plot_histogram(raw_logits, mod_logits, show=False)
40
+ buf = io.BytesIO(); fig.savefig(buf, format="png"); fig.clf()
41
+ img_uri = "data:image/png;base64," + base64.b64encode(buf.getvalue()).decode()
42
 
43
+ raw_next = tokenizer.decode(int(raw_logits.argmax()))
44
+ mod_next = tokenizer.decode(int(mod_logits.argmax()))
45
+ return prompt + raw_next, prompt + mod_next, metrics_html, img_uri
46
 
47
+ except Exception as e:
48
+ return "", "", f"<b style='color:red'>Error:</b> {str(e)}", None
 
 
49
 
50
+
51
+ css = """
52
+ #prompt-row {margin-bottom: 1.0rem}
53
+ .gr-box {font-size: 0.85rem}
54
+ """
55
+
56
+ with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
57
  gr.Markdown(
58
  """
59
+ ### 🧠 WFGY 1-click Variance Gate
60
 
61
+ Turn GPT-2 into a calmer thinker in seconds.<br>
62
+ **Bigger LLMs → even stronger gains.**
63
 
64
  | Metric | Meaning |
65
  |--------|---------|
66
+ | **variance ▼** | logits become less noisy |
67
+ | **KL** | distribution reshaped |
68
+ | **top-1** | most-likely token swapped ✔/✘ |
69
 
70
+ **Benchmarks (WFGY 1.0 vs base)**
71
 
72
  | Task | Base % | WFGY % | Δ |
73
+ |------|-------|--------|---|
74
  | MMLU | 61.0 | **89.8** | +47 % |
75
  | TruthfulQA | 62.4 | **90.4** | +45 % |
76
  | GSM8K | 78.0 | **98.7** | +27 % |
 
 
 
 
77
  """
78
  )
79
 
80
+ with gr.Row(elem_id="prompt-row"):
81
+ prompt = gr.Textbox(label="Prompt", lines=2, placeholder="Ask anything…")
82
+ enable = gr.Checkbox(label="Enable WFGY", value=True)
83
+ run_btn = gr.Button("Run")
84
 
85
  with gr.Row():
86
+ raw_box = gr.Textbox(label="Raw GPT-2")
87
+ mod_box = gr.Textbox(label="After WFGY")
88
 
89
  metrics = gr.HTML()
90
+ hist_img = gr.Image(label="Logit distribution", width=440)
91
 
92
+ run_btn.click(wfgy_pipeline, [prompt, enable],
93
+ [raw_box, mod_box, metrics, hist_img])
 
 
 
94
 
95
  gr.Markdown(
96
  """
97
+ **PDF mode** – feed <code>I_am_not_lizardman/WFGY_1.0.pdf</code> to any chat-LLM,
98
+ prepend <code>Use WFGY:</code> and watch replies get sharper. Prompt revolution!
99
+
100
+ ⭐ **10 000 GitHub stars before 2025-08-01** unlocks **WFGY 2.0**
101
+ (secret adaptive-gamma, multimodal edition).
102
+
103
+ 📂 Hidden folder <b>I_am_not_lizardman/</b> holds 8 + 1 “Challenge-Einstein” papers — tweet a screenshot if you find them!
104
  """,
105
  elem_id="footer"
106
  )