Spaces:
Sleeping
Sleeping
Upload captcha_solver/solvers/audio_solver.py with huggingface_hub
Browse files
captcha_solver/solvers/audio_solver.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Audio captcha solver.
|
| 2 |
+
|
| 3 |
+
Strategy:
|
| 4 |
+
1. faster-whisper (tiny) with beam_size=1, language='en'
|
| 5 |
+
2. If ollama enabled, ask it to clean the digits
|
| 6 |
+
3. Local LLM (Qwen2.5-1.5B) as a digit-extraction fallback
|
| 7 |
+
|
| 8 |
+
We accept:
|
| 9 |
+
- MP3, WAV, OGG (any ffmpeg-decodable audio)
|
| 10 |
+
- Tries to extract ONLY digits if the text is clearly digit-only audio
|
| 11 |
+
- Returns the full transcript otherwise
|
| 12 |
+
"""
|
| 13 |
+
|
| 14 |
+
from __future__ import annotations
|
| 15 |
+
|
| 16 |
+
import re
|
| 17 |
+
from typing import Optional
|
| 18 |
+
|
| 19 |
+
from captcha_solver.solvers.base import BaseSolver, SolveAttempt
|
| 20 |
+
from captcha_solver.utils.image import decode_base64_audio
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
class AudioSolver(BaseSolver):
|
| 24 |
+
name = "audio"
|
| 25 |
+
captcha_type = "audio"
|
| 26 |
+
|
| 27 |
+
def __init__(self, ctx) -> None:
|
| 28 |
+
super().__init__(ctx)
|
| 29 |
+
self._audio: bytes = b""
|
| 30 |
+
|
| 31 |
+
def prepare(self, image_b64: Optional[str], audio_b64: Optional[str], hint: Optional[str]) -> None:
|
| 32 |
+
if not audio_b64:
|
| 33 |
+
self._audio = b""
|
| 34 |
+
return
|
| 35 |
+
try:
|
| 36 |
+
self._audio = decode_base64_audio(audio_b64)
|
| 37 |
+
except Exception as exc:
|
| 38 |
+
self._audio = b""
|
| 39 |
+
self._last_error = f"decode: {exc}"
|
| 40 |
+
|
| 41 |
+
def attempts(self):
|
| 42 |
+
return [
|
| 43 |
+
self._whisper_only,
|
| 44 |
+
self._whisper_plus_llm,
|
| 45 |
+
]
|
| 46 |
+
|
| 47 |
+
def _whisper_only(self) -> SolveAttempt:
|
| 48 |
+
if not self._audio:
|
| 49 |
+
return SolveAttempt(answer="", confidence=0.0, solver_name="audio.whisper", error="no audio")
|
| 50 |
+
text, conf = self.ctx.whisper.transcribe(self._audio, language="en", beam_size=1)
|
| 51 |
+
digits = _extract_digits(text)
|
| 52 |
+
if digits and len(digits) >= 3:
|
| 53 |
+
return SolveAttempt(answer=digits, confidence=max(conf, 0.6), solver_name="audio.whisper.digits")
|
| 54 |
+
if text:
|
| 55 |
+
return SolveAttempt(answer=text, confidence=conf, solver_name="audio.whisper.text")
|
| 56 |
+
return SolveAttempt(answer="", confidence=0.0, solver_name="audio.whisper", error="empty transcript")
|
| 57 |
+
|
| 58 |
+
def _whisper_plus_llm(self) -> SolveAttempt:
|
| 59 |
+
if not self._audio:
|
| 60 |
+
return SolveAttempt(answer="", confidence=0.0, solver_name="audio.whisper_llm", error="no audio")
|
| 61 |
+
text, conf = self.ctx.whisper.transcribe(self._audio, language="en", beam_size=5)
|
| 62 |
+
if not text:
|
| 63 |
+
return SolveAttempt(answer="", confidence=0.0, solver_name="audio.whisper_llm", error="empty")
|
| 64 |
+
try:
|
| 65 |
+
cleaned = self.ctx.qwen.generate(
|
| 66 |
+
f"Extract only the digits/words from this audio transcript. "
|
| 67 |
+
f"Return them in the order spoken, space-separated. No commentary.\n\nTranscript: {text}",
|
| 68 |
+
max_new_tokens=40,
|
| 69 |
+
)
|
| 70 |
+
except Exception as exc:
|
| 71 |
+
return SolveAttempt(answer="", confidence=0.0, solver_name="audio.whisper_llm", error=str(exc))
|
| 72 |
+
cleaned = cleaned.strip()
|
| 73 |
+
if not cleaned:
|
| 74 |
+
return SolveAttempt(answer="", confidence=0.0, solver_name="audio.whisper_llm", error="llm empty")
|
| 75 |
+
return SolveAttempt(answer=cleaned, confidence=min(conf + 0.1, 0.95), solver_name="audio.whisper_llm")
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
def _extract_digits(text: str) -> str:
|
| 79 |
+
digits = re.findall(r"\d", text)
|
| 80 |
+
if len(digits) >= 3:
|
| 81 |
+
return " ".join(digits)
|
| 82 |
+
return ""
|