from PIL import Image from typing import Dict, Any from .base_agent import LLMAgent, AgentResponse class TextExtractionAgent(LLMAgent): name = "text_extraction_agent" description = "Extracts and interprets text from PDF pages using OCR and intelligent text processing" def build_prompt(self, pil_image: Image.Image) -> str: width, height = pil_image.size return f"""Analyze this PDF page image (size: {width}x{height}). What text can be extracted? Describe what you see: 1. What language(s) is the text in? 2. What is the general layout structure? 3. Are there any tables, forms, or special elements? 4. What is the quality of the text - is it clear or blurry? Be specific about the content and any challenges for text extraction.""" def execute(self, pil_image: Image.Image, reasoning: str) -> Dict[str, Any]: import pytesseract try: text = pytesseract.image_to_string(pil_image) return { "text": text, "success": True, "method": "tesseract_ocr", "reasoning": reasoning, "language_detected": self._detect_lang(text), } except Exception as e: return { "text": "", "success": False, "error": str(e), "reasoning": reasoning, } def _detect_lang(self, text: str) -> str: if not text.strip(): return "unknown" return "en"