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39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#!/usr/bin/env python3
"""Export selected checkpoints as model-only .pt state dictionaries."""

from __future__ import annotations

import argparse
import gc
import hashlib
import json
from pathlib import Path
from typing import Any

import torch

from model_registry import LANGUAGES, MODES, MODEL_SPECS, all_slots, model_spec, weight_path


CARD_ARTICLE = (
    "Tokom šestonedeljnog testiranja, redakcija je više puta kontaktirala "
    "<aspect>Primer Grupu</aspect> zbog nove usluge. Prvi odgovor "
    "<aspect>Primer Grupe</aspect> stigao je istog dana, a tehnički tim je "
    "zatim otklonio prijavljenu grešku bez dodatnih troškova. U završnom "
    "upitniku većina korisnika ocenila je podršku kao jasnu i pouzdanu."
)


def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser()
    parser.add_argument("--source-root", required=True)
    parser.add_argument("--output-root", required=True)
    parser.add_argument("--force", action="store_true")
    parser.add_argument("--model", choices=sorted(MODEL_SPECS), action="append")
    return parser.parse_args()


def sha256(path: Path) -> str:
    digest = hashlib.sha256()
    with path.open("rb") as handle:
        for chunk in iter(lambda: handle.read(8 * 1024 * 1024), b""):
            digest.update(chunk)
    return digest.hexdigest()


def torch_load(path: Path) -> Any:
    try:
        return torch.load(path, map_location="cpu", weights_only=False, mmap=True)
    except TypeError:
        return torch.load(path, map_location="cpu")


def export_state(source: Path, destination: Path, force: bool) -> dict[str, Any]:
    if destination.exists() and not force:
        loaded = torch.load(destination, map_location="cpu", weights_only=True, mmap=True)
        if not isinstance(loaded, dict) or not loaded:
            raise ValueError(f"Existing output is not a non-empty state dict: {destination}")
        return {
            "status": "existing",
            "size": destination.stat().st_size,
            "sha256": sha256(destination),
            "tensor_count": len(loaded),
        }

    checkpoint = torch_load(source)
    if not isinstance(checkpoint, dict):
        raise TypeError(f"Checkpoint is not a dictionary: {source}")
    state = checkpoint.get("model_state_dict", checkpoint.get("state_dict", checkpoint))
    if not isinstance(state, dict) or not state:
        raise ValueError(f"No model state found in {source}")
    if not all(isinstance(value, torch.Tensor) for value in state.values()):
        raise TypeError(f"Model state contains non-tensor values: {source}")
    destination.parent.mkdir(parents=True, exist_ok=True)
    torch.save(state, destination)
    del checkpoint, state
    gc.collect()

    verified = torch.load(destination, map_location="cpu", weights_only=True, mmap=True)
    forbidden = [
        key
        for key in verified
        if any(word in key.lower() for word in ("optimizer", "scheduler", "scaler"))
    ]
    if forbidden:
        raise ValueError(f"Training-state keys leaked into {destination}: {forbidden[:8]}")
    return {
        "status": "exported",
        "size": destination.stat().st_size,
        "sha256": sha256(destination),
        "tensor_count": len(verified),
    }


def family_readme(model_name: str, entries: list[dict[str, Any]]) -> str:
    from license_policy import decorate_model_card

    return decorate_model_card(_family_readme(model_name, entries), model_name)


