OneStarDao commited on
Commit
822e03e
·
verified ·
1 Parent(s): e95bc22

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -41
app.py CHANGED
@@ -2,60 +2,54 @@
2
  HF Space · WFGY 1-click Variance Gate
3
  """
4
 
5
- import gradio as gr, numpy as np, torch
 
 
6
  from transformers import AutoModelForCausalLM, AutoTokenizer
7
  from wfgy_sdk import get_engine
8
  from wfgy_sdk.evaluator import compare_logits
9
 
10
- MODEL = "sshleifer/tiny-gpt2"
11
- tok = AutoTokenizer.from_pretrained(MODEL)
12
- mdl = AutoModelForCausalLM.from_pretrained(MODEL)
13
- ENGINE = get_engine()
 
14
 
15
-
16
- def run(prompt, enable, boost):
17
  if not prompt.strip():
18
- return gr.update(value="-", visible=True)
19
 
20
- # raw logits
21
- inputs = tok(prompt, return_tensors="pt")
22
- rawL = mdl(**inputs).logits[0, -1].detach().cpu().float().numpy()
23
 
24
- # demo-only fake semantic vectors
25
- I = np.random.randn(256).astype(np.float32)
26
  G = np.random.randn(256).astype(np.float32)
 
27
 
28
- if enable:
29
- modL = ENGINE.run(
30
- logits = rawL,
31
- input_vec = I,
32
- ground_vec = G,
33
- boost = boost,
34
- )
35
- else:
36
- modL = rawL
37
 
38
- raw_txt = prompt + tok.decode(int(rawL.argmax()))
39
- mod_txt = prompt + tok.decode(int(modL.argmax()))
 
40
  m = compare_logits(rawL, modL)
41
-
42
  headline = f"variance ▼ {int(m['var_drop']*100)} % | KL {m['kl']:.2f} | top-1 {'✔' if m['top1'] else '✘'}"
43
 
44
- return (
45
- raw_txt,
46
- mod_txt,
47
- headline,
48
- )
49
 
 
 
 
 
50
 
51
- with gr.Blocks(title="WFGY 1-click Variance Gate") as demo:
52
- gr.Markdown("## 🧠 WFGY 1-click Variance Gate\nTurn GPT-2 into a calmer thinker. Move the slider → watch variance dive.")
 
 
53
 
54
- prompt = gr.Textbox(label="Prompt")
55
- enable = gr.Checkbox(value=True, label="Enable WFGY")
56
- boost = gr.Slider(0.5, 3.0, value=1.0, label="Demo Boost (higher → bigger effect)")
57
-
58
- run_btn = gr.Button("Run", variant="primary")
59
 
60
  with gr.Row():
61
  out_raw = gr.Textbox(label="Raw GPT-2")
@@ -64,11 +58,12 @@ with gr.Blocks(title="WFGY 1-click Variance Gate") as demo:
64
  headline = gr.Markdown("")
65
 
66
  run_btn.click(
67
- run,
68
  inputs=[prompt, enable, boost],
69
  outputs=[out_raw, out_mod, headline],
70
  )
 
 
71
  if __name__ == "__main__":
72
- demo = build_ui() # 你的 build_ui() 保持不變
73
- demo.queue() # 不帶任何參數 → 用預設 concurrency=2
74
- demo.launch() # Space 入口
 
2
  HF Space · WFGY 1-click Variance Gate
3
  """
4
 
5
+ import gradio as gr
6
+ import numpy as np
7
+ import torch
8
  from transformers import AutoModelForCausalLM, AutoTokenizer
9
  from wfgy_sdk import get_engine
10
  from wfgy_sdk.evaluator import compare_logits
11
 
12
+ # ---------- tiny demo backbone ----------
13
+ MODEL = "sshleifer/tiny-gpt2"
14
+ tokenizer = AutoTokenizer.from_pretrained(MODEL)
15
+ model = AutoModelForCausalLM.from_pretrained(MODEL)
16
+ ENGINE = get_engine()
17
 
18
+ # ---------- core inference ----------
19
+ def wfgy_run(prompt: str, enable: bool, boost: float):
20
  if not prompt.strip():
21
+ return "", "—", "Please enter a prompt."
22
 
23
+ # 1) raw logits from tiny-GPT-2
24
+ toks = tokenizer(prompt, return_tensors="pt")
25
+ rawL = model(**toks).logits[0, -1].detach().cpu().numpy()
26
 
27
+ # 2) dummy semantic vectors (demo only)
 
28
  G = np.random.randn(256).astype(np.float32)
29
+ I = (G + np.random.normal(scale=0.05, size=256).astype(np.float32)) * boost
30
 
31
+ # 3) WFGY gate
32
+ modL = ENGINE.run(I, G, rawL) if enable else rawL
 
 
 
 
 
 
 
33
 
34
+ # 4) text + metrics
35
+ raw_txt = prompt + tokenizer.decode(int(rawL.argmax()))
36
+ mod_txt = prompt + tokenizer.decode(int(modL.argmax()))
37
  m = compare_logits(rawL, modL)
 
38
  headline = f"variance ▼ {int(m['var_drop']*100)} % | KL {m['kl']:.2f} | top-1 {'✔' if m['top1'] else '✘'}"
39
 
40
+ return raw_txt, mod_txt, headline
 
 
 
 
41
 
42
+ # ---------- UI ----------
43
+ with gr.Blocks(title="WFGY 1-click Variance Gate", theme="soft") as demo:
44
+ gr.Markdown("## 🧠 **WFGY 1-click Variance Gate**
45
+ Turn GPT-2 into a calmer thinker. Move the slider → watch variance dive.\n")
46
 
47
+ prompt = gr.Textbox(label="Prompt")
48
+ enable = gr.Checkbox(value=True, label="Enable WFGY")
49
+ boost = gr.Slider(0.5, 3.0, value=1.0, step=0.1,
50
+ label="Demo Boost (higher → bigger effect)")
51
 
52
+ run_btn = gr.Button("Run", variant="primary")
 
 
 
 
53
 
54
  with gr.Row():
55
  out_raw = gr.Textbox(label="Raw GPT-2")
 
58
  headline = gr.Markdown("")
59
 
60
  run_btn.click(
61
+ wfgy_run,
62
  inputs=[prompt, enable, boost],
63
  outputs=[out_raw, out_mod, headline],
64
  )
65
+
66
+ # ---------- launch ----------
67
  if __name__ == "__main__":
68
+ demo.queue() # default concurrency = 2
69
+ demo.launch()