Repository navigation
GMFT (Table Extraction)
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.
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.
Because of the few dependencies, gmft is very lightweight. The architecture (Table Transformer) allows GMFT to run on cpu.
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.
- Library Imports: Standard libraries such as
os,re,logging,pandas,numpy, andtorchare imported. Additionally, fuzzy matching and platform information libraries are included. - Logging Configuration: The
loggingmodule 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.
- Configuration Classes:
TATRTableDetectorConfigandTATRFormatConfigfrom GMFT are used to set up table detection and formatting. - Detector and Formatter Settings:
TATRTableDetectorConfigconfigures the detection threshold (detector_base_threshold) and device.TATRFormatConfigsets parameters like image processor paths, formatter paths, verbosity levels, and device information.
- Initialization: Instances of
AutoTableDetectorandAutoTableFormatterare created based on these configurations to manage table detection and formatting within PDFs.
-
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.
-
process_complex_table(table): Usesformatter.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 usingdetector.detect(page)and appends detected tables to a list for further processing.
-
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.
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.