Mirror of anon-review-meld-2026/meld@8990324
Browse files- config.json +85 -0
- meld.py +256 -0
- meld_config.json +113 -0
- model.safetensors +3 -0
- tokenizer.json +0 -0
- tokenizer_config.json +17 -0
config.json
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"architectures": [
|
| 3 |
+
"ModernBertForMaskedLM"
|
| 4 |
+
],
|
| 5 |
+
"attention_bias": false,
|
| 6 |
+
"attention_dropout": 0.0,
|
| 7 |
+
"bos_token_id": 50281,
|
| 8 |
+
"causal_mask": false,
|
| 9 |
+
"classifier_activation": "gelu",
|
| 10 |
+
"classifier_bias": false,
|
| 11 |
+
"classifier_dropout": 0.0,
|
| 12 |
+
"classifier_pooling": "mean",
|
| 13 |
+
"cls_token_id": 50281,
|
| 14 |
+
"decoder_bias": true,
|
| 15 |
+
"deterministic_flash_attn": false,
|
| 16 |
+
"dtype": "float32",
|
| 17 |
+
"embedding_dropout": 0.0,
|
| 18 |
+
"eos_token_id": 50282,
|
| 19 |
+
"global_attn_every_n_layers": 3,
|
| 20 |
+
"gradient_checkpointing": false,
|
| 21 |
+
"hidden_activation": "gelu",
|
| 22 |
+
"hidden_size": 1792,
|
| 23 |
+
"initializer_cutoff_factor": 2.0,
|
| 24 |
+
"initializer_range": 0.02,
|
| 25 |
+
"intermediate_size": 3840,
|
| 26 |
+
"is_causal": false,
|
| 27 |
+
"layer_norm_eps": 1e-05,
|
| 28 |
+
"layer_types": [
|
| 29 |
+
"full_attention",
|
| 30 |
+
"sliding_attention",
|
| 31 |
+
"sliding_attention",
|
| 32 |
+
"full_attention",
|
| 33 |
+
"sliding_attention",
|
| 34 |
+
"sliding_attention",
|
| 35 |
+
"full_attention",
|
| 36 |
+
"sliding_attention",
|
| 37 |
+
"sliding_attention",
|
| 38 |
+
"full_attention",
|
| 39 |
+
"sliding_attention",
|
| 40 |
+
"sliding_attention",
|
| 41 |
+
"full_attention",
|
| 42 |
+
"sliding_attention",
|
| 43 |
+
"sliding_attention",
|
| 44 |
+
"full_attention",
|
| 45 |
+
"sliding_attention",
|
| 46 |
+
"sliding_attention",
|
| 47 |
+
"full_attention",
|
| 48 |
+
"sliding_attention",
|
| 49 |
+
"sliding_attention",
|
| 50 |
+
"full_attention",
|
| 51 |
+
"sliding_attention",
|
| 52 |
+
"sliding_attention",
|
| 53 |
+
"full_attention",
|
| 54 |
+
"sliding_attention",
|
| 55 |
+
"sliding_attention",
|
| 56 |
+
"full_attention"
|
| 57 |
+
],
|
| 58 |
+
"local_attention": 128,
|
| 59 |
+
"max_position_embeddings": 7999,
|
| 60 |
+
"mlp_bias": false,
|
| 61 |
+
"mlp_dropout": 0.0,
|
| 62 |
+
"model_type": "modernbert",
|
| 63 |
+
"norm_bias": false,
|
| 64 |
+
"norm_eps": 1e-05,
|
| 65 |
+
"num_attention_heads": 28,
|
| 66 |
+
"num_hidden_layers": 28,
|
| 67 |
+
"pad_token_id": 50283,
|
| 68 |
+
"position_embedding_type": "sans_pos",
|
| 69 |
+
"rope_parameters": {
|
| 70 |
+
"full_attention": {
|
| 71 |
+
"rope_theta": 160000.0,
|
| 72 |
+
"rope_type": "default"
|
| 73 |
+
},
|
| 74 |
+
"sliding_attention": {
|
| 75 |
+
"rope_theta": 160000.0,
|
| 76 |
+
"rope_type": "default"
|
| 77 |
+
}
|
| 78 |
+
},
|
| 79 |
+
"sep_token_id": 50282,
|
| 80 |
+
"sparse_pred_ignore_index": -100,
|
| 81 |
+
"sparse_prediction": false,
|
| 82 |
+
"tie_word_embeddings": true,
|
| 83 |
+
"transformers_version": "5.8.0",
|
| 84 |
+
"vocab_size": 50368
|
| 85 |
+
}
|
meld.py
ADDED
|
@@ -0,0 +1,256 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Score text with MELD.
|
| 2 |
+
|
| 3 |
+
pip install torch transformers safetensors
|
| 4 |
+
python meld.py "Paste the text to check here." ["another text" ...]
|
| 5 |
+
python meld.py --file document.txt
|
| 6 |
+
|
| 7 |
+
Run it from the folder you downloaded this repository into, or pass
|
| 8 |
+
--model-dir. Long inputs are scored in consecutive windows and pooled once, so
|
| 9 |
+
a long document is scored the same way a short one is. Inputs are canonicalized
|
| 10 |
+
first (look-alike characters folded, invisible characters and markdown removed)
|
| 11 |
+
unless --raw is given.
|
| 12 |
+
"""
|
| 13 |
+
from __future__ import annotations
|
| 14 |
+
|
| 15 |
+
import argparse
|
| 16 |
+
import json
|
| 17 |
+
import math
|
| 18 |
+
import os
|
| 19 |
+
import re
|
| 20 |
+
import sys
|
| 21 |
+
import unicodedata
|
| 22 |
+
from functools import lru_cache
|
| 23 |
+
|
| 24 |
+
import torch
|
| 25 |
+
import torch.nn as nn
|
| 26 |
+
from safetensors.torch import load_file
|
| 27 |
+
from transformers import AutoConfig, AutoModel, AutoTokenizer
|
| 28 |
+
|
| 29 |
+
# ----------------------------------------------------------------- canonical form
|
| 30 |
+
_FENCE_LINE = re.compile(r"(?m)^[ \t]*```.*$")
|
| 31 |
+
_HEAD_MARK = re.compile(r"(?m)^[ \t]{0,3}#{1,6}[ \t]+")
|
| 32 |
+
_QUOTE_MARK = re.compile(r"(?m)^[ \t]{0,3}>[ \t]?")
|
| 33 |
+
_BULLET_MARK = re.compile(r"(?m)^[ \t]{0,3}(?:[-*+]|•|\U0001F539|✅|\U0001F4CC|➡️)[ \t]+")
|
| 34 |
+
_NUM_MARK = re.compile(r"(?m)^\d{1,3}[.)][ \t]+")
|
| 35 |
+
_TABLE_SEP = re.compile(r"(?m)^[ \t]{0,3}\|?[\s:|-]*-{2,}[\s:|-]*\|?[\s:|-]*$")
|
| 36 |
+
_EMPH = re.compile(r"(\*\*\*|\*\*|__)(?=\S)(.+?)(?<=\S)\1", re.S)
|
| 37 |
+
_INLINE_CODE = re.compile(r"`([^`\n]+)`")
|
| 38 |
+
_LINK = re.compile(r"\[([^\]\n]*)\]\([^)\n]*\)")
|
| 39 |
+
_WS = re.compile(r"[ \t]+")
|
| 40 |
+
_CELL_SPLIT = re.compile(r"(?<!\\)\|")
|
| 41 |
+
_ADDED = ("Overview", "Key points", "Section", "Point", "Detail", "Topic",
|
| 42 |
+
"Let me know if you'd like more detail.")
|
| 43 |
+
_HOMOGLYPH_FOLD = {
|
| 44 |
+
"а": "a", "А": "A", "Α": "A", "В": "B", "Β": "B",
|
| 45 |
+
"е": "e", "Е": "E", "Ε": "E", "с": "c", "р": "p",
|
| 46 |
+
"К": "K", "Κ": "K", "О": "O", "Ο": "O", "Р": "P",
|
| 47 |
+
"Ρ": "P", "М": "M", "Μ": "M", "Н": "H", "Η": "H",
|
| 48 |
+
"Т": "T", "Τ": "T", "Х": "X", "Χ": "X", "С": "C",
|
| 49 |
+
"у": "y", "о": "o", "х": "x", "І": "I", "Ι": "I",
|
| 50 |
+
"і": "i", "Ν": "N", "Ζ": "Z",
|
| 51 |
+
}
|
| 52 |
+
_INVISIBLE = dict.fromkeys((0x200B, 0x200C, 0x200D, 0x2060, 0xFEFF, 0x00AD, 0x180E), None)
|
| 53 |
+
_TYPO_FOLD = {
|
| 54 |
+
0x2010: "-", 0x2011: "-", 0x2212: "-",
|
| 55 |
+
0x00A0: " ", 0x202F: " ", 0x2007: " ", 0x2008: " ", 0x2009: " ", 0x200A: " ",
|
| 56 |
+
0x2002: " ", 0x2003: " ", 0x3000: " ",
|
| 57 |
+
0x2018: "'", 0x2019: "'", 0x201C: '"', 0x201D: '"', 0x2032: "'", 0x2033: '"',
|
| 58 |
+
}
|
| 59 |
+
_SURFACE_FOLD = {**_INVISIBLE, **_TYPO_FOLD}
|
| 60 |
+
_CONFUSABLE_CHARS = frozenset(_HOMOGLYPH_FOLD)
|
| 61 |
+
_TOKEN = re.compile(r"\S+")
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
@lru_cache(maxsize=8192)
|
| 65 |
+
def _blocks_fold(ch: str) -> bool:
|
| 66 |
+
if ch in _CONFUSABLE_CHARS or not ch.isalpha():
|
| 67 |
+
return False
|
| 68 |
+
try:
|
| 69 |
+
return unicodedata.name(ch).startswith("CYRILLIC SMALL ")
|
| 70 |
+
except ValueError:
|
| 71 |
+
return False
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
def _fold_token(tok: str) -> str:
|
| 75 |
+
if not any(c in _CONFUSABLE_CHARS for c in tok):
|
| 76 |
+
return tok
|
| 77 |
+
if any(_blocks_fold(c) for c in tok):
|
| 78 |
+
return tok
|
| 79 |
+
return "".join(_HOMOGLYPH_FOLD.get(c, c) for c in tok)
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
def fold_surface(text: str) -> str:
|
| 83 |
+
"""Delete invisible characters and fold mixed-script look-alikes. Idempotent."""
|
| 84 |
+
t = str(text or "").translate(_SURFACE_FOLD)
|
| 85 |
+
if not any(c in _CONFUSABLE_CHARS for c in t):
|
| 86 |
+
return t
|
| 87 |
+
return _TOKEN.sub(lambda m: _fold_token(m.group(0)), t)
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
def canonicalize(text: str) -> str:
|
| 91 |
+
"""Strip formatting, leaving a plain paragraph sequence. Idempotent."""
|
| 92 |
+
t = fold_surface(text)
|
| 93 |
+
t = _FENCE_LINE.sub("", t)
|
| 94 |
+
t = _LINK.sub(r"\1", t)
|
| 95 |
+
t = _INLINE_CODE.sub(r"\1", t)
|
| 96 |
+
for _ in range(3):
|
| 97 |
+
new = _EMPH.sub(lambda m: m.group(2), t)
|
| 98 |
+
if new == t:
|
| 99 |
+
break
|
| 100 |
+
t = new
|
| 101 |
+
out = []
|
| 102 |
+
for line in t.split("\n"):
|
| 103 |
+
if _TABLE_SEP.match(line):
|
| 104 |
+
continue
|
| 105 |
+
s = line.strip()
|
| 106 |
+
if s.startswith("|") and len(_CELL_SPLIT.split(s)) >= 3:
|
| 107 |
+
cells = [c.strip().replace(r"\|", "|") for c in _CELL_SPLIT.split(s.strip("|"))]
|
| 108 |
+
cells = [c for c in cells if c and c not in _ADDED]
|
| 109 |
+
line = " ".join(cells)
|
| 110 |
+
for mark in (_HEAD_MARK, _QUOTE_MARK, _BULLET_MARK, _NUM_MARK):
|
| 111 |
+
stripped = mark.sub("", line, count=1)
|
| 112 |
+
if stripped != line:
|
| 113 |
+
line = stripped
|
| 114 |
+
break
|
| 115 |
+
out.append(_WS.sub(" ", line).strip())
|
| 116 |
+
kept = []
|
| 117 |
+
for line in out:
|
| 118 |
+
bare = re.sub(r"\s*\d+\s*$", "", line).strip().rstrip(":")
|
| 119 |
+
if bare in _ADDED:
|
| 120 |
+
continue
|
| 121 |
+
kept.append(line)
|
| 122 |
+
paras, cur = [], []
|
| 123 |
+
for line in kept:
|
| 124 |
+
if line:
|
| 125 |
+
cur.append(line)
|
| 126 |
+
elif cur:
|
| 127 |
+
paras.append(" ".join(cur))
|
| 128 |
+
cur = []
|
| 129 |
+
if cur:
|
| 130 |
+
paras.append(" ".join(cur))
|
| 131 |
+
return "\n\n".join(p for p in paras if p.strip())
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
# ------------------------------------------------------------------------ model
|
| 135 |
+
class Meld(nn.Module):
|
| 136 |
+
def __init__(self, model_dir: str):
|
| 137 |
+
super().__init__()
|
| 138 |
+
self.cfg = json.load(open(os.path.join(model_dir, "meld_config.json")))
|
| 139 |
+
r, H = self.cfg["style_rank"], self.cfg["backbone_hidden_size"]
|
| 140 |
+
self.backbone = AutoModel.from_config(
|
| 141 |
+
AutoConfig.from_pretrained(model_dir), attn_implementation="sdpa")
|
| 142 |
+
self.style_proj = nn.Linear(H, r, bias=False)
|
| 143 |
+
self.style_ln = nn.LayerNorm(r)
|
| 144 |
+
self.human_anchors = nn.Parameter(torch.zeros(self.cfg["n_human_anchors"], r))
|
| 145 |
+
self.family_protos = nn.Parameter(torch.zeros(self.cfg["n_families"], r))
|
| 146 |
+
self.family_bias = nn.Parameter(torch.zeros(self.cfg["n_families"]))
|
| 147 |
+
self.log_tau = nn.Parameter(torch.zeros(()))
|
| 148 |
+
self.op_protos = nn.Parameter(torch.zeros(self.cfg["n_ops"], r))
|
| 149 |
+
self.op_bias = nn.Parameter(torch.zeros(self.cfg["n_ops"]))
|
| 150 |
+
self.load_state_dict(load_file(os.path.join(model_dir, "model.safetensors")), strict=True)
|
| 151 |
+
self.eval()
|
| 152 |
+
|
| 153 |
+
@staticmethod
|
| 154 |
+
def _sqdist(u, p):
|
| 155 |
+
return ((u * u).sum(-1, keepdim=True) - 2.0 * u @ p.t() + (p * p).sum(-1).view(1, 1, -1))
|
| 156 |
+
|
| 157 |
+
@torch.no_grad()
|
| 158 |
+
def token_scores(self, input_ids, attention_mask):
|
| 159 |
+
h = self.backbone(input_ids=input_ids, attention_mask=attention_mask).last_hidden_state.float()
|
| 160 |
+
u = self.style_ln(self.style_proj(h))
|
| 161 |
+
tau = self.log_tau.clamp(-4.0, 4.0).exp()
|
| 162 |
+
logit_h = -tau * self._sqdist(u, self.human_anchors)
|
| 163 |
+
logit_g = -tau * self._sqdist(u, self.family_protos) + self.family_bias.view(1, 1, -1)
|
| 164 |
+
if self.cfg.get("null_reduction", "lse") == "max":
|
| 165 |
+
human = logit_h.max(dim=-1, keepdim=True).values
|
| 166 |
+
else:
|
| 167 |
+
human = torch.logsumexp(logit_h, dim=-1, keepdim=True)
|
| 168 |
+
s_tok = (logit_g - human).clamp(-30.0, 30.0) # (B, L, G)
|
| 169 |
+
c_tok = self.cfg["tau_agg"] * torch.logsumexp(s_tok / self.cfg["tau_agg"], dim=-1)
|
| 170 |
+
o_tok = -tau * self._sqdist(u, self.op_protos) + self.op_bias.view(1, 1, -1)
|
| 171 |
+
return s_tok, c_tok, o_tok
|
| 172 |
+
|
| 173 |
+
|
| 174 |
+
def top_quantile_mean(x: torch.Tensor, rho: float) -> torch.Tensor:
|
| 175 |
+
"""Mean of the top ceil(rho * n) entries of x along dim 0."""
|
| 176 |
+
k = max(1, math.ceil(x.shape[0] * rho))
|
| 177 |
+
vals, _ = x.sort(dim=0, descending=True)
|
| 178 |
+
return vals[:k].mean(dim=0)
|
| 179 |
+
|
| 180 |
+
|
| 181 |
+
class Scorer:
|
| 182 |
+
def __init__(self, model_dir: str = ".", device: str | None = None, canonical: bool = True):
|
| 183 |
+
self.device = torch.device(device or ("cuda" if torch.cuda.is_available() else "cpu"))
|
| 184 |
+
self.model = Meld(model_dir).to(self.device)
|
| 185 |
+
self.tok = AutoTokenizer.from_pretrained(model_dir)
|
| 186 |
+
self.cfg = self.model.cfg
|
| 187 |
+
self.canonical = canonical
|
| 188 |
+
self.window = int(self.cfg["max_length"]) - 2
|
| 189 |
+
self.cap = int(self.cfg.get("cap_tokens", 16384))
|
| 190 |
+
self.rho = float(self.cfg["rho"])
|
| 191 |
+
self.threshold = float(self.cfg["score_offsets"]["overall"]["fpr_0.01"])
|
| 192 |
+
cls = self.tok.cls_token_id if self.tok.cls_token_id is not None else self.tok.bos_token_id
|
| 193 |
+
sep = self.tok.sep_token_id if self.tok.sep_token_id is not None else self.tok.eos_token_id
|
| 194 |
+
self.cls_id, self.sep_id = int(cls), int(sep)
|
| 195 |
+
self.pad_id = int(self.tok.pad_token_id if self.tok.pad_token_id is not None else 0)
|
| 196 |
+
|
| 197 |
+
@torch.no_grad()
|
| 198 |
+
def score(self, text: str, batch_size: int = 4) -> dict:
|
| 199 |
+
if self.canonical:
|
| 200 |
+
text = canonicalize(text)
|
| 201 |
+
ids = self.tok(text, add_special_tokens=False, truncation=False)["input_ids"]
|
| 202 |
+
truncated = len(ids) > self.cap
|
| 203 |
+
ids = ids[: self.cap]
|
| 204 |
+
if not ids:
|
| 205 |
+
raise ValueError("no scoreable tokens in input")
|
| 206 |
+
windows = [ids[i:i + self.window] for i in range(0, len(ids), self.window)]
|
| 207 |
+
c_parts, s_parts, o_parts = [], [], []
|
| 208 |
+
for b0 in range(0, len(windows), batch_size):
|
| 209 |
+
batch = windows[b0:b0 + batch_size]
|
| 210 |
+
width = max(len(w) for w in batch) + 2
|
| 211 |
+
inp = torch.full((len(batch), width), self.pad_id, dtype=torch.long)
|
| 212 |
+
att = torch.zeros((len(batch), width), dtype=torch.long)
|
| 213 |
+
for j, w in enumerate(batch):
|
| 214 |
+
inp[j, 0] = self.cls_id
|
| 215 |
+
inp[j, 1:1 + len(w)] = torch.tensor(w, dtype=torch.long)
|
| 216 |
+
inp[j, 1 + len(w)] = self.sep_id
|
| 217 |
+
att[j, : len(w) + 2] = 1
|
| 218 |
+
s_tok, c_tok, o_tok = self.model.token_scores(inp.to(self.device), att.to(self.device))
|
| 219 |
+
for j, w in enumerate(batch):
|
| 220 |
+
sl = slice(1, 1 + len(w)) # drop CLS/SEP/pad
|
| 221 |
+
c_parts.append(c_tok[j, sl].cpu())
|
| 222 |
+
s_parts.append(s_tok[j, sl].cpu())
|
| 223 |
+
o_parts.append(o_tok[j, sl].cpu())
|
| 224 |
+
c_tok = torch.cat(c_parts)
|
| 225 |
+
s_fam = top_quantile_mean(torch.cat(s_parts), self.rho)
|
| 226 |
+
s_op = top_quantile_mean(torch.cat(o_parts), self.rho)
|
| 227 |
+
s = float(top_quantile_mean(c_tok, self.rho))
|
| 228 |
+
return {
|
| 229 |
+
"score": s,
|
| 230 |
+
"p_ai": 1.0 / (1.0 + math.exp(-s)),
|
| 231 |
+
"flagged": s > self.threshold,
|
| 232 |
+
"threshold_fpr_0.01": self.threshold,
|
| 233 |
+
"family": self.cfg["families"][int(s_fam.argmax())],
|
| 234 |
+
"task": self.cfg["ops"][int(s_op.argmax())],
|
| 235 |
+
"n_tokens": int(c_tok.numel()),
|
| 236 |
+
"truncated": truncated,
|
| 237 |
+
}
|
| 238 |
+
|
| 239 |
+
|
| 240 |
+
def main():
|
| 241 |
+
ap = argparse.ArgumentParser(description="Score text with MELD.")
|
| 242 |
+
ap.add_argument("texts", nargs="*", help="texts to score")
|
| 243 |
+
ap.add_argument("--file", action="append", default=[], help="text file to score (repeatable)")
|
| 244 |
+
ap.add_argument("--model-dir", default=os.path.dirname(os.path.abspath(__file__)))
|
| 245 |
+
ap.add_argument("--raw", action="store_true", help="score the text as given, without canonicalization")
|
| 246 |
+
a = ap.parse_args()
|
| 247 |
+
inputs = list(a.texts) + [open(f, encoding="utf-8").read() for f in a.file]
|
| 248 |
+
if not inputs:
|
| 249 |
+
ap.error("give at least one text or --file")
|
| 250 |
+
scorer = Scorer(a.model_dir, canonical=not a.raw)
|
| 251 |
+
for text in inputs:
|
| 252 |
+
print(json.dumps(scorer.score(text)))
|
| 253 |
+
|
| 254 |
+
|
| 255 |
+
if __name__ == "__main__":
|
| 256 |
+
sys.exit(main())
|
meld_config.json
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"model_type": "meld",
|
| 3 |
+
"architecture": "MeldV7Detector",
|
| 4 |
+
"version": "v8",
|
| 5 |
+
"backbone": "jhu-clsp/ettin-encoder-1b",
|
| 6 |
+
"backbone_hidden_size": 1792,
|
| 7 |
+
"style_rank": 48,
|
| 8 |
+
"n_human_anchors": 8,
|
| 9 |
+
"n_families": 7,
|
| 10 |
+
"families": [
|
| 11 |
+
"openai_gpt",
|
| 12 |
+
"anthropic_claude",
|
| 13 |
+
"google_gemini_gemma",
|
| 14 |
+
"meta_llama",
|
| 15 |
+
"mistral",
|
| 16 |
+
"qwen",
|
| 17 |
+
"others"
|
| 18 |
+
],
|
| 19 |
+
"n_ops": 6,
|
| 20 |
+
"ops": [
|
| 21 |
+
"generate",
|
| 22 |
+
"polish_formalize",
|
| 23 |
+
"paraphrase_rewrite",
|
| 24 |
+
"humanize_mimic",
|
| 25 |
+
"summarize",
|
| 26 |
+
"expand"
|
| 27 |
+
],
|
| 28 |
+
"rho": 0.25,
|
| 29 |
+
"tau_agg": 1.0,
|
| 30 |
+
"max_length": 2048,
|
| 31 |
+
"backbone_max_length": 8192,
|
| 32 |
+
"cap_tokens": 16384,
|
| 33 |
+
"null_reduction": "max",
|
| 34 |
+
"score_offsets": {
|
| 35 |
+
"overall": {
|
| 36 |
+
"fpr_0.01": 2.008819580078125,
|
| 37 |
+
"fpr_0.05": -0.4406634569168091,
|
| 38 |
+
"fpr_0.1": -1.7987771034240723
|
| 39 |
+
},
|
| 40 |
+
"n_human": 22190,
|
| 41 |
+
"strata": {
|
| 42 |
+
"consumer_opinion": {
|
| 43 |
+
"n": 2490,
|
| 44 |
+
"fpr_0.01": 1.5814101696014404,
|
| 45 |
+
"fpr_0.05": -0.3111945688724518,
|
| 46 |
+
"fpr_0.1": -1.286853551864624
|
| 47 |
+
},
|
| 48 |
+
"creative": {
|
| 49 |
+
"n": 2500,
|
| 50 |
+
"fpr_0.01": 0.45580801367759705,
|
| 51 |
+
"fpr_0.05": -2.677462100982666,
|
| 52 |
+
"fpr_0.1": -3.904209613800049
|
| 53 |
+
},
|
| 54 |
+
"general_web": {
|
| 55 |
+
"n": 2500,
|
| 56 |
+
"fpr_0.01": 1.5588948726654053,
|
| 57 |
+
"fpr_0.05": -1.170554280281067,
|
| 58 |
+
"fpr_0.1": -2.748509168624878
|
| 59 |
+
},
|
| 60 |
+
"news": {
|
| 61 |
+
"n": 2500,
|
| 62 |
+
"fpr_0.01": 2.726632833480835,
|
| 63 |
+
"fpr_0.05": 1.2478327751159668,
|
| 64 |
+
"fpr_0.1": 0.1839705854654312
|
| 65 |
+
},
|
| 66 |
+
"peer_review": {
|
| 67 |
+
"n": 2500,
|
| 68 |
+
"fpr_0.01": -4.437159061431885,
|
| 69 |
+
"fpr_0.05": -5.963565826416016,
|
| 70 |
+
"fpr_0.1": -6.306556701660156
|
| 71 |
+
},
|
| 72 |
+
"reference_howto": {
|
| 73 |
+
"n": 2500,
|
| 74 |
+
"fpr_0.01": -1.06562077999115,
|
| 75 |
+
"fpr_0.05": -3.6017556190490723,
|
| 76 |
+
"fpr_0.1": -4.638963222503662
|
| 77 |
+
},
|
| 78 |
+
"scientific_paper": {
|
| 79 |
+
"n": 2500,
|
| 80 |
+
"fpr_0.01": 2.3307437896728516,
|
| 81 |
+
"fpr_0.05": 0.10951182246208191,
|
| 82 |
+
"fpr_0.1": -1.107822299003601
|
| 83 |
+
},
|
| 84 |
+
"social_qa_chat": {
|
| 85 |
+
"n": 2490,
|
| 86 |
+
"fpr_0.01": 2.3400607109069824,
|
| 87 |
+
"fpr_0.05": -0.013203833252191544,
|
| 88 |
+
"fpr_0.1": -0.9907770752906799
|
| 89 |
+
},
|
| 90 |
+
"student_essay": {
|
| 91 |
+
"n": 2210,
|
| 92 |
+
"fpr_0.01": 3.0058677196502686,
|
| 93 |
+
"fpr_0.05": 0.2508733570575714,
|
| 94 |
+
"fpr_0.1": -1.1999095678329468
|
| 95 |
+
}
|
| 96 |
+
}
|
| 97 |
+
},
|
| 98 |
+
"released_step": 15629,
|
| 99 |
+
"checkpoint_sha12": "2d6ec6314168",
|
| 100 |
+
"scoring_contract": {
|
| 101 |
+
"schema": "meld-canonical-scorer-v1",
|
| 102 |
+
"window_tokens": 2048,
|
| 103 |
+
"content_tokens_per_window": 2046,
|
| 104 |
+
"window_overlap": "none",
|
| 105 |
+
"fresh_special_tokens_per_window": true,
|
| 106 |
+
"special_tokens_excluded": true,
|
| 107 |
+
"precision": "fp32",
|
| 108 |
+
"autocast": "disabled",
|
| 109 |
+
"input_cap_tokens": 16384,
|
| 110 |
+
"pooling": "top_quantile_mean_over_all_windows_jointly",
|
| 111 |
+
"rho": 0.25
|
| 112 |
+
}
|
| 113 |
+
}
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:86700f566059951fc9326a6b04234256d31691b61b4c0a738422616f0d62d303
|
| 3 |
+
size 4112570616
|
tokenizer.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
tokenizer_config.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"backend": "tokenizers",
|
| 3 |
+
"clean_up_tokenization_spaces": true,
|
| 4 |
+
"cls_token": "[CLS]",
|
| 5 |
+
"is_local": false,
|
| 6 |
+
"local_files_only": false,
|
| 7 |
+
"mask_token": "[MASK]",
|
| 8 |
+
"model_input_names": [
|
| 9 |
+
"input_ids",
|
| 10 |
+
"attention_mask"
|
| 11 |
+
],
|
| 12 |
+
"model_max_length": 8192,
|
| 13 |
+
"pad_token": "[PAD]",
|
| 14 |
+
"sep_token": "[SEP]",
|
| 15 |
+
"tokenizer_class": "TokenizersBackend",
|
| 16 |
+
"unk_token": "[UNK]"
|
| 17 |
+
}
|