receipt-ocr 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,163 @@
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ share/python-wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+ MANIFEST
28
+
29
+ # PyInstaller
30
+ # Usually these files are written by a python script from a template
31
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
+ *.manifest
33
+ *.spec
34
+
35
+ # Installer logs
36
+ pip-log.txt
37
+ pip-delete-this-directory.txt
38
+
39
+ # Unit test / coverage reports
40
+ htmlcov/
41
+ .tox/
42
+ .nox/
43
+ .coverage
44
+ .coverage.*
45
+ .cache
46
+ nosetests.xml
47
+ coverage.xml
48
+ *.cover
49
+ *.py,cover
50
+ .hypothesis/
51
+ .pytest_cache/
52
+ cover/
53
+
54
+ # Translations
55
+ *.mo
56
+ *.pot
57
+
58
+ # Django stuff:
59
+ *.log
60
+ local_settings.py
61
+ db.sqlite3
62
+ db.sqlite3-journal
63
+
64
+ # Flask stuff:
65
+ instance/
66
+ .webassets-cache
67
+
68
+ # Scrapy stuff:
69
+ .scrapy
70
+
71
+ # Sphinx documentation
72
+ docs/_build/
73
+
74
+ # PyBuilder
75
+ .pybuilder/
76
+ target/
77
+
78
+ # Jupyter Notebook
79
+ .ipynb_checkpoints
80
+
81
+ # IPython
82
+ profile_default/
83
+ ipython_config.py
84
+
85
+ # pyenv
86
+ # For a library or package, you might want to ignore these files since the code is
87
+ # intended to run in multiple environments; otherwise, check them in:
88
+ # .python-version
89
+
90
+ # pipenv
91
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
93
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
94
+ # install all needed dependencies.
95
+ #Pipfile.lock
96
+
97
+ # poetry
98
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
99
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
100
+ # commonly ignored for libraries.
101
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
102
+ #poetry.lock
103
+
104
+ # pdm
105
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
106
+ #pdm.lock
107
+ # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
108
+ # in version control.
109
+ # https://pdm.fming.dev/#use-with-ide
110
+ .pdm.toml
111
+
112
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
113
+ __pypackages__/
114
+
115
+ # Celery stuff
116
+ celerybeat-schedule
117
+ celerybeat.pid
118
+
119
+ # SageMath parsed files
120
+ *.sage.py
121
+
122
+ # Environments
123
+ .env
124
+ .venv
125
+ env/
126
+ venv/
127
+ ENV/
128
+ env.bak/
129
+ venv.bak/
130
+
131
+ # Spyder project settings
132
+ .spyderproject
133
+ .spyproject
134
+
135
+ # Rope project settings
136
+ .ropeproject
137
+
138
+ # mkdocs documentation
139
+ /site
140
+
141
+ # mypy
142
+ .mypy_cache/
143
+ .dmypy.json
144
+ dmypy.json
145
+
146
+ # Pyre type checker
147
+ .pyre/
148
+
149
+ # pytype static type analyzer
150
+ .pytype/
151
+
152
+ # Cython debug symbols
153
+ cython_debug/
154
+
155
+ # PyCharm
156
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
157
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
158
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
159
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
160
+ #.idea/
161
+
162
+ .DS_Store
163
+ receipts/
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Bhimraj Yadav
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,154 @@
1
+ Metadata-Version: 2.4
2
+ Name: receipt-ocr
3
+ Version: 0.1.0
4
+ Summary: A package for extracting structured data from receipts.
5
+ Project-URL: Homepage, https://github.com/bhimrazy/receipt-ocr
6
+ Project-URL: Bug Tracker, https://github.com/bhimrazy/receipt-ocr/issues
7
+ Project-URL: Documentation, https://github.com/bhimrazy/receipt-ocr
8
+ Project-URL: Source Code, https://github.com/bhimrazy/receipt-ocr
9
+ Project-URL: Download, https://github.com/bhimrazy/receipt-ocr
10
+ Author-email: Bhimraj Yadav <bhimrajyadav977@gmail.com>
11
+ License: MIT License
12
+
13
+ Copyright (c) 2025 Bhimraj Yadav
14
+
15
+ Permission is hereby granted, free of charge, to any person obtaining a copy
16
+ of this software and associated documentation files (the "Software"), to deal
17
+ in the Software without restriction, including without limitation the rights
18
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19
+ copies of the Software, and to permit persons to whom the Software is
20
+ furnished to do so, subject to the following conditions:
21
+
22
+ The above copyright notice and this permission notice shall be included in all
23
+ copies or substantial portions of the Software.
24
+
25
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31
+ SOFTWARE.
32
+ License-File: LICENSE
33
+ Keywords: llm,ocr,receipts,tesseract
34
+ Classifier: Development Status :: 4 - Beta
35
+ Classifier: Environment :: Console
36
+ Classifier: Intended Audience :: Developers
37
+ Classifier: License :: OSI Approved :: MIT License
38
+ Classifier: Natural Language :: English
39
+ Classifier: Operating System :: OS Independent
40
+ Classifier: Programming Language :: Python :: 3
41
+ Classifier: Programming Language :: Python :: 3.9
42
+ Classifier: Programming Language :: Python :: 3.10
43
+ Classifier: Programming Language :: Python :: 3.11
44
+ Classifier: Programming Language :: Python :: 3.12
45
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
46
+ Classifier: Topic :: Scientific/Engineering :: Information Analysis
47
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
48
+ Requires-Python: >=3.9
49
+ Requires-Dist: openai
50
+ Requires-Dist: pillow
51
+ Requires-Dist: python-dotenv
52
+ Description-Content-Type: text/markdown
53
+
54
+ # Receipt OCR Engine
55
+
56
+ This repository hosts a script and a Docker-compose setup for performing Optical Character Recognition (OCR) on receipt images.
57
+
58
+ ![image](https://github.com/bhimrazy/receipt-ocr/assets/46085301/305df68d-50d8-41d4-81d0-9324966fb6c9)
59
+
60
+ ## Star History
61
+
62
+ <a href="https://star-history.com/#bhimrazy/receipt-ocr&Date">
63
+ <picture>
64
+ <source media="(prefers-color-scheme: dark)" srcset="https://api.star-history.com/svg?repos=bhimrazy/receipt-ocr&type=Date&theme=dark" />
65
+ <source media="(prefers-color-scheme: light)" srcset="https://api.star-history.com/svg?repos=bhimrazy/receipt-ocr&type=Date" />
66
+ <img alt="Star History Chart" src="https://api.star-history.com/svg?repos=bhimrazy/receipt-ocr&type=Date" />
67
+ </picture>
68
+ </a>
69
+
70
+ ## Prerequisites
71
+
72
+ - Python 3.x
73
+ - Docker
74
+ - Docker-compose
75
+
76
+ ## Installation
77
+
78
+ ### Clone the repository
79
+
80
+ ```bash
81
+ git clone https://github.com/bhimrazy/receipt-ocr.git
82
+ cd receipt-ocr
83
+ ```
84
+
85
+ ### Set up Python environment (if not using Docker)
86
+ - Install [tesseract](https://tesseract-ocr.github.io/tessdoc/Installation.html)
87
+
88
+ ```bash
89
+ # Create and activate a virtual environment (optional but recommended)
90
+ python -m venv venv
91
+ source venv/bin/activate # For Windows, use venv\Scripts\activate
92
+
93
+ # Install dependencies
94
+ pip install -r requirements.txt
95
+ ```
96
+
97
+ ## Usage
98
+
99
+ ### Running the script locally
100
+
101
+ #### `main.py`
102
+
103
+ The `main.py` script performs OCR on an input image of a receipt.
104
+
105
+ ```bash
106
+ python main.py -i images/receipt.jpg
107
+ ```
108
+
109
+ Replace `images/receipt.jpg` with the path to your receipt image.
110
+ >Please ensure that the image is well-lit and that the edges of the receipt are clearly visible and detectable within the image.
111
+ <img src="https://github.com/bhimrazy/receipt-ocr/assets/46085301/2ea009f0-9e15-42b2-9f15-063a8ec169f1" alt="Receipt Image" width="300" height="400">
112
+
113
+
114
+ ### Using Docker-compose
115
+
116
+ The repository includes a Docker-compose setup for running the OCR engine as a service.
117
+
118
+ ```bash
119
+ docker-compose up
120
+ ```
121
+
122
+ Once the service is up and running, you can perform OCR on receipt images by sending a POST request to `http://localhost:8000/ocr/` with the image file.
123
+
124
+ ## API Endpoint
125
+
126
+ The OCR functionality can be accessed via a FastAPI endpoint:
127
+
128
+ - **POST** `/ocr/`: Upload a receipt image file to perform OCR. The response will contain the extracted text from the receipt.
129
+
130
+ Example usage with cURL:
131
+
132
+ ```bash
133
+ curl -X 'POST' \
134
+ 'http://localhost:8000/ocr/' \
135
+ -H 'accept: application/json' \
136
+ -H 'Content-Type: multipart/form-data' \
137
+ -F 'file=@images/paper-cash-sell-receipt-vector-23876532.jpg;type=image/jpeg'
138
+ ```
139
+
140
+ ## Using Gemini
141
+ - Gemini Docs:https://ai.google.dev/tutorials/python_quickstart
142
+ - LinkedIn Post: https://www.linkedin.com/feed/update/urn:li:activity:7145860319150505984/
143
+
144
+ ![image](https://github.com/bhimrazy/receipt-ocr/assets/46085301/ee4a0c82-f134-4a19-a275-93a59c7503b8)
145
+
146
+
147
+ ## License
148
+
149
+ This project is licensed under the terms of the MIT license.
150
+
151
+ ## References
152
+ - [Automatically OCR’ing Receipts and Scans](https://pyimagesearch.com/2021/10/27/automatically-ocring-receipts-and-scans/)
153
+
154
+
@@ -0,0 +1,101 @@
1
+ # Receipt OCR Engine
2
+
3
+ This repository hosts a script and a Docker-compose setup for performing Optical Character Recognition (OCR) on receipt images.
4
+
5
+ ![image](https://github.com/bhimrazy/receipt-ocr/assets/46085301/305df68d-50d8-41d4-81d0-9324966fb6c9)
6
+
7
+ ## Star History
8
+
9
+ <a href="https://star-history.com/#bhimrazy/receipt-ocr&Date">
10
+ <picture>
11
+ <source media="(prefers-color-scheme: dark)" srcset="https://api.star-history.com/svg?repos=bhimrazy/receipt-ocr&type=Date&theme=dark" />
12
+ <source media="(prefers-color-scheme: light)" srcset="https://api.star-history.com/svg?repos=bhimrazy/receipt-ocr&type=Date" />
13
+ <img alt="Star History Chart" src="https://api.star-history.com/svg?repos=bhimrazy/receipt-ocr&type=Date" />
14
+ </picture>
15
+ </a>
16
+
17
+ ## Prerequisites
18
+
19
+ - Python 3.x
20
+ - Docker
21
+ - Docker-compose
22
+
23
+ ## Installation
24
+
25
+ ### Clone the repository
26
+
27
+ ```bash
28
+ git clone https://github.com/bhimrazy/receipt-ocr.git
29
+ cd receipt-ocr
30
+ ```
31
+
32
+ ### Set up Python environment (if not using Docker)
33
+ - Install [tesseract](https://tesseract-ocr.github.io/tessdoc/Installation.html)
34
+
35
+ ```bash
36
+ # Create and activate a virtual environment (optional but recommended)
37
+ python -m venv venv
38
+ source venv/bin/activate # For Windows, use venv\Scripts\activate
39
+
40
+ # Install dependencies
41
+ pip install -r requirements.txt
42
+ ```
43
+
44
+ ## Usage
45
+
46
+ ### Running the script locally
47
+
48
+ #### `main.py`
49
+
50
+ The `main.py` script performs OCR on an input image of a receipt.
51
+
52
+ ```bash
53
+ python main.py -i images/receipt.jpg
54
+ ```
55
+
56
+ Replace `images/receipt.jpg` with the path to your receipt image.
57
+ >Please ensure that the image is well-lit and that the edges of the receipt are clearly visible and detectable within the image.
58
+ <img src="https://github.com/bhimrazy/receipt-ocr/assets/46085301/2ea009f0-9e15-42b2-9f15-063a8ec169f1" alt="Receipt Image" width="300" height="400">
59
+
60
+
61
+ ### Using Docker-compose
62
+
63
+ The repository includes a Docker-compose setup for running the OCR engine as a service.
64
+
65
+ ```bash
66
+ docker-compose up
67
+ ```
68
+
69
+ Once the service is up and running, you can perform OCR on receipt images by sending a POST request to `http://localhost:8000/ocr/` with the image file.
70
+
71
+ ## API Endpoint
72
+
73
+ The OCR functionality can be accessed via a FastAPI endpoint:
74
+
75
+ - **POST** `/ocr/`: Upload a receipt image file to perform OCR. The response will contain the extracted text from the receipt.
76
+
77
+ Example usage with cURL:
78
+
79
+ ```bash
80
+ curl -X 'POST' \
81
+ 'http://localhost:8000/ocr/' \
82
+ -H 'accept: application/json' \
83
+ -H 'Content-Type: multipart/form-data' \
84
+ -F 'file=@images/paper-cash-sell-receipt-vector-23876532.jpg;type=image/jpeg'
85
+ ```
86
+
87
+ ## Using Gemini
88
+ - Gemini Docs:https://ai.google.dev/tutorials/python_quickstart
89
+ - LinkedIn Post: https://www.linkedin.com/feed/update/urn:li:activity:7145860319150505984/
90
+
91
+ ![image](https://github.com/bhimrazy/receipt-ocr/assets/46085301/ee4a0c82-f134-4a19-a275-93a59c7503b8)
92
+
93
+
94
+ ## License
95
+
96
+ This project is licensed under the terms of the MIT license.
97
+
98
+ ## References
99
+ - [Automatically OCR’ing Receipts and Scans](https://pyimagesearch.com/2021/10/27/automatically-ocring-receipts-and-scans/)
100
+
101
+
@@ -0,0 +1,54 @@
1
+
2
+ [project]
3
+ name = "receipt-ocr"
4
+ dynamic = ["version"]
5
+ description = "A package for extracting structured data from receipts."
6
+ authors = [{ name = "Bhimraj Yadav", email = "bhimrajyadav977@gmail.com" }]
7
+ license = { file = "LICENSE" }
8
+ readme = "README.md"
9
+ requires-python = ">=3.9"
10
+ classifiers = [
11
+ "Environment :: Console",
12
+ "Natural Language :: English",
13
+ "Development Status :: 4 - Beta",
14
+ "Intended Audience :: Developers",
15
+ "Topic :: Scientific/Engineering :: Artificial Intelligence",
16
+ "Topic :: Scientific/Engineering :: Information Analysis",
17
+ "License :: OSI Approved :: MIT License",
18
+ "Operating System :: OS Independent",
19
+ "Programming Language :: Python :: 3",
20
+ "Programming Language :: Python :: 3.9",
21
+ "Programming Language :: Python :: 3.10",
22
+ "Programming Language :: Python :: 3.11",
23
+ "Programming Language :: Python :: 3.12",
24
+ "Topic :: Software Development :: Libraries :: Python Modules",
25
+ ]
26
+ keywords = ["ocr", "receipts", "llm", "tesseract"]
27
+ dependencies = ["openai", "python-dotenv", "Pillow"]
28
+
29
+ [project.urls]
30
+ Homepage = "https://github.com/bhimrazy/receipt-ocr"
31
+ "Bug Tracker" = "https://github.com/bhimrazy/receipt-ocr/issues"
32
+ Documentation = "https://github.com/bhimrazy/receipt-ocr"
33
+ "Source Code" = "https://github.com/bhimrazy/receipt-ocr"
34
+ Download = "https://github.com/bhimrazy/receipt-ocr"
35
+
36
+ [project.scripts]
37
+ receipt-ocr = "receipt_ocr.cli:main"
38
+
39
+ [build-system]
40
+ requires = ["hatchling"]
41
+ build-backend = "hatchling.build"
42
+
43
+ [tool.hatch.version]
44
+ path = "src/receipt_ocr/__about__.py"
45
+ pattern = "__version__ = \"(?P<version>.+)\""
46
+
47
+ [tool.hatch.build]
48
+ include = ["/src/receipt_ocr"]
49
+
50
+ [tool.hatch.metadata]
51
+ allow-direct-references = true
52
+
53
+ [dependency-groups]
54
+ dev = ["pytest", "coverage", "codecov", "pytest-cov"]
@@ -0,0 +1,17 @@
1
+ __version__ = "0.1.0"
2
+ __docs__ = "A package for extracting structured data from receipts."
3
+ __author__ = "Bhimraj Yadav"
4
+ __author_email__ = "bhimrajyadav977@gmail.com"
5
+ __license__ = "MIT"
6
+ __copyright__ = "Copyright (c) 2025 Bhimraj Yadav"
7
+ __homepage__ = "https://github.com/bhimrazy/receipt-ocr"
8
+
9
+ __all__ = [
10
+ "__author__",
11
+ "__author_email__",
12
+ "__copyright__",
13
+ "__docs__",
14
+ "__homepage__",
15
+ "__license__",
16
+ "__version__",
17
+ ]
@@ -0,0 +1,9 @@
1
+ from .cli import ReceiptProcessor
2
+ from .parsers import ReceiptParser
3
+ from .providers import OpenAIProvider
4
+
5
+ __all__ = [
6
+ "ReceiptProcessor",
7
+ "OpenAIProvider",
8
+ "ReceiptParser",
9
+ ]
@@ -0,0 +1,89 @@
1
+ import argparse
2
+ import os
3
+ import json
4
+ from .providers import OpenAIProvider
5
+ from .parsers import ReceiptParser
6
+ from dotenv import load_dotenv
7
+
8
+ load_dotenv()
9
+
10
+
11
+ class ReceiptProcessor:
12
+ """
13
+ Process a receipt image and return a structured JSON object.
14
+ """
15
+
16
+ def __init__(self, provider, parser):
17
+ """
18
+ Initialize the receipt processor.
19
+ """
20
+ self.provider = provider
21
+ self.parser = parser
22
+
23
+ def process_receipt(self, image_path: str, json_schema: dict, model: str) -> dict:
24
+ """
25
+ Process a receipt image and return a structured JSON object.
26
+ """
27
+ response = self.provider.get_response(image_path, json_schema, model)
28
+ return self.parser.parse(response)
29
+
30
+
31
+ def main():
32
+ """
33
+ Main function for the CLI.
34
+ """
35
+ parser = argparse.ArgumentParser(
36
+ description="Extract information from a receipt image."
37
+ )
38
+ parser.add_argument("image_path", type=str, help="The path to the receipt image.")
39
+ parser.add_argument(
40
+ "--schema_path", type=str, help="The path to a custom JSON schema file."
41
+ )
42
+ parser.add_argument(
43
+ "--model",
44
+ type=str,
45
+ default="gpt-4-vision-preview",
46
+ help="The model to use for the LLM.",
47
+ )
48
+ parser.add_argument("--api_key", type=str, help="The API key for the LLM provider.")
49
+ parser.add_argument(
50
+ "--base_url", type=str, help="The base URL for the LLM provider."
51
+ )
52
+ args = parser.parse_args()
53
+
54
+ # Load the JSON schema
55
+ if args.schema_path:
56
+ with open(args.schema_path, "r") as f:
57
+ json_schema = json.load(f)
58
+ else:
59
+ json_schema = {
60
+ "merchant_name": "string",
61
+ "merchant_address": "string",
62
+ "transaction_date": "string",
63
+ "transaction_time": "string",
64
+ "total_amount": "number",
65
+ "line_items": [
66
+ {
67
+ "item_name": "string",
68
+ "item_quantity": "number",
69
+ "item_price": "number",
70
+ }
71
+ ],
72
+ }
73
+
74
+ # Initialize the provider and parser
75
+ provider = OpenAIProvider(api_key=args.api_key, base_url=args.base_url)
76
+ parser = ReceiptParser()
77
+
78
+ # Initialize the processor
79
+ processor = ReceiptProcessor(provider, parser)
80
+
81
+ # Process the receipt
82
+ result = processor.process_receipt(args.image_path, json_schema, args.model)
83
+
84
+ # Print the result
85
+ print(json.dumps(result, indent=4))
86
+
87
+
88
+ if __name__ == "__main__":
89
+ main()
@@ -0,0 +1,23 @@
1
+ import json
2
+
3
+
4
+ class ReceiptParser:
5
+ """
6
+ Parser for the LLM's response.
7
+ """
8
+
9
+ def parse(self, response: str) -> dict:
10
+ """
11
+ Parse the LLM's response and return a JSON object.
12
+ """
13
+ try:
14
+ # The response is often wrapped in a code block
15
+ response = response.strip()
16
+ if response.startswith("```json"):
17
+ response = response[7:-4]
18
+ elif response.startswith("```"):
19
+ response = response[3:-3]
20
+ return json.loads(response)
21
+ except json.JSONDecodeError:
22
+ # Handle the case where the response is not valid JSON
23
+ return {"error": "The LLM's response was not valid JSON."}
@@ -0,0 +1,35 @@
1
+ SYSTEM_PROMPT = """
2
+ You are a world-class receipt processing expert. Your task is to accurately extract information from a receipt image and provide it in a structured JSON format.
3
+
4
+ Here is an example of a desired JSON output:
5
+
6
+ ```json
7
+ {{
8
+ "merchant_name": "Example Store",
9
+ "merchant_address": "123 Main St, Anytown, USA 12345",
10
+ "transaction_date": "2023-01-01",
11
+ "transaction_time": "12:34:56",
12
+ "total_amount": 75.50,
13
+ "line_items": [
14
+ {{
15
+ "item_name": "Item 1",
16
+ "item_quantity": 2,
17
+ "item_price": 20.00
18
+ }},
19
+ {{
20
+ "item_name": "Item 2",
21
+ "item_quantity": 1,
22
+ "item_price": 35.50
23
+ }}
24
+ ]
25
+ }}
26
+ ```
27
+
28
+ Please extract the information from the receipt image and provide it in the following JSON schema:
29
+
30
+ ```json
31
+ {{json_schema_content}}
32
+ ```
33
+ """
34
+
35
+ USER_PROMPT = "Please extract the information from this receipt image."
@@ -0,0 +1,73 @@
1
+ import os
2
+ from abc import ABC, abstractmethod
3
+ from openai import OpenAI
4
+ from PIL import Image
5
+ import base64
6
+ import io
7
+ import json
8
+ from .prompts import SYSTEM_PROMPT, USER_PROMPT
9
+
10
+
11
+ class LLMProvider(ABC):
12
+ """
13
+ Abstract base class for LLM providers.
14
+ """
15
+
16
+ @abstractmethod
17
+ def get_response(self, image_path: str, json_schema: dict, model: str) -> str:
18
+ """
19
+ Get the response from the LLM provider.
20
+ """
21
+ pass
22
+
23
+
24
+ class OpenAIProvider(LLMProvider):
25
+ """
26
+ LLM provider for OpenAI-compatible APIs.
27
+ """
28
+
29
+ def __init__(self, api_key: str = None, base_url: str = None):
30
+ """
31
+ Initialize the OpenAI provider.
32
+ """
33
+ self.api_key = api_key or os.getenv("OPENAI_API_KEY")
34
+ self.base_url = base_url or os.getenv("OPENAI_BASE_URL")
35
+ self.client = OpenAI(api_key=self.api_key, base_url=self.base_url)
36
+
37
+ def get_response(self, image_path: str, json_schema: dict, model: str) -> str:
38
+ """
39
+ Get the response from the OpenAI API.
40
+ """
41
+ # Load the image
42
+ image = Image.open(image_path)
43
+
44
+ # Convert the image to base64
45
+ buffered = io.BytesIO()
46
+ image.save(buffered, format="PNG")
47
+ img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
48
+
49
+ # Create the system prompt
50
+ system_prompt = SYSTEM_PROMPT
51
+
52
+ response = self.client.chat.completions.create(
53
+ model=model,
54
+ response_format={"type": "json_object"},
55
+ messages=[
56
+ {
57
+ "role": "system",
58
+ "content": system_prompt,
59
+ },
60
+ {
61
+ "role": "user",
62
+ "content": [
63
+ {"type": "text", "text": USER_PROMPT},
64
+ {
65
+ "type": "image_url",
66
+ "image_url": {"url": f"data:image/png;base64,{img_str}"},
67
+ },
68
+ ],
69
+ },
70
+ ],
71
+ )
72
+
73
+ return response.choices[0].message.content