segovia commited on
Commit
fd842a4
·
1 Parent(s): 6c35fb5

first commit

Browse files
.gitignore ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ .env
2
+ .venv/
3
+ __pycache__/
.gradio/certificate.pem ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ -----BEGIN CERTIFICATE-----
2
+ MIIFazCCA1OgAwIBAgIRAIIQz7DSQONZRGPgu2OCiwAwDQYJKoZIhvcNAQELBQAw
3
+ TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh
4
+ cmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwHhcNMTUwNjA0MTEwNDM4
5
+ WhcNMzUwNjA0MTEwNDM4WjBPMQswCQYDVQQGEwJVUzEpMCcGA1UEChMgSW50ZXJu
6
+ ZXQgU2VjdXJpdHkgUmVzZWFyY2ggR3JvdXAxFTATBgNVBAMTDElTUkcgUm9vdCBY
7
+ MTCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIBAK3oJHP0FDfzm54rVygc
8
+ h77ct984kIxuPOZXoHj3dcKi/vVqbvYATyjb3miGbESTtrFj/RQSa78f0uoxmyF+
9
+ 0TM8ukj13Xnfs7j/EvEhmkvBioZxaUpmZmyPfjxwv60pIgbz5MDmgK7iS4+3mX6U
10
+ A5/TR5d8mUgjU+g4rk8Kb4Mu0UlXjIB0ttov0DiNewNwIRt18jA8+o+u3dpjq+sW
11
+ T8KOEUt+zwvo/7V3LvSye0rgTBIlDHCNAymg4VMk7BPZ7hm/ELNKjD+Jo2FR3qyH
12
+ B5T0Y3HsLuJvW5iB4YlcNHlsdu87kGJ55tukmi8mxdAQ4Q7e2RCOFvu396j3x+UC
13
+ B5iPNgiV5+I3lg02dZ77DnKxHZu8A/lJBdiB3QW0KtZB6awBdpUKD9jf1b0SHzUv
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U0Zsnvgc/ubhPgXRR4Xq37Z0j4r7g1SgEEzwxA57d
30
+ emyPxgcYxn/eR44/KJ4EBs+lVDR3veyJm+kXQ99b21/+jh5Xos1AnX5iItreGCc=
31
+ -----END CERTIFICATE-----
.vscode/launch.json ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ // Use IntelliSense to learn about possible attributes.
3
+ // Hover to view descriptions of existing attributes.
4
+ // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5
+ "version": "0.2.0",
6
+ "configurations": [
7
+ {
8
+ "name": "Python Debugger: Current File",
9
+ "type": "debugpy",
10
+ "request": "launch",
11
+ "program": "${file}",
12
+ "console": "integratedTerminal"
13
+ }
14
+ ]
15
+ }
.vscode/settings.json ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ {
2
+ "python.defaultInterpreterPath": "${workspaceFolder}\\.venv\\Scripts\\python.exe",
3
+ "python.terminal.activateEnvironment": true
4
+ }
document_processing.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import base64
4
+ from pathlib import Path
5
+
6
+
7
+ SUPPORTED_EXTENSIONS = {".txt", ".pdf", ".docx", ".jpg", ".jpeg", ".png"}
8
+ IMAGE_EXTENSIONS = {".jpg", ".jpeg", ".png"}
9
+
10
+
11
+ def extract_text(file_path: Path) -> str:
12
+ suffix = file_path.suffix.lower()
13
+
14
+ if suffix == ".txt":
15
+ return file_path.read_text(encoding="utf-8", errors="ignore")
16
+
17
+ if suffix == ".pdf":
18
+ from pypdf import PdfReader
19
+
20
+ reader = PdfReader(str(file_path))
21
+ pages = [page.extract_text() or "" for page in reader.pages]
22
+ return "\n".join(pages)
23
+
24
+ if suffix == ".docx":
25
+ from docx import Document
26
+
27
+ document = Document(str(file_path))
28
+ paragraphs = [paragraph.text for paragraph in document.paragraphs]
29
+ return "\n".join(paragraphs)
30
+
31
+ raise ValueError(f"Unsupported file type: {file_path.suffix}")
32
+
33
+
34
+ def load_documents(folder: Path) -> list[Path]:
35
+ if not folder.exists():
36
+ raise FileNotFoundError(f"Input folder does not exist: {folder}")
37
+
38
+ files = [path for path in folder.rglob("*") if path.is_file() and path.suffix.lower() in SUPPORTED_EXTENSIONS]
39
+ return sorted(files)
40
+
41
+
42
+ def is_image_document(file_path: Path) -> bool:
43
+ return file_path.suffix.lower() in IMAGE_EXTENSIONS
44
+
45
+
46
+ def to_data_uri(file_path: Path) -> str:
47
+ mime_type = {
48
+ ".jpg": "image/jpeg",
49
+ ".jpeg": "image/jpeg",
50
+ ".png": "image/png",
51
+ }.get(file_path.suffix.lower())
52
+
53
+ if not mime_type:
54
+ raise ValueError(f"Unsupported image file type: {file_path.suffix}")
55
+
56
+ encoded = base64.b64encode(file_path.read_bytes()).decode("ascii")
57
+ return f"data:{mime_type};base64,{encoded}"
gradio_app.py ADDED
@@ -0,0 +1,127 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import os
4
+ import csv
5
+ import tempfile
6
+ from pathlib import Path
7
+
8
+ import gradio as gr
9
+
10
+ from document_processing import extract_text, is_image_document, to_data_uri
11
+ from receipt_ai import extract_receipt_json, extract_receipt_json_from_image
12
+ from receipt_results import build_empty_result
13
+
14
+
15
+ RESULT_COLUMNS = [
16
+ "source_file",
17
+ "document_type",
18
+ "fecha",
19
+ "fecha_vencimiento",
20
+ "cuit_proveedor",
21
+ "description_proveedor",
22
+ "moneda",
23
+ "subtotal",
24
+ "taxes",
25
+ "total",
26
+ "confidence",
27
+ ]
28
+
29
+
30
+ def parse_uploaded_file(client, model: str, file_path: str) -> dict:
31
+ path = Path(file_path)
32
+ if is_image_document(path):
33
+ result = extract_receipt_json_from_image(client, model, path, to_data_uri(path))
34
+ else:
35
+ text = extract_text(path)
36
+ if not text.strip():
37
+ return build_empty_result(path.name)
38
+ result = extract_receipt_json(client, model, path, text)
39
+
40
+ result["source_file"] = path.name
41
+ return result
42
+
43
+
44
+ def rows_to_table(rows: list[dict]) -> list[list[object]]:
45
+ return [[row.get(column) for column in RESULT_COLUMNS] for row in rows]
46
+
47
+
48
+ def build_csv_download(rows: list[dict]) -> str:
49
+ temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".csv", mode="w", newline="", encoding="utf-8")
50
+ with temp_file as csv_file:
51
+ writer = csv.DictWriter(csv_file, fieldnames=RESULT_COLUMNS)
52
+ writer.writeheader()
53
+ for row in rows:
54
+ writer.writerow({column: row.get(column) for column in RESULT_COLUMNS})
55
+
56
+ return temp_file.name
57
+
58
+
59
+ def process_files(files: list[str] | None, existing_rows: list[dict] | None, model: str):
60
+ from dotenv import load_dotenv
61
+ from openai import OpenAI
62
+
63
+ load_dotenv()
64
+ client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
65
+
66
+ rows = list(existing_rows or [])
67
+ if not files:
68
+ return rows, gr.update(value=rows_to_table(rows)), "No files selected."
69
+
70
+ for file_path in files:
71
+ rows.append(parse_uploaded_file(client, model, file_path))
72
+
73
+ return rows, gr.update(value=rows_to_table(rows)), f"Parsed {len(files)} file(s). Total rows: {len(rows)}."
74
+
75
+
76
+ def clear_results():
77
+ return [], gr.update(value=[]), "Results cleared."
78
+
79
+
80
+ def build_app() -> gr.Blocks:
81
+ default_model = os.getenv("OPENAI_MODEL", "gpt-4o")
82
+
83
+ with gr.Blocks(title="Document Parser", theme=gr.themes.Soft()) as demo:
84
+ gr.Markdown("# Purchase Receipt Parser\nUpload one or more files and append the extracted receipt data into the grid.")
85
+
86
+ with gr.Row():
87
+ files = gr.File(label="Upload files", file_count="multiple", type="filepath")
88
+ model = gr.Textbox(label="Model", value=default_model)
89
+
90
+ status = gr.Markdown("Ready.")
91
+ results_state = gr.State([])
92
+ results_grid = gr.Dataframe(
93
+ label="Parsed documents",
94
+ headers=RESULT_COLUMNS,
95
+ value=[],
96
+ interactive=False,
97
+ wrap=True,
98
+ )
99
+
100
+ with gr.Row():
101
+ parse_button = gr.Button("Parse documents", variant="primary")
102
+ clear_button = gr.Button("Clear grid")
103
+ download_button = gr.DownloadButton(label="Download CSV", variant="secondary", value=build_csv_download, inputs=[results_state])
104
+
105
+ parse_button.click(
106
+ process_files,
107
+ inputs=[files, results_state, model],
108
+ outputs=[results_state, results_grid, status],
109
+ )
110
+
111
+ clear_button.click(
112
+ clear_results,
113
+ inputs=[],
114
+ outputs=[results_state, results_grid, status],
115
+ )
116
+
117
+ return demo
118
+
119
+
120
+ if __name__ == "__main__":
121
+ share_enabled = True
122
+
123
+ try:
124
+ build_app().launch(share=True)
125
+ except Exception as exc:
126
+ print(f"Share link unavailable ({exc}). Starting local-only Gradio app instead.")
127
+ build_app().launch(share=False)
receipt_ai.py ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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. "
11
+ "Use null for missing values, empty arrays for missing line items, and numbers for amounts. "
12
+ "Prefer the receipt's own values over guesses. "
13
+ "Extract only these fields: fecha, fecha_vencimiento, cuit_proveedor, description_proveedor, moneda, subtotal, taxes, total. "
14
+ "If the document is not a purchase receipt, set document_type to unknown but still capture any purchase-related hints you can find. "
15
+ "Do not add markdown, prose, or extra keys."
16
+ )
17
+
18
+
19
+ def build_user_prompt(file_name: str, source_type: str) -> str:
20
+ return (
21
+ f"File name: {file_name}\n\n"
22
+ f"Source type: {source_type}\n"
23
+ "Important rules:\n"
24
+ "- Read the full document before answering.\n"
25
+ "- Copy amounts as numbers, not strings.\n"
26
+ "- Use ISO 8601 for dates when possible.\n"
27
+ "- Extract only: fecha, fecha_vencimiento, cuit_proveedor, description_proveedor, moneda, subtotal, taxes, total.\n"
28
+ "- If a field cannot be found confidently, set it to null.\n"
29
+ "- Keep output compact and do not include extra fields."
30
+ )
31
+
32
+
33
+ def build_schema() -> dict[str, Any]:
34
+ return {
35
+ "name": "purchase_receipt_extraction",
36
+ "schema": {
37
+ "type": "object",
38
+ "additionalProperties": False,
39
+ "properties": {
40
+ "source_file": {"type": "string"},
41
+ "document_type": {"type": "string", "enum": ["purchase_receipt", "unknown"]},
42
+ "fecha": {"type": ["string", "null"], "description": "ISO 8601 date when possible."},
43
+ "fecha_vencimiento": {"type": ["string", "null"], "description": "ISO 8601 date when possible."},
44
+ "cuit_proveedor": {"type": ["string", "null"]},
45
+ "description_proveedor": {"type": ["string", "null"]},
46
+ "moneda": {"type": ["string", "null"]},
47
+ "subtotal": {"type": ["number", "null"]},
48
+ "taxes": {"type": ["number", "null"]},
49
+ "total": {"type": ["number", "null"]},
50
+ "confidence": {"type": ["number", "null"], "minimum": 0, "maximum": 1},
51
+ },
52
+ "required": [
53
+ "source_file",
54
+ "document_type",
55
+ "fecha",
56
+ "fecha_vencimiento",
57
+ "cuit_proveedor",
58
+ "description_proveedor",
59
+ "moneda",
60
+ "subtotal",
61
+ "taxes",
62
+ "total",
63
+ "confidence",
64
+ ],
65
+ },
66
+ "strict": True,
67
+ }
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}")
receipt_parser.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import argparse
4
+ import json
5
+ import os
6
+ from pathlib import Path
7
+ from typing import Any
8
+
9
+ from document_processing import extract_text, is_image_document, load_documents, to_data_uri
10
+ from receipt_ai import extract_receipt_json, extract_receipt_json_from_image
11
+ from receipt_results import build_empty_result
12
+
13
+
14
+ def parse_args() -> argparse.Namespace:
15
+ parser = argparse.ArgumentParser(description="Extract purchase receipt data from documents using GPT.")
16
+ parser.add_argument("--input", default="documents", help="Folder containing documents to process.")
17
+ parser.add_argument("--output", help="Optional output JSON file. Prints to stdout when omitted.")
18
+ parser.add_argument("--model", default=os.getenv("OPENAI_MODEL", "gpt-4o"), help="OpenAI model name.")
19
+ return parser.parse_args()
20
+
21
+
22
+ def main() -> int:
23
+ from dotenv import load_dotenv
24
+ from openai import OpenAI
25
+
26
+ load_dotenv()
27
+ args = parse_args()
28
+ client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
29
+
30
+ input_folder = Path(args.input)
31
+ files = load_documents(input_folder)
32
+ results: list[dict[str, Any]] = []
33
+
34
+ for file_path in files:
35
+ relative_name = file_path.relative_to(input_folder).as_posix()
36
+ if is_image_document(file_path):
37
+ image_data_uri = to_data_uri(file_path)
38
+ result = extract_receipt_json_from_image(client, args.model, file_path, image_data_uri)
39
+ else:
40
+ text = extract_text(file_path)
41
+ if not text.strip():
42
+ results.append(build_empty_result(relative_name))
43
+ continue
44
+
45
+ result = extract_receipt_json(client, args.model, file_path, text)
46
+
47
+ result["source_file"] = relative_name
48
+ results.append(result)
49
+
50
+ payload = {"input_folder": str(input_folder), "results": results}
51
+ rendered = json.dumps(payload, indent=2, ensure_ascii=False)
52
+
53
+ if args.output:
54
+ output_path = Path(args.output)
55
+ output_path.parent.mkdir(parents=True, exist_ok=True)
56
+ output_path.write_text(rendered, encoding="utf-8")
57
+ else:
58
+ print(rendered)
59
+
60
+ return 0
61
+
62
+
63
+ if __name__ == "__main__":
64
+ raise SystemExit(main())
receipt_results.py ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+
4
+ def build_empty_result(source_file: str) -> dict:
5
+ return {
6
+ "source_file": source_file,
7
+ "document_type": "unknown",
8
+ "fecha": None,
9
+ "fecha_vencimiento": None,
10
+ "cuit_proveedor": None,
11
+ "description_proveedor": None,
12
+ "moneda": None,
13
+ "subtotal": None,
14
+ "taxes": None,
15
+ "total": None,
16
+ "confidence": 0,
17
+ }
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ openai>=1.40.0
2
+ pypdf>=4.2.0
3
+ python-docx>=1.1.2
4
+ python-dotenv>=1.0.1
5
+ gradio>=4.0.0