Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,69 +1,54 @@
|
|
1 |
"""
|
2 |
-
HF Space
|
3 |
"""
|
4 |
-
|
5 |
-
import gradio as gr
|
6 |
-
import numpy as np
|
7 |
import torch
|
8 |
-
from transformers import
|
|
|
|
|
|
|
9 |
from wfgy_sdk import get_engine
|
10 |
from wfgy_sdk.evaluator import compare_logits
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
ENGINE = get_engine()
|
17 |
|
18 |
-
|
19 |
-
def wfgy_run(prompt: str, enable: bool, boost: float):
|
20 |
if not prompt.strip():
|
21 |
-
return "
|
22 |
|
23 |
-
|
24 |
-
|
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 =
|
30 |
|
31 |
-
# 3) WFGY gate
|
32 |
modL = ENGINE.run(I, G, rawL) if enable else rawL
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
mod_txt = prompt + tokenizer.decode(int(modL.argmax()))
|
37 |
m = compare_logits(rawL, modL)
|
38 |
-
headline = f"variance ▼ {int(m['var_drop']*100)} %
|
39 |
-
|
40 |
return raw_txt, mod_txt, headline
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
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 |
-
|
56 |
-
|
57 |
-
|
58 |
-
headline = gr.Markdown("")
|
59 |
|
60 |
-
run_btn.click(
|
61 |
-
|
62 |
-
|
63 |
-
outputs=[out_raw, out_mod, headline],
|
64 |
-
)
|
65 |
|
66 |
-
# ---------- launch ----------
|
67 |
if __name__ == "__main__":
|
68 |
-
demo.queue()
|
69 |
-
demo.launch()
|
|
|
1 |
"""
|
2 |
+
HF Space – WFGY 1-click Variance Gate
|
3 |
"""
|
4 |
+
import os, sys, numpy as np, gradio as gr
|
|
|
|
|
5 |
import torch
|
6 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
7 |
+
# ensure repo root (so wfgy_sdk import works)
|
8 |
+
sys.path.append(os.path.dirname(__file__))
|
9 |
+
|
10 |
from wfgy_sdk import get_engine
|
11 |
from wfgy_sdk.evaluator import compare_logits
|
12 |
|
13 |
+
MODEL = "sshleifer/tiny-gpt2"
|
14 |
+
tok = AutoTokenizer.from_pretrained(MODEL)
|
15 |
+
mdl = AutoModelForCausalLM.from_pretrained(MODEL)
|
16 |
+
ENGINE = get_engine()
|
|
|
17 |
|
18 |
+
def run(prompt, enable, boost):
|
|
|
19 |
if not prompt.strip():
|
20 |
+
return "-", "-", "⚠ Empty prompt"
|
21 |
|
22 |
+
toks = tok(prompt, return_tensors="pt")
|
23 |
+
rawL = mdl(**toks).logits[0, -1].detach().cpu().numpy()
|
|
|
24 |
|
|
|
25 |
G = np.random.randn(256).astype(np.float32)
|
26 |
+
I = G + np.random.normal(scale=0.05, size=256).astype(np.float32)
|
27 |
|
|
|
28 |
modL = ENGINE.run(I, G, rawL) if enable else rawL
|
29 |
|
30 |
+
raw_txt = prompt + tok.decode(int(rawL.argmax()))
|
31 |
+
mod_txt = prompt + tok.decode(int(modL.argmax()))
|
|
|
32 |
m = compare_logits(rawL, modL)
|
33 |
+
headline = f"variance ▼ {int(m['var_drop']*100)} % | KL {m['kl']:.2f} | top-1 {'✔' if m['top1'] else '✘'}"
|
|
|
34 |
return raw_txt, mod_txt, headline
|
35 |
|
36 |
+
with gr.Blocks(title="WFGY 1-click Variance Gate") as demo:
|
37 |
+
gr.Markdown("## 🧠 WFGY 1-click Variance Gate\n"
|
38 |
+
"Turn GPT-2 into a calmer thinker. Move the slider → watch variance dive.")
|
39 |
+
prompt = gr.Textbox(label="Prompt")
|
40 |
+
enable = gr.Checkbox(True, label="Enable WFGY")
|
41 |
+
boost = gr.Slider(0.5, 3.0, 1.0, step=0.1,
|
42 |
+
label="Demo Boost (placeholder – effect fixed in tiny demo)")
|
|
|
|
|
|
|
43 |
run_btn = gr.Button("Run", variant="primary")
|
|
|
44 |
with gr.Row():
|
45 |
+
raw_box = gr.Textbox(label="Raw GPT-2")
|
46 |
+
mod_box = gr.Textbox(label="After WFGY")
|
47 |
+
headline = gr.Markdown()
|
|
|
48 |
|
49 |
+
run_btn.click(run,
|
50 |
+
inputs=[prompt, enable, boost],
|
51 |
+
outputs=[raw_box, mod_box, headline])
|
|
|
|
|
52 |
|
|
|
53 |
if __name__ == "__main__":
|
54 |
+
demo.queue().launch() # no custom arguments
|
|