VTdevelops/bond-text-extraction
0
1---2title: bond-text-extraction3app_file: app.py4sdk: gradio5sdk_version: 6.0.06---7# Text Extraction8 9Python 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.10 11## Features12 13- PDF text extraction powered by [pypdf](https://pypi.org/project/pypdf/).14- Structured bond-information retrieval via the OpenAI Agents SDK with a JSON schema guardrail.15- Captures an extended institutional dataset: status, identification codes, product terms, coupon mechanics, and XML-ready summaries.16- XML serialisation using `lxml` with the shape:17 ```xml18 <?xml version='1.0' encoding='utf-8'?>19 <Bonds>20 <Bond>21 <ISIN>...</ISIN>22 <Interest>...</Interest>23 <Rating>...</Rating>24 </Bond>25 </Bonds>26 ```27- CLI for batch processing and optional XML file output.28 29## Getting Started30 31This project uses [Poetry](https://python-poetry.org/) for dependency management. Ensure you have Poetry installed, then run:32 33```bash34poetry install35```36 37Set your OpenAI credentials (requires access to the Agents API):38 39```bash40export OPENAI_API_KEY="your_api_key_here"41```42 43Alternatively, copy `.env.example` to `.env`, place the key there, and it will be loaded automatically on startup.44 45## Usage46 47Run the CLI via Poetry, pointing to one or more PDF files:48 49```bash50poetry run text-extraction path/to/doc1.pdf path/to/doc2.pdf --output bonds.xml51```52 53Optional flags:54 55- `--model <model_name>`: override the default `gpt-4.1-mini`.56- `--instructions "additional guidance"`: add inline hints to the LLM.57- `--instructions-file extra.txt`: load extra guidance from a text file.58- `--output result.xml`: write the XML document to disk (otherwise it prints to stdout).59 60## Library Usage61 62```python63from pathlib import Path64from text_extraction import ExtractionPipeline65 66pipeline = ExtractionPipeline(model="gpt-4.1-mini")67records, xml_doc = pipeline.run([Path("bond.pdf")])68print(records)69print(xml_doc)70```71 72`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.73 74## Gradio App & Deployment75 76- 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.77- 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.78- 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.79- Deploy to a Hugging Face Space (or an existing Space) with a single command:80 ```bash81 gradio deploy82 ```83 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.84 85## Notes & Next Steps86 87- 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.88- Add automated tests by mocking the OpenAI client to validate prompt formatting and XML generation.89- Consider chunking and rate limiting for very large document batches.90 