captcha_breaker.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import os
  2. import io
  3. import mimetypes
  4. import base64
  5. from typing import Union, Optional, Any
  6. from openai import OpenAI
  7. from configure import CAPTCHA_LLM_CONFIG
  8. class CaptchaBreakerError(Exception):
  9. """Base exception for captcha breaking."""
  10. pass
  11. def normalize_image_to_data_url(
  12. image_input: Union[str, bytes, io.BytesIO],
  13. default_mime_type: str = "image/png"
  14. ) -> str:
  15. """
  16. Normalizes multiple image input types (file path, raw bytes, BytesIO, Base64 string, Data URL)
  17. into standard Data URL format (data:<mime-type>;base64,<data>) acceptable by multimodal models.
  18. """
  19. if isinstance(image_input, (bytes, bytearray)):
  20. b64_str = base64.b64encode(image_input).decode("utf-8")
  21. return f"data:{default_mime_type};base64,{b64_str}"
  22. if isinstance(image_input, io.BytesIO):
  23. b64_str = base64.b64encode(image_input.getvalue()).decode("utf-8")
  24. return f"data:{default_mime_type};base64,{b64_str}"
  25. if isinstance(image_input, str):
  26. # Already a standard Data URL
  27. if image_input.startswith("data:image/"):
  28. return image_input
  29. # Local file path
  30. if os.path.isfile(image_input):
  31. mime_type, _ = mimetypes.guess_type(image_input)
  32. mime_type = mime_type or default_mime_type
  33. with open(image_input, "rb") as f:
  34. b64_str = base64.b64encode(f.read()).decode("utf-8")
  35. return f"data:{mime_type};base64,{b64_str}"
  36. # Attempt to process as pure Base64 string
  37. clean_b64 = image_input.strip()
  38. return f"data:{default_mime_type};base64,{clean_b64}"
  39. raise ValueError(f"Unsupported image input type: {type(image_input)}")
  40. class CaptchaBreaker:
  41. """
  42. General LLM Captcha Breaker
  43. Supports multimodal LLM endpoints based on OpenAI compatible protocol (e.g. Qwen-VL, GPT-4o, DeepSeek-VL, OneAPI, etc.)
  44. """
  45. 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."
  46. def __init__(
  47. self,
  48. api_key: Optional[str] = None,
  49. base_url: Optional[str] = None,
  50. model: Optional[str] = None,
  51. timeout: Optional[float] = None,
  52. client: Optional[OpenAI] = None
  53. ):
  54. # Config resolution logic:
  55. # 1. Prefer explicit constructor parameters
  56. # 2. If CAPTCHA_LLM_CONFIG is a dict, read from it
  57. # 3. If CAPTCHA_LLM_CONFIG is None or not provided, read from environment variables
  58. cfg = CAPTCHA_LLM_CONFIG if isinstance(CAPTCHA_LLM_CONFIG, dict) else {}
  59. self.api_key = api_key or cfg.get("api_key") or os.getenv("CAPTCHA_LLM_API_KEY")
  60. self.base_url = (
  61. base_url
  62. or cfg.get("base_url")
  63. or os.getenv("CAPTCHA_LLM_BASE_URL", "https://dashscope.aliyuncs.com/compatible-mode/v1")
  64. )
  65. self.model = (
  66. model
  67. or cfg.get("model")
  68. or os.getenv("CAPTCHA_LLM_MODEL", "qwen-vl-max")
  69. )
  70. timeout_val = timeout or cfg.get("timeout") or os.getenv("CAPTCHA_LLM_TIMEOUT")
  71. self.timeout = float(timeout_val) if timeout_val is not None else 30.0
  72. self._client = client
  73. @property
  74. def client(self) -> OpenAI:
  75. """Gets or lazily initializes OpenAI client."""
  76. if self._client is None:
  77. if not self.api_key:
  78. raise CaptchaBreakerError(
  79. "API Key is not configured for CaptchaBreaker. "
  80. "CAPTCHA_LLM_CONFIG is None and CAPTCHA_LLM_API_KEY environment variable is not set."
  81. )
  82. self._client = OpenAI(
  83. api_key=self.api_key,
  84. base_url=self.base_url,
  85. timeout=self.timeout
  86. )
  87. return self._client
  88. def _clean_result(self, text: str) -> str:
  89. """Cleans LLM response text, stripping markdown code blocks and surrounding quotes."""
  90. result = text.strip()
  91. # Strip markdown code block wrappers (e.g. ``` or ```text ... ```)
  92. if result.startswith("```"):
  93. lines = result.splitlines()
  94. if len(lines) >= 2 and lines[-1].strip() == "```":
  95. result = "\n".join(lines[1:-1]).strip()
  96. # Strip surrounding quotes
  97. if (result.startswith('"') and result.endswith('"')) or (result.startswith("'") and result.endswith("'")):
  98. result = result[1:-1].strip()
  99. return result
  100. def break_captcha(
  101. self,
  102. image: Union[str, bytes, io.BytesIO],
  103. prompt: Optional[str] = None,
  104. temperature: float = 0.0,
  105. **kwargs: Any
  106. ) -> str:
  107. """
  108. Recognizes captcha from image.
  109. :param image: Image input (path, raw bytes, BytesIO, or Base64 / Data URL string)
  110. :param prompt: Prompt text, defaults to general OCR prompt
  111. :param temperature: Sampling temperature, default 0.0 for determinism
  112. :param kwargs: Extra parameters forwarded to chat.completions.create
  113. :return: Recognized captcha text
  114. :raises CaptchaBreakerError: Raised when call fails or cannot be parsed
  115. """
  116. data_url = normalize_image_to_data_url(image)
  117. used_prompt = prompt or self.DEFAULT_PROMPT
  118. try:
  119. response = self.client.chat.completions.create(
  120. model=self.model,
  121. messages=[
  122. {
  123. "role": "user",
  124. "content": [
  125. {"type": "text", "text": used_prompt},
  126. {
  127. "type": "image_url",
  128. "image_url": {
  129. "url": data_url
  130. }
  131. }
  132. ]
  133. }
  134. ],
  135. temperature=temperature,
  136. **kwargs
  137. )
  138. raw_content = response.choices[0].message.content or ""
  139. return self._clean_result(raw_content)
  140. except CaptchaBreakerError:
  141. raise
  142. except Exception as e:
  143. raise CaptchaBreakerError(f"Captcha recognition failed [{self.model}]: {e}") from e
  144. def __call__(
  145. self,
  146. image: Union[str, bytes, io.BytesIO],
  147. prompt: Optional[str] = None,
  148. **kwargs: Any
  149. ) -> str:
  150. """Enables calling instance directly as a function."""
  151. return self.break_captcha(image, prompt=prompt, **kwargs)
  152. if __name__ == "__main__":
  153. import sys
  154. print("CaptchaBreaker initialized.")
  155. if len(sys.argv) > 1:
  156. test_path = sys.argv[1]
  157. breaker = CaptchaBreaker()
  158. print(f"Testing recognition on: {test_path}")
  159. res = breaker.break_captcha(test_path)
  160. print(f"Result: {res}")