OneStarDao commited on
Commit
8c8ffbc
Β·
verified Β·
1 Parent(s): 6d21131

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -34
app.py CHANGED
@@ -1,32 +1,31 @@
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
4
 
5
- import torch
 
6
  from transformers import AutoModelForCausalLM, AutoTokenizer, set_seed
7
 
8
- # ----------------------------------------------------------------------
9
- # tiny GPT-2 so the Space stays within free CPU limits
10
- # ----------------------------------------------------------------------
11
- MODEL = "sshleifer/tiny-gpt2"
12
  tokenizer = AutoTokenizer.from_pretrained(MODEL)
13
  model = AutoModelForCausalLM.from_pretrained(MODEL)
14
  set_seed(42)
15
 
16
  ENGINE = w.get_engine()
17
 
18
- # ----------------------------------------------------------------------
19
- # helper
20
- # ----------------------------------------------------------------------
21
  def wfgy_pipeline(prompt: str, enable_wfgy: bool):
 
22
  if not prompt.strip():
23
  return "", "", "<i>Please enter a prompt.</i>", None
24
 
25
  try:
 
26
  ids = tokenizer(prompt, return_tensors="pt").input_ids
27
  raw_logits = model(ids).logits[0, -1].detach().cpu().numpy()
28
 
29
- # dummy semantic vectors (demo only)
30
  G = np.random.randn(256); G /= np.linalg.norm(G)
31
  I = G + np.random.normal(scale=0.05, size=256)
32
 
@@ -35,32 +34,30 @@ def wfgy_pipeline(prompt: str, enable_wfgy: bool):
35
  if enable_wfgy else raw_logits.copy()
36
  )
37
 
38
- # metrics
39
- m = compare_logits(raw_logits, mod_logits)
40
- top1 = "βœ”" if m["top1_shift"] else "✘"
41
- metric = (f"<b>variance β–Ό {(1-m['std_ratio'])*100:.0f}%</b> | "
42
- f"<b>KL {m['kl_divergence']:.2f}</b> | top-1 {top1}")
 
 
43
 
44
- # histogram (support both β€œreturn fig” or β€œdraw directly” versions)
45
- maybe_fig = plot_histogram(raw_logits, mod_logits) # **no show kwarg**
46
- import matplotlib.pyplot as plt
47
- fig = maybe_fig if maybe_fig is not None else plt.gcf()
48
- buf = io.BytesIO(); fig.savefig(buf, format="png"); fig.clf()
49
- hist_uri = "data:image/png;base64," + base64.b64encode(buf.getvalue()).decode()
50
 
51
- # one-token continuations
52
  raw_txt = prompt + tokenizer.decode(int(raw_logits.argmax()))
53
  mod_txt = prompt + tokenizer.decode(int(mod_logits.argmax()))
54
 
55
- return raw_txt, mod_txt, metric, hist_uri
56
 
57
- except Exception as e:
58
- err = f"<b style='color:red'>Error:</b> {str(e)}"
59
  return "", "", err, None
60
 
61
- # ----------------------------------------------------------------------
62
- # UI
63
- # ----------------------------------------------------------------------
64
  css = "#prompt-row{margin-bottom:1rem}.gr-box{font-size:.85rem}"
65
 
66
  with gr.Blocks(title="WFGY Variance Gate", css=css, theme=gr.themes.Soft()) as demo:
@@ -68,7 +65,7 @@ with gr.Blocks(title="WFGY Variance Gate", css=css, theme=gr.themes.Soft()) as d
68
  """
69
  ### 🧠 WFGY 1-click Variance Gate
70
 
71
- Turn GPT-2 into a calmer thinker in seconds. **Bigger LLMs β†’ even stronger gains.**
72
 
73
  | Metric | Meaning |
74
  |--------|---------|
@@ -79,7 +76,7 @@ Turn GPT-2 into a calmer thinker in seconds. **Bigger LLMs β†’ even stronger gai
79
  **Benchmarks (WFGY 1.0 vs base)**
80
 
81
  | Task | Base % | WFGY % | Ξ” |
82
- |------|-------:|-------:|---:|
83
  | MMLU | 61.0 | **89.8** | +47 % |
84
  | TruthfulQA | 62.4 | **90.4** | +45 % |
85
  | GSM8K | 78.0 | **98.7** | +27 % |
@@ -95,15 +92,15 @@ Turn GPT-2 into a calmer thinker in seconds. **Bigger LLMs β†’ even stronger gai
95
  raw_box = gr.Textbox(label="Raw GPT-2")
96
  mod_box = gr.Textbox(label="After WFGY")
97
 
98
- metric_html = gr.HTML()
99
- hist_img = gr.Image(label="Logit distribution", width=440)
100
 
101
  runbtn.click(wfgy_pipeline, [prompt, enable],
102
- [raw_box, mod_box, metric_html, hist_img])
103
 
104
  gr.Markdown(
105
  """
