Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,35 +6,34 @@ WFGY Space β tiny-GPT-2 variance-gate demo
|
|
| 6 |
import io, numpy as np, pandas as pd, gradio as gr
|
| 7 |
from PIL import Image
|
| 8 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 9 |
-
|
| 10 |
from wfgy_sdk import get_engine
|
| 11 |
from wfgy_sdk.evaluator import compare_logits, plot_histogram, softmax
|
| 12 |
|
| 13 |
-
# tiny model
|
| 14 |
tok = AutoTokenizer.from_pretrained("sshleifer/tiny-gpt2")
|
| 15 |
mdl = AutoModelForCausalLM.from_pretrained("sshleifer/tiny-gpt2")
|
| 16 |
eng = get_engine()
|
| 17 |
|
| 18 |
-
# paper benchmarks
|
| 19 |
bench = pd.DataFrame({
|
| 20 |
"Benchmark": ["MMLU","GSM8K","BBH","MathBench","TruthfulQA",
|
| 21 |
"XNLI","MLQA","LongBench","VQAv2","OK-VQA"],
|
| 22 |
"Baseline": [61,78,79.3,72.2,62.4,59.5,78.1,51.4,69.1,65.7],
|
| 23 |
"WFGY": [89.8,98.7,100.7,87.4,90.4,77.3,106.6,69.6,86.6,86.8]
|
| 24 |
})
|
| 25 |
-
bench["Abs_gain"] = (bench["WFGY"]-bench["Baseline"]).round(1)
|
| 26 |
-
bench["Rel_gain%"] = ((bench["Abs_gain"]/bench["Baseline"])*100).round(0)
|
| 27 |
bench_style = (
|
| 28 |
bench.style
|
| 29 |
.background_gradient(cmap="Greens", subset=["Abs_gain","Rel_gain%"])
|
| 30 |
.format({"Abs_gain":"{:.1f}","Rel_gain%":"{:.0f}"})
|
| 31 |
)
|
| 32 |
|
| 33 |
-
# marketing banner
|
| 34 |
-
|
| 35 |
**π WFGY: One Click to Activate the AI Taiji Cycle**
|
| 36 |
|
| 37 |
-
**π Semantic Accuracy β 22.4 %
|
| 38 |
|
| 39 |
_No beliefs. Only experiments._
|
| 40 |
WFGY 1.0 has already proven itself.
|
|
@@ -51,10 +50,10 @@ Prompt examples: *TBD*
|
|
| 51 |
---
|
| 52 |
|
| 53 |
π **Star Reminder** β [Star the repo](https://github.com/onestardao/WFGY)
|
| 54 |
-
_10 k β before 2025-08-01 unlocks WFGY 2.0
|
| 55 |
"""
|
| 56 |
|
| 57 |
-
#
|
| 58 |
def run(prompt: str):
|
| 59 |
prompt = prompt.strip()
|
| 60 |
if not prompt:
|
|
@@ -65,8 +64,8 @@ def run(prompt: str):
|
|
| 65 |
I, G = np.random.randn(2, 256).astype(np.float32)
|
| 66 |
mod_L = eng.run(I, G, raw_L)
|
| 67 |
|
| 68 |
-
m
|
| 69 |
-
|
| 70 |
|
| 71 |
def top5(logits):
|
| 72 |
p = softmax(logits)
|
|
@@ -79,11 +78,11 @@ def run(prompt: str):
|
|
| 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,
|
| 83 |
|
| 84 |
-
# UI
|
| 85 |
with gr.Blocks(title="WFGY variance-gate demo") as demo:
|
| 86 |
-
gr.Markdown(
|
| 87 |
|
| 88 |
prompt = gr.Textbox(label="Prompt", value="Explain SchrΓΆdinger's cat")
|
| 89 |
btn = gr.Button("π Run")
|
|
|
|
| 6 |
import io, numpy as np, pandas as pd, gradio as gr
|
| 7 |
from PIL import Image
|
| 8 |
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"],
|
| 21 |
"Baseline": [61,78,79.3,72.2,62.4,59.5,78.1,51.4,69.1,65.7],
|
| 22 |
"WFGY": [89.8,98.7,100.7,87.4,90.4,77.3,106.6,69.6,86.6,86.8]
|
| 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 |
|
| 36 |
+
**π Semantic Accuracy β 22.4 %β|βReasoning Success β 42.1 %β|βStability β 3.6 Γ**
|
| 37 |
|
| 38 |
_No beliefs. Only experiments._
|
| 39 |
WFGY 1.0 has already proven itself.
|
|
|
|
| 50 |
---
|
| 51 |
|
| 52 |
π **Star Reminder** β [Star the repo](https://github.com/onestardao/WFGY)
|
| 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:
|
|
|
|
| 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)
|
|
|
|
| 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 |
|
| 87 |
prompt = gr.Textbox(label="Prompt", value="Explain SchrΓΆdinger's cat")
|
| 88 |
btn = gr.Button("π Run")
|