Skip to content

GMFT (Table Extraction)

MING YUE edited this page Oct 31, 2024 · 2 revisions

What is GMFT?

GMFT is a toolkit for extracting tables from PDFs, It relies on Microsoft's Table Transformer (TATR), which is trained on a diverse dataset PubTables-1M.

Many Formats

GMFT supports the following export options:

  • Pandas dataframe
  • By extension: markdown, latex, HTML, CSV, JSON, etc.
  • List of text + positions
  • Cropped image of table
  • Table caption

Cropped images can be passed into a vision recognizer, like:

  • GPT-4 vision
  • Mathpix/Adobe/Google/Amazon/Azure/etc.

Cropped image are useful for verifying correctness of output.

Lightweight

No GPU necessary

Because of the few dependencies, gmft is very lightweight. The architecture (Table Transformer) allows GMFT to run on cpu.

High throughput

Benchmark using Colab's cpu indicates an approximate rate of ~1.381 s/page; converting to df takes ~1.168 s/table.

  • The base model, Smock et al.'s Table Transformer, is blazing fast.
  • GMFT focuses on table extraction, so figures, titles, sections, etc. are not extracted.
  • In most cases, OCR is not necessary; pdfs already contain text positional data. Using this existing data drastically speeds up inference. With that being said, gmft can still extract tables from images and scanned pdfs through the image output.
  • PyPDFium2 is chosen for its high throughput and permissive license.

How we use GMFT

1. Environment Setup

  • Library Imports: Standard libraries such as os, re, logging, pandas, numpy, and torch are imported. Additionally, fuzzy matching and platform information libraries are included.
  • Logging Configuration: The logging module is set up to suppress non-essential warnings and retain key information.
  • Device Detection: The get_device() function identifies the optimal device for processing (e.g., CPU, CUDA, or MPS), potentially utilizing GPU for faster computation.

2. GMFT Module Configuration

  • Configuration Classes: TATRTableDetectorConfig and TATRFormatConfig from GMFT are used to set up table detection and formatting.
  • Detector and Formatter Settings:
  • TATRTableDetectorConfig configures the detection threshold (detector_base_threshold) and device.
  • TATRFormatConfig sets parameters like image processor paths, formatter paths, verbosity levels, and device information.
  • Initialization: Instances of AutoTableDetector and AutoTableFormatter are created based on these configurations to manage table detection and formatting within PDFs.

3. Table Processing Functions

  • normalize_headers(df): Standardizes column names in data tables to maintain consistent format for downstream tasks.
  • clean_dataframe(df): Cleans the data by removing empty rows/columns and resetting the index.
  • check_numeric_content(df): Ensures that the table contains numeric data.
  • contains_year(df): Checks if the table includes a year format, crucial for verifying date-based data.
  • validate_data(df): Custom data validation that can be modified as needed.

4. PDF Table Detection and Formatting

  • process_complex_table(table): Uses formatter.format(table) to format detected tables and convert them to Pandas DataFrames, flattening multi-level headers if present.
  • ingest_pdf(pdf_path): Reads the PDF, detecting tables on each page using detector.detect(page) and appends detected tables to a list for further processing.

5. Table Processing and CSV Export

  • process_tables(pdf_path, output_dir): Loops through detected tables, formats, cleans, standardizes, and validates them. Verified tables are saved as CSV files, and processing logs are outputted, including the count of valid tables.
  • convert_pdf_to_csv(pdf_path: str, output_dir: str) -> dict: The main function that processes PDF tables, returning a summary with status, processed table count, and output directory.

6. Program Main Entry

In the if __name__ == "__main__": block, the paths for the PDF and output directory are defined. The convert_pdf_to_csv() function processes the PDF and outputs results, including detected devices.

Clone this wiki locally