Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,32 +1,31 @@
|
|
1 |
-
import
|
2 |
from wfgy_sdk.evaluator import compare_logits
|
3 |
from wfgy_sdk.visual import plot_histogram
|
4 |
|
5 |
-
import
|
|
|
6 |
from transformers import AutoModelForCausalLM, AutoTokenizer, set_seed
|
7 |
|
8 |
-
#
|
9 |
-
|
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 |
-
#
|
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
|
40 |
-
top1
|
41 |
-
|
42 |
-
|
|
|
|
|
43 |
|
44 |
-
# histogram
|
45 |
-
|
46 |
-
|
47 |
-
|
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,
|
56 |
|
57 |
-
except Exception as
|
58 |
-
err = f"<b style='color:red'>Error:</b> {
|
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 β
|
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 |
-
|
99 |
-
|
100 |
|
101 |
runbtn.click(wfgy_pipeline, [prompt, enable],
|
102 |
-
[raw_box, mod_box,
|
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> | "
|
42 |
+
f"<b>KL {m['kl_divergence']:.2f}</b> | 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**
|