Skip to main content

A Python library for batch text extraction and processing using Google Cloud Vertex AI

Project description

PyRTex

CI Python 3.9+

A simple Python library for batch text extraction and processing using Google Cloud Vertex AI.

PyRTex makes it easy to process multiple documents, images, or text snippets with Gemini models and get back structured, type-safe results using Pydantic models.

✨ Features

  • 🚀 Simple API: Just 3 steps - configure, submit, get results
  • 📦 Batch Processing: Process multiple inputs efficiently
  • 🔒 Type Safety: Pydantic models for structured output
  • 🎨 Flexible Templates: Jinja2 templates for prompt engineering
  • ☁️ GCP Integration: Seamless Vertex AI and BigQuery integration
  • 🧪 Testing Mode: Simulate without GCP costs

📦 Installation

Install from PyPI (recommended):

pip install pyrtex

Or install from source:

git clone https://github.com/CaptainTrojan/pyrtex.git
cd pyrtex
pip install -e .

For development:

pip install -e .[dev]

🚀 Quick Start

from pydantic import BaseModel
from pyrtex import Job

# Define your data structures
class TextInput(BaseModel):
    content: str

class Analysis(BaseModel):
    summary: str
    sentiment: str
    key_points: list[str]

# Create a job
job = Job(
    model="gemini-2.0-flash-lite-001",
    output_schema=Analysis,
    prompt_template="Analyze this text: {{ content }}",
    simulation_mode=True  # Set to False for real processing
)

# Add your data
job.add_request("doc1", TextInput(content="Your text here"))
job.add_request("doc2", TextInput(content="Another document"))

# Process and get results
for result in job.submit().wait().results():
    if result.was_successful:
        print(f"Summary: {result.output.summary}")
        print(f"Sentiment: {result.output.sentiment}")
    else:
        print(f"Error: {result.error}")

📋 Core Workflow

PyRTex uses a simple 3-step workflow:

1. Configure & Add Data

job = Job[YourSchema](model="gemini-2.0-flash-lite-001", ...)
job.add_request("key1", YourModel(data="value1"))
job.add_request("key2", YourModel(data="value2"))

2. Submit & Wait

job.submit().wait()  # Can be chained

3. Get Results

for result in job.results():
    if result.was_successful:
        # Use result.output (typed!)
    else:
        # Handle result.error

⚙️ Configuration

For Simulation Mode (No GCP Required)

job = Job(
    model="gemini-2.0-flash-lite-001",
    output_schema=YourSchema,
    prompt_template="Your prompt",
    simulation_mode=True  # No GCP setup needed
)

For Production (GCP Required)

Set your GCP project ID:

export GOOGLE_PROJECT_ID="your-project-id"

Or configure directly in code:

from pyrtex.config import InfrastructureConfig

config = InfrastructureConfig(project_id="your-project-id")
job = Job(
    model="gemini-2.0-flash-lite-001",
    output_schema=YourSchema,
    prompt_template="Your prompt",
    config=config,
    simulation_mode=False
)

Then authenticate with GCP:

gcloud auth application-default login

Troubleshooting

Error: "Project was not passed and could not be determined from the environment"

This happens when GCP project ID is not set. You have three options:

  1. Use simulation mode (recommended for testing):

    simulation_mode=True  # No GCP setup needed
    
  2. Set environment variable:

    export GOOGLE_PROJECT_ID="your-project-id"
    
  3. Configure in code:

    from pyrtex.config import InfrastructureConfig
    config = InfrastructureConfig(project_id="your-project-id")
    job = Job(..., config=config)
    

📚 Examples

The examples/ directory contains complete working examples:

cd examples

# Generate sample files
python generate_sample_data.py

# Extract contact info from business cards
python 01_simple_text_extraction.py

# Parse product catalogs  
python 02_pdf_product_parsing.py

# Extract invoice data from PDFs
python 03_image_description.py

Example Use Cases

  • 📇 Business Cards: Extract contact information
  • 📄 Documents: Process PDFs, images (PNG, JPEG)
  • 🛍️ Product Catalogs: Parse pricing and inventory
  • 🧾 Invoices: Extract structured financial data
  • 📊 Batch Processing: Handle multiple files efficiently

🧪 Development

Running Tests

# All tests (mocked, safe)
./test_runner.sh

# Specific test types
./test_runner.sh --unit
./test_runner.sh --integration
./test_runner.sh --flake

# Real GCP tests (costs money!)
./test_runner.sh --real --project-id your-project-id

Windows users:

test_runner.bat --unit
test_runner.bat --flake

Code Quality

  • flake8: Linting
  • black: Code formatting
  • isort: Import sorting
  • pytest: Testing with coverage

🤝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Run tests: ./test_runner.sh
  5. Submit a pull request

📄 License

MIT License - see LICENSE for details.

🆘 Support

  • Issues: GitHub Issues
  • Examples: Check the examples/ directory
  • Testing: Use simulation_mode=True for development

Common Issues

"Project was not passed and could not be determined from the environment"

  • Solution: Set GOOGLE_PROJECT_ID environment variable or use simulation_mode=True

"Failed to initialize GCP clients"

  • Solution: Run gcloud auth application-default login or use simulation mode

Examples not working

  • Solution: Run python generate_sample_data.py first to create sample files

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyrtex-0.1.4.tar.gz (19.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyrtex-0.1.4-py3-none-any.whl (17.3 kB view details)

Uploaded Python 3

File details

Details for the file pyrtex-0.1.4.tar.gz.

File metadata

  • Download URL: pyrtex-0.1.4.tar.gz
  • Upload date:
  • Size: 19.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for pyrtex-0.1.4.tar.gz
Algorithm Hash digest
SHA256 d09c116fadb29f46b31f339006b10c49fb22f59796a9d2496cbe6f7681490339
MD5 ed38b52448b27a9c73da322fe9be562c
BLAKE2b-256 9e4d99854156dbfc15e2d329046d20fa0304635c1b04ae84ec337a3487afb644

See more details on using hashes here.

File details

Details for the file pyrtex-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: pyrtex-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 17.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for pyrtex-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 de78d99a49aeabc56b74d116a14003d07117a4494e76ad1d2293e0e6f5523268
MD5 de256dd521d5cb408346a53b40bca3a5
BLAKE2b-256 96eb87c2552dccd3b84b882b84cda82ab9a5c278071a95de78ef32cbaaec520f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page