This Flask application uses the Google Gemini API to intelligently extract key information from invoice documents (PDFs or images), including invoice numbers, dates, and line items. The application is designed to handle multiple invoices and return structured JSON output.
- Extracts invoice number, invoice date, and line items (description, quantity, unit price, line total).
- Supports PDF and common image formats (PNG, JPG, JPEG).
- Handles multiple invoice file uploads in a single request.
Before you begin, ensure you have the following installed:
- Python 3.9+ (Python 3.12 recommended)
- Poetry (for dependency management)
- Poppler (required for PDF to image conversion by
pdf2image)
On macOS, you can install Poppler using Homebrew:
brew install poppler-
Clone the repository (if you haven't already):
git clone <repository_url> cd <repository_directory>
-
Install Poetry dependencies: This project uses Poetry for dependency management. Install the required packages:
poetry install
-
Set up your Google API Key: Create a
.envfile in the root directory of the project and add your Google Gemini API key:GOOGLE_API_KEY="YOUR_GEMINI_API_KEY"You can obtain a Gemini API key from Google AI Studio.
To start the Flask application, use Poetry to run it:
poetry run flask --app app run --host=0.0.0.0 --port=5001The application will be accessible at http://127.0.0.1:5001.
- Endpoint:
/parse_invoice - Method:
POST - Description: Upload one or more invoice files (PDF or images) to extract structured data.
- Request Body:
multipart/form-datawith file fields (e.g.,file1,file2, etc.). - Response: A JSON array of objects, where each object represents an extracted invoice with fields like
invoice_number,invoice_date, andline_items.
You can test the /parse_invoice endpoint using curl.
To upload a single PDF or image file:
curl -X POST -F "file1=@/path/to/your/invoice.pdf" http://127.0.0.1:5001/parse_invoiceReplace /path/to/your/invoice.pdf with the actual path to your invoice file (e.g., .pdf, .png, .jpg, .jpeg).
To upload multiple invoice files in a single request:
curl -X POST \
-F "file1=@/path/to/your/invoice1.pdf" \
-F "file2=@/path/to/your/invoice2.png" \
http://127.0.0.1:5001/parse_invoiceReplace /path/to/your/invoice1.pdf and /path/to/your/invoice2.png with the actual paths to your respective invoice files. You can add more -F flags for additional files.