Spaces:
Sleeping
Sleeping
Drop token_type_ids; DistilBERT doesn't accept them
Browse files
app.py
CHANGED
|
@@ -13,18 +13,19 @@ person their proposal will fail. So instead it reports:
|
|
| 13 |
"""
|
| 14 |
|
| 15 |
import json
|
|
|
|
| 16 |
import gradio as gr
|
| 17 |
import numpy as np
|
| 18 |
import torch
|
| 19 |
import torch.nn.functional as F
|
| 20 |
from datasets import load_dataset
|
| 21 |
-
from transformers import
|
| 22 |
|
| 23 |
MODEL_ID = "gramajo/nouns-proposal-predictor"
|
| 24 |
DATASET_ID = "gramajo/nouns-proposals"
|
| 25 |
-
BREAK_ID = 786
|
| 26 |
MAX_LENGTH = 512
|
| 27 |
-
AUC = 0.65
|
| 28 |
ACC, BASELINE = 0.589, 0.502
|
| 29 |
|
| 30 |
print("Loading model...")
|
|
@@ -54,9 +55,13 @@ def _encode(texts, batch=16):
|
|
| 54 |
for i in range(0, len(texts), batch):
|
| 55 |
chunk = texts[i : i + batch]
|
| 56 |
enc = tokenizer(
|
| 57 |
-
chunk,
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
| 59 |
)
|
|
|
|
| 60 |
with torch.no_grad():
|
| 61 |
out = model(**enc, output_hidden_states=True)
|
| 62 |
probs.extend(F.softmax(out.logits, dim=-1)[:, 1].tolist())
|
|
@@ -70,7 +75,9 @@ _texts = [(r["title"] + " " + (r.get("description") or ""))[:2000] for r in CORP
|
|
| 70 |
CORPUS_PROBS, CORPUS_EMBS = _encode(_texts)
|
| 71 |
_norms = np.linalg.norm(CORPUS_EMBS, axis=1, keepdims=True)
|
| 72 |
CORPUS_EMBS_N = CORPUS_EMBS / np.clip(_norms, 1e-9, None)
|
| 73 |
-
POST_PROBS = np.array(
|
|
|
|
|
|
|
| 74 |
print("Ready.")
|
| 75 |
|
| 76 |
|
|
@@ -125,14 +132,18 @@ def analyze(title, description):
|
|
| 125 |
)
|
| 126 |
|
| 127 |
md.append("\n## Most similar past proposals\n")
|
| 128 |
-
md.append(
|
|
|
|
|
|
|
| 129 |
md.append("| outcome | similarity | proposal |\n|---|---|---|\n")
|
| 130 |
for i in top:
|
| 131 |
r = CORPUS[i]
|
| 132 |
outcome = "✅ passed" if r["passed"] else "❌ failed"
|
| 133 |
era = "post-BreakEven" if int(r["id"]) >= BREAK_ID else "pre-BreakEven"
|
| 134 |
t = r["title"][:70]
|
| 135 |
-
md.append(
|
|
|
|
|
|
|
| 136 |
|
| 137 |
md.append(
|
| 138 |
f"\n---\n\n*Model AUC {AUC:.2f} — it ranks proposals better than chance, but it "
|
|
@@ -191,9 +202,14 @@ with gr.Blocks(title="Nouns Proposal Check") as demo:
|
|
| 191 |
|
| 192 |
with gr.Row():
|
| 193 |
with gr.Column(scale=1):
|
| 194 |
-
title = gr.Textbox(
|
| 195 |
-
|
| 196 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
btn = gr.Button("Compare against past proposals", variant="primary")
|
| 198 |
with gr.Column(scale=1):
|
| 199 |
out = gr.Markdown("Enter a proposal to see how it compares.")
|
|
|
|
| 13 |
"""
|
| 14 |
|
| 15 |
import json
|
| 16 |
+
|
| 17 |
import gradio as gr
|
| 18 |
import numpy as np
|
| 19 |
import torch
|
| 20 |
import torch.nn.functional as F
|
| 21 |
from datasets import load_dataset
|
| 22 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
| 23 |
|
| 24 |
MODEL_ID = "gramajo/nouns-proposal-predictor"
|
| 25 |
DATASET_ID = "gramajo/nouns-proposals"
|
| 26 |
+
BREAK_ID = 786 # BreakEven bloc regime change
|
| 27 |
MAX_LENGTH = 512
|
| 28 |
+
AUC = 0.65 # measured, run A (stratified random split)
|
| 29 |
ACC, BASELINE = 0.589, 0.502
|
| 30 |
|
| 31 |
print("Loading model...")
|
|
|
|
| 55 |
for i in range(0, len(texts), batch):
|
| 56 |
chunk = texts[i : i + batch]
|
| 57 |
enc = tokenizer(
|
| 58 |
+
chunk,
|
| 59 |
+
padding=True,
|
| 60 |
+
truncation=True,
|
| 61 |
+
max_length=MAX_LENGTH,
|
| 62 |
+
return_tensors="pt",
|
| 63 |
)
|
| 64 |
+
enc.pop("token_type_ids", None) # DistilBERT has no segment embeddings
|
| 65 |
with torch.no_grad():
|
| 66 |
out = model(**enc, output_hidden_states=True)
|
| 67 |
probs.extend(F.softmax(out.logits, dim=-1)[:, 1].tolist())
|
|
|
|
| 75 |
CORPUS_PROBS, CORPUS_EMBS = _encode(_texts)
|
| 76 |
_norms = np.linalg.norm(CORPUS_EMBS, axis=1, keepdims=True)
|
| 77 |
CORPUS_EMBS_N = CORPUS_EMBS / np.clip(_norms, 1e-9, None)
|
| 78 |
+
POST_PROBS = np.array(
|
| 79 |
+
[CORPUS_PROBS[i] for i, r in enumerate(CORPUS) if int(r["id"]) >= BREAK_ID]
|
| 80 |
+
)
|
| 81 |
print("Ready.")
|
| 82 |
|
| 83 |
|
|
|
|
| 132 |
)
|
| 133 |
|
| 134 |
md.append("\n## Most similar past proposals\n")
|
| 135 |
+
md.append(
|
| 136 |
+
"Read these. They're more informative than any score this tool prints.\n\n"
|
| 137 |
+
)
|
| 138 |
md.append("| outcome | similarity | proposal |\n|---|---|---|\n")
|
| 139 |
for i in top:
|
| 140 |
r = CORPUS[i]
|
| 141 |
outcome = "✅ passed" if r["passed"] else "❌ failed"
|
| 142 |
era = "post-BreakEven" if int(r["id"]) >= BREAK_ID else "pre-BreakEven"
|
| 143 |
t = r["title"][:70]
|
| 144 |
+
md.append(
|
| 145 |
+
f"| {outcome} | {sims[i]:.2f} | **#{r['id']}** {t} <br/><sub>{era}</sub> |\n"
|
| 146 |
+
)
|
| 147 |
|
| 148 |
md.append(
|
| 149 |
f"\n---\n\n*Model AUC {AUC:.2f} — it ranks proposals better than chance, but it "
|
|
|
|
| 202 |
|
| 203 |
with gr.Row():
|
| 204 |
with gr.Column(scale=1):
|
| 205 |
+
title = gr.Textbox(
|
| 206 |
+
label="Proposal title", placeholder="Nouns × ...", lines=1
|
| 207 |
+
)
|
| 208 |
+
desc = gr.Textbox(
|
| 209 |
+
label="Proposal description",
|
| 210 |
+
lines=14,
|
| 211 |
+
placeholder="Paste the full proposal body here...",
|
| 212 |
+
)
|
| 213 |
btn = gr.Button("Compare against past proposals", variant="primary")
|
| 214 |
with gr.Column(scale=1):
|
| 215 |
out = gr.Markdown("Enter a proposal to see how it compares.")
|