File size: 4,763 Bytes
2c161be
f65336d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2c161be
f65336d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2c161be
f65336d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2c161be
f65336d
 
 
 
 
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
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
"""Clairic's Transformers loader and System One extraction interface."""

from __future__ import annotations

import asyncio
import base64
import io
import json
from pathlib import Path
from typing import Any, Mapping

from clairic_runtime.api import PredictRequest, serialize_response
from clairic_runtime.backend import ClairicFlashModel
from clairic_runtime.errors import ClairRequestError
from clairic_runtime.large import Clairic27BModel
from clairic_runtime.settings import RuntimeSettings


def _request(value: Mapping[str, Any] | PredictRequest) -> PredictRequest:
    if isinstance(value, PredictRequest):
        return value
    from PIL import Image, ImageOps

    request = dict(value)
    if "state" in request and not isinstance(request["state"], str):
        request["state"] = json.dumps(request["state"], ensure_ascii=False)
    images = request.get("images")
    if isinstance(images, (list, tuple)):
        images = list(images)
        for index, image in enumerate(images):
            if isinstance(image, Image.Image):
                buffer = io.BytesIO()
                ImageOps.exif_transpose(image).convert("RGBA").save(
                    buffer, format="PNG"
                )
                images[index] = "data:image/png;base64," + base64.b64encode(
                    buffer.getvalue()
                ).decode("ascii")
        request["images"] = images
    return PredictRequest.model_validate(request)


class ClairicModel:
    """A model and its typed extraction head, with synchronous and async APIs."""

    def __init__(self, runtime: ClairicFlashModel, public_name: str) -> None:
        self.runtime = runtime
        self.model_id = public_name
        names = {public_name, runtime.model_id}
        self._model_selectors = names | {"@cf/cloudflare/" + name for name in names}
        self.processor = runtime.processor
        self.backbone = runtime.engine.backbone
        self.head = runtime.engine.head
        self.device = runtime.engine.device

    async def predict_async(
        self, request: Mapping[str, Any] | PredictRequest
    ) -> dict[str, Any]:
        validated = _request(request)
        if validated.model is not None and validated.model not in self._model_selectors:
            raise ClairRequestError(
                f"Unsupported model '{validated.model}'. Use '{self.model_id}'."
            )
        output = await self.runtime.predict_async(validated)
        return serialize_response(validated, output, self.model_id)

    def predict(self, request: Mapping[str, Any] | PredictRequest) -> dict[str, Any]:
        """Return structured values and scores for a System One: Extract request."""
        try:
            asyncio.get_running_loop()
        except RuntimeError:
            return asyncio.run(self.predict_async(request))
        raise RuntimeError(
            "Use await model.predict_async(request) inside an active event loop"
        )

    def eval(self) -> ClairicModel:
        self.backbone.eval()
        self.head.eval()
        return self

    def close(self) -> None:
        self.runtime.shutdown()


def load_release_model(
    model_path: str | Path,
    device: Any = "cuda",
    dtype: Any = None,
    max_length: int = 65_273,
    **from_pretrained_kwargs: Any,
) -> tuple[ClairicModel, Any]:
    """Load a local snapshot or a Hugging Face repository with Transformers."""
    path = Path(model_path)
    if not path.is_dir():
        from huggingface_hub import snapshot_download

        path = Path(snapshot_download(str(model_path)))
    configuration = json.loads((path / "clair_head_config.json").read_text())
    large = int(configuration["hidden_size"]) == 5120
    model_id = "clairic-27b" if large else "clairic-flash"
    settings = RuntimeSettings(
        model_path=str(path),
        version="1",
        model_id=model_id,
        max_length=max_length,
        max_concurrency=1,
    )
    implementation = Clairic27BModel if large else ClairicFlashModel
    runtime = implementation.load(
        settings, device=str(device), dtype=dtype, **from_pretrained_kwargs
    )
    return ClairicModel(
        runtime, "clairic" if large else "clairic-flash"
    ), runtime.processor


def systemone(
    model: ClairicModel,
    processor: Any,
    request: Mapping[str, Any] | PredictRequest,
    max_length: int | None = None,
) -> dict[str, Any]:
    """Answer a System One: Extract request with typed fields and confidence."""
    if processor is not model.processor:
        raise ValueError("Use the processor returned with this model")
    if max_length is not None and max_length != model.runtime.settings.max_length:
        raise ValueError("Set max_length when calling load_release_model")
    return model.predict(request)