Spaces:
Sleeping
Sleeping
Upload captcha_solver/models.py with huggingface_hub
Browse files- captcha_solver/models.py +76 -0
captcha_solver/models.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Pydantic request/response schemas."""
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
from typing import Literal, Optional
|
| 6 |
+
|
| 7 |
+
from pydantic import BaseModel, Field, field_validator
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
CaptchaType = Literal["auto", "math", "audio", "text_ocr", "image_grid"]
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
class SolveRequest(BaseModel):
|
| 14 |
+
"""Request to solve a captcha."""
|
| 15 |
+
|
| 16 |
+
type: CaptchaType = "auto"
|
| 17 |
+
image_base64: Optional[str] = Field(
|
| 18 |
+
default=None,
|
| 19 |
+
description="Base64-encoded image of the captcha (PNG/JPG/WebP)",
|
| 20 |
+
)
|
| 21 |
+
audio_base64: Optional[str] = Field(
|
| 22 |
+
default=None,
|
| 23 |
+
description="Base64-encoded audio of the captcha (MP3/WAV/OGG)",
|
| 24 |
+
)
|
| 25 |
+
hint: Optional[str] = Field(
|
| 26 |
+
default=None,
|
| 27 |
+
description="Optional human hint to bias the solver (e.g. language code, category)",
|
| 28 |
+
)
|
| 29 |
+
timeout: int = Field(default=30, ge=1, le=120, description="Timeout in seconds")
|
| 30 |
+
use_cache: bool = Field(default=True, description="Whether to use result cache")
|
| 31 |
+
|
| 32 |
+
@field_validator("image_base64", "audio_base64")
|
| 33 |
+
@classmethod
|
| 34 |
+
def strip_data_url(cls, v: Optional[str]) -> Optional[str]:
|
| 35 |
+
"""Accept 'data:image/png;base64,XXX' or just 'XXX'."""
|
| 36 |
+
if v is None:
|
| 37 |
+
return v
|
| 38 |
+
if "," in v and v.startswith("data:"):
|
| 39 |
+
return v.split(",", 1)[1]
|
| 40 |
+
return v.strip()
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
class SolveResponse(BaseModel):
|
| 44 |
+
"""Response with the captcha answer."""
|
| 45 |
+
|
| 46 |
+
success: bool
|
| 47 |
+
answer: Optional[str] = None
|
| 48 |
+
confidence: float = Field(ge=0.0, le=1.0, default=0.0)
|
| 49 |
+
solver: str = Field(description="Which solver produced the answer (e.g. 'math.regex')")
|
| 50 |
+
elapsed_ms: int = 0
|
| 51 |
+
cache_hit: bool = False
|
| 52 |
+
attempts: int = 1
|
| 53 |
+
error: Optional[str] = None
|
| 54 |
+
metadata: dict = Field(default_factory=dict)
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
class HealthResponse(BaseModel):
|
| 58 |
+
status: Literal["ok", "degraded", "down"]
|
| 59 |
+
version: str
|
| 60 |
+
engines: dict[str, str]
|
| 61 |
+
uptime_s: float
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
class StatsResponse(BaseModel):
|
| 65 |
+
total_requests: int
|
| 66 |
+
by_type: dict[str, int]
|
| 67 |
+
by_solver: dict[str, int]
|
| 68 |
+
success_rate: float
|
| 69 |
+
avg_elapsed_ms: float
|
| 70 |
+
cache_hits: int
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
class ModelsResponse(BaseModel):
|
| 74 |
+
loaded: list[str]
|
| 75 |
+
available: list[dict]
|
| 76 |
+
ollama_enabled: bool
|