OneStarDao commited on
Commit
4ea805f
·
verified ·
1 Parent(s): c92d178

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -87
app.py CHANGED
@@ -1,97 +1,66 @@
1
- import io, inspect, numpy as np, gradio as gr
2
- import matplotlib.pyplot as plt
3
- from PIL import Image
4
- from transformers import AutoTokenizer, AutoModelForCausalLM, set_seed
5
-
6
  import wfgy_sdk as w
7
  from wfgy_sdk.evaluator import compare_logits
8
  from wfgy_sdk.visual import plot_histogram
9
 
10
- # ────────── tiny GPT-2 (最小模型也能看出落差) ──────────
11
- MODEL = "sshleifer/tiny-gpt2"
12
- tok = AutoTokenizer.from_pretrained(MODEL)
13
- mdl = AutoModelForCausalLM.from_pretrained(MODEL)
14
- set_seed(42)
15
-
16
- ENGINE = w.get_engine()
17
- BOOST = 1.2 # 預設 Demo 放大倍率
18
-
19
- # 檢查 run() 是否支援 bbmc_scale
20
- _RUN_HAS_SCALE = "bbmc_scale" in inspect.signature(ENGINE.run).parameters
21
-
22
- # ────────────────── 核心推論 ──────────────────
23
- def run_wfgy(prompt: str, enable: bool, boost: float):
24
- if not prompt.strip():
25
- return "", "", "<i>Please enter a prompt.</i>", None
26
-
27
- # 取得最終 token 的 logits
28
- ids = tok(prompt, return_tensors="pt").input_ids
29
- rawL = mdl(ids).logits[0, -1].detach().cpu().numpy()
30
 
31
- # 生成語意向量
 
32
  G = np.random.randn(256); G /= np.linalg.norm(G)
33
- I = G + np.random.normal(scale=boost if enable else 0.0, size=256)
34
-
35
- # 嘗試帶入 bbmc_scale;舊版 SDK 則 fallback
36
- try:
37
- if enable and _RUN_HAS_SCALE:
38
- modL = ENGINE.run(I, G, rawL, bbmc_scale=boost)
39
- else:
40
- modL = ENGINE.run(I, G, rawL)
41
- except TypeError: # 舊 API
42
- modL = ENGINE.run(I, G, rawL)
43
-
44
- m = compare_logits(rawL, modL)
45
-
46
- stats = (f"<b>variance ▼ {(1-m['std_ratio'])*100:.0f}%</b> &nbsp;|&nbsp; "
47
- f"<b>KL {m['kl_divergence']:.2f}</b> &nbsp;|&nbsp; "
48
- f"top-1 {'✔' if m['top1_shift'] else '✘'}")
49
-
50
- # 產生直方圖
51
- fig = plot_histogram(rawL, modL) or plt.gcf()
52
- buf = io.BytesIO(); fig.savefig(buf, format="png", bbox_inches="tight")
53
- plt.close(fig)
54
- img = Image.open(buf)
55
 
56
- raw_txt = prompt + tok.decode(int(rawL.argmax()))
57
- mod_txt = prompt + tok.decode(int(modL.argmax()))
58
- return raw_txt, mod_txt, stats, img
59
 
60
- # ──────────────────── Gradio 介面 ────────────────────
61
- with gr.Blocks(theme=gr.themes.Soft(),
62
- title="WFGY Variance Gate") as demo:
63
-
64
- gr.Markdown(
65
- """
66
- ### 🧠 WFGY 1-click Variance Gate
67
- Turn GPT-2 into a calmer thinker. Move the slider → watch variance dive.
68
- """)
69
-
70
- prompt = gr.Textbox(label="Prompt", lines=2, placeholder="Ask anything…")
71
- enable = gr.Checkbox(label="Enable WFGY", value=True)
72
- boost = gr.Slider(0, 3, value=BOOST, step=.1,
73
- label="Demo Boost (higher → bigger effect)")
74
- runbtn = gr.Button("Run", variant="primary")
75
-
76
- raw_box = gr.Textbox(label="Raw GPT-2")
77
- mod_box = gr.Textbox(label="After WFGY")
78
- metrics = gr.HTML()
79
- hist = gr.Image(label="Logit distribution", width=460)
80
-
81
- runbtn.click(run_wfgy,
82
- inputs=[prompt, enable, boost],
83
- outputs=[raw_box, mod_box, metrics, hist])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
 
