CoolFace
Apppublic

VTdevelops/bond-text-extraction

sourceHugging Faceupdated 5mo agoView on Hugging Face
0likes
App README

Text Extraction

Python project for extracting bond information (status, identification, interest rate, rating, and more) from PDF documents and serialising the results to XML using the OpenAI Agents SDK.

Features

  • —PDF text extraction powered by pypdf.
  • —Structured bond-information retrieval via the OpenAI Agents SDK with a JSON schema guardrail.
  • —Captures an extended institutional dataset: status, identification codes, product terms, coupon mechanics, and XML-ready summaries.
  • —XML serialisation using lxml with the shape:
xml
  <?xml version='1.0' encoding='utf-8'?>
  <Bonds>
    <Bond>
      <ISIN>...</ISIN>
      <Interest>...</Interest>
      <Rating>...</Rating>
    </Bond>
  </Bonds>
  • —CLI for batch processing and optional XML file output.

Getting Started

This project uses Poetry for dependency management. Ensure you have Poetry installed, then run:

bash
poetry install

Set your OpenAI credentials (requires access to the Agents API):

bash
export OPENAI_API_KEY="your_api_key_here"

Alternatively, copy .env.example to .env, place the key there, and it will be loaded automatically on startup.

Usage

Run the CLI via Poetry, pointing to one or more PDF files:

bash
poetry run text-extraction path/to/doc1.pdf path/to/doc2.pdf --output bonds.xml

Optional flags:

  • —--model <model_name>: override the default gpt-4.1-mini.
  • —--instructions "additional guidance": add inline hints to the LLM.
  • —--instructions-file extra.txt: load extra guidance from a text file.
  • —--output result.xml: write the XML document to disk (otherwise it prints to stdout).

Library Usage

python
from pathlib import Path
from text_extraction import ExtractionPipeline

pipeline = ExtractionPipeline(model="gpt-4.1-mini")
records, xml_doc = pipeline.run([Path("bond.pdf")])
print(records)
print(xml_doc)

records is a list of BondRecord instances spanning identification, product terms, coupon logistics, interest rate, and rating. xml_doc is the XML string shown above.

Gradio App & Deployment

  • —Launch the Gradio interface locally with either python app.py or gradio app.py. The Space uses a two-step portfolio workflow: upload documents, then curate the detected bonds.
  • —Ensure the OPENAI_API_KEY environment variable is defined (or stored in a local .env) before starting the UI so the LLM client can authenticate.
  • —Adjust extracted bond fields directly in the editable table — spanning identification, product terms, coupon logistics, and more — then press Regenerate XML from edited table and use the download button to collect the curated XML payload.
  • —Deploy to a Hugging Face Space (or an existing Space) with a single command:
bash
  gradio deploy

The CLI will prompt you to log in (if needed), select a destination Space, and push the contents of this directory—including app.py and requirements.txt—to Hugging Face.

Notes & Next Steps

  • —The current PDF extraction relies on embedded text; scanned documents without OCR will not work. Integrating an OCR layer (e.g. Tesseract) would address this.
  • —Add automated tests by mocking the OpenAI client to validate prompt formatting and XML generation.
  • —Consider chunking and rate limiting for very large document batches.