Main class for converting documents to LLM-ready formats.
| 25 | |
| 26 | |
| 27 | class DocumentExtractor: |
| 28 | """Main class for converting documents to LLM-ready formats.""" |
| 29 | |
| 30 | def __init__( |
| 31 | self, |
| 32 | preserve_layout: bool = True, |
| 33 | include_images: bool = True, |
| 34 | ocr_enabled: bool = True, |
| 35 | api_key: Optional[str] = None, |
| 36 | model: Optional[str] = None, |
| 37 | gpu: bool = False |
| 38 | ): |
| 39 | """Initialize the file extractor. |
| 40 | |
| 41 | Args: |
| 42 | preserve_layout: Whether to preserve document layout |
| 43 | include_images: Whether to include images in output |
| 44 | ocr_enabled: Whether to enable OCR for image and PDF processing |
| 45 | api_key: API key for cloud processing (optional). Prefer 'docstrange login' for 10k docs/month; API key from https://app.nanonets.com/#/keys is an alternative |
| 46 | model: Model to use for cloud processing (gemini, openapi) - only for cloud mode |
| 47 | gpu: Force local GPU processing (disables cloud mode, requires GPU) |
| 48 | |
| 49 | Note: |
| 50 | - Cloud mode is the default unless gpu is specified |
| 51 | - Without login or API key, limited calls per day |
| 52 | - For 10k docs/month, run 'docstrange login' (recommended) or use an API key from https://app.nanonets.com/#/keys |
| 53 | """ |
| 54 | self.preserve_layout = preserve_layout |
| 55 | self.include_images = include_images |
| 56 | self.api_key = api_key |
| 57 | self.model = model |
| 58 | self.gpu = gpu |
| 59 | |
| 60 | # Determine processing mode |
| 61 | # Cloud mode is default unless GPU preference is explicitly set |
| 62 | self.cloud_mode = not self.gpu |
| 63 | |
| 64 | # Check GPU availability if GPU preference is set |
| 65 | if self.gpu and not should_use_gpu_processor(): |
| 66 | raise RuntimeError( |
| 67 | "GPU preference specified but no GPU is available. " |
| 68 | "Please ensure CUDA is installed and a compatible GPU is present." |
| 69 | ) |
| 70 | |
| 71 | # Default to True if not explicitly set |
| 72 | if ocr_enabled is None: |
| 73 | self.ocr_enabled = True |
| 74 | else: |
| 75 | self.ocr_enabled = ocr_enabled |
| 76 | |
| 77 | # Try to get API key from environment if not provided |
| 78 | if self.cloud_mode and not self.api_key: |
| 79 | self.api_key = os.environ.get('NANONETS_API_KEY') |
| 80 | |
| 81 | # If still no API key, try to get from cached credentials |
| 82 | if not self.api_key: |
| 83 | try: |
| 84 | from .services.auth_service import get_authenticated_token |
no outgoing calls