File size: 12,789 Bytes
92910a1
 
aaa0ba6
92910a1
 
 
 
045d8c3
92910a1
 
 
 
 
 
 
 
 
 
 
045d8c3
 
 
 
92910a1
045d8c3
92910a1
 
 
 
 
 
 
 
 
 
 
045d8c3
 
 
 
 
 
 
92910a1
045d8c3
 
 
 
 
 
 
 
 
92910a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
045d8c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92910a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aaa0ba6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92910a1
aaa0ba6
 
 
 
92910a1
aaa0ba6
 
92910a1
 
 
 
 
 
 
 
 
045d8c3
 
 
 
 
92910a1
045d8c3
 
 
92910a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
045d8c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92910a1
 
045d8c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92910a1
045d8c3
 
 
 
 
 
 
 
92910a1
 
 
 
 
 
 
 
 
 
045d8c3
92910a1
 
 
 
045d8c3
92910a1
 
045d8c3
 
92910a1
 
045d8c3
 
92910a1
 
 
045d8c3
 
 
 
 
 
 
 
 
 
92910a1
045d8c3
92910a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
045d8c3
92910a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
045d8c3
92910a1
 
 
 
 
 
 
 
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
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
import json
import re
import numpy as np
from pathlib import Path

import torch
from huggingface_hub import snapshot_download
from PIL import Image, ImageOps
from transformers import (
    AutoImageProcessor,
    AutoModelForImageClassification,
    TrOCRProcessor,
    VisionEncoderDecoderModel,
)


