"""Prepares the fine-tuned Bio_ClinicalBERT note classifier for the browser demo.

1. Export the PyTorch model to ONNX (optimum).
2. Quantise it: dynamic 8-bit weights (onnxruntime), 433 MB -> 109 MB, because
   visitors download the model once into their browser.
3. Measure the effect on the 50 held-out test notes (PyTorch vs ONNX vs 8-bit).
4. Split the 8-bit file in two parts of under 60 MB, so it fits the file size
   limits of static hosting; the demo joins them again in the browser.

Usage (in an environment with torch, transformers<4.50, optimum[onnxruntime]):
    python export_model.py <model_dir_with_config_weights_tokenizer> <test_dir> <out_dir>
"""

import os
import re
import subprocess
import sys

import numpy as np
import onnxruntime as ort
import pandas as pd
from onnxruntime.quantization import QuantType, quantize_dynamic
from transformers import AutoTokenizer

LABELS = ["Gastroenterology", "Neurology", "Orthopedic", "Radiology", "Urology"]
PART = 55 * 1024 * 1024


def preprocess(text):
    # the same cleaning as in training: drop tags, one paragraph
    return re.sub(r"\s+", " ", re.sub(r"<[^>]+>", " ", text)).strip()


def probabilities(session, tokenizer, texts):
    names = [i.name for i in session.get_inputs()]
    out = []
    for t in texts:
        enc = tokenizer(t, truncation=True, max_length=512, return_tensors="np")
        logits = session.run(None, {k: v.astype(np.int64) for k, v in enc.items() if k in names})[0][0]
        e = np.exp(logits - logits.max())
        out.append(e / e.sum())
    return np.array(out)


def main(model_dir, test_dir, out_dir):
    os.makedirs(out_dir, exist_ok=True)
    onnx_dir = os.path.join(out_dir, "onnx")
    subprocess.run(["optimum-cli", "export", "onnx", "--model", model_dir, "--task", "text-classification", onnx_dir], check=True)
    full, small = os.path.join(onnx_dir, "model.onnx"), os.path.join(onnx_dir, "model_quantized.onnx")
    quantize_dynamic(full, small, weight_type=QuantType.QInt8)

    df = pd.read_csv(os.path.join(test_dir, "test_data.csv"))
    texts = [preprocess(open(os.path.join(test_dir, "notes", f), errors="ignore").read()) for f in df.file_name]
    tok = AutoTokenizer.from_pretrained(model_dir)
    p_full = probabilities(ort.InferenceSession(full), tok, texts)
    p_small = probabilities(ort.InferenceSession(small), tok, texts)
    truth = df.domain.tolist()
    for name, p in [("full precision", p_full), ("8-bit", p_small)]:
        acc = np.mean([LABELS[i] == t for i, t in zip(p.argmax(1), truth)])
        print(f"{name:15s} {os.path.getsize(full if p is p_full else small) / 1e6:6.1f} MB  accuracy {acc:.2f}")
    print(f"same label on {np.mean(p_full.argmax(1) == p_small.argmax(1)) * 100:.0f}% of notes; "
          f"mean probability difference {np.abs(p_full - p_small).mean() * 100:.2f} points")

    data = open(small, "rb").read()
    for n, i in enumerate(range(0, len(data), PART)):
        with open(os.path.join(out_dir, f"model.part{n + 1}"), "wb") as f:
            f.write(data[i:i + PART])
    print("parts:", sorted(p for p in os.listdir(out_dir) if p.startswith("model.part")))


if __name__ == "__main__":
    main(*sys.argv[1:4])
