segovia commited on
Commit
5234643
·
1 Parent(s): 028d116

some fixes

Browse files
app.py CHANGED
@@ -7,7 +7,7 @@ os.environ["GRADIO_SSR_MODE"] = "false"
7
 
8
  import gradio as gr
9
 
10
- from document_orchester import orchestrate_all_active_tenants
11
  from google_sheets_service import GoogleSheetsConfigError, append_row_to_google_sheet
12
 
13
  try:
@@ -28,7 +28,7 @@ def gpu_decorator(fn):
28
  return spaces.GPU(fn)
29
 
30
  @gpu_decorator
31
- def run_orchestrator_endpoint(model: str, include_subfolders: bool, send_to_sheet: bool, api_key: str) -> dict:
32
  """Multi-tenant orchestration endpoint - processes all active tenants and their rules."""
33
  expected_api_key = str(os.getenv("ORCHESTRATOR_API_KEY") or "").strip()
34
  provided_api_key = str(api_key or "").strip()
@@ -44,8 +44,8 @@ def run_orchestrator_endpoint(model: str, include_subfolders: bool, send_to_shee
44
  # Load all active tenants from Firebase
45
  return orchestrate_all_active_tenants(
46
  model=model_name,
47
- include_subfolders=bool(include_subfolders),
48
- send_to_sheet=bool(send_to_sheet),
49
  )
50
  except Exception as exc:
51
  return {
@@ -60,18 +60,7 @@ def build_app() -> gr.Blocks:
60
 
61
  with gr.Accordion("Orquestador API - Multi Tenant", open=False):
62
  gr.Markdown("Endpoint publico para ejecutar escaneo + parseo + persistencia del orquestador en modo multi-tenant (carga todos los tenants activos de Firebase).")
63
- mt_orchestrator_model = gr.Textbox(
64
- label="Modelo OpenAI",
65
- value=os.getenv("OPENAI_MODEL", "gpt-4o"),
66
- )
67
- mt_orchestrator_subfolders = gr.Checkbox(
68
- label="Incluir subcarpetas",
69
- value=True,
70
- )
71
- mt_orchestrator_send = gr.Checkbox(
72
- label="Enviar a Google Sheets",
73
- value=True,
74
- )
75
  mt_orchestrator_api_key = gr.Textbox(
76
  label="API Key",
77
  type="password",
@@ -82,7 +71,7 @@ def build_app() -> gr.Blocks:
82
 
83
  mt_orchestrator_button.click(
84
  run_orchestrator_endpoint,
85
- inputs=[mt_orchestrator_model, mt_orchestrator_subfolders, mt_orchestrator_send, mt_orchestrator_api_key],
86
  outputs=[mt_orchestrator_output],
87
  api_name="orchestrate_all_tenants",
88
  )
 
7
 
8
  import gradio as gr
9
 
10
+ from document_orchestrator import orchestrate_all_active_tenants
11
  from google_sheets_service import GoogleSheetsConfigError, append_row_to_google_sheet
12
 
13
  try:
 
28
  return spaces.GPU(fn)
29
 
30
  @gpu_decorator
31
+ def run_orchestrator_endpoint(model: str, api_key: str) -> dict:
32
  """Multi-tenant orchestration endpoint - processes all active tenants and their rules."""
33
  expected_api_key = str(os.getenv("ORCHESTRATOR_API_KEY") or "").strip()
34
  provided_api_key = str(api_key or "").strip()
 
44
  # Load all active tenants from Firebase
45
  return orchestrate_all_active_tenants(
46
  model=model_name,
47
+ include_subfolders=True,
48
+ send_to_sheet=True,
49
  )
50
  except Exception as exc:
51
  return {
 
60
 
61
  with gr.Accordion("Orquestador API - Multi Tenant", open=False):
62
  gr.Markdown("Endpoint publico para ejecutar escaneo + parseo + persistencia del orquestador en modo multi-tenant (carga todos los tenants activos de Firebase).")
63
+
 
 
 
 
 
 
 
 
 
 
 
64
  mt_orchestrator_api_key = gr.Textbox(
65
  label="API Key",
66
  type="password",
 
71
 
72
  mt_orchestrator_button.click(
73
  run_orchestrator_endpoint,
74
+ inputs=[mt_orchestrator_api_key],
75
  outputs=[mt_orchestrator_output],
76
  api_name="orchestrate_all_tenants",
77
  )
document_orchester.py → document_orchestrator.py RENAMED
@@ -31,7 +31,7 @@ from google_drive_service import (
31
  scan_drive_supported_documents,
32
  )
33
  from google_sheets_service import GoogleSheetsConfigError, append_row_to_google_sheet
34
- from receipt_ai import extract_receipt_json, extract_receipt_json_from_image
35
  from receipt_results import build_empty_result
36
 
37
 
@@ -43,7 +43,7 @@ if not logger.handlers:
43
  handler = logging.StreamHandler()
44
  handler.setFormatter(logging.Formatter("%(asctime)s %(levelname)s %(name)s: %(message)s"))
45
  logger.addHandler(handler)
46
- logger.setLevel(logging.INFO)
47
  logger.propagate = False
48
 
49
 
@@ -132,17 +132,41 @@ def _build_hyperlink_formula(url: str, label: str) -> str:
132
 
133
  def _parse_document(client: OpenAI, model: str, local_path: str, source_file: str) -> dict[str, Any]:
134
  path = Path(local_path)
135
- if is_image_document(path):
136
- parsed = extract_receipt_json_from_image(client, model, path, to_data_uri(path))
137
- else:
138
- text = extract_text(path)
139
- if not text.strip():
140
- parsed = build_empty_result(source_file)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
141
  else:
142
- parsed = extract_receipt_json(client, model, path, text)
143
-
144
- parsed["source_file"] = source_file
145
- return parsed
 
 
 
 
 
 
 
 
146
 
147
 
148
  def _create_default_tenant_from_env() -> tuple[TenantConfig, RuleObject] | None:
 
31
  scan_drive_supported_documents,
32
  )
33
  from google_sheets_service import GoogleSheetsConfigError, append_row_to_google_sheet
34
+ from receipt_ai import extract_receipt_json, extract_receipt_json_from_image, extract_receipt_json_from_pdf, extract_receipt_json_from_document
35
  from receipt_results import build_empty_result
36
 
37
 
 
43
  handler = logging.StreamHandler()
44
  handler.setFormatter(logging.Formatter("%(asctime)s %(levelname)s %(name)s: %(message)s"))
45
  logger.addHandler(handler)
46
+ logger.setLevel(logging.DEBUG)
47
  logger.propagate = False
48
 
49
 
 
132
 
133
  def _parse_document(client: OpenAI, model: str, local_path: str, source_file: str) -> dict[str, Any]:
134
  path = Path(local_path)
135
+
136
+ # Debug: Check if file exists
137
+ if not path.exists():
138
+ logger.error("Document file does not exist: %s", local_path)
139
+ return build_empty_result(source_file)
140
+
141
+ logger.debug("Parsing document: %s (size: %s bytes)", local_path, path.stat().st_size)
142
+
143
+ try:
144
+ # Send all documents in their original format to OpenAI
145
+ file_type = path.suffix.lower()
146
+
147
+ if file_type in {".jpg", ".jpeg", ".png"}:
148
+ # Send image in original format
149
+ logger.debug("Sending image in original format to OpenAI")
150
+ parsed = extract_receipt_json_from_image(client, model, path, to_data_uri(path))
151
+
152
+ elif file_type == ".pdf":
153
+ # Send PDF in original format
154
+ logger.debug("Sending PDF in original format to OpenAI")
155
+ parsed = extract_receipt_json_from_pdf(client, model, path)
156
+
157
  else:
158
+ # Send other document types (TXT, DOCX, etc) in original format
159
+ logger.debug("Sending %s in original format to OpenAI", file_type)
160
+ parsed = extract_receipt_json_from_document(client, model, path)
161
+
162
+ parsed["source_file"] = source_file
163
+ return parsed
164
+
165
+ except Exception as exc:
166
+ logger.exception("Error parsing document %s: %s", local_path, exc)
167
+ parsed = build_empty_result(source_file)
168
+ parsed["source_file"] = source_file
169
+ return parsed
170
 
171
 
172
  def _create_default_tenant_from_env() -> tuple[TenantConfig, RuleObject] | None:
document_processing.py CHANGED
@@ -1,6 +1,7 @@
1
  from __future__ import annotations
2
 
3
  import base64
 
4
  from pathlib import Path
5
 
6
 
 
1
  from __future__ import annotations
2
 
3
  import base64
4
+ import io
5
  from pathlib import Path
6
 
7
 
firebase_tenant_config.py CHANGED
@@ -207,3 +207,17 @@ class FirebaseTenantConfigManager:
207
  tenant_doc = self._get_db().collection(self.collection_name).document(tenant_id)
208
  rules_subcoll = tenant_doc.collection("rules")
209
  rules_subcoll.document(rule_id).delete()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
207
  tenant_doc = self._get_db().collection(self.collection_name).document(tenant_id)
208
  rules_subcoll = tenant_doc.collection("rules")
209
  rules_subcoll.document(rule_id).delete()
210
+
211
+
212
+ # Module-level convenience functions for backward compatibility
213
+ _manager = FirebaseTenantConfigManager()
214
+
215
+
216
+ def load_all_tenants() -> list[TenantConfig]:
217
+ """Load all active tenants from Firebase."""
218
+ return _manager.list_active_tenants()
219
+
220
+
221
+ def load_tenant_rules(tenant_id: str) -> list[RuleObject]:
222
+ """Load all rules for a specific tenant."""
223
+ return _manager.get_all_rules(tenant_id)
receipt_ai.py CHANGED
@@ -1,10 +1,15 @@
1
  from __future__ import annotations
2
 
 
3
  import json
 
4
  from pathlib import Path
5
  from typing import Any
6
 
7
 
 
 
 
8
  EXTRACTION_INSTRUCTIONS = (
9
  "You extract purchase receipt information from a single document. "
10
  "Return one JSON object that matches the schema exactly. "
@@ -68,65 +73,260 @@ def build_schema() -> dict[str, Any]:
68
 
69
 
70
  def extract_receipt_json(client: Any, model: str, file_path: Path, text: str) -> dict[str, Any]:
71
- response = client.responses.create(
72
- model=model,
73
- instructions=EXTRACTION_INSTRUCTIONS,
74
- input=f"{build_user_prompt(file_path.name, 'text')}\n\nDocument text:\n{text[:20000]}",
75
- text={
76
- "format": {
77
- "type": "json_schema",
78
- "name": "purchase_receipt_extraction",
79
- "schema": build_schema()["schema"],
80
- "strict": True,
81
- }
82
- },
83
- )
 
 
 
 
 
 
84
 
85
- output_text = getattr(response, "output_text", None)
86
- if output_text:
87
- return json.loads(output_text)
 
 
 
 
88
 
89
- if response.output and response.output[0].content:
90
- return json.loads(response.output[0].content[0].text)
 
 
 
 
91
 
92
- raise RuntimeError(f"No JSON returned for {file_path.name}")
 
 
 
 
 
 
 
 
93
 
94
 
95
  def extract_receipt_json_from_image(client: Any, model: str, file_path: Path, image_data_uri: str) -> dict[str, Any]:
96
- response = client.responses.create(
97
- model=model,
98
- instructions=EXTRACTION_INSTRUCTIONS,
99
- input=[
100
- {
101
- "role": "user",
102
- "content": [
103
- {
104
- "type": "input_text",
105
- "text": build_user_prompt(file_path.name, "image"),
106
- },
107
- {
108
- "type": "input_image",
109
- "image_url": image_data_uri,
110
- "detail": "high",
111
- },
112
- ],
113
- }
114
- ],
115
- text={
116
- "format": {
117
- "type": "json_schema",
118
- "name": "purchase_receipt_extraction",
119
- "schema": build_schema()["schema"],
120
- "strict": True,
121
- }
122
- },
123
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
124
 
125
- output_text = getattr(response, "output_text", None)
126
- if output_text:
127
- return json.loads(output_text)
 
 
 
 
128
 
129
- if response.output and response.output[0].content:
130
- return json.loads(response.output[0].content[0].text)
 
 
 
 
131
 
132
- raise RuntimeError(f"No JSON returned for {file_path.name}")
 
 
 
 
 
 
 
 
 
1
  from __future__ import annotations
2
 
3
+ import base64
4
  import json
5
+ import logging
6
  from pathlib import Path
7
  from typing import Any
8
 
9
 
10
+ logger = logging.getLogger(__name__)
11
+
12
+
13
  EXTRACTION_INSTRUCTIONS = (
14
  "You extract purchase receipt information from a single document. "
15
  "Return one JSON object that matches the schema exactly. "
 
73
 
74
 
75
  def extract_receipt_json(client: Any, model: str, file_path: Path, text: str) -> dict[str, Any]:
76
+ logger.debug("extract_receipt_json called: file=%s, model=%s, text_length=%s", file_path.name, model, len(text))
77
+
78
+ try:
79
+ logger.debug("Sending text extraction request to OpenAI for %s", file_path.name)
80
+ response = client.responses.create(
81
+ model=model,
82
+ instructions=EXTRACTION_INSTRUCTIONS,
83
+ input=f"{build_user_prompt(file_path.name, 'text')}\n\nDocument text:\n{text[:20000]}",
84
+ text={
85
+ "format": {
86
+ "type": "json_schema",
87
+ "name": "purchase_receipt_extraction",
88
+ "schema": build_schema()["schema"],
89
+ "strict": True,
90
+ }
91
+ },
92
+ )
93
+
94
+ logger.debug("Received response from OpenAI for %s", file_path.name)
95
 
96
+ output_text = getattr(response, "output_text", None)
97
+ if output_text:
98
+ logger.debug("Parsing output_text from response")
99
+ result = json.loads(output_text)
100
+ logger.debug("Successfully parsed JSON: document_type=%s, total=%s, confidence=%s",
101
+ result.get("document_type"), result.get("total"), result.get("confidence"))
102
+ return result
103
 
104
+ if response.output and response.output[0].content:
105
+ logger.debug("Parsing response.output content")
106
+ result = json.loads(response.output[0].content[0].text)
107
+ logger.debug("Successfully parsed JSON: document_type=%s, total=%s, confidence=%s",
108
+ result.get("document_type"), result.get("total"), result.get("confidence"))
109
+ return result
110
 
111
+ logger.error("No JSON returned for %s - no output_text and no response.output", file_path.name)
112
+ raise RuntimeError(f"No JSON returned for {file_path.name}")
113
+
114
+ except json.JSONDecodeError as e:
115
+ logger.error("JSON decode error for %s: %s", file_path.name, e)
116
+ raise
117
+ except Exception as e:
118
+ logger.error("Error in extract_receipt_json for %s: %s", file_path.name, e)
119
+ raise
120
 
121
 
122
  def extract_receipt_json_from_image(client: Any, model: str, file_path: Path, image_data_uri: str) -> dict[str, Any]:
123
+ logger.debug("extract_receipt_json_from_image called: file=%s, model=%s, image_size=%s bytes",
124
+ file_path.name, model, len(image_data_uri))
125
+
126
+ try:
127
+ logger.debug("Sending image extraction request to OpenAI for %s", file_path.name)
128
+ response = client.responses.create(
129
+ model=model,
130
+ instructions=EXTRACTION_INSTRUCTIONS,
131
+ input=[
132
+ {
133
+ "role": "user",
134
+ "content": [
135
+ {
136
+ "type": "input_text",
137
+ "text": build_user_prompt(file_path.name, "image"),
138
+ },
139
+ {
140
+ "type": "input_image",
141
+ "image_url": image_data_uri,
142
+ "detail": "high",
143
+ },
144
+ ],
145
+ }
146
+ ],
147
+ text={
148
+ "format": {
149
+ "type": "json_schema",
150
+ "name": "purchase_receipt_extraction",
151
+ "schema": build_schema()["schema"],
152
+ "strict": True,
153
+ }
154
+ },
155
+ )
156
+
157
+ logger.debug("Received response from OpenAI for image %s", file_path.name)
158
+
159
+ output_text = getattr(response, "output_text", None)
160
+ if output_text:
161
+ logger.debug("Parsing output_text from image response")
162
+ result = json.loads(output_text)
163
+ logger.debug("Successfully parsed image JSON: document_type=%s, total=%s, confidence=%s",
164
+ result.get("document_type"), result.get("total"), result.get("confidence"))
165
+ return result
166
+
167
+ if response.output and response.output[0].content:
168
+ logger.debug("Parsing response.output content from image")
169
+ result = json.loads(response.output[0].content[0].text)
170
+ logger.debug("Successfully parsed image JSON: document_type=%s, total=%s, confidence=%s",
171
+ result.get("document_type"), result.get("total"), result.get("confidence"))
172
+ return result
173
+
174
+ logger.error("No JSON returned for image %s - no output_text and no response.output", file_path.name)
175
+ raise RuntimeError(f"No JSON returned for {file_path.name}")
176
+
177
+ except json.JSONDecodeError as e:
178
+ logger.error("JSON decode error for image %s: %s", file_path.name, e)
179
+ raise
180
+ except Exception as e:
181
+ logger.error("Error in extract_receipt_json_from_image for %s: %s", file_path.name, e)
182
+ raise
183
+
184
+
185
+ def extract_receipt_json_from_pdf(client: Any, model: str, file_path: Path) -> dict[str, Any]:
186
+ """Upload PDF to OpenAI Files API and extract structured data."""
187
+ logger.debug("extract_receipt_json_from_pdf called: file=%s, model=%s", file_path.name, model)
188
+
189
+ file_id = None
190
+ try:
191
+ # 1. Upload PDF to Files API
192
+ logger.debug("Uploading PDF to OpenAI Files API: %s", file_path.name)
193
+ with open(file_path, "rb") as pdf_file:
194
+ uploaded_file = client.files.create(
195
+ file=pdf_file,
196
+ purpose="user_data"
197
+ )
198
+ file_id = uploaded_file.id
199
+ logger.debug("PDF uploaded successfully, file_id: %s", file_id)
200
+
201
+ # 2. Send file_id to model for processing
202
+ logger.debug("Sending PDF to OpenAI via Files API for %s", file_path.name)
203
+ response = client.responses.create(
204
+ model=model,
205
+ instructions=EXTRACTION_INSTRUCTIONS,
206
+ input=[
207
+ {
208
+ "role": "user",
209
+ "content": [
210
+ {
211
+ "type": "input_file",
212
+ "file_id": file_id
213
+ },
214
+ {
215
+ "type": "input_text",
216
+ "text": build_user_prompt(file_path.name, "pdf")
217
+ }
218
+ ]
219
+ }
220
+ ],
221
+ text={
222
+ "format": {
223
+ "type": "json_schema",
224
+ "name": "purchase_receipt_extraction",
225
+ "schema": build_schema()["schema"],
226
+ "strict": True,
227
+ }
228
+ },
229
+ )
230
+
231
+ logger.debug("Received response from OpenAI for PDF %s", file_path.name)
232
+
233
+ output_text = getattr(response, "output_text", None)
234
+ if output_text:
235
+ logger.debug("Parsing output_text from PDF response")
236
+ result = json.loads(output_text)
237
+ logger.debug("Successfully parsed PDF JSON: document_type=%s, total=%s, confidence=%s",
238
+ result.get("document_type"), result.get("total"), result.get("confidence"))
239
+ return result
240
+
241
+ if response.output and response.output[0].content:
242
+ logger.debug("Parsing response.output content from PDF")
243
+ result = json.loads(response.output[0].content[0].text)
244
+ logger.debug("Successfully parsed PDF JSON: document_type=%s, total=%s, confidence=%s",
245
+ result.get("document_type"), result.get("total"), result.get("confidence"))
246
+ return result
247
+
248
+ logger.error("No JSON returned for PDF %s - no output_text and no response.output", file_path.name)
249
+ raise RuntimeError(f"No JSON returned for {file_path.name}")
250
+
251
+ except json.JSONDecodeError as e:
252
+ logger.error("JSON decode error for PDF %s: %s", file_path.name, e)
253
+ raise
254
+ except Exception as e:
255
+ logger.error("Error in extract_receipt_json_from_pdf for %s: %s", file_path.name, e)
256
+ raise
257
+ finally:
258
+ # Clean up uploaded file
259
+ if file_id:
260
+ try:
261
+ logger.debug("Deleting uploaded file: %s", file_id)
262
+ client.files.delete(file_id)
263
+ except Exception as e:
264
+ logger.warning("Failed to delete file %s: %s", file_id, e)
265
+
266
+
267
+ def extract_receipt_json_from_document(client: Any, model: str, file_path: Path) -> dict[str, Any]:
268
+ """Send document type to OpenAI by extracting text (DOCX, TXT, etc)."""
269
+ logger.debug("extract_receipt_json_from_document called: file=%s, model=%s", file_path.name, model)
270
+
271
+ try:
272
+ logger.debug("Extracting text from document: %s", file_path.name)
273
+
274
+ # Extract text based on file type
275
+ if file_path.suffix.lower() == ".txt":
276
+ text = file_path.read_text(encoding="utf-8", errors="ignore")
277
+ logger.debug("Read TXT file: %s characters", len(text))
278
+
279
+ elif file_path.suffix.lower() == ".docx":
280
+ from docx import Document
281
+ document = Document(str(file_path))
282
+ paragraphs = [paragraph.text for paragraph in document.paragraphs]
283
+ text = "\n".join(paragraphs)
284
+ logger.debug("Extracted DOCX file: %s characters", len(text))
285
+
286
+ else:
287
+ logger.warning("Unsupported document format: %s, attempting generic text read", file_path.suffix)
288
+ text = file_path.read_text(encoding="utf-8", errors="ignore")
289
+ logger.debug("Read document file: %s characters", len(text))
290
+
291
+ # Send extracted text to OpenAI
292
+ logger.debug("Sending extracted document text to OpenAI for %s", file_path.name)
293
+ response = client.responses.create(
294
+ model=model,
295
+ instructions=EXTRACTION_INSTRUCTIONS,
296
+ input=f"{build_user_prompt(file_path.name, 'document')}\n\nDocument text:\n{text[:20000]}",
297
+ text={
298
+ "format": {
299
+ "type": "json_schema",
300
+ "name": "purchase_receipt_extraction",
301
+ "schema": build_schema()["schema"],
302
+ "strict": True,
303
+ }
304
+ },
305
+ )
306
+
307
+ logger.debug("Received response from OpenAI for document %s", file_path.name)
308
 
309
+ output_text = getattr(response, "output_text", None)
310
+ if output_text:
311
+ logger.debug("Parsing output_text from document response")
312
+ result = json.loads(output_text)
313
+ logger.debug("Successfully parsed document JSON: document_type=%s, total=%s, confidence=%s",
314
+ result.get("document_type"), result.get("total"), result.get("confidence"))
315
+ return result
316
 
317
+ if response.output and response.output[0].content:
318
+ logger.debug("Parsing response.output content from document")
319
+ result = json.loads(response.output[0].content[0].text)
320
+ logger.debug("Successfully parsed document JSON: document_type=%s, total=%s, confidence=%s",
321
+ result.get("document_type"), result.get("total"), result.get("confidence"))
322
+ return result
323
 
324
+ logger.error("No JSON returned for document %s - no output_text and no response.output", file_path.name)
325
+ raise RuntimeError(f"No JSON returned for {file_path.name}")
326
+
327
+ except json.JSONDecodeError as e:
328
+ logger.error("JSON decode error for document %s: %s", file_path.name, e)
329
+ raise
330
+ except Exception as e:
331
+ logger.error("Error in extract_receipt_json_from_document for %s: %s", file_path.name, e)
332
+ raise