laya-document-batch / classifier.py
seromy's picture
Upload folder using huggingface_hub
e780c3d verified
Raw History Blame Contribute Delete
2.59 kB
"""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,
}