File size: 2,586 Bytes
cff910f
 
 
 
 
 
 
 
 
e780c3d
cff910f
 
 
 
e780c3d
cff910f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
"""Shared document policy and inference; importing this module does not load weights."""
import math
import os
import threading
import time
from PIL import Image, ImageOps

MODEL_ID = "thaitea/laya-vision-201m"
REVISION = "0b6228f7a0762566de1c4539e9aa4eb1c1aef5f4"
POLICY_VERSION = "document-v2-short-prompt"
EXTENSIONS = {".jpg", ".jpeg", ".png", ".webp", ".bmp", ".tif", ".tiff"}
QUESTION = {
    "document": {
        "type": "noul",
        "instructions": "Is the main subject a printed form or document?",
    }
}
_agent = None
_lock = threading.Lock()


def load_agent(device=None):
    """Load the pinned checkpoint once; prefer available GPU, then Apple MPS, then CPU."""
    global _agent
    with _lock:
        if _agent is None:
            import laya
            import torch
            torch.set_num_threads(min(8, os.cpu_count() or 1))
            selected = device or os.environ.get("LAYA_DEVICE")
            print(f"Loading {MODEL_ID}@{REVISION} device={selected or 'auto'}", flush=True)
            _agent = laya.load_vlm(MODEL_ID, revision=REVISION, device=selected)
            print(f"Model ready on {_agent.device}", flush=True)
    return _agent


def decision(probability, threshold=0.5, review_confidence=0.8):
    p = float(probability)
    if not math.isfinite(p) or not 0 <= p <= 1:
        raise ValueError("Model returned an invalid probability")
    if not 0 < threshold < 1 or not 0.5 <= review_confidence <= 1:
        raise ValueError("Invalid threshold or review confidence")
    is_document = p >= threshold
    selected_probability = p if is_document else 1 - p
    return {
        "label": "document" if is_document else "non_document",
        "document_probability": p,
        "selected_probability": selected_probability,
        "needs_review": selected_probability < review_confidence,
        "threshold": threshold,
        "review_confidence": review_confidence,
    }


def classify(image, threshold=0.5, review_confidence=0.8, agent=None):
    """Predict whether an image is primarily a document under the agreed policy."""
    model = agent if agent is not None else load_agent()
    start = time.monotonic()
    with _lock:
        normalized = ImageOps.exif_transpose(image).convert("RGB")
        answer = model.predict({"image": normalized}, QUESTION, strict=True)["answers"]["document"]
    return {
        **decision(answer["noul"], threshold, review_confidence),
        "elapsed_seconds": round(time.monotonic() - start, 3),
        "model": MODEL_ID,
        "revision": REVISION,
        "policy": POLICY_VERSION,
    }