class CaptchaResolver:
    # Route a captcha to digit/math TrOCR experts with a safe fallback.

    def __init__(
        self, repo_dir, token=None, device=None,
        math_confidence_threshold=None,
    ):
        self.repo_dir = Path(repo_dir)
        self.token = token
        self.device = device or (
            "cuda" if torch.cuda.is_available() else "cpu"
        )
        self.config = json.loads(
            (self.repo_dir / "pipeline_config.json").read_text(
                encoding="utf-8"
            )
        )
        self.threshold = float(
            self.config.get("router_confidence_threshold", 0.95)
        )
        configured_math_threshold = float(
            self.config.get(
                "math_confidence_threshold",
                self.config.get(
                    "math_conflict_probability_threshold", 0.85
                ),
            )
        )
        self.math_confidence_threshold = float(
            configured_math_threshold
            if math_confidence_threshold is None
            else math_confidence_threshold
        )
        if not 0.0 <= self.math_confidence_threshold <= 1.0:
            raise ValueError(
                "math_confidence_threshold must be between 0.0 and 1.0"
            )

        router_dir = self.repo_dir / self.config["router_path"]
        self.router_processor = AutoImageProcessor.from_pretrained(
            router_dir
        )
        self.router_model = (
            AutoModelForImageClassification.from_pretrained(router_dir)
            .to(self.device)
            .eval()
        )

        self._experts = {}
        self.full_math = re.compile(self.config["full_math_pattern"])
        self.partial_math = re.compile(
            self.config["partial_math_pattern"]
        )
        self.digits_only = re.compile(self.config["digit_pattern"])

    @classmethod
    def from_pretrained(
        cls, repo_id, token=None, device=None,
        math_confidence_threshold=None,
    ):
        repo_dir = snapshot_download(
            repo_id=repo_id,
            token=token,
            allow_patterns=[
                "config.json",
                "preprocessor_config.json",
                "model.safetensors",
                "model-*.safetensors",
                "model.safetensors.index.json",
                "pytorch_model.bin",
                "pytorch_model-*.bin",
                "pytorch_model.bin.index.json",
                "pipeline_config.json",
                "resolver.py",
                "README.md",
            ],
        )
        return cls(
            repo_dir=repo_dir,
            token=token,
            device=device,
            math_confidence_threshold=math_confidence_threshold,
        )

    @staticmethod
    def normalize(text):
        return (
            str(text)
            .replace("−", "-")
            .replace("–", "-")
            .replace("—", "-")
            .replace(" ", "")
            .strip()
        )

    @staticmethod
    def pad_router_image(image):
        image = image.convert("RGB")
        side = max(image.size)

        corners = np.array(
            [
                image.getpixel((0, 0)),
                image.getpixel((image.width - 1, 0)),
                image.getpixel((0, image.height - 1)),
                image.getpixel(
                    (image.width - 1, image.height - 1)
                ),
            ],
            dtype=np.uint8,
        )

        background = tuple(
            np.median(corners, axis=0)
            .astype(np.uint8)
            .tolist()
        )

        padded = Image.new(
            "RGB",
            (side, side),
            background,
        )

        padded.paste(
            image,
            (
                (side - image.width) // 2,
                (side - image.height) // 2,
            ),
        )

        return padded

    def repair_math(self, text):
        text = self.normalize(text)
        if re.fullmatch(r"\d+[+-]\d+=", text):
            return text + "?"
        return text

    def _load_expert(self, route):
        if route not in self._experts:
            key = "math_model_id" if route == "math" else "digit_model_id"
            model_id = self.config[key]
            processor = TrOCRProcessor.from_pretrained(
                model_id, token=self.token
            )
            model = (
                VisionEncoderDecoderModel.from_pretrained(
                    model_id, token=self.token
                )
                .to(self.device)
                .eval()
            )
            self._experts[route] = (processor, model)
        return self._experts[route]

    def route(self, image):
        padded = self.pad_router_image(image)
        values = self.router_processor(
            images=padded, return_tensors="pt"
        ).pixel_values.to(self.device)
        with torch.inference_mode():
            probabilities = torch.softmax(
                self.router_model(pixel_values=values).logits, dim=-1
            )[0]
        route_id = int(probabilities.argmax().item())
        id2label = self.router_model.config.id2label
        label = id2label.get(route_id, id2label.get(str(route_id)))
        scores = {
            id2label.get(i, id2label.get(str(i))): float(value.item())
            for i, value in enumerate(probabilities)
        }
        return label, float(probabilities[route_id].item()), scores

    def _digit_views(self, image):
        original = image.convert("RGB")
        gray = ImageOps.autocontrast(original.convert("L"))
        views = [original, gray.convert("RGB")]
        if float(np.asarray(gray, dtype=np.float32).mean()) < 128.0:
            views.append(ImageOps.invert(gray).convert("RGB"))
        return views

    def _digit_token_ids(self, tokenizer):
        if hasattr(self, "_cached_digit_token_ids"):
            return self._cached_digit_token_ids
        allowed = []
        for token_id in tokenizer.get_vocab().values():
            piece = tokenizer.decode(
                [token_id],
                skip_special_tokens=True,
                clean_up_tokenization_spaces=False,
            )
            if re.fullmatch(r"\s*\d+\s*", piece or ""):
                allowed.append(int(token_id))
        self._cached_digit_token_ids = sorted(set(allowed))
        return self._cached_digit_token_ids

    def run_expert(self, image, route):
        processor, model = self._load_expert(route)
        if route == "math":
            values = processor(
                images=image.convert("RGB"), return_tensors="pt"
            ).pixel_values.to(self.device)
            with torch.inference_mode():
                generated = model.generate(
                    values, num_beams=4, max_length=32
                )
            decoded = processor.batch_decode(
                generated, skip_special_tokens=True
            )[0]
            return self.normalize(decoded)

        digit_ids = self._digit_token_ids(processor.tokenizer)
        eos_id = model.generation_config.eos_token_id

        def allow_digit_tokens(batch_id, input_ids):
            prefix = self.normalize(
                processor.tokenizer.decode(
                    input_ids.tolist(),
                    skip_special_tokens=True,
                    clean_up_tokenization_spaces=False,
                )
            )
            digit_count = len(re.sub(r"\D", "", prefix))
            if digit_count >= 7 and eos_id is not None:
                return [int(eos_id)]
            allowed = list(digit_ids)
            if digit_count >= 4 and eos_id is not None:
                allowed.append(int(eos_id))
            return allowed

        candidates = []
        for view in self._digit_views(image):
            values = processor(
                images=view, return_tensors="pt"
            ).pixel_values.to(self.device)
            generation_kwargs = {
                "num_beams": 4,
                "max_length": 16,
                "return_dict_in_generate": True,
                "output_scores": True,
            }
            if digit_ids:
                generation_kwargs["prefix_allowed_tokens_fn"] = (
                    allow_digit_tokens
                )
            with torch.inference_mode():
                output = model.generate(values, **generation_kwargs)
            decoded = self.normalize(
                processor.batch_decode(
                    output.sequences, skip_special_tokens=True
                )[0]
            )
            score = float(output.sequences_scores[0].item())
            candidates.append((decoded, score))

        valid = [
            candidate for candidate in candidates
            if self.digits_only.fullmatch(candidate[0])
        ]
        return max(valid or candidates, key=lambda item: item[1])[0]

    def choose_candidates(
        self, digits_text, math_text, raw_route, probabilities
    ):
        digits_text = self.normalize(digits_text)
        math_text = self.repair_math(math_text)

        digits_valid = bool(self.digits_only.fullmatch(digits_text))
        math_valid = bool(self.full_math.fullmatch(math_text))
        math_partial = bool(self.partial_math.fullmatch(math_text))
        math_probability = float(probabilities.get("math", 0.0))

        if digits_valid and math_valid:
            if (
                raw_route == "math"
                and math_probability >= self.math_confidence_threshold
            ):
                return (
                    "math", math_text, True,
                    "both valid; calibrated router strongly supports math",
                )
            return (
                "digits", digits_text, True,
                "both valid; preserve constrained digit candidate",
            )

        if digits_valid:
            return "digits", digits_text, True, "digit candidate is valid"
        if math_valid:
            return "math", math_text, True, "math candidate is valid"

        # A math expert can hallucinate '-' or '=' from grid lines. A partial
        # equation must never turn a digit-routed image into a false math result.
        reason = (
            "partial math candidate rejected"
            if math_partial
            else "neither candidate matched its grammar"
        )
        return "unknown", digits_text or math_text, False, reason

    def predict(self, image):
        if not isinstance(image, Image.Image):
            image = Image.open(image).convert("RGB")
        else:
            image = image.convert("RGB")

        raw_route, confidence, probabilities = self.route(image)
        primary_text = self.run_expert(image, raw_route)
        if raw_route == "math":
            primary_text = self.repair_math(primary_text)
            primary_valid = bool(self.full_math.fullmatch(primary_text))
        else:
            primary_valid = bool(self.digits_only.fullmatch(primary_text))

        use_fallback = confidence < self.threshold or not primary_valid
        if not use_fallback:
            return {
                "text": primary_text,
                "route": raw_route,
                "raw_router_route": raw_route,
                "router_confidence": confidence,
                "router_probabilities": probabilities,
                "math_confidence_threshold": self.math_confidence_threshold,
                "used_dual_model_fallback": False,
                "selection_reason": "high-confidence router and valid primary output",
                "valid_format": True,
            }

        if raw_route == "math":
            math_text = primary_text
            digits_text = self.run_expert(image, "digits")
        else:
            digits_text = primary_text
            math_text = self.run_expert(image, "math")

        route, text, valid, selection_reason = self.choose_candidates(
            digits_text,
            math_text,
            raw_route,
            probabilities,
        )
        return {
            "text": text,
            "route": route,
            "raw_router_route": raw_route,
            "router_confidence": confidence,
            "router_probabilities": probabilities,
            "math_confidence_threshold": self.math_confidence_threshold,
            "used_dual_model_fallback": True,
            "digits_candidate": digits_text,
            "math_candidate": math_text,
            "selection_reason": selection_reason,
            "valid_format": valid,
        }

    __call__ = predict