Update README.md
Browse files
README.md
CHANGED
|
@@ -16,4 +16,142 @@ widget:
|
|
| 16 |
- text: search for previous sentence delete
|
| 17 |
base_model:
|
| 18 |
- distilbert/distilbert-base-uncased
|
| 19 |
-
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
- text: search for previous sentence delete
|
| 17 |
base_model:
|
| 18 |
- distilbert/distilbert-base-uncased
|
| 19 |
+
---
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
# distilbert-command-data-tagger
|
| 23 |
+
|
| 24 |
+
`distilbert-command-data-tagger` is a lightweight, optimized ONNX model trained for joint **Intent Classification** and **Named Entity Recognition (NER) / Data Tagging** from natural language user commands.
|
| 25 |
+
|
| 26 |
+
It is designed for lightweight deployment on low-resource runtimes and Hugging Face Inference Endpoints using ONNX Runtime.
|
| 27 |
+
|
| 28 |
+
---
|
| 29 |
+
|
| 30 |
+
## Model Architecture & Files
|
| 31 |
+
|
| 32 |
+
- **Base Model**: `distilbert-base-uncased`
|
| 33 |
+
- **Model Format**: ONNX (`joint_command_parser.onnx` + `joint_command_parser.onnx.data`)
|
| 34 |
+
- **Max Sequence Length**: 64 tokens
|
| 35 |
+
- **Output Heads**:
|
| 36 |
+
1. **Intent Logits**: Decodes top-level command intent (with a strict `0.65` confidence threshold fallback to `unknown`).
|
| 37 |
+
2. **NER / Data Tag Logits**: Tagging token sequences (e.g., target text to edit, recipient email address, or search queries).
|
| 38 |
+
|
| 39 |
+
---
|
| 40 |
+
|
| 41 |
+
## Supported Intents
|
| 42 |
+
|
| 43 |
+
The model categorizes inputs into one of the following structured categories:
|
| 44 |
+
|
| 45 |
+
- **Document Editing**: `add_text`, `search_delete`, `prev_sentence_delete`, `bold`, `italic`, `underline`
|
| 46 |
+
- **Navigation**: `navigate_email`, `navigate_dm`
|
| 47 |
+
- **Email Page Operations**: `email_compose`, `email_send`, `email_reply`, `email_forward`, `email_delete`, `email_search`
|
| 48 |
+
- **Direct Message (DM) Operations**: `dm_send`, `dm_reply`, `dm_delete`, `dm_search`
|
| 49 |
+
- **Fallback**: `unknown`
|
| 50 |
+
|
| 51 |
+
---
|
| 52 |
+
|
| 53 |
+
## Inference with Custom Endpoint Handler (`handler.py`)
|
| 54 |
+
|
| 55 |
+
Below is the standard Hugging Face `EndpointHandler` used to load and run inference on the ONNX graph:
|
| 56 |
+
|
| 57 |
+
```python
|
| 58 |
+
import os
|
| 59 |
+
import numpy as np
|
| 60 |
+
import onnxruntime as ort
|
| 61 |
+
from transformers import AutoTokenizer
|
| 62 |
+
|
| 63 |
+
class EndpointHandler:
|
| 64 |
+
def __init__(self, path=""):
|
| 65 |
+
# 1. Resolve paths for BOTH structural graph and matrix weights
|
| 66 |
+
model_path = os.path.join(path, "joint_command_parser.onnx")
|
| 67 |
+
|
| 68 |
+
# Initialize the ONNX Runtime execution engine thread pool
|
| 69 |
+
self.session = ort.InferenceSession(model_path, providers=["CPUExecutionProvider"])
|
| 70 |
+
|
| 71 |
+
# 2. Match the exact tokenizer backbone used during training
|
| 72 |
+
self.tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased")
|
| 73 |
+
|
| 74 |
+
# 3. Structural target layouts
|
| 75 |
+
self.intents = [
|
| 76 |
+
# document editing
|
| 77 |
+
"add_text",
|
| 78 |
+
"search_delete",
|
| 79 |
+
"prev_sentence_delete",
|
| 80 |
+
"bold",
|
| 81 |
+
"italic",
|
| 82 |
+
"underline",
|
| 83 |
+
# navigation
|
| 84 |
+
"navigate_email",
|
| 85 |
+
"navigate_dm",
|
| 86 |
+
# email page
|
| 87 |
+
"email_compose",
|
| 88 |
+
"email_send",
|
| 89 |
+
"email_reply",
|
| 90 |
+
"email_forward",
|
| 91 |
+
"email_delete",
|
| 92 |
+
"email_search",
|
| 93 |
+
# dm page
|
| 94 |
+
"dm_send",
|
| 95 |
+
"dm_reply",
|
| 96 |
+
"dm_delete",
|
| 97 |
+
"dm_search",
|
| 98 |
+
# fallback
|
| 99 |
+
"unknown",
|
| 100 |
+
]
|
| 101 |
+
self.max_len = 64
|
| 102 |
+
|
| 103 |
+
def __call__(self, data):
|
| 104 |
+
# Extract input text payload sent via HTTP POST Request
|
| 105 |
+
inputs_payload = data.get("inputs", "")
|
| 106 |
+
if not inputs_payload:
|
| 107 |
+
return {"error": "Missing 'inputs' string parameter inside payload."}
|
| 108 |
+
|
| 109 |
+
# Tokenize incoming sequence matching matrix bounds
|
| 110 |
+
tokenized = self.tokenizer(
|
| 111 |
+
inputs_payload,
|
| 112 |
+
max_length=self.max_len,
|
| 113 |
+
padding="max_length",
|
| 114 |
+
truncation=True,
|
| 115 |
+
return_tensors="np"
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
onnx_feeds = {
|
| 119 |
+
"input_ids": tokenized["input_ids"].astype(np.int64),
|
| 120 |
+
"attention_mask": tokenized["attention_mask"].astype(np.int64)
|
| 121 |
+
}
|
| 122 |
+
|
| 123 |
+
# Run execution graph matrix calculations
|
| 124 |
+
intent_logits, ner_logits = self.session.run(None, onnx_feeds)
|
| 125 |
+
|
| 126 |
+
# Decode Intent array using Softmax calculation
|
| 127 |
+
logits_exp = np.exp(intent_logits[0])
|
| 128 |
+
probs = logits_exp / np.sum(logits_exp)
|
| 129 |
+
intent_idx = np.argmax(probs)
|
| 130 |
+
|
| 131 |
+
# Apply strict fallback thresholds
|
| 132 |
+
confidence = float(probs[intent_idx])
|
| 133 |
+
final_intent = self.intents[intent_idx] if confidence >= 0.65 else "unknown"
|
| 134 |
+
|
| 135 |
+
# Process and decode non-zero NER spans
|
| 136 |
+
ner_tags = np.argmax(ner_logits[0], axis=-1)
|
| 137 |
+
tokens = self.tokenizer.convert_ids_to_tokens(tokenized["input_ids"][0])
|
| 138 |
+
|
| 139 |
+
extracted_tokens = []
|
| 140 |
+
for token, tag in zip(tokens, ner_tags):
|
| 141 |
+
if token in [self.tokenizer.cls_token, self.tokenizer.sep_token, self.tokenizer.pad_token]:
|
| 142 |
+
continue
|
| 143 |
+
if tag > 0: # Valid B-DATA or I-DATA sequences
|
| 144 |
+
cleaned = token.replace("##", "")
|
| 145 |
+
if token.startswith("##") and extracted_tokens:
|
| 146 |
+
extracted_tokens[-1] += cleaned
|
| 147 |
+
else:
|
| 148 |
+
extracted_tokens.append(cleaned)
|
| 149 |
+
|
| 150 |
+
extracted_data = " ".join(extracted_tokens).strip()
|
| 151 |
+
|
| 152 |
+
return {
|
| 153 |
+
"intent": final_intent,
|
| 154 |
+
"confidence": confidence,
|
| 155 |
+
"extracted_data": extracted_data
|
| 156 |
+
}
|
| 157 |
+
|