|
| 1 | +# Google Document AI Document Loader |
| 2 | + |
| 3 | +> Google Document AI transforms unstructured document data into structured, actionable insights using machine learning. |
| 4 | +
|
| 5 | +## Prerequisite |
| 6 | + |
| 7 | +You need Google Cloud credentials and a Document AI processor. You will need: |
| 8 | +- `DOCUMENTAI_GOOGLE_CREDENTIALS` |
| 9 | +- `DOCUMENTAI_LOCATION` |
| 10 | +- `DOCUMENTAI_PROCESSOR_NAME` |
| 11 | + |
| 12 | +```python |
| 13 | +%pip install --upgrade --quiet extract_thinker google-cloud-documentai |
| 14 | +``` |
| 15 | + |
| 16 | +## Basic Usage |
| 17 | + |
| 18 | +Here's a simple example of using the Google Document AI loader: |
| 19 | + |
| 20 | +```python |
| 21 | +from extract_thinker import DocumentLoaderDocumentAI |
| 22 | + |
| 23 | +# Initialize the loader |
| 24 | +loader = DocumentLoaderDocumentAI( |
| 25 | + credentials=os.getenv("DOCUMENTAI_GOOGLE_CREDENTIALS"), |
| 26 | + location=os.getenv("DOCUMENTAI_LOCATION"), |
| 27 | + processor_name=os.getenv("DOCUMENTAI_PROCESSOR_NAME") |
| 28 | +) |
| 29 | + |
| 30 | +# Load CV/Resume content |
| 31 | +content = loader.load_content_from_file("CV_Candidate.pdf") |
| 32 | +``` |
| 33 | + |
| 34 | +## Response Structure |
| 35 | + |
| 36 | +The loader returns a dictionary containing: |
| 37 | +```python |
| 38 | +{ |
| 39 | + "pages": [ |
| 40 | + { |
| 41 | + "content": "Full text content of the page", |
| 42 | + "paragraphs": ["Paragraph 1", "Paragraph 2"], |
| 43 | + "tables": [ |
| 44 | + [ |
| 45 | + ["Header 1", "Header 2"], |
| 46 | + ["Value 1", "Value 2"] |
| 47 | + ] |
| 48 | + ] |
| 49 | + } |
| 50 | + ] |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +## Processing Different Document Types |
| 55 | + |
| 56 | +```python |
| 57 | +# Process forms with tables |
| 58 | +content = loader.load_content_from_file("form_with_tables.pdf") |
| 59 | + |
| 60 | +# Process from stream |
| 61 | +with open("document.pdf", "rb") as f: |
| 62 | + content = loader.load_content_from_stream( |
| 63 | + stream=f, |
| 64 | + mime_type="application/pdf" |
| 65 | + ) |
| 66 | +``` |
| 67 | + |
| 68 | +## Best Practices |
| 69 | + |
| 70 | +1. **Document Types** |
| 71 | + - Use appropriate processor for document type |
| 72 | + - Ensure correct MIME type for streams |
| 73 | + - Validate content structure |
| 74 | + |
| 75 | +2. **Performance** |
| 76 | + - Process in batches when possible |
| 77 | + - Cache results for repeated access |
| 78 | + - Monitor API quotas |
| 79 | + |
| 80 | +Document AI supports PDF, TIFF, GIF, JPEG, PNG with a maximum file size of 20MB or 2000 pages. |
| 81 | + |
| 82 | +For more examples and implementation details, check out the [examples directory](examples/) in the repository. |
0 commit comments