Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,66 +1,72 @@
|
|
1 |
-
|
2 |
-
|
|
|
|
|
|
|
|
|
|
|
3 |
from wfgy_sdk.evaluator import compare_logits
|
4 |
-
from wfgy_sdk.visual import plot_histogram
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
I = G + np.random.normal(scale=boost, size=256)
|
14 |
|
15 |
-
#
|
16 |
-
|
|
|
17 |
|
18 |
-
if
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
23 |
else:
|
24 |
-
|
25 |
|
26 |
-
raw_txt = prompt +
|
27 |
-
mod_txt = prompt +
|
28 |
-
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
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("
|
40 |
-
|
41 |
-
prompt
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
run_btn = gr.Button("Run", variant="primary")
|
47 |
|
48 |
with gr.Row():
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
run_btn.click(
|
55 |
-
|
56 |
-
|
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 |
-
|
66 |
-
demo.launch()
|
|
|
1 |
+
"""
|
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")
|
62 |
+
out_mod = gr.Textbox(label="After WFGY")
|
63 |
+
|
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 |
|
72 |
+
demo.queue(concurrency_count=2).launch()
|
|