def _family_readme(model_name: str, entries: list[dict[str, Any]]) -> str:
    spec = MODEL_SPECS[model_name]
    available = sum(item["available"] for item in entries)
    rows = []
    for item in entries:
        score = (
            f"{item['validation_macro_f1']:.4f}"
            if item["validation_macro_f1"] is not None
            else "—"
        )
        status = "Available" if item["available"] else "Checkpoint file unavailable"
        rows.append(
            f"| {item['language']} | {item['mode']} | {status} | {score} |"
        )
    table = "\n".join(rows)
    example = next((item for item in entries if item["available"]), entries[0])
    example_language = example["language"]
    example_mode = example["mode"]
    repo_id = spec["hf_repo"]
    if available == 0:
        return f"""---
library_name: pytorch
tags:
- aspect-based-sentiment-analysis
- south-slavic
- text-classification
license: other
---

# AspectBench {spec['display_name']}

This repository reserves the release location for the HBS and Slovenian
document-level aspect-based sentiment analysis checkpoints for this family.

## Checkpoint status

| Language | Mode | Status | Best validation Macro-F1 |
|---|---|---|---:|
{table}

The validation results survived, but none of the four selected trained MLP-head
files are present in the source tree or model archive. Consequently this
repository currently contains metadata only and cannot be used for inference.
The heads must be recovered or retrained before `masked.pt` and `unmasked.pt`
can be published. No checkpoint from another architecture or mode is used as a
substitute.

Input articles will use the same literal target markup as the other
AspectBench families:

```text
{CARD_ARTICLE}
```

See the shared toolkit at
[`nishan-chatterjee/aspect-based-sentiment-analysis`](https://huggingface.co/nishan-chatterjee/aspect-based-sentiment-analysis)
for the available model families and validation tooling.
"""
    return f"""---
library_name: pytorch
tags:
- aspect-based-sentiment-analysis
- south-slavic
- text-classification
license: other
---

# AspectBench {spec['display_name']}

Model-only checkpoints for HBS and Slovenian document-level aspect-based
sentiment analysis. This repository contains {available}/4 language-mode
checkpoint slots. It is used with the shared inference toolkit in
[`nishan-chatterjee/aspect-based-sentiment-analysis`](https://huggingface.co/nishan-chatterjee/aspect-based-sentiment-analysis).

## Input format

Every article must mark the target span with literal tags, even when using an
unmasked checkpoint:

```text
{CARD_ARTICLE}
```

- `masked`: the tagged text is replaced with `[ASPECT]`; the model does not see
  the target name.
- `unmasked`: the tags are removed and the model sees the target name.
- Gold `sentiment` is optional: `-1` = negative, `0` = neutral, `1` = positive.
  It is reported in the result but never used to produce the prediction.

## Available checkpoints

| Language | Mode | Status | Best validation Macro-F1 |
|---|---|---|---:|
{table}

`availability.json` contains the machine-readable selection record. A missing
checkpoint is never replaced with a checkpoint from another mode or language.

## Getting started

Create the portable environment from the toolkit repository:

```bash
conda env create -f environment.yml
conda activate aspectbench
```

`environment.yml` is maintained once in the shared toolkit rather than copied
into every model repository, preventing dependency versions from drifting
between family releases.

Or install the runtime packages in an existing environment:

```bash
python -m pip install -U torch transformers accelerate huggingface-hub sentencepiece numpy spacy sentence-transformers
```

Download the toolkit and this model repository into the expected directory
layout:

```python
from pathlib import Path
from huggingface_hub import snapshot_download

ROOT = Path("huggingface")
snapshot_download(
    repo_id="nishan-chatterjee/aspect-based-sentiment-analysis",
    local_dir=ROOT,
)
snapshot_download(
    repo_id="{repo_id}",
    local_dir=ROOT / "models" / "{model_name}",
)
```

The model repository includes the tokenizer and configuration assets required
to reconstruct the architecture. No separate base-model cache is needed.

## Python / Jupyter prediction

```python
from pathlib import Path
import sys

ROOT = Path("huggingface").resolve()
sys.path.insert(0, str(ROOT / "scripts"))

from inference import InferenceEngine

engine = InferenceEngine(
    model_name="{model_name}",
    language="{example_language}",
    mode="{example_mode}",
    model_root=ROOT / "models",
    device="cuda",  # use "cpu" when no GPU is available
)

prediction = engine.predict(
    {{
        "article": "{CARD_ARTICLE}",
        "sentiment": 1,
    }},
    mc_passes=10,
)
prediction
```

For a real batch, reuse the loaded engine:

```python
records = [
    {{"article": "{CARD_ARTICLE}", "sentiment": 1}},
    {{"article": "Pritužbe na <aspect>Drugi Sistem</aspect> nisu riješene.", "sentiment": -1}},
]
predictions = engine.predict_batch(records, batch_size=2, mc_passes=10)
```

## Command-line prediction

Run from the toolkit directory:

```bash
python scripts/predict.py \\
  --model-name {model_name} \\
  --language {example_language} \\
  --mode {example_mode} \\
  --model-root models \\
  --device cuda \\
  --mc-passes 10 \\
  --article '{CARD_ARTICLE}' \\
  --sentiment 1
```

## Output fields

| Field | Meaning |
|---|---|
| `input_article` | Original article, including `<aspect>` tags. |
| `tagged_aspects` | Target strings extracted from the tags. |
| `aspect_used` | Target representation actually supplied to the model. |
| `gold_sentiment` | Optional user-supplied reference label. |
| `predicted_sentiment` | Predicted integer label: `-1`, `0`, or `1`. |
| `predicted_sentiment_name` | Human-readable class name. |
| `class_probabilities` | Probability assigned to every sentiment class. |
| `uncertainty_across_classes` | Entropy, confidence, probability margin, and—when MC dropout is enabled—mutual information and vote statistics. |
| `inference` | Device, MC-dropout flag, and checkpoint path. |

The `.pt` files contain model tensors only. Optimizer, scheduler, and
gradient-scaler state is excluded.
"""


