Spaces:
Sleeping
Sleeping
Upload captcha_solver/learning/collector.py with huggingface_hub
Browse files
captcha_solver/learning/collector.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Hook that collects solve results and stores them in the learning DB.
|
| 2 |
+
|
| 3 |
+
Call `capture()` from any solver to record every attempt."""
|
| 4 |
+
from __future__ import annotations
|
| 5 |
+
|
| 6 |
+
import hashlib
|
| 7 |
+
import time
|
| 8 |
+
from typing import Any
|
| 9 |
+
|
| 10 |
+
from .db import LearningDB
|
| 11 |
+
|
| 12 |
+
_db = LearningDB()
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def image_hash(data: bytes) -> str:
|
| 16 |
+
return hashlib.sha256(data).hexdigest()[:16]
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
class Collector:
|
| 20 |
+
"""Collector that wraps a solver's solve method to record results."""
|
| 21 |
+
|
| 22 |
+
def __init__(self, solver_name: str) -> None:
|
| 23 |
+
self.solver_name = solver_name
|
| 24 |
+
self._last_run: dict[str, Any] = {}
|
| 25 |
+
|
| 26 |
+
def capture(
|
| 27 |
+
self,
|
| 28 |
+
captcha_type: str,
|
| 29 |
+
answer: str | None,
|
| 30 |
+
start_time: float,
|
| 31 |
+
expected: str | None = None,
|
| 32 |
+
correct: bool | None = None,
|
| 33 |
+
confidence: float | None = None,
|
| 34 |
+
hint: str | None = None,
|
| 35 |
+
image_bytes: bytes | None = None,
|
| 36 |
+
preprocess_steps: str | None = "",
|
| 37 |
+
run_source: str = "api",
|
| 38 |
+
) -> int:
|
| 39 |
+
latency = int((time.time() - start_time) * 1000)
|
| 40 |
+
img_hash = image_hash(image_bytes) if image_bytes else None
|
| 41 |
+
self._last_run = {
|
| 42 |
+
"type": captcha_type,
|
| 43 |
+
"answer": answer,
|
| 44 |
+
"expected": expected,
|
| 45 |
+
"correct": correct,
|
| 46 |
+
"latency_ms": latency,
|
| 47 |
+
}
|
| 48 |
+
return _db.record_attempt(
|
| 49 |
+
captcha_type=captcha_type,
|
| 50 |
+
solver_used=self.solver_name,
|
| 51 |
+
answer=answer,
|
| 52 |
+
expected=expected,
|
| 53 |
+
correct=correct,
|
| 54 |
+
confidence=confidence,
|
| 55 |
+
latency_ms=latency,
|
| 56 |
+
hint=hint,
|
| 57 |
+
image_hash=img_hash,
|
| 58 |
+
preprocess_steps=preprocess_steps,
|
| 59 |
+
run_source=run_source,
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
@property
|
| 63 |
+
def last_run(self) -> dict[str, Any]:
|
| 64 |
+
return self._last_run
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
def get_stats() -> dict:
|
| 68 |
+
return _db.summary()
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
def get_solver_ranking(captcha_type: str | None = None) -> list[dict]:
|
| 72 |
+
return _db.get_solver_ranking(captcha_type)
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
def get_best_solver(captcha_type: str) -> str | None:
|
| 76 |
+
best = _db.get_best_solver(captcha_type, min_samples=3)
|
| 77 |
+
return best["solver_name"] if best else None
|