update to modern bert
Browse files
README.md
CHANGED
|
@@ -7,34 +7,33 @@ tags:
|
|
| 7 |
- nli
|
| 8 |
- cross-encoder
|
| 9 |
- intent-routing
|
| 10 |
-
-
|
| 11 |
pipeline_tag: zero-shot-classification
|
| 12 |
widget:
|
| 13 |
- text: Generate a formal project proposal in Word format.
|
| 14 |
-
candidate_labels: a Word document creation request, an Excel spreadsheet creation
|
| 15 |
-
request, a PowerPoint presentation deck creation request, a PDF document analysis
|
| 16 |
-
query, general chitchat
|
| 17 |
---
|
| 18 |
|
| 19 |
-
# MarkOne
|
| 20 |
|
| 21 |
-
**MarkOne** is a high-precision, zero-shot NLI cross-encoder model
|
| 22 |
|
| 23 |
-
It evaluates user prompts against arbitrary numbers of candidate choice labels (N = 3, 6, 8,..) by computing sequence pair entailment probabilities
|
| 24 |
|
| 25 |
---
|
| 26 |
|
| 27 |
-
##
|
| 28 |
|
|
|
|
|
|
|
| 29 |
- **Dynamic Choice Counts:** Handles variable numbers of candidate labels on the fly across different routing domains.
|
| 30 |
- **Cross-Encoder Accuracy:** Outperforms single-text classifiers by evaluating joint attention over `(Premise, Hypothesis)` pairs.
|
| 31 |
-
- **DocGeniee Native:** Pre-configured for routing document generation workflows (Word, Excel, PowerPoint), PDF RAG queries, and chitchat/out-of-scope filtering.
|
| 32 |
|
| 33 |
---
|
| 34 |
|
| 35 |
-
##
|
| 36 |
|
| 37 |
-
|
| 38 |
|
| 39 |
### Zero-Shot NLI Classification (Python)
|
| 40 |
|
|
@@ -44,16 +43,23 @@ import torch
|
|
| 44 |
import torch.nn.functional as F
|
| 45 |
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
| 46 |
|
| 47 |
-
# Load model and tokenizer
|
| 48 |
-
MODEL_ID = "
|
| 49 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 50 |
|
| 51 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 52 |
model = AutoModelForSequenceClassification.from_pretrained(MODEL_ID).to(device)
|
| 53 |
model.eval()
|
| 54 |
|
| 55 |
-
# Entailment label index
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
# 1. Define Candidate Labels and Hypothesis Template
|
| 59 |
CANDIDATE_LABELS = [
|
|
@@ -79,26 +85,23 @@ inputs = tokenizer(
|
|
| 79 |
text_pair=hypotheses,
|
| 80 |
padding=True,
|
| 81 |
truncation=True,
|
|
|
|
| 82 |
return_tensors="pt",
|
| 83 |
).to(device)
|
| 84 |
|
| 85 |
# 5. Model Inference
|
| 86 |
with torch.no_grad():
|
| 87 |
outputs = model(**inputs)
|
| 88 |
-
|
| 89 |
-
entailment_logits =
|
| 90 |
|
| 91 |
-
# Softmax probability normalization
|
| 92 |
probabilities = F.softmax(entailment_logits / 0.5, dim=-1)
|
| 93 |
top_prob, top_idx = torch.max(probabilities, dim=-1)
|
| 94 |
|
| 95 |
predicted_label = CANDIDATE_LABELS[top_idx.item()]
|
| 96 |
confidence = top_prob.item() * 100
|
| 97 |
|
| 98 |
-
# =====================================================================
|
| 99 |
-
# MODIFICATION FOR JSON OUTPUT
|
| 100 |
-
# =====================================================================
|
| 101 |
-
|
| 102 |
# Build dictionary containing key details and all category probabilities
|
| 103 |
label_scores = {
|
| 104 |
label: round(prob.item() * 100, 2)
|
|
|
|
| 7 |
- nli
|
| 8 |
- cross-encoder
|
| 9 |
- intent-routing
|
| 10 |
+
- modernbert
|
| 11 |
pipeline_tag: zero-shot-classification
|
| 12 |
widget:
|
| 13 |
- text: Generate a formal project proposal in Word format.
|
| 14 |
+
candidate_labels: a Word document creation request, an Excel spreadsheet creation request, a PowerPoint presentation deck creation request, a PDF document analysis query, general chitchat
|
|
|
|
|
|
|
| 15 |
---
|
| 16 |
|
| 17 |
+
# MarkOne
|
| 18 |
|
| 19 |
+
**MarkOne** is a high-precision, zero-shot NLI cross-encoder model powered by **ModernBERT** designed for dynamic intent routing and query classification within modular AI architectures.
|
| 20 |
|
| 21 |
+
It evaluates user prompts against arbitrary numbers of candidate choice labels (N = 3, 6, 8,..) by computing sequence pair entailment probabilities using modern attention mechanics and an expanded context window.
|
| 22 |
|
| 23 |
---
|
| 24 |
|
| 25 |
+
## Key Features
|
| 26 |
|
| 27 |
+
- **ModernBERT Backbone:** Built on ModernBERT architecture featuring FlashAttention, unpadded processing, and Rotary Positional Embeddings (RoPE) for blazing-fast inference.
|
| 28 |
+
- **Extended Context Window:** Natively supports up to 8,192 tokens, allowing for rich, detailed candidate descriptions and long-form document inputs.
|
| 29 |
- **Dynamic Choice Counts:** Handles variable numbers of candidate labels on the fly across different routing domains.
|
| 30 |
- **Cross-Encoder Accuracy:** Outperforms single-text classifiers by evaluating joint attention over `(Premise, Hypothesis)` pairs.
|
|
|
|
| 31 |
|
| 32 |
---
|
| 33 |
|
| 34 |
+
## Quickstart & Usage
|
| 35 |
|
| 36 |
+
MarkOne operates as an NLI cross-encoder, evaluating candidate choices by pairing the input prompt as a Premise with a set of generated candidate Hypotheses.
|
| 37 |
|
| 38 |
### Zero-Shot NLI Classification (Python)
|
| 39 |
|
|
|
|
| 43 |
import torch.nn.functional as F
|
| 44 |
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
| 45 |
|
| 46 |
+
# Load ModernBERT NLI model and tokenizer
|
| 47 |
+
MODEL_ID = "dleemiller/ModernCE-base-nli"
|
| 48 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 49 |
|
| 50 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 51 |
model = AutoModelForSequenceClassification.from_pretrained(MODEL_ID).to(device)
|
| 52 |
model.eval()
|
| 53 |
|
| 54 |
+
# Dynamically resolve Entailment label index from config
|
| 55 |
+
label_mapping = model.config.id2label
|
| 56 |
+
ENTAILMENT_IDX = None
|
| 57 |
+
for idx, label_name in label_mapping.items():
|
| 58 |
+
if label_name.lower() in ["entailment", "entail"]:
|
| 59 |
+
ENTAILMENT_IDX = idx
|
| 60 |
+
break
|
| 61 |
+
if ENTAILMENT_IDX is None:
|
| 62 |
+
ENTAILMENT_IDX = 1
|
| 63 |
|
| 64 |
# 1. Define Candidate Labels and Hypothesis Template
|
| 65 |
CANDIDATE_LABELS = [
|
|
|
|
| 85 |
text_pair=hypotheses,
|
| 86 |
padding=True,
|
| 87 |
truncation=True,
|
| 88 |
+
max_length=1024,
|
| 89 |
return_tensors="pt",
|
| 90 |
).to(device)
|
| 91 |
|
| 92 |
# 5. Model Inference
|
| 93 |
with torch.no_grad():
|
| 94 |
outputs = model(**inputs)
|
| 95 |
+
logits = outputs.logits
|
| 96 |
+
entailment_logits = logits[:, ENTAILMENT_IDX] if logits.shape[-1] >= 3 else logits[:, 1]
|
| 97 |
|
| 98 |
+
# Softmax probability normalization with temperature scaling
|
| 99 |
probabilities = F.softmax(entailment_logits / 0.5, dim=-1)
|
| 100 |
top_prob, top_idx = torch.max(probabilities, dim=-1)
|
| 101 |
|
| 102 |
predicted_label = CANDIDATE_LABELS[top_idx.item()]
|
| 103 |
confidence = top_prob.item() * 100
|
| 104 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
# Build dictionary containing key details and all category probabilities
|
| 106 |
label_scores = {
|
| 107 |
label: round(prob.item() * 100, 2)
|