--- title: Invoice Parser Agent emoji: 📚 colorFrom: pink colorTo: purple sdk: gradio sdk_version: 6.20.0 python_version: '3.12' app_file: app.py pinned: false --- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference ## Drive Folder Scan (Unprocessed Files) The app supports scanning one or more Google Drive folders and parsing only files that were not processed before. ### Required environment variables - `GOOGLE_SERVICE_ACCOUNT_JSON` or `GOOGLE_SERVICE_ACCOUNT_FILE` - `GOOGLE_DRIVE_FOLDER_IDS`: comma-separated Drive folder IDs Optional: - `FIREBASE_TRACK_PROCESSED`: defaults to `true` - `FIREBASE_TRACKER_COLLECTION`: defaults to `processed_files` - `FIREBASE_DOCUMENTS_COLLECTION`: defaults to `processed_documents` - `GOOGLE_DRIVE_FACTURAS_BASE_PATH`: defaults to `Facturas` - `GOOGLE_DRIVE_INVOICE_ROOT_FOLDER_ID`: optional Drive folder id used as root when creating `GOOGLE_DRIVE_FACTURAS_BASE_PATH` ### How it works 1. Click **Scan Drive folders** in the UI. 2. The app recursively scans configured Drive folders. 3. Supported file types are downloaded to a temporary local folder (`.txt`, `.pdf`, `.docx`, `.jpg`, `.jpeg`, `.png`). 4. Files already registered in Firebase are skipped. 5. New files are parsed and appended to the same grid used by uploads. ## Drive Orchestration (Status + modificationDate) The orchestration script scans Drive, checks if a document was already processed, parses only new/modified documents, and saves parsed payload + lifecycle status in Firebase. Before each document is persisted, the file is moved in Drive using this path rule: - `Facturas/yyyyMM` for valid invoices where `fecha` can be parsed. - `Facturas/Corrupted` when required sheet data is missing or `fecha` is not parseable. You can change `Facturas` with `GOOGLE_DRIVE_FACTURAS_BASE_PATH`. ### Status rules - `Parsed`: document was never processed before. - `Modified`: document exists in Firebase but Drive `modifiedTime` changed. - `Sent`: document was parsed and then sent to Google Sheets. ### Run orchestration ```bash python document_orchester.py --model gpt-4o ``` Send parsed rows to Google Sheets and mark them as `Sent`: ```bash python document_orchester.py --model gpt-4o --send ``` Optional flags: - `--no-subfolders`: scan only direct files in configured folders. - `--output `: save orchestration summary JSON to a file. ## Public API Endpoint For Orchestrator The Gradio app exposes a public API endpoint that internally calls the orchestrator flow. Security: - Set `ORCHESTRATOR_API_KEY` in the server environment. - Every endpoint call must include that key in the payload. Endpoint name: - `orchestrate_drive_documents` Local URL example: ```bash curl -X POST "http://localhost:7860/gradio_api/call/orchestrate_drive_documents" \ -H "Content-Type: application/json" \ -d '{"data": ["gpt-4o", true, false, "YOUR_API_KEY"]}' ``` `data` argument order: 1. `model` (string) 2. `include_subfolders` (boolean) 3. `send_to_sheet` (boolean) 4. `api_key` (string) Response payload is the same summary produced by `orchestrate_drive_documents` in `document_orchester.py`. When a row is missing required sheet data, it is written to a worksheet named `corrupted data` instead of the main sheet.