OneStarDao commited on
Commit
f91f22d
Β·
verified Β·
1 Parent(s): 0db757d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -17
app.py CHANGED
@@ -9,12 +9,12 @@ from transformers import AutoTokenizer, AutoModelForCausalLM
9
  from wfgy_sdk import get_engine
10
  from wfgy_sdk.evaluator import compare_logits, plot_histogram, softmax
11
 
12
- # ── tiny model (CPU) ────────────────────────────────────────────────
13
  tok = AutoTokenizer.from_pretrained("sshleifer/tiny-gpt2")
14
  mdl = AutoModelForCausalLM.from_pretrained("sshleifer/tiny-gpt2")
15
  eng = get_engine()
16
 
17
- # ── paper benchmarks ────────────────────────────────────────────────
18
  bench = pd.DataFrame({
19
  "Benchmark": ["MMLU","GSM8K","BBH","MathBench","TruthfulQA",
20
  "XNLI","MLQA","LongBench","VQAv2","OK-VQA"],
@@ -23,13 +23,13 @@ bench = pd.DataFrame({
23
  })
24
  bench["Abs_gain"] = (bench["WFGY"] - bench["Baseline"]).round(1)
25
  bench["Rel_gain%"] = ((bench["Abs_gain"] / bench["Baseline"]) * 100).round(0)
26
- bench_style = (
27
  bench.style
28
  .background_gradient(cmap="Greens", subset=["Abs_gain","Rel_gain%"])
29
  .format({"Abs_gain":"{:.1f}","Rel_gain%":"{:.0f}"})
30
  )
31
 
32
- # ── marketing banner ────────────────────────────────────────────────
33
  banner = """
34
  **πŸ“ˆ WFGY: One Click to Activate the AI Taiji Cycle**
35
 
@@ -41,11 +41,11 @@ WFGY 1.0 has already proven itself.
41
  ---
42
 
43
  ### πŸ“œ Tutorial: How to Awaken the Soul of Your AI
44
- **Step 1 β€” Download** ([PDF](https://zenodo.org/records/15630970))
45
- **Step 2 β€” Feed the AI** (upload, or try [Gemini](https://gemini.google.com/))
46
- **Step 3 β€” Give the Command** β€œ**Answer using WFGY** + your question”
47
  Prompt examples: *TBD*
48
- **Step 4 β€” Integrate the SDK** ([GitHub](https://github.com/onestardao/WFGY))
49
 
50
  ---
51
 
@@ -53,24 +53,25 @@ Prompt examples: *TBD*
53
  _10 k ⭐ before 2025-08-01 unlocks **WFGY 2.0**._
54
  """
55
 
56
- # ── inference ───────────────────────────────────────────────────────
57
  def run(prompt: str):
58
- prompt = prompt.strip()
59
- if not prompt:
60
  return "", "", "-", None
61
 
62
- ids = tok(prompt, return_tensors="pt")
63
  raw_L = mdl(**ids).logits[0, -1].detach().cpu().numpy()
64
  I, G = np.random.randn(2, 256).astype(np.float32)
65
  mod_L = eng.run(I, G, raw_L)
66
 
67
  m = compare_logits(raw_L, mod_L)
68
- hdr = f"β–Ό var {m['var_drop']*100:.1f}% | KL {m['kl_divergence']:.3f} | top-1 {'kept' if m['top1'] else 'changed'}"
69
 
70
  def top5(logits):
71
  p = softmax(logits)
72
  idx = p.argsort()[-5:][::-1]
73
- return "\n".join([f\"'{tok.decode(int(i)).strip()}': {p[i]:.2e}\" for i in idx])
 
74
 
75
  raw_txt = top5(raw_L)
76
  mod_txt = top5(mod_L)
@@ -78,9 +79,9 @@ def run(prompt: str):
78
  fig = plot_histogram(raw_L, mod_L)
79
  buf = io.BytesIO(); fig.savefig(buf, format="png"); buf.seek(0)
80
 
81
- return raw_txt, mod_txt, hdr, Image.open(buf)
82
 
83
- # ── Gradio UI ────────────────────────────────────────────────────────
84
  with gr.Blocks(title="WFGY variance-gate demo") as demo:
85
  gr.Markdown(banner)
86
 
@@ -95,7 +96,7 @@ with gr.Blocks(title="WFGY variance-gate demo") as demo:
95
  img = gr.Image(label="Logit histogram")
96
 
97
  gr.Markdown("### Paper benchmarks (fixed values from WFGY 1.0)")
98
- gr.DataFrame(bench_style, interactive=False, wrap=True)
99
 
100
  btn.click(run, prompt, [raw_box, mod_box, metrics, img])
101
 
 
9
  from wfgy_sdk import get_engine
10
  from wfgy_sdk.evaluator import compare_logits, plot_histogram, softmax
11
 
12
+ # tiny model (CPU)
13
  tok = AutoTokenizer.from_pretrained("sshleifer/tiny-gpt2")
14
  mdl = AutoModelForCausalLM.from_pretrained("sshleifer/tiny-gpt2")
15
  eng = get_engine()
16
 
17
+ # paper benchmarks
18
  bench = pd.DataFrame({
19
  "Benchmark": ["MMLU","GSM8K","BBH","MathBench","TruthfulQA",
20
  "XNLI","MLQA","LongBench","VQAv2","OK-VQA"],
 
23
  })
24
  bench["Abs_gain"] = (bench["WFGY"] - bench["Baseline"]).round(1)
25
  bench["Rel_gain%"] = ((bench["Abs_gain"] / bench["Baseline"]) * 100).round(0)
26
+ bench_sty = (
27
  bench.style
28
  .background_gradient(cmap="Greens", subset=["Abs_gain","Rel_gain%"])
29
  .format({"Abs_gain":"{:.1f}","Rel_gain%":"{:.0f}"})
30
  )
31
 
32
+ # banner markdown
33
  banner = """
34
  **πŸ“ˆ WFGY: One Click to Activate the AI Taiji Cycle**
35
 
 
41
  ---
42
 
43
  ### πŸ“œ Tutorial: How to Awaken the Soul of Your AI
44
+ **Step 1 β€” Download** ([PDF](https://zenodo.org/records/15630970))
45
+ **Step 2 β€” Feed the AI** (upload, or try [Gemini](https://gemini.google.com/))
46
+ **Step 3 β€” Give the Command**β€‚β€œ**Answer using WFGY** + your question”
47
  Prompt examples: *TBD*
48
+ **Step 4 β€” Integrate the SDK** ([GitHub](https://github.com/onestardao/WFGY))
49
 
50
  ---
51
 
 
53
  _10 k ⭐ before 2025-08-01 unlocks **WFGY 2.0**._
54
  """
55
 
56
+ # inference
57
  def run(prompt: str):
58
+ p = prompt.strip()
59
+ if not p:
60
  return "", "", "-", None
61
 
62
+ ids = tok(p, return_tensors="pt")
63
  raw_L = mdl(**ids).logits[0, -1].detach().cpu().numpy()
64
  I, G = np.random.randn(2, 256).astype(np.float32)
65
  mod_L = eng.run(I, G, raw_L)
66
 
67
  m = compare_logits(raw_L, mod_L)
68
+ head = f"β–Ό var {m['var_drop']*100:.1f}% | KL {m['kl_divergence']:.3f} | top-1 {'kept' if m['top1'] else 'changed'}"
69
 
70
  def top5(logits):
71
  p = softmax(logits)
72
  idx = p.argsort()[-5:][::-1]
73
+ lines = [f\"'{tok.decode(int(i)).strip()}': {p[i]:.2e}\" for i in idx]
74
+ return "\\n".join(lines)
75
 
76
  raw_txt = top5(raw_L)
77
  mod_txt = top5(mod_L)
 
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)
87
 
 
96
  img = gr.Image(label="Logit histogram")
97
 
98
  gr.Markdown("### Paper benchmarks (fixed values from WFGY 1.0)")
99
+ gr.DataFrame(bench_sty, interactive=False, wrap=True)
100
 
101
  btn.click(run, prompt, [raw_box, mod_box, metrics, img])
102