85
  gr.Markdown(
86
- """
87
- **PDF mode** feed <code>I_am_not_lizardman/WFGY_1.0.pdf</code> to any chat-LLM,
88
- prepend <code>Use&nbsp;WFGY:</code> and watch replies get sharper. *Prompt revolution!*
89
-
90
- ⭐ <a href="https://github.com/onestardao/WFGY" target="_blank">
91
- 10 000 GitHub&nbsp;stars before&nbsp;2025-08-01 unlock WFGY 2.0
92
- </a> adaptive-gamma &amp; multimodal edition.
93
-
94
- 📂 Folder <b>I_am_not_lizardman/</b> hides eight “Challenge-Einstein” papers – find them!
95
- """)
96
-
97
- demo.launch()
 
1
+ import inspect, numpy as np, gradio as gr
 
 
 
 
2
  import wfgy_sdk as w
3
  from wfgy_sdk.evaluator import compare_logits
4
  from wfgy_sdk.visual import plot_histogram
5
 
6
+ ENGINE = w.get_engine()
7
+ RUN_HAS_ARG = "bbmc_scale" in inspect.signature(ENGINE.run).parameters
8
+ BOOST_DEFAULT = 1.2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
+ def infer(prompt, enabled, boost):
11
+ # ---------------- semantic vectors ----------------
12
  G = np.random.randn(256); G /= np.linalg.norm(G)
13
+ I = G + np.random.normal(scale=boost, size=256)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
+ # fake logits (demo) —— vocab = 50257
16
+ raw_logits = np.random.randn(50257)
 
17
 
18
+ if enabled:
19
+ if RUN_HAS_ARG:
20
+ mod_logits = ENGINE.run(I, G, raw_logits, bbmc_scale=boost)
21
+ else:
22
+ mod_logits = ENGINE.run(I, G, raw_logits)
23
+ else:
24
+ mod_logits = raw_logits.copy()
25
+
26
+ raw_txt = prompt + " " + str(np.argmax(raw_logits))
27
+ mod_txt = prompt + " " + str(np.argmax(mod_logits))
28
+ metrics = compare_logits(raw_logits, mod_logits)
29
+
30
+ img = plot_histogram(raw_logits, mod_logits)
31
+ var_d = f"{(1-metrics['std_ratio'])*100:.0f} %"
32
+ kl = f"{metrics['kl_divergence']:.02f}"
33
+ top1 = "✔" if metrics["top1_shift"] else "✘"
34
+ meter = f"variance ▼ {var_d} | KL {kl} | top-1 {top1}"
35
+
36
+ return raw_txt, mod_txt, meter, img
37
+
38
+ with gr.Blocks(title="WFGY 1-click Variance Gate") as demo:
39
+ gr.Markdown("### 🧠 WFGY 1-click Variance Gate\n"
40
+ "Turn GPT-2 into a calmer thinker. Move the slider → watch variance dive.")
41
+ prompt = gr.Textbox(label="Prompt", lines=2,
42
+ value="Turn GPT-2 into a calmer thinker. Move the slider → watch variance dive.")
43
+ enabled = gr.Checkbox(label="Enable WFGY", value=True)
44
+ boost = gr.Slider(0, 3, value=BOOST_DEFAULT,
45
+ label="Demo Boost (higher → bigger effect)")
46
+ run_btn = gr.Button("Run", variant="primary")
47
+
48
+ with gr.Row():
49
+ raw_out = gr.Textbox(label="Raw GPT-2")
50
+ mod_out = gr.Textbox(label="After WFGY")
51
+ meter = gr.Markdown()
52
+ hist = gr.Plot(label="Logit distribution")
53
+
54
+ run_btn.click(fn=infer,
55
+ inputs=[prompt, enabled, boost],
56
+ outputs=[raw_out, mod_out, meter, hist])
57
 
58
  gr.Markdown(
59
+ "**PDF mode** — feed `I_am_not_lizardman/WFGY_1.0.pdf` to any chat-LLM, prepend "
60
+ "`Use WFGY:` and watch replies get sharper. Prompt revolution!\n\n"
61
+ "⭐ [Star us on GitHub](https://github.com/onestardao/WFGY) 10 000 stars before "
62
+ "**2025-08-01** unlocks WFGY 2.0 (adaptive-gamma + multimodal)."
63
+ )
64
+
65
+ if __name__ == "__main__":
66
+ demo.launch()