File size: 1,517 Bytes
734b5b4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | 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"
|