updated the output format
Browse files
README.md
CHANGED
|
@@ -37,9 +37,10 @@ Because **MarkOne** operates as an NLI cross-encoder, it evaluates candidate cho
|
|
| 37 |
### Zero-Shot NLI Classification (Python)
|
| 38 |
|
| 39 |
```python
|
|
|
|
| 40 |
import torch
|
| 41 |
import torch.nn.functional as F
|
| 42 |
-
from transformers import
|
| 43 |
|
| 44 |
# Load model and tokenizer
|
| 45 |
MODEL_ID = "ChandruK/MarkOne"
|
|
@@ -50,7 +51,7 @@ model = AutoModelForSequenceClassification.from_pretrained(MODEL_ID).to(device)
|
|
| 50 |
model.eval()
|
| 51 |
|
| 52 |
# Entailment label index (Index 2 for standard DeBERTa MNLI architecture)
|
| 53 |
-
ENTAILMENT_IDX = 2
|
| 54 |
|
| 55 |
# 1. Define Candidate Labels and Hypothesis Template
|
| 56 |
CANDIDATE_LABELS = [
|
|
@@ -58,7 +59,8 @@ CANDIDATE_LABELS = [
|
|
| 58 |
"an Excel spreadsheet creation request",
|
| 59 |
"a PowerPoint presentation deck creation request",
|
| 60 |
"a PDF document analysis or extraction query",
|
| 61 |
-
"general chitchat or off-topic conversation"
|
|
|
|
| 62 |
]
|
| 63 |
HYPOTHESIS_TEMPLATE = "This user prompt is {}."
|
| 64 |
|
|
@@ -75,7 +77,7 @@ inputs = tokenizer(
|
|
| 75 |
text_pair=hypotheses,
|
| 76 |
padding=True,
|
| 77 |
truncation=True,
|
| 78 |
-
return_tensors="pt"
|
| 79 |
).to(device)
|
| 80 |
|
| 81 |
# 5. Model Inference
|
|
@@ -83,7 +85,7 @@ with torch.no_grad():
|
|
| 83 |
outputs = model(**inputs)
|
| 84 |
# Extract Entailment logits vector across all dynamic candidates
|
| 85 |
entailment_logits = outputs.logits[:, ENTAILMENT_IDX]
|
| 86 |
-
|
| 87 |
# Softmax probability normalization
|
| 88 |
probabilities = F.softmax(entailment_logits / 0.5, dim=-1)
|
| 89 |
top_prob, top_idx = torch.max(probabilities, dim=-1)
|
|
@@ -91,5 +93,23 @@ with torch.no_grad():
|
|
| 91 |
predicted_label = CANDIDATE_LABELS[top_idx.item()]
|
| 92 |
confidence = top_prob.item() * 100
|
| 93 |
|
| 94 |
-
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
### Zero-Shot NLI Classification (Python)
|
| 38 |
|
| 39 |
```python
|
| 40 |
+
import json
|
| 41 |
import torch
|
| 42 |
import torch.nn.functional as F
|
| 43 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
| 44 |
|
| 45 |
# Load model and tokenizer
|
| 46 |
MODEL_ID = "ChandruK/MarkOne"
|
|
|
|
| 51 |
model.eval()
|
| 52 |
|
| 53 |
# Entailment label index (Index 2 for standard DeBERTa MNLI architecture)
|
| 54 |
+
ENTAILMENT_IDX = 2
|
| 55 |
|
| 56 |
# 1. Define Candidate Labels and Hypothesis Template
|
| 57 |
CANDIDATE_LABELS = [
|
|
|
|
| 59 |
"an Excel spreadsheet creation request",
|
| 60 |
"a PowerPoint presentation deck creation request",
|
| 61 |
"a PDF document analysis or extraction query",
|
| 62 |
+
"general chitchat or off-topic conversation",
|
| 63 |
+
"a Question answer related",
|
| 64 |
]
|
| 65 |
HYPOTHESIS_TEMPLATE = "This user prompt is {}."
|
| 66 |
|
|
|
|
| 77 |
text_pair=hypotheses,
|
| 78 |
padding=True,
|
| 79 |
truncation=True,
|
| 80 |
+
return_tensors="pt",
|
| 81 |
).to(device)
|
| 82 |
|
| 83 |
# 5. Model Inference
|
|
|
|
| 85 |
outputs = model(**inputs)
|
| 86 |
# Extract Entailment logits vector across all dynamic candidates
|
| 87 |
entailment_logits = outputs.logits[:, ENTAILMENT_IDX]
|
| 88 |
+
|
| 89 |
# Softmax probability normalization
|
| 90 |
probabilities = F.softmax(entailment_logits / 0.5, dim=-1)
|
| 91 |
top_prob, top_idx = torch.max(probabilities, dim=-1)
|
|
|
|
| 93 |
predicted_label = CANDIDATE_LABELS[top_idx.item()]
|
| 94 |
confidence = top_prob.item() * 100
|
| 95 |
|
| 96 |
+
# =====================================================================
|
| 97 |
+
# MODIFICATION FOR JSON OUTPUT
|
| 98 |
+
# =====================================================================
|
| 99 |
+
|
| 100 |
+
# Build dictionary containing key details and all category probabilities
|
| 101 |
+
label_scores = {
|
| 102 |
+
label: round(prob.item() * 100, 2)
|
| 103 |
+
for label, prob in zip(CANDIDATE_LABELS, probabilities)
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
output_json_data = {
|
| 107 |
+
"prompt": prompt,
|
| 108 |
+
"predicted_label": predicted_label,
|
| 109 |
+
"confidence": round(confidence, 2),
|
| 110 |
+
"all_scores": label_scores,
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
# Output as formatted JSON string
|
| 114 |
+
json_output = json.dumps(output_json_data, indent=2)
|
| 115 |
+
print(json_output)
|