ae3-tl / extract.py
usmane3's picture
Full extraction: extract.py
aa0d4bb verified
Raw History Blame Contribute Delete
1.43 kB
"""Read tables out of a PDF.
pip install -r requirements.txt
python extract.py paper.pdf --out tables/
"""
import argparse
import asyncio
import json
import os
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parent
sys.path.insert(0, str(ROOT / "src"))
os.environ.setdefault("AE3_WEIGHTS_DIR", str(ROOT / "weights"))
os.environ.setdefault("AE3_PDF_INPUT", "1")
from ae3_tl.backends.engine_ort import EngineOrt
from ae3_tl.pdf.pdfpool import PdfPool
from ae3_tl.serve import extract as serve_extract
from ae3_tl.serve.settings import load_settings
def main() -> int:
ap = argparse.ArgumentParser(description="Read tables out of a PDF.")
ap.add_argument("pdf", nargs="+")
ap.add_argument("--out", default="tables")
args = ap.parse_args()
settings = load_settings()
engine = EngineOrt(settings)
pool = PdfPool(settings.pdf_workers, page_timeout_s=settings.page_timeout_s)
out = Path(args.out)
out.mkdir(parents=True, exist_ok=True)
for name in args.pdf:
document = asyncio.run(serve_extract.extract(engine, settings, pool, Path(name).read_bytes()))
target = out / (Path(name).stem + ".json")
target.write_text(json.dumps(document, indent=2, ensure_ascii=False))
print(f"{name}: {document['pages_total']} pages, {document['tables_total']} tables -> {target}")
return 0
if __name__ == "__main__":
raise SystemExit(main())