RyanWalker277/masters-assignment

★ 0Forks 0PythonGitHub ↗Compare

README

Intelligent Invoice Parser

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.

Table of Contents

Features

  • 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.

Prerequisites

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)

Installing Poppler

On macOS, you can install Poppler using Homebrew:

brew install poppler

Setup Instructions

  1. Clone the repository (if you haven't already):

    git clone <repository_url>
    cd <repository_directory>
  2. Install Poetry dependencies: This project uses Poetry for dependency management. Install the required packages:

    poetry install
  3. Set up your Google API Key: Create a .env file 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.

Running the Application

To start the Flask application, use Poetry to run it:

poetry run flask --app app run --host=0.0.0.0 --port=5001

The application will be accessible at http://127.0.0.1:5001.

API Endpoint

  • Endpoint: /parse_invoice
  • Method: POST
  • Description: Upload one or more invoice files (PDF or images) to extract structured data.
  • Request Body: multipart/form-data with 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, and line_items.

Usage with cURL

You can test the /parse_invoice endpoint using curl.

Single Invoice File Upload

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_invoice

Replace /path/to/your/invoice.pdf with the actual path to your invoice file (e.g., .pdf, .png, .jpg, .jpeg).

Multiple Invoice Files Upload

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_invoice

Replace /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.

Contributors

RyanWalker277

Issues