File size: 3,302 Bytes
6c35fb5
028d116
6c35fb5
 
 
 
 
 
 
 
 
 
 
db9f03c
 
 
 
 
 
 
 
 
 
 
 
 
028d116
db9f03c
028d116
 
db9f03c
 
 
 
 
 
 
 
 
 
 
 
 
028d116
 
 
 
 
 
 
db9f03c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
207d271
 
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
---
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 <path>`: 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.