def main() -> None:
    args = parse_args()
    source_root = Path(args.source_root).resolve()
    output_root = Path(args.output_root).resolve()
    selected_models = set(args.model or MODEL_SPECS)
    manifest: dict[str, Any] = {
        "schema_version": 1,
        "selection_rule": "highest validation Macro-F1 among three train-validation runs; test metrics unused",
        "expected_slots": len(MODEL_SPECS) * len(LANGUAGES) * len(MODES),
        "entries": [],
    }

    for model_name in MODEL_SPECS:
        family_entries = []
        for current_model, language, mode, selection in all_slots():
            if current_model != model_name:
                continue
            entry = {
                "model": model_name,
                "language": language,
                "mode": mode,
                "validation_macro_f1": selection["validation_macro_f1"],
                "available": bool(selection["available"]),
                "unavailable_reason": selection["unavailable_reason"],
                "base_model": model_spec(model_name, language)["base_model"],
                "weight_path": f"{model_name}/{language}/{mode}.pt",
            }
            if selection["available"] and model_name in selected_models:
                source = source_root / selection["source"]
                if not source.is_file():
                    raise FileNotFoundError(source)
                result = export_state(
                    source,
                    weight_path(output_root, model_name, language, mode),
                    force=args.force,
                )
                entry.update(result)
            elif selection["available"]:
                entry["status"] = "not_selected_for_this_run"
            else:
                entry["status"] = "unavailable"
            manifest["entries"].append(entry)
            family_entries.append(entry)
            print(f"{model_name}/{language}/{mode}: {entry['status']}", flush=True)

        family_dir = output_root / model_name
        family_dir.mkdir(parents=True, exist_ok=True)
        (family_dir / "availability.json").write_text(
            json.dumps({"model": model_name, "entries": family_entries}, indent=2) + "\n",
            encoding="utf-8",
        )
        (family_dir / "README.md").write_text(
            family_readme(model_name, family_entries), encoding="utf-8"
        )
        from license_policy import BIBTEX, model_license_notice

        (family_dir / "LICENSE").write_text(
            model_license_notice(model_name), encoding="utf-8"
        )
        (family_dir / "citation.bib").write_text(BIBTEX + "\n", encoding="utf-8")

    manifest["available_slots"] = sum(
        entry["available"] for entry in manifest["entries"]
    )
    manifest["unavailable_slots"] = manifest["expected_slots"] - manifest["available_slots"]
    output_root.mkdir(parents=True, exist_ok=True)
    (output_root / "manifest.json").write_text(
        json.dumps(manifest, indent=2) + "\n", encoding="utf-8"
    )


if __name__ == "__main__":
    main()