106
- **PDF mode** – feed <code>I_am_not_lizardman/WFGY_1.0.pdf</code> to any chat-LLM,
107
  prepend <code>Use WFGY:</code> and watch replies get sharper. Prompt revolution!
108
 
109
  ⭐ **10 000 GitHub stars before 2025-08-01** unlocks **WFGY 2.0**
 
1
+ import 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
4
 
5
+ from PIL import Image
6
+ import matplotlib.pyplot as plt
7
  from transformers import AutoModelForCausalLM, AutoTokenizer, set_seed
8
 
9
+ # ─────────────────────────── config ────────────────────────────
10
+ MODEL = "sshleifer/tiny-gpt2" # 124 MB, fits free tier
 
 
11
  tokenizer = AutoTokenizer.from_pretrained(MODEL)
12
  model = AutoModelForCausalLM.from_pretrained(MODEL)
13
  set_seed(42)
14
 
15
  ENGINE = w.get_engine()
16
 
17
+ # ─────────────────────────── core fn ───────────────────────────
 
 
18
  def wfgy_pipeline(prompt: str, enable_wfgy: bool):
19
+ """Return raw text, mod text, HTML metrics and PIL histogram."""
20
  if not prompt.strip():
21
  return "", "", "<i>Please enter a prompt.</i>", None
22
 
23
  try:
24
+ # ── logits from tiny GPT-2
25
  ids = tokenizer(prompt, return_tensors="pt").input_ids
26
  raw_logits = model(ids).logits[0, -1].detach().cpu().numpy()
27
 
28
+ # demo semantic vectors
29
  G = np.random.randn(256); G /= np.linalg.norm(G)
30
  I = G + np.random.normal(scale=0.05, size=256)
31
 
 
34
  if enable_wfgy else raw_logits.copy()
35
  )
36
 
37
+ # ── metrics
38
+ m = compare_logits(raw_logits, mod_logits)
39
+ top1 = "βœ”" if m["top1_shift"] else "✘"
40
+ stats = (
41
+ f"<b>variance β–Ό {(1-m['std_ratio'])*100:.0f}%</b> &nbsp;|&nbsp; "
42
+ f"<b>KL {m['kl_divergence']:.2f}</b> &nbsp;|&nbsp; top-1 {top1}"
43
+ )
44
 
45
+ # ── histogram β†’ PIL
46
+ fig = plot_histogram(raw_logits, mod_logits) or plt.gcf()
47
+ buf = io.BytesIO(); fig.savefig(buf, format="png", bbox_inches="tight"); plt.close(fig)
48
+ hist_img = Image.open(buf)
 
 
49
 
50
+ # ── one-token continuations
51
  raw_txt = prompt + tokenizer.decode(int(raw_logits.argmax()))
52
  mod_txt = prompt + tokenizer.decode(int(mod_logits.argmax()))
53
 
54
+ return raw_txt, mod_txt, stats, hist_img
55
 
56
+ except Exception as exc:
57
+ err = f"<b style='color:red'>Error:</b> {exc}"
58
  return "", "", err, None
59
 
60
+ # ─────────────────────────── UI ────────────────────────────────
 
 
61
  css = "#prompt-row{margin-bottom:1rem}.gr-box{font-size:.85rem}"
62
 
63
  with gr.Blocks(title="WFGY Variance Gate", css=css, theme=gr.themes.Soft()) as demo:
 
65
  """
66
  ### 🧠 WFGY 1-click Variance Gate
67
 
68
+ Turn GPT-2 into a calmer thinker in seconds. **Bigger LLMs β†’ stronger gains.**
69
 
70
  | Metric | Meaning |
71
  |--------|---------|
 
76
  **Benchmarks (WFGY 1.0 vs base)**
77
 
78
  | Task | Base % | WFGY % | Ξ” |
79
+ |------|------:|------:|--:|
80
  | MMLU | 61.0 | **89.8** | +47 % |
81
  | TruthfulQA | 62.4 | **90.4** | +45 % |
82
  | GSM8K | 78.0 | **98.7** | +27 % |
 
92
  raw_box = gr.Textbox(label="Raw GPT-2")
93
  mod_box = gr.Textbox(label="After WFGY")
94
 
95
+ metrics = gr.HTML()
96
+ hist = gr.Image(label="Logit distribution", width=440, show_label=True)
97
 
98
  runbtn.click(wfgy_pipeline, [prompt, enable],
99
+ [raw_box, mod_box, metrics, hist])
100
 
101
  gr.Markdown(
102
  """
103
+ **PDF mode ** – feed <code>I_am_not_lizardman/WFGY_1.0.pdf</code> to any chat-LLM,
104
  prepend <code>Use WFGY:</code> and watch replies get sharper. Prompt revolution!
105
 
106
  ⭐ **10 000 GitHub stars before 2025-08-01** unlocks **WFGY 2.0**