| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- import os
- import io
- import mimetypes
- import base64
- from typing import Union, Optional, Any
- from openai import OpenAI
- from configure import CAPTCHA_LLM_CONFIG
- class CaptchaBreakerError(Exception):
- """Base exception for captcha breaking."""
- pass
- def normalize_image_to_data_url(
- image_input: Union[str, bytes, io.BytesIO],
- default_mime_type: str = "image/png"
- ) -> str:
- """
- Normalizes multiple image input types (file path, raw bytes, BytesIO, Base64 string, Data URL)
- into standard Data URL format (data:<mime-type>;base64,<data>) acceptable by multimodal models.
- """
- if isinstance(image_input, (bytes, bytearray)):
- b64_str = base64.b64encode(image_input).decode("utf-8")
- return f"data:{default_mime_type};base64,{b64_str}"
- if isinstance(image_input, io.BytesIO):
- b64_str = base64.b64encode(image_input.getvalue()).decode("utf-8")
- return f"data:{default_mime_type};base64,{b64_str}"
- if isinstance(image_input, str):
- # Already a standard Data URL
- if image_input.startswith("data:image/"):
- return image_input
- # Local file path
- if os.path.isfile(image_input):
- mime_type, _ = mimetypes.guess_type(image_input)
- mime_type = mime_type or default_mime_type
- with open(image_input, "rb") as f:
- b64_str = base64.b64encode(f.read()).decode("utf-8")
- return f"data:{mime_type};base64,{b64_str}"
- # Attempt to process as pure Base64 string
- clean_b64 = image_input.strip()
- return f"data:{default_mime_type};base64,{clean_b64}"
- raise ValueError(f"Unsupported image input type: {type(image_input)}")
- class CaptchaBreaker:
- """
- General LLM Captcha Breaker
- Supports multimodal LLM endpoints based on OpenAI compatible protocol (e.g. Qwen-VL, GPT-4o, DeepSeek-VL, OneAPI, etc.)
- """
- DEFAULT_PROMPT = "You are a precise OCR agent. Please recognize the captcha characters in the image. Output only the captcha text itself, without any explanations or extra characters."
- def __init__(
- self,
- api_key: Optional[str] = None,
- base_url: Optional[str] = None,
- model: Optional[str] = None,
- timeout: Optional[float] = None,
- client: Optional[OpenAI] = None
- ):
- # Config resolution logic:
- # 1. Prefer explicit constructor parameters
- # 2. If CAPTCHA_LLM_CONFIG is a dict, read from it
- # 3. If CAPTCHA_LLM_CONFIG is None or not provided, read from environment variables
- cfg = CAPTCHA_LLM_CONFIG if isinstance(CAPTCHA_LLM_CONFIG, dict) else {}
- self.api_key = api_key or cfg.get("api_key") or os.getenv("CAPTCHA_LLM_API_KEY")
- self.base_url = (
- base_url
- or cfg.get("base_url")
- or os.getenv("CAPTCHA_LLM_BASE_URL", "https://dashscope.aliyuncs.com/compatible-mode/v1")
- )
- self.model = (
- model
- or cfg.get("model")
- or os.getenv("CAPTCHA_LLM_MODEL", "qwen-vl-max")
- )
-
- timeout_val = timeout or cfg.get("timeout") or os.getenv("CAPTCHA_LLM_TIMEOUT")
- self.timeout = float(timeout_val) if timeout_val is not None else 30.0
- self._client = client
- @property
- def client(self) -> OpenAI:
- """Gets or lazily initializes OpenAI client."""
- if self._client is None:
- if not self.api_key:
- raise CaptchaBreakerError(
- "API Key is not configured for CaptchaBreaker. "
- "CAPTCHA_LLM_CONFIG is None and CAPTCHA_LLM_API_KEY environment variable is not set."
- )
- self._client = OpenAI(
- api_key=self.api_key,
- base_url=self.base_url,
- timeout=self.timeout
- )
- return self._client
- def _clean_result(self, text: str) -> str:
- """Cleans LLM response text, stripping markdown code blocks and surrounding quotes."""
- result = text.strip()
- # Strip markdown code block wrappers (e.g. ``` or ```text ... ```)
- if result.startswith("```"):
- lines = result.splitlines()
- if len(lines) >= 2 and lines[-1].strip() == "```":
- result = "\n".join(lines[1:-1]).strip()
- # Strip surrounding quotes
- if (result.startswith('"') and result.endswith('"')) or (result.startswith("'") and result.endswith("'")):
- result = result[1:-1].strip()
- return result
- def break_captcha(
- self,
- image: Union[str, bytes, io.BytesIO],
- prompt: Optional[str] = None,
- temperature: float = 0.0,
- **kwargs: Any
- ) -> str:
- """
- Recognizes captcha from image.
- :param image: Image input (path, raw bytes, BytesIO, or Base64 / Data URL string)
- :param prompt: Prompt text, defaults to general OCR prompt
- :param temperature: Sampling temperature, default 0.0 for determinism
- :param kwargs: Extra parameters forwarded to chat.completions.create
- :return: Recognized captcha text
- :raises CaptchaBreakerError: Raised when call fails or cannot be parsed
- """
- data_url = normalize_image_to_data_url(image)
- used_prompt = prompt or self.DEFAULT_PROMPT
- try:
- response = self.client.chat.completions.create(
- model=self.model,
- messages=[
- {
- "role": "user",
- "content": [
- {"type": "text", "text": used_prompt},
- {
- "type": "image_url",
- "image_url": {
- "url": data_url
- }
- }
- ]
- }
- ],
- temperature=temperature,
- **kwargs
- )
- raw_content = response.choices[0].message.content or ""
- return self._clean_result(raw_content)
- except CaptchaBreakerError:
- raise
- except Exception as e:
- raise CaptchaBreakerError(f"Captcha recognition failed [{self.model}]: {e}") from e
- def __call__(
- self,
- image: Union[str, bytes, io.BytesIO],
- prompt: Optional[str] = None,
- **kwargs: Any
- ) -> str:
- """Enables calling instance directly as a function."""
- return self.break_captcha(image, prompt=prompt, **kwargs)
- if __name__ == "__main__":
- import sys
- print("CaptchaBreaker initialized.")
- if len(sys.argv) > 1:
- test_path = sys.argv[1]
- breaker = CaptchaBreaker()
- print(f"Testing recognition on: {test_path}")
- res = breaker.break_captcha(test_path)
- print(f"Result: {res}")
|