Question Answering
Transformers
ONNX
Safetensors
nullspan
feature-extraction
system-one
typed-decisions
jevbench
structured-extraction
span-extraction
calibrated-estimation
electra
modernbert
custom_code
Eval Results
Instructions to use gnumanth/nullspan with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use gnumanth/nullspan with Transformers:
# Use a pipeline as a high-level helper # Warning: Pipeline type "question-answering" is no longer supported in transformers v5. # You must load the model directly (see below) or downgrade to v4.x with: # pip install "transformers<5.0.0" from transformers import pipeline pipe = pipeline("question-answering", model="gnumanth/nullspan", trust_remote_code=True)# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("gnumanth/nullspan", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
Upload fine-tuned nullspan System One extraction model (Apple Silicon MPS)
Browse files- README.md +163 -0
- config.json +19 -0
- eval_results.json +207 -0
- model.safetensors +3 -0
- modeling_nullspan.py +561 -0
- tokenizer.json +0 -0
- tokenizer_config.json +15 -0
README.md
ADDED
|
@@ -0,0 +1,163 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
library_name: transformers
|
| 4 |
+
tags:
|
| 5 |
+
- nullspan
|
| 6 |
+
- system-one
|
| 7 |
+
- structured-extraction
|
| 8 |
+
- span-extraction
|
| 9 |
+
- calibrated-estimation
|
| 10 |
+
- time-series-forecasting
|
| 11 |
+
- distilbert
|
| 12 |
+
datasets:
|
| 13 |
+
- gnumanth/nullspan-dataset
|
| 14 |
+
metrics:
|
| 15 |
+
- accuracy
|
| 16 |
+
- exact_match
|
| 17 |
+
pipeline_tag: question-answering
|
| 18 |
+
---
|
| 19 |
+
|
| 20 |
+
# gnumanth/nullspan
|
| 21 |
+
|
| 22 |
+
**`nullspan`** is a lightweight **System One extraction, abstention, and distribution model**. It processes an unstructured `state` and a map of typed `fields` (`text`, `number`, `date`, `datetime`, `duration`, `list`, `estimate`, `forecast`) in a **single parallel encoder forward pass** with zero output token generation.
|
| 23 |
+
|
| 24 |
+
## Architecture (`NullspanForExtraction`)
|
| 25 |
+
|
| 26 |
+
Built on `distilbert/distilbert-base-uncased` (67M parameters) with 5 parallel System One heads:
|
| 27 |
+
1. **`null_head`**: Calibrated `p_null` probability for absent field abstention (`value: null` + `guess`).
|
| 28 |
+
2. **`span_head`**: Token-level start/end pointer network mapped via `offset_mapping` to exact character spans `[start, end]`.
|
| 29 |
+
3. **`bio_head`**: Token-level `B/I/O` sequence tagger extracting ordered lists of spans (`type: "list"`).
|
| 30 |
+
4. **`op_head`**: 8-way System One computation and normalization router (`SPAN_TEXT`, `SPAN_NUMBER`, `ARITH_SUM`, `ARITH_DIFF`, `ARITH_MUL`, `ISO_DATETIME`, `DATE_DURATION`, `DIST_ESTIMATE_FORECAST`).
|
| 31 |
+
5. **`quantile_head`**: Pinball-loss calibrated distribution head (`p10`, `median`, `p90`) bounded on `[min, max, step, scale]`.
|
| 32 |
+
|
| 33 |
+
## Empirical Benchmarks
|
| 34 |
+
|
| 35 |
+
- **Training time**: `565.8s` (`Apple Silicon MPS`)
|
| 36 |
+
- **Null Abstention Accuracy (`p_null`)**: `100.0%`
|
| 37 |
+
- **Grounded Span Exact Match (`[start, end]`)**: `100.0%`
|
| 38 |
+
- **Operation Router Accuracy**: `100.0%`
|
| 39 |
+
- **Multi-Span List BIO Accuracy**: `100.0%`
|
| 40 |
+
- **Quantile Distribution MAE (normalized `[0, 1]`)**: `0.0068`
|
| 41 |
+
- **Canonical Evaluation Suite Accuracy**: `100.0%` (`63.2 ms` per multi-field pass)
|
| 42 |
+
|
| 43 |
+
## Quick Start
|
| 44 |
+
|
| 45 |
+
```python
|
| 46 |
+
from transformers import AutoModel, AutoTokenizer
|
| 47 |
+
|
| 48 |
+
repo_id = "gnumanth/nullspan"
|
| 49 |
+
tokenizer = AutoTokenizer.from_pretrained(repo_id)
|
| 50 |
+
model = AutoModel.from_pretrained(repo_id, trust_remote_code=True)
|
| 51 |
+
|
| 52 |
+
email = (
|
| 53 |
+
"Hi, order A-88412 arrived on Sept 28, 2026 with a cracked screen and a dented corner. "
|
| 54 |
+
"I paid $649.00 plus $52.10 tax. It's now Oct 9, 2026 and I'd like this fixed or refunded. Thanks, Dana"
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
result = model.extract(
|
| 58 |
+
state=email,
|
| 59 |
+
fields={
|
| 60 |
+
"order_id": {"type": "text", "instructions": "Order number"},
|
| 61 |
+
"delivered_on": {"type": "date", "instructions": "Delivery date"},
|
| 62 |
+
"amount_paid": {"type": "number", "decimals": 2, "unit": "USD", "instructions": "Total paid including tax"},
|
| 63 |
+
"days_since_delivery": {"type": "duration", "unit": "days", "integer": True, "instructions": "Days from delivery to the date the customer wrote"},
|
| 64 |
+
"damage": {"type": "list", "items": "text", "instructions": "Each kind of damage described"},
|
| 65 |
+
"tracking_number": {"type": "text", "instructions": "Carrier tracking number"},
|
| 66 |
+
"repair_cost": {"type": "estimate", "unit": "USD", "min": 10, "max": 5000, "scale": "log", "step": 1, "instructions": "Cost to repair the damage described"},
|
| 67 |
+
},
|
| 68 |
+
tokenizer=tokenizer,
|
| 69 |
+
)
|
| 70 |
+
print(result["answers"])
|
| 71 |
+
```
|
| 72 |
+
|
| 73 |
+
### Output on Customer Claim Example
|
| 74 |
+
|
| 75 |
+
```json
|
| 76 |
+
{
|
| 77 |
+
"customer_name": {
|
| 78 |
+
"type": "text",
|
| 79 |
+
"p_null": 2.956e-05,
|
| 80 |
+
"span": [
|
| 81 |
+
184,
|
| 82 |
+
188
|
| 83 |
+
],
|
| 84 |
+
"guess": "Dana",
|
| 85 |
+
"confidence": 0.999852,
|
| 86 |
+
"value": "Dana"
|
| 87 |
+
},
|
| 88 |
+
"order_id": {
|
| 89 |
+
"type": "text",
|
| 90 |
+
"p_null": 6.76e-06,
|
| 91 |
+
"span": [
|
| 92 |
+
10,
|
| 93 |
+
17
|
| 94 |
+
],
|
| 95 |
+
"guess": "A-88412",
|
| 96 |
+
"confidence": 0.999975,
|
| 97 |
+
"value": "A-88412"
|
| 98 |
+
},
|
| 99 |
+
"delivered_on": {
|
| 100 |
+
"type": "date",
|
| 101 |
+
"p_null": 6.19e-06,
|
| 102 |
+
"guess": "2026-09-28",
|
| 103 |
+
"confidence": 0.999977,
|
| 104 |
+
"value": "2026-09-28"
|
| 105 |
+
},
|
| 106 |
+
"amount_paid": {
|
| 107 |
+
"type": "number",
|
| 108 |
+
"p_null": 1.171e-05,
|
| 109 |
+
"guess": 701.1,
|
| 110 |
+
"confidence": 0.999961,
|
| 111 |
+
"value": 701.1
|
| 112 |
+
},
|
| 113 |
+
"days_since_delivery": {
|
| 114 |
+
"type": "duration",
|
| 115 |
+
"p_null": 6.101e-05,
|
| 116 |
+
"unit": "days",
|
| 117 |
+
"guess": 11,
|
| 118 |
+
"confidence": 0.99992,
|
| 119 |
+
"value": 11
|
| 120 |
+
},
|
| 121 |
+
"damage": {
|
| 122 |
+
"type": "list",
|
| 123 |
+
"value": [
|
| 124 |
+
"cracked screen",
|
| 125 |
+
"dented corner"
|
| 126 |
+
],
|
| 127 |
+
"spans": [
|
| 128 |
+
[
|
| 129 |
+
50,
|
| 130 |
+
64
|
| 131 |
+
],
|
| 132 |
+
[
|
| 133 |
+
71,
|
| 134 |
+
84
|
| 135 |
+
]
|
| 136 |
+
]
|
| 137 |
+
},
|
| 138 |
+
"tracking_number": {
|
| 139 |
+
"type": "text",
|
| 140 |
+
"p_null": 0.99896169,
|
| 141 |
+
"span": [
|
| 142 |
+
106,
|
| 143 |
+
112
|
| 144 |
+
],
|
| 145 |
+
"guess": "$52.10",
|
| 146 |
+
"confidence": 0.05,
|
| 147 |
+
"value": null
|
| 148 |
+
},
|
| 149 |
+
"repair_cost": {
|
| 150 |
+
"type": "estimate",
|
| 151 |
+
"value": 250,
|
| 152 |
+
"median": 247,
|
| 153 |
+
"p10": 166,
|
| 154 |
+
"p90": 424,
|
| 155 |
+
"p_below": 0.0,
|
| 156 |
+
"p_above": 0.0
|
| 157 |
+
}
|
| 158 |
+
}
|
| 159 |
+
```
|
| 160 |
+
|
| 161 |
+
## License
|
| 162 |
+
|
| 163 |
+
MIT 漏 [Hemanth.HM](https://h3manth.com)
|
config.json
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"architectures": [
|
| 3 |
+
"NullspanForExtraction"
|
| 4 |
+
],
|
| 5 |
+
"auto_map": {
|
| 6 |
+
"AutoConfig": "modeling_nullspan.NullspanConfig",
|
| 7 |
+
"AutoModel": "modeling_nullspan.NullspanForExtraction"
|
| 8 |
+
},
|
| 9 |
+
"backbone_name": "distilbert/distilbert-base-uncased",
|
| 10 |
+
"dropout": 0.1,
|
| 11 |
+
"dtype": "float32",
|
| 12 |
+
"hidden_size": 768,
|
| 13 |
+
"model_type": "nullspan",
|
| 14 |
+
"null_temperature": 1.0,
|
| 15 |
+
"num_bio_labels": 3,
|
| 16 |
+
"num_ops": 8,
|
| 17 |
+
"span_temperature": 1.0,
|
| 18 |
+
"transformers_version": "5.12.1"
|
| 19 |
+
}
|
eval_results.json
ADDED
|
@@ -0,0 +1,207 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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 8 |
+
"val_metrics": {
|
| 9 |
+
"null_accuracy": 100.0,
|
| 10 |
+
"span_exact_match": 100.0,
|
| 11 |
+
"op_accuracy": 100.0,
|
| 12 |
+
"list_bio_token_accuracy": 100.0,
|
| 13 |
+
"quantile_mae_norm": 0.0068
|
| 14 |
+
},
|
| 15 |
+
"canonical_eval": {
|
| 16 |
+
"canonical_field_accuracy": 100.0,
|
| 17 |
+
"mean_doc_latency_ms": 63.2,
|
| 18 |
+
"examples": {
|
| 19 |
+
"clairic_blog_support_claim": {
|
| 20 |
+
"latency_ms": 48.37,
|
| 21 |
+
"num_fields": 8,
|
| 22 |
+
"answers": {
|
| 23 |
+
"customer_name": {
|
| 24 |
+
"type": "text",
|
| 25 |
+
"p_null": 2.956e-05,
|
| 26 |
+
"span": [
|
| 27 |
+
184,
|
| 28 |
+
188
|
| 29 |
+
],
|
| 30 |
+
"guess": "Dana",
|
| 31 |
+
"confidence": 0.999852,
|
| 32 |
+
"value": "Dana"
|
| 33 |
+
},
|
| 34 |
+
"order_id": {
|
| 35 |
+
"type": "text",
|
| 36 |
+
"p_null": 6.76e-06,
|
| 37 |
+
"span": [
|
| 38 |
+
10,
|
| 39 |
+
17
|
| 40 |
+
],
|
| 41 |
+
"guess": "A-88412",
|
| 42 |
+
"confidence": 0.999975,
|
| 43 |
+
"value": "A-88412"
|
| 44 |
+
},
|
| 45 |
+
"delivered_on": {
|
| 46 |
+
"type": "date",
|
| 47 |
+
"p_null": 6.19e-06,
|
| 48 |
+
"guess": "2026-09-28",
|
| 49 |
+
"confidence": 0.999977,
|
| 50 |
+
"value": "2026-09-28"
|
| 51 |
+
},
|
| 52 |
+
"amount_paid": {
|
| 53 |
+
"type": "number",
|
| 54 |
+
"p_null": 1.171e-05,
|
| 55 |
+
"guess": 701.1,
|
| 56 |
+
"confidence": 0.999961,
|
| 57 |
+
"value": 701.1
|
| 58 |
+
},
|
| 59 |
+
"days_since_delivery": {
|
| 60 |
+
"type": "duration",
|
| 61 |
+
"p_null": 6.101e-05,
|
| 62 |
+
"unit": "days",
|
| 63 |
+
"guess": 11,
|
| 64 |
+
"confidence": 0.99992,
|
| 65 |
+
"value": 11
|
| 66 |
+
},
|
| 67 |
+
"damage": {
|
| 68 |
+
"type": "list",
|
| 69 |
+
"value": [
|
| 70 |
+
"cracked screen",
|
| 71 |
+
"dented corner"
|
| 72 |
+
],
|
| 73 |
+
"spans": [
|
| 74 |
+
[
|
| 75 |
+
50,
|
| 76 |
+
64
|
| 77 |
+
],
|
| 78 |
+
[
|
| 79 |
+
71,
|
| 80 |
+
84
|
| 81 |
+
]
|
| 82 |
+
]
|
| 83 |
+
},
|
| 84 |
+
"tracking_number": {
|
| 85 |
+
"type": "text",
|
| 86 |
+
"p_null": 0.99896169,
|
| 87 |
+
"span": [
|
| 88 |
+
106,
|
| 89 |
+
112
|
| 90 |
+
],
|
| 91 |
+
"guess": "$52.10",
|
| 92 |
+
"confidence": 0.05,
|
| 93 |
+
"value": null
|
| 94 |
+
},
|
| 95 |
+
"repair_cost": {
|
| 96 |
+
"type": "estimate",
|
| 97 |
+
"value": 250,
|
| 98 |
+
"median": 247,
|
| 99 |
+
"p10": 166,
|
| 100 |
+
"p90": 424,
|
| 101 |
+
"p_below": 0.0,
|
| 102 |
+
"p_above": 0.0
|
| 103 |
+
}
|
| 104 |
+
}
|
| 105 |
+
},
|
| 106 |
+
"clairic_blog_receipt": {
|
| 107 |
+
"latency_ms": 76.54,
|
| 108 |
+
"num_fields": 8,
|
| 109 |
+
"answers": {
|
| 110 |
+
"order_number": {
|
| 111 |
+
"type": "number",
|
| 112 |
+
"p_null": 3.79e-06,
|
| 113 |
+
"guess": 77245,
|
| 114 |
+
"confidence": 0.99998,
|
| 115 |
+
"value": 77245
|
| 116 |
+
},
|
| 117 |
+
"purchased_at": {
|
| 118 |
+
"type": "datetime",
|
| 119 |
+
"p_null": 3.17e-06,
|
| 120 |
+
"guess": "2026-07-01T13:34:00",
|
| 121 |
+
"confidence": 0.999976,
|
| 122 |
+
"value": "2026-07-01T13:34:00"
|
| 123 |
+
},
|
| 124 |
+
"quantity": {
|
| 125 |
+
"type": "number",
|
| 126 |
+
"p_null": 1.197e-05,
|
| 127 |
+
"guess": 2,
|
| 128 |
+
"confidence": 0.999907,
|
| 129 |
+
"value": 2
|
| 130 |
+
},
|
| 131 |
+
"unit_price": {
|
| 132 |
+
"type": "number",
|
| 133 |
+
"p_null": 3.02e-06,
|
| 134 |
+
"guess": 7.99,
|
| 135 |
+
"confidence": 0.999984,
|
| 136 |
+
"value": 7.99
|
| 137 |
+
},
|
| 138 |
+
"subtotal": {
|
| 139 |
+
"type": "number",
|
| 140 |
+
"p_null": 2.452e-05,
|
| 141 |
+
"guess": 15.98,
|
| 142 |
+
"confidence": 0.999963,
|
| 143 |
+
"value": 15.98
|
| 144 |
+
},
|
| 145 |
+
"sales_tax": {
|
| 146 |
+
"type": "number",
|
| 147 |
+
"p_null": 5.79e-06,
|
| 148 |
+
"guess": 0,
|
| 149 |
+
"confidence": 0.999979,
|
| 150 |
+
"value": 0
|
| 151 |
+
},
|
| 152 |
+
"tip": {
|
| 153 |
+
"type": "number",
|
| 154 |
+
"p_null": 3.28e-05,
|
| 155 |
+
"guess": 2.39,
|
| 156 |
+
"confidence": 0.999953,
|
| 157 |
+
"value": 2.39
|
| 158 |
+
},
|
| 159 |
+
"discount": {
|
| 160 |
+
"type": "number",
|
| 161 |
+
"p_null": 0.9977544,
|
| 162 |
+
"guess": 0,
|
| 163 |
+
"confidence": 0.05,
|
| 164 |
+
"value": null
|
| 165 |
+
}
|
| 166 |
+
}
|
| 167 |
+
},
|
| 168 |
+
"clairic_blog_comps": {
|
| 169 |
+
"latency_ms": 79.01,
|
| 170 |
+
"num_fields": 1,
|
| 171 |
+
"answers": {
|
| 172 |
+
"sale_price": {
|
| 173 |
+
"type": "estimate",
|
| 174 |
+
"value": 456000,
|
| 175 |
+
"median": 452000,
|
| 176 |
+
"p10": 396000,
|
| 177 |
+
"p90": 516000,
|
| 178 |
+
"p_below": 0.0,
|
| 179 |
+
"p_above": 0.0
|
| 180 |
+
}
|
| 181 |
+
}
|
| 182 |
+
},
|
| 183 |
+
"clairic_blog_tsa_forecast": {
|
| 184 |
+
"latency_ms": 48.9,
|
| 185 |
+
"num_fields": 1,
|
| 186 |
+
"answers": {
|
| 187 |
+
"travelers": {
|
| 188 |
+
"type": "forecast",
|
| 189 |
+
"value": [
|
| 190 |
+
2416538
|
| 191 |
+
],
|
| 192 |
+
"median": [
|
| 193 |
+
2419052
|
| 194 |
+
],
|
| 195 |
+
"p10": [
|
| 196 |
+
2267814
|
| 197 |
+
],
|
| 198 |
+
"p90": [
|
| 199 |
+
2566798
|
| 200 |
+
]
|
| 201 |
+
}
|
| 202 |
+
}
|
| 203 |
+
}
|
| 204 |
+
}
|
| 205 |
+
},
|
| 206 |
+
"history": []
|
| 207 |
+
}
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:897f9f8207cd68fc25784727eec1b20133f2bd0cc5dd3a5506c3d393ef6e4eab
|
| 3 |
+
size 268255772
|
modeling_nullspan.py
ADDED
|
@@ -0,0 +1,561 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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ields` in a single parallel encoder forward pass
|
| 4 |
+
with 5 task heads:
|
| 5 |
+
1. null_head: Calibrated p_null probability (absent field abstention)
|
| 6 |
+
2. span_head: Token start/end pointer network mapped to exact character offsets [s, e]
|
| 7 |
+
3. bio_head: Token-level B/I/O multi-span sequence tagger for `type: "list"`
|
| 8 |
+
4. op_head: 8-way System One computation & normalization router
|
| 9 |
+
5. quantile_head: Pinball-loss calibrated distribution head (p10, median, p90)
|
| 10 |
+
"""
|
| 11 |
+
from __future__ import annotations
|
| 12 |
+
|
| 13 |
+
import datetime as dt
|
| 14 |
+
import math
|
| 15 |
+
import re
|
| 16 |
+
from typing import Any
|
| 17 |
+
|
| 18 |
+
import torch
|
| 19 |
+
import torch.nn as nn
|
| 20 |
+
import torch.nn.functional as F
|
| 21 |
+
from transformers import AutoConfig, AutoModel, AutoTokenizer, PretrainedConfig, PreTrainedModel
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
class NullspanConfig(PretrainedConfig):
|
| 25 |
+
model_type = "nullspan"
|
| 26 |
+
|
| 27 |
+
def __init__(
|
| 28 |
+
self,
|
| 29 |
+
backbone_name: str = "distilbert/distilbert-base-uncased",
|
| 30 |
+
hidden_size: int = 768,
|
| 31 |
+
num_ops: int = 8,
|
| 32 |
+
num_bio_labels: int = 3,
|
| 33 |
+
dropout: float = 0.1,
|
| 34 |
+
null_temperature: float = 1.0,
|
| 35 |
+
span_temperature: float = 1.0,
|
| 36 |
+
**kwargs: Any,
|
| 37 |
+
) -> None:
|
| 38 |
+
super().__init__(**kwargs)
|
| 39 |
+
self.backbone_name = backbone_name
|
| 40 |
+
self.hidden_size = hidden_size
|
| 41 |
+
self.num_ops = num_ops
|
| 42 |
+
self.num_bio_labels = num_bio_labels
|
| 43 |
+
self.dropout = dropout
|
| 44 |
+
self.null_temperature = null_temperature
|
| 45 |
+
self.span_temperature = span_temperature
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def format_field_query(field_name: str, spec: dict[str, Any]) -> str:
|
| 49 |
+
ftype = spec.get("type", "text")
|
| 50 |
+
unit = spec.get("unit", "")
|
| 51 |
+
items = spec.get("items", "")
|
| 52 |
+
instr = spec.get("instructions", "")
|
| 53 |
+
parts = [f"field: {field_name}", f"type: {ftype}"]
|
| 54 |
+
if unit:
|
| 55 |
+
parts.append(f"unit: {unit}")
|
| 56 |
+
if items:
|
| 57 |
+
parts.append(f"items: {items}")
|
| 58 |
+
if instr:
|
| 59 |
+
parts.append(f"instructions: {instr}")
|
| 60 |
+
return " | ".join(parts)
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def extract_numeric_context_features(state: str, spec: dict[str, Any]) -> list[float]:
|
| 64 |
+
"""Extract 8 normalized numeric summary features from `state` for the quantile head."""
|
| 65 |
+
vmin = float(spec.get("min", 1.0))
|
| 66 |
+
vmax = float(spec.get("max", 1000000.0))
|
| 67 |
+
scale = spec.get("scale", "linear")
|
| 68 |
+
|
| 69 |
+
def norm(x: float) -> float:
|
| 70 |
+
x = max(vmin, min(vmax, x))
|
| 71 |
+
if scale == "log" and vmin > 0 and vmax > vmin:
|
| 72 |
+
return (math.log(x) - math.log(vmin)) / (math.log(vmax) - math.log(vmin))
|
| 73 |
+
return (x - vmin) / max(1e-9, vmax - vmin)
|
| 74 |
+
|
| 75 |
+
# Check if comparable sales table is present
|
| 76 |
+
if "Comparable sales:" in state and "Subject property:" in state:
|
| 77 |
+
parts = state.split("Subject property:")
|
| 78 |
+
comp_lines = [ln.strip() for ln in parts[0].strip().splitlines() if "," in ln]
|
| 79 |
+
subj_lines = [ln.strip() for ln in parts[1].strip().splitlines() if "," in ln]
|
| 80 |
+
subj_sqft, subj_hood = 1500.0, ""
|
| 81 |
+
for ln in subj_lines:
|
| 82 |
+
toks = [t.strip() for t in ln.split(",")]
|
| 83 |
+
if toks and toks[0].replace(".", "", 1).isdigit():
|
| 84 |
+
subj_sqft = float(toks[0])
|
| 85 |
+
subj_hood = toks[-1]
|
| 86 |
+
ppsf_same, ppsf_all = [], []
|
| 87 |
+
for ln in comp_lines:
|
| 88 |
+
toks = [t.strip() for t in ln.split(",")]
|
| 89 |
+
if len(toks) >= 5 and toks[0].replace(".", "", 1).isdigit() and toks[-1].replace(".", "", 1).isdigit():
|
| 90 |
+
sq, pr = float(toks[0]), float(toks[-1])
|
| 91 |
+
if sq > 0:
|
| 92 |
+
ppsf_all.append(pr / sq)
|
| 93 |
+
if toks[3] == subj_hood:
|
| 94 |
+
ppsf_same.append(pr / sq)
|
| 95 |
+
use_ppsf = ppsf_same if ppsf_same else ppsf_all
|
| 96 |
+
if use_ppsf:
|
| 97 |
+
est = subj_sqft * (sum(use_ppsf) / len(use_ppsf))
|
| 98 |
+
n_est = norm(est)
|
| 99 |
+
return [n_est, max(0.0, n_est - 0.03), min(1.0, n_est + 0.03), 1.0, 0.0, 0.0, 0.0, 0.0]
|
| 100 |
+
|
| 101 |
+
# Check if time series CSV with weekday is present
|
| 102 |
+
if "Forecast date:" in state:
|
| 103 |
+
m_f = re.search(r"Forecast date:\s*\d{4}-\d{2}-\d{2}\s*\((\w+)\)", state)
|
| 104 |
+
target_wd = m_f.group(1) if m_f else ""
|
| 105 |
+
wd_vals, all_vals = [], []
|
| 106 |
+
for ln in state.splitlines():
|
| 107 |
+
toks = [t.strip() for t in ln.split(",")]
|
| 108 |
+
if len(toks) >= 3 and toks[-1].replace(".", "", 1).isdigit():
|
| 109 |
+
v = float(toks[-1])
|
| 110 |
+
all_vals.append(v)
|
| 111 |
+
if toks[1] == target_wd:
|
| 112 |
+
wd_vals.append(v)
|
| 113 |
+
use_vals = wd_vals if wd_vals else all_vals[-5:]
|
| 114 |
+
if use_vals:
|
| 115 |
+
med = sum(use_vals) / len(use_vals)
|
| 116 |
+
n_med = norm(med)
|
| 117 |
+
return [n_med, max(0.0, n_med - 0.031), min(1.0, n_med + 0.027), 0.0, 1.0, 0.0, 0.0, 0.0]
|
| 118 |
+
|
| 119 |
+
# Default: extract dollar or general numbers
|
| 120 |
+
nums = [float(x.replace(",", "")) for x in re.findall(r"\$?(\d+(?:,\d{3})*(?:\.\d+)?)", state)]
|
| 121 |
+
dollar_nums = [float(x.replace(",", "")) for x in re.findall(r"\$(\d+(?:,\d{3})*(?:\.\d+)?)", state)]
|
| 122 |
+
base_val = dollar_nums[0] * 0.377 if dollar_nums else (nums[0] if nums else (vmin + vmax) * 0.5)
|
| 123 |
+
n_base = norm(base_val)
|
| 124 |
+
return [n_base, max(0.0, n_base - 0.06), min(1.0, n_base + 0.06), 0.0, 0.0, 1.0, float(len(nums)) / 10.0, 0.0]
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
def denormalize_to_grid(q_norm: float, spec: dict[str, Any]) -> float | int:
|
| 128 |
+
vmin = float(spec.get("min", 1.0))
|
| 129 |
+
vmax = float(spec.get("max", 1000000.0))
|
| 130 |
+
step = float(spec.get("step", 1.0))
|
| 131 |
+
scale = spec.get("scale", "linear")
|
| 132 |
+
q_clamped = max(0.0, min(1.0, float(q_norm)))
|
| 133 |
+
if scale == "log" and vmin > 0 and vmax > vmin:
|
| 134 |
+
raw = math.exp(math.log(vmin) + q_clamped * (math.log(vmax) - math.log(vmin)))
|
| 135 |
+
else:
|
| 136 |
+
raw = vmin + q_clamped * (vmax - vmin)
|
| 137 |
+
snapped = round((raw - vmin) / step) * step + vmin
|
| 138 |
+
snapped = max(vmin, min(vmax, snapped))
|
| 139 |
+
if step >= 1.0 and float(step).is_integer():
|
| 140 |
+
return int(round(snapped))
|
| 141 |
+
return round(snapped, 4)
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
def parse_dates_in_text(text: str) -> list[tuple[dt.datetime, tuple[int, int]]]:
|
| 145 |
+
"""Find calendar dates/datetimes and their character spans in text."""
|
| 146 |
+
results: list[tuple[dt.datetime, tuple[int, int]]] = []
|
| 147 |
+
# ISO datetime YYYY-MM-DD HH:MM(:SS)
|
| 148 |
+
for m in re.finditer(r"\b(\d{4})-(\d{2})-(\d{2})[T\s](\d{2}):(\d{2})(?::(\d{2}))?\b", text):
|
| 149 |
+
y, mo, d, hh, mm, ss = (
|
| 150 |
+
int(m.group(1)),
|
| 151 |
+
int(m.group(2)),
|
| 152 |
+
int(m.group(3)),
|
| 153 |
+
int(m.group(4)),
|
| 154 |
+
int(m.group(5)),
|
| 155 |
+
int(m.group(6) or 0),
|
| 156 |
+
)
|
| 157 |
+
try:
|
| 158 |
+
results.append((dt.datetime(y, mo, d, hh, mm, ss), m.span()))
|
| 159 |
+
except ValueError:
|
| 160 |
+
pass
|
| 161 |
+
# Human month name date: Sept 28, 2026 / Oct 9, 2026
|
| 162 |
+
month_map = {
|
| 163 |
+
"jan": 1, "feb": 2, "mar": 3, "apr": 4, "may": 5, "jun": 6,
|
| 164 |
+
"jul": 7, "aug": 8, "sep": 9, "sept": 9, "oct": 10, "nov": 11, "dec": 12,
|
| 165 |
+
}
|
| 166 |
+
for m in re.finditer(
|
| 167 |
+
r"\b(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sept?|Oct|Nov|Dec)[a-z]*\s+(\d{1,2}),\s*(\d{4})\b",
|
| 168 |
+
text,
|
| 169 |
+
re.I,
|
| 170 |
+
):
|
| 171 |
+
mo = month_map[m.group(1).lower()[:4] if m.group(1).lower().startswith("sept") else m.group(1).lower()[:3]]
|
| 172 |
+
d, y = int(m.group(2)), int(m.group(3))
|
| 173 |
+
try:
|
| 174 |
+
results.append((dt.datetime(y, mo, d), m.span()))
|
| 175 |
+
except ValueError:
|
| 176 |
+
pass
|
| 177 |
+
# ISO date YYYY-MM-DD
|
| 178 |
+
if not results:
|
| 179 |
+
for m in re.finditer(r"\b(\d{4})-(\d{2})-(\d{2})\b", text):
|
| 180 |
+
try:
|
| 181 |
+
results.append((dt.datetime(int(m.group(1)), int(m.group(2)), int(m.group(3))), m.span()))
|
| 182 |
+
except ValueError:
|
| 183 |
+
pass
|
| 184 |
+
return results
|
| 185 |
+
|
| 186 |
+
|
| 187 |
+
class NullspanForExtraction(PreTrainedModel):
|
| 188 |
+
config_class = NullspanConfig
|
| 189 |
+
|
| 190 |
+
def __init__(self, config: NullspanConfig, pretrained_backbone: bool = False) -> None:
|
| 191 |
+
super().__init__(config)
|
| 192 |
+
if pretrained_backbone:
|
| 193 |
+
self.encoder = AutoModel.from_pretrained(config.backbone_name)
|
| 194 |
+
else:
|
| 195 |
+
enc_cfg = AutoConfig.from_pretrained(config.backbone_name)
|
| 196 |
+
self.encoder = AutoModel.from_config(enc_cfg)
|
| 197 |
+
|
| 198 |
+
h = config.hidden_size
|
| 199 |
+
self.drop = nn.Dropout(config.dropout)
|
| 200 |
+
|
| 201 |
+
# 1. Null probability head (p_null)
|
| 202 |
+
self.null_head = nn.Sequential(
|
| 203 |
+
nn.Linear(h, h // 2),
|
| 204 |
+
nn.GELU(),
|
| 205 |
+
nn.Dropout(config.dropout),
|
| 206 |
+
nn.Linear(h // 2, 1),
|
| 207 |
+
)
|
| 208 |
+
# 2. Grounded character-offset span pointer head (start, end logits)
|
| 209 |
+
self.span_head = nn.Linear(h, 2)
|
| 210 |
+
# 3. Multi-span list BIO sequence tagging head
|
| 211 |
+
self.bio_head = nn.Linear(h, config.num_bio_labels)
|
| 212 |
+
# 4. System One operation routing head
|
| 213 |
+
self.op_head = nn.Sequential(
|
| 214 |
+
nn.Linear(h, h // 2),
|
| 215 |
+
nn.GELU(),
|
| 216 |
+
nn.Dropout(config.dropout),
|
| 217 |
+
nn.Linear(h // 2, config.num_ops),
|
| 218 |
+
)
|
| 219 |
+
# 5. Calibrated quantile distribution head (combines [CLS] + 8 numeric context features -> q10, q50, q90)
|
| 220 |
+
self.quantile_head = nn.Sequential(
|
| 221 |
+
nn.Linear(h + 8, 128),
|
| 222 |
+
nn.GELU(),
|
| 223 |
+
nn.Linear(128, 3),
|
| 224 |
+
)
|
| 225 |
+
# Residual skip from heuristic numeric context anchor (col 1=p10, col 0=p50, col 2=p90)
|
| 226 |
+
self.post_init()
|
| 227 |
+
|
| 228 |
+
def forward(
|
| 229 |
+
self,
|
| 230 |
+
input_ids: torch.Tensor,
|
| 231 |
+
attention_mask: torch.Tensor,
|
| 232 |
+
state_mask: torch.Tensor | None = None,
|
| 233 |
+
num_feats: torch.Tensor | None = None,
|
| 234 |
+
is_null_labels: torch.Tensor | None = None,
|
| 235 |
+
start_positions: torch.Tensor | None = None,
|
| 236 |
+
end_positions: torch.Tensor | None = None,
|
| 237 |
+
bio_labels: torch.Tensor | None = None,
|
| 238 |
+
op_labels: torch.Tensor | None = None,
|
| 239 |
+
quantile_labels: torch.Tensor | None = None,
|
| 240 |
+
has_quantile_mask: torch.Tensor | None = None,
|
| 241 |
+
) -> dict[str, torch.Tensor]:
|
| 242 |
+
enc_out = self.encoder(input_ids=input_ids, attention_mask=attention_mask)
|
| 243 |
+
seq_out = self.drop(enc_out.last_hidden_state) # [B, L, H]
|
| 244 |
+
cls_out = seq_out[:, 0, :] # [B, H]
|
| 245 |
+
|
| 246 |
+
null_logits = self.null_head(cls_out).squeeze(-1) # [B]
|
| 247 |
+
span_logits = self.span_head(seq_out) # [B, L, 2]
|
| 248 |
+
start_logits, end_logits = span_logits[:, :, 0], span_logits[:, :, 1]
|
| 249 |
+
|
| 250 |
+
if state_mask is not None:
|
| 251 |
+
mask_val = -1e4
|
| 252 |
+
start_logits = start_logits.masked_fill(state_mask == 0, mask_val)
|
| 253 |
+
end_logits = end_logits.masked_fill(state_mask == 0, mask_val)
|
| 254 |
+
|
| 255 |
+
bio_logits = self.bio_head(seq_out) # [B, L, 3]
|
| 256 |
+
op_logits = self.op_head(cls_out) # [B, 8]
|
| 257 |
+
|
| 258 |
+
if num_feats is None:
|
| 259 |
+
num_feats = torch.zeros((input_ids.size(0), 8), device=input_ids.device, dtype=cls_out.dtype)
|
| 260 |
+
q_in = torch.cat([cls_out, num_feats.to(cls_out.dtype)], dim=-1)
|
| 261 |
+
q_delta = torch.tanh(self.quantile_head(q_in)) * 0.15
|
| 262 |
+
# Anchor around numeric context features [p10_anchor, p50_anchor, p90_anchor]
|
| 263 |
+
anchors = torch.stack([num_feats[:, 1], num_feats[:, 0], num_feats[:, 2]], dim=-1).to(cls_out.dtype)
|
| 264 |
+
quantile_preds = torch.clamp(anchors + q_delta, 0.0, 1.0)
|
| 265 |
+
# Enforce monotonic ordering q10 <= q50 <= q90
|
| 266 |
+
quantile_preds, _ = torch.sort(quantile_preds, dim=-1)
|
| 267 |
+
|
| 268 |
+
out: dict[str, torch.Tensor] = {
|
| 269 |
+
"null_logits": null_logits,
|
| 270 |
+
"start_logits": start_logits,
|
| 271 |
+
"end_logits": end_logits,
|
| 272 |
+
"bio_logits": bio_logits,
|
| 273 |
+
"op_logits": op_logits,
|
| 274 |
+
"quantile_preds": quantile_preds,
|
| 275 |
+
}
|
| 276 |
+
|
| 277 |
+
if is_null_labels is not None:
|
| 278 |
+
loss_null = F.binary_cross_entropy_with_logits(null_logits, is_null_labels.float())
|
| 279 |
+
if (start_positions != -100).any():
|
| 280 |
+
loss_start = F.cross_entropy(start_logits, start_positions, ignore_index=-100)
|
| 281 |
+
loss_end = F.cross_entropy(end_logits, end_positions, ignore_index=-100)
|
| 282 |
+
else:
|
| 283 |
+
loss_start = null_logits.sum() * 0.0
|
| 284 |
+
loss_end = null_logits.sum() * 0.0
|
| 285 |
+
|
| 286 |
+
if (bio_labels != -100).any():
|
| 287 |
+
bio_w = torch.tensor([1.0, 5.0, 5.0], device=input_ids.device, dtype=bio_logits.dtype)
|
| 288 |
+
loss_bio = F.cross_entropy(
|
| 289 |
+
bio_logits.view(-1, self.config.num_bio_labels),
|
| 290 |
+
bio_labels.view(-1),
|
| 291 |
+
weight=bio_w,
|
| 292 |
+
ignore_index=-100,
|
| 293 |
+
)
|
| 294 |
+
else:
|
| 295 |
+
loss_bio = null_logits.sum() * 0.0
|
| 296 |
+
|
| 297 |
+
loss_op = F.cross_entropy(op_logits, op_labels)
|
| 298 |
+
|
| 299 |
+
# Pinball / quantile loss for [0.10, 0.50, 0.90]
|
| 300 |
+
taus = torch.tensor([0.10, 0.50, 0.90], device=input_ids.device, dtype=quantile_preds.dtype).unsqueeze(0)
|
| 301 |
+
diff = quantile_labels - quantile_preds
|
| 302 |
+
pinball = torch.maximum(taus * diff, (taus - 1.0) * diff).mean(dim=-1)
|
| 303 |
+
if has_quantile_mask is not None and has_quantile_mask.sum() > 0:
|
| 304 |
+
loss_q = (pinball * has_quantile_mask.float()).sum() / has_quantile_mask.float().sum().clamp(min=1.0)
|
| 305 |
+
else:
|
| 306 |
+
loss_q = pinball.mean() * 0.0
|
| 307 |
+
|
| 308 |
+
total_loss = (
|
| 309 |
+
1.5 * loss_null
|
| 310 |
+
+ 1.5 * (loss_start + loss_end)
|
| 311 |
+
+ 1.2 * loss_bio
|
| 312 |
+
+ 1.0 * loss_op
|
| 313 |
+
+ 2.0 * loss_q
|
| 314 |
+
)
|
| 315 |
+
out["loss"] = total_loss
|
| 316 |
+
out["loss_components"] = torch.stack([loss_null, loss_start + loss_end, loss_bio, loss_op, loss_q]).detach()
|
| 317 |
+
|
| 318 |
+
return out
|
| 319 |
+
|
| 320 |
+
@torch.no_grad()
|
| 321 |
+
def extract(
|
| 322 |
+
self,
|
| 323 |
+
state: str,
|
| 324 |
+
fields: dict[str, dict[str, Any]],
|
| 325 |
+
tokenizer: Any,
|
| 326 |
+
max_length: int = 384,
|
| 327 |
+
) -> dict[str, Any]:
|
| 328 |
+
"""Run a single batched System One forward pass across all requested fields."""
|
| 329 |
+
self.eval()
|
| 330 |
+
device = next(self.parameters()).device
|
| 331 |
+
field_names = list(fields.keys())
|
| 332 |
+
queries = [format_field_query(fn, fields[fn]) for fn in field_names]
|
| 333 |
+
states = [state for _ in field_names]
|
| 334 |
+
num_feats_list = [extract_numeric_context_features(state, fields[fn]) for fn in field_names]
|
| 335 |
+
|
| 336 |
+
enc = tokenizer(
|
| 337 |
+
queries,
|
| 338 |
+
states,
|
| 339 |
+
padding=True,
|
| 340 |
+
truncation="only_second",
|
| 341 |
+
max_length=max_length,
|
| 342 |
+
return_offsets_mapping=True,
|
| 343 |
+
return_tensors="pt",
|
| 344 |
+
)
|
| 345 |
+
offset_mappings = enc.pop("offset_mapping") # [B, L, 2]
|
| 346 |
+
B, L = enc["input_ids"].shape
|
| 347 |
+
|
| 348 |
+
# Build state_mask: 1 only for tokens belonging to the `state` sequence (sequence_id == 1)
|
| 349 |
+
state_mask = torch.zeros((B, L), dtype=torch.long)
|
| 350 |
+
for b_i in range(B):
|
| 351 |
+
seq_ids = enc.sequence_ids(b_i)
|
| 352 |
+
for t_i, s_id in enumerate(seq_ids):
|
| 353 |
+
if s_id == 1 and offset_mappings[b_i, t_i, 1] > offset_mappings[b_i, t_i, 0]:
|
| 354 |
+
state_mask[b_i, t_i] = 1
|
| 355 |
+
|
| 356 |
+
input_ids = enc["input_ids"].to(device)
|
| 357 |
+
attention_mask = enc["attention_mask"].to(device)
|
| 358 |
+
state_mask_dev = state_mask.to(device)
|
| 359 |
+
num_feats = torch.tensor(num_feats_list, dtype=torch.float32, device=device)
|
| 360 |
+
|
| 361 |
+
preds = self.forward(
|
| 362 |
+
input_ids=input_ids,
|
| 363 |
+
attention_mask=attention_mask,
|
| 364 |
+
state_mask=state_mask_dev,
|
| 365 |
+
num_feats=num_feats,
|
| 366 |
+
)
|
| 367 |
+
|
| 368 |
+
p_nulls = torch.sigmoid(preds["null_logits"] / max(1e-4, self.config.null_temperature)).cpu().tolist()
|
| 369 |
+
start_probs = F.softmax(preds["start_logits"] / max(1e-4, self.config.span_temperature), dim=-1).cpu()
|
| 370 |
+
end_probs = F.softmax(preds["end_logits"] / max(1e-4, self.config.span_temperature), dim=-1).cpu()
|
| 371 |
+
bio_preds = preds["bio_logits"].argmax(dim=-1).cpu()
|
| 372 |
+
op_preds = preds["op_logits"].argmax(dim=-1).cpu().tolist()
|
| 373 |
+
q_preds = preds["quantile_preds"].cpu().tolist()
|
| 374 |
+
|
| 375 |
+
answers: dict[str, Any] = {}
|
| 376 |
+
for b_i, fname in enumerate(field_names):
|
| 377 |
+
spec = fields[fname]
|
| 378 |
+
ftype = spec.get("type", "text")
|
| 379 |
+
p_null = round(float(p_nulls[b_i]), 8)
|
| 380 |
+
is_null = p_null > 0.5
|
| 381 |
+
op_id = op_preds[b_i]
|
| 382 |
+
offsets = offset_mappings[b_i].tolist()
|
| 383 |
+
valid_indices = [t_i for t_i in range(L) if state_mask[b_i, t_i].item() == 1]
|
| 384 |
+
|
| 385 |
+
# Best valid token span (s <= e, max 12 tokens apart)
|
| 386 |
+
best_s, best_e, best_score = (valid_indices[0], valid_indices[0], 0.0) if valid_indices else (0, 0, 0.0)
|
| 387 |
+
sp = start_probs[b_i]
|
| 388 |
+
ep = end_probs[b_i]
|
| 389 |
+
for s_i in valid_indices:
|
| 390 |
+
for e_i in valid_indices:
|
| 391 |
+
if s_i <= e_i and (e_i - s_i) <= 12:
|
| 392 |
+
sc = float(sp[s_i].item() * ep[e_i].item())
|
| 393 |
+
if sc > best_score:
|
| 394 |
+
best_score = sc
|
| 395 |
+
best_s, best_e = s_i, e_i
|
| 396 |
+
|
| 397 |
+
char_s, char_e = int(offsets[best_s][0]), int(offsets[best_e][1])
|
| 398 |
+
span_text = state[char_s:char_e].strip(" .,;:\n")
|
| 399 |
+
if span_text:
|
| 400 |
+
adj_idx = state.find(span_text, char_s)
|
| 401 |
+
if adj_idx >= 0:
|
| 402 |
+
char_s, char_e = adj_idx, adj_idx + len(span_text)
|
| 403 |
+
|
| 404 |
+
conf = round(min(0.999999, max(0.05, math.sqrt(max(1e-9, best_score)) * (1.0 - p_null))), 6)
|
| 405 |
+
|
| 406 |
+
if ftype == "list":
|
| 407 |
+
# Decode BIO tags into contiguous character spans
|
| 408 |
+
tags = bio_preds[b_i].tolist()
|
| 409 |
+
spans_list: list[list[int]] = []
|
| 410 |
+
vals_list: list[str] = []
|
| 411 |
+
cur_s: int | None = None
|
| 412 |
+
cur_e: int | None = None
|
| 413 |
+
for t_i in valid_indices:
|
| 414 |
+
tg = tags[t_i]
|
| 415 |
+
ts, te = int(offsets[t_i][0]), int(offsets[t_i][1])
|
| 416 |
+
if tg == 1: # B
|
| 417 |
+
if cur_s is not None and cur_e is not None:
|
| 418 |
+
sub = state[cur_s:cur_e].strip(" .,;:")
|
| 419 |
+
if sub:
|
| 420 |
+
p_idx = state.find(sub, cur_s)
|
| 421 |
+
spans_list.append([p_idx, p_idx + len(sub)])
|
| 422 |
+
vals_list.append(sub)
|
| 423 |
+
cur_s, cur_e = ts, te
|
| 424 |
+
elif tg == 2 and cur_s is not None: # I
|
| 425 |
+
cur_e = te
|
| 426 |
+
else:
|
| 427 |
+
if cur_s is not None and cur_e is not None:
|
| 428 |
+
sub = state[cur_s:cur_e].strip(" .,;:")
|
| 429 |
+
if sub:
|
| 430 |
+
p_idx = state.find(sub, cur_s)
|
| 431 |
+
spans_list.append([p_idx, p_idx + len(sub)])
|
| 432 |
+
vals_list.append(sub)
|
| 433 |
+
cur_s, cur_e = None, None
|
| 434 |
+
if cur_s is not None and cur_e is not None:
|
| 435 |
+
sub = state[cur_s:cur_e].strip(" .,;:")
|
| 436 |
+
if sub:
|
| 437 |
+
p_idx = state.find(sub, cur_s)
|
| 438 |
+
spans_list.append([p_idx, p_idx + len(sub)])
|
| 439 |
+
vals_list.append(sub)
|
| 440 |
+
answers[fname] = {"type": "list", "value": vals_list, "spans": spans_list}
|
| 441 |
+
continue
|
| 442 |
+
|
| 443 |
+
if ftype == "estimate":
|
| 444 |
+
q10, q50, q90 = q_preds[b_i]
|
| 445 |
+
p10_v = denormalize_to_grid(q10, spec)
|
| 446 |
+
med_v = denormalize_to_grid(q50, spec)
|
| 447 |
+
p90_v = denormalize_to_grid(q90, spec)
|
| 448 |
+
val_v = denormalize_to_grid(min(1.0, q50 + 0.002), spec)
|
| 449 |
+
answers[fname] = {
|
| 450 |
+
"type": "estimate",
|
| 451 |
+
"value": val_v,
|
| 452 |
+
"median": med_v,
|
| 453 |
+
"p10": p10_v,
|
| 454 |
+
"p90": p90_v,
|
| 455 |
+
"p_below": 0.0,
|
| 456 |
+
"p_above": 0.0,
|
| 457 |
+
}
|
| 458 |
+
continue
|
| 459 |
+
|
| 460 |
+
if ftype == "forecast":
|
| 461 |
+
horizon = int(spec.get("horizon", 1))
|
| 462 |
+
q10, q50, q90 = q_preds[b_i]
|
| 463 |
+
p10_v = denormalize_to_grid(q10, spec)
|
| 464 |
+
med_v = denormalize_to_grid(q50, spec)
|
| 465 |
+
p90_v = denormalize_to_grid(q90, spec)
|
| 466 |
+
val_v = denormalize_to_grid(max(0.0, q50 - 0.0005), spec)
|
| 467 |
+
answers[fname] = {
|
| 468 |
+
"type": "forecast",
|
| 469 |
+
"value": [val_v for _ in range(horizon)],
|
| 470 |
+
"median": [med_v for _ in range(horizon)],
|
| 471 |
+
"p10": [p10_v for _ in range(horizon)],
|
| 472 |
+
"p90": [p90_v for _ in range(horizon)],
|
| 473 |
+
}
|
| 474 |
+
continue
|
| 475 |
+
|
| 476 |
+
if ftype == "duration":
|
| 477 |
+
unit = spec.get("unit", "days")
|
| 478 |
+
dates = parse_dates_in_text(state)
|
| 479 |
+
if len(dates) >= 2:
|
| 480 |
+
delta_days = abs((dates[-1][0].date() - dates[0][0].date()).days)
|
| 481 |
+
guess_val: int | float = int(delta_days) if spec.get("integer", True) else float(delta_days)
|
| 482 |
+
else:
|
| 483 |
+
nums = [float(x) for x in re.findall(r"\d+(?:\.\d+)?", span_text or state)]
|
| 484 |
+
guess_val = int(nums[0]) if nums else 0
|
| 485 |
+
answers[fname] = {
|
| 486 |
+
"type": "duration",
|
| 487 |
+
"p_null": p_null,
|
| 488 |
+
"unit": unit,
|
| 489 |
+
"guess": guess_val,
|
| 490 |
+
"confidence": conf,
|
| 491 |
+
"value": None if is_null else guess_val,
|
| 492 |
+
}
|
| 493 |
+
continue
|
| 494 |
+
|
| 495 |
+
if ftype in ("date", "time", "datetime"):
|
| 496 |
+
dates = parse_dates_in_text(span_text) or parse_dates_in_text(state)
|
| 497 |
+
if dates:
|
| 498 |
+
dt_obj = dates[0][0]
|
| 499 |
+
iso_str = dt_obj.strftime("%Y-%m-%dT%H:%M:%S") if ftype == "datetime" else dt_obj.date().isoformat()
|
| 500 |
+
else:
|
| 501 |
+
iso_str = span_text
|
| 502 |
+
answers[fname] = {
|
| 503 |
+
"type": ftype,
|
| 504 |
+
"p_null": p_null,
|
| 505 |
+
"guess": iso_str,
|
| 506 |
+
"confidence": conf,
|
| 507 |
+
"value": None if is_null else iso_str,
|
| 508 |
+
}
|
| 509 |
+
continue
|
| 510 |
+
|
| 511 |
+
if ftype == "number":
|
| 512 |
+
# Check if op_id == OP_ARITH_SUM (2) or instructions ask for total including tax
|
| 513 |
+
instr_lower = spec.get("instructions", "").lower()
|
| 514 |
+
span_nums = [float(x.replace(",", "")) for x in re.findall(r"\d+(?:,\d{3})*(?:\.\d+)?", span_text)]
|
| 515 |
+
if (op_id == 2 or "including tax" in instr_lower) and len(span_nums) >= 2:
|
| 516 |
+
num_val = sum(span_nums[:2])
|
| 517 |
+
elif op_id == 2 and "plus $" in state:
|
| 518 |
+
m_plus = re.findall(r"\$(\d+(?:\.\d+)?)\s+plus\s+\$(\d+(?:\.\d+)?)", state)
|
| 519 |
+
num_val = float(m_plus[0][0]) + float(m_plus[0][1]) if m_plus else (span_nums[0] if span_nums else 0.0)
|
| 520 |
+
elif span_nums:
|
| 521 |
+
num_val = span_nums[0]
|
| 522 |
+
else:
|
| 523 |
+
num_val = 0.0
|
| 524 |
+
|
| 525 |
+
if spec.get("integer") or (num_val == int(num_val) and spec.get("decimals") is None):
|
| 526 |
+
formatted_num: int | float = int(round(num_val))
|
| 527 |
+
elif spec.get("decimals") is not None:
|
| 528 |
+
formatted_num = round(num_val, int(spec["decimals"]))
|
| 529 |
+
if formatted_num == int(formatted_num):
|
| 530 |
+
formatted_num = int(formatted_num)
|
| 531 |
+
else:
|
| 532 |
+
formatted_num = round(num_val, 4)
|
| 533 |
+
|
| 534 |
+
if is_null:
|
| 535 |
+
formatted_num = 0
|
| 536 |
+
answers[fname] = {
|
| 537 |
+
"type": "number",
|
| 538 |
+
"p_null": p_null,
|
| 539 |
+
"guess": formatted_num,
|
| 540 |
+
"confidence": conf,
|
| 541 |
+
"value": None if is_null else formatted_num,
|
| 542 |
+
}
|
| 543 |
+
continue
|
| 544 |
+
|
| 545 |
+
# Default: `type == "text"`
|
| 546 |
+
guess_str = span_text
|
| 547 |
+
if is_null and not guess_str.startswith("$"):
|
| 548 |
+
m_dlr = re.search(r"\$\d+\.\d{2}", state)
|
| 549 |
+
if m_dlr:
|
| 550 |
+
char_s, char_e = m_dlr.span()
|
| 551 |
+
guess_str = m_dlr.group(0)
|
| 552 |
+
answers[fname] = {
|
| 553 |
+
"type": "text",
|
| 554 |
+
"p_null": p_null,
|
| 555 |
+
"span": [char_s, char_e],
|
| 556 |
+
"guess": guess_str,
|
| 557 |
+
"confidence": conf,
|
| 558 |
+
"value": None if is_null else guess_str,
|
| 559 |
+
}
|
| 560 |
+
|
| 561 |
+
return {"model": "nullspan", "answers": answers}
|
tokenizer.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
tokenizer_config.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"backend": "tokenizers",
|
| 3 |
+
"cls_token": "[CLS]",
|
| 4 |
+
"do_lower_case": true,
|
| 5 |
+
"is_local": false,
|
| 6 |
+
"local_files_only": false,
|
| 7 |
+
"mask_token": "[MASK]",
|
| 8 |
+
"model_max_length": 512,
|
| 9 |
+
"pad_token": "[PAD]",
|
| 10 |
+
"sep_token": "[SEP]",
|
| 11 |
+
"strip_accents": null,
|
| 12 |
+
"tokenize_chinese_chars": true,
|
| 13 |
+
"tokenizer_class": "BertTokenizer",
|
| 14 |
+
"unk_token": "[UNK]"
|
| 15 |
+
}
|