Skip to main content

HN Deep OCR Client

Python SDK for Haina Deep OCR API - PDF parsing and document processing made easy.

Python Version License Code Style

Features

  • PDF file upload with automatic chunking (configurable chunk size)
  • Parsing status polling with optimized timeouts
  • Multi-format result generation (ZIP, JSON, Markdown, Image, Formula, Table)
  • Multiple algorithm support: DeepOCR (default), MinerU, and Chemical engines
  • Comprehensive error handling with custom exception classes
  • Built-in logging configuration
  • Type hints for better IDE support
  • Pydantic Settings for configuration management
  • Connection pooling and HTTP session reuse
  • Intelligent retry mechanism with exponential backoff
  • Granular timeout configuration per operation type
  • Full test coverage

Installation

pip install hn-deepocr-client

Or using uv:

uv add hn-deepocr-client

Quick Start

1. Get API Credentials

Apply for Access Key (AK) and Secret Key (SK) from Haina Platform.

2. Configure Environment Variables

Create a .env file in your project root:

AK=your_access_key_here
SK=your_secret_key_here
PRESIGNED_URL=https://open-sci-datahub.zero2x.org

Or use .env.example as a template:

cp .env.example .env
# Edit .env with your credentials

3. Basic Usage

from hn_deepocr_client import ParserPDF, load_config

config = load_config()

parser = ParserPDF(
    ak=config.AK,
    sk=config.SK,
    presigned_url=config.PRESIGNED_URL,
)

# DeepOCR (default)
result = parser.start(
    file_name="document.pdf",
    file_path="./document.pdf",
)

print(f"Parse completed! DataID: {result['data_id']}")
print(f"Download URLs: {result['download_urls']}")

4. Using Alternative Algorithms (MinerU / Chemical)

# Use MinerU engine instead of DeepOCR
result = parser.start(
    file_name="document.pdf",
    file_path="./document.pdf",
    algorithm="mineru",
)

print(f"MinerU parse completed! DataID: {result['data_id']}")
for file_type, url in result.get("download_urls", {}).items():
    print(f"  {file_type}: {url}")

# Use Chemical engine (same output types as DeepOCR)
result = parser.start(
    file_name="chemistry.pdf",
    file_path="./chemistry.pdf",
    algorithm="chemical",
)

Supported File Types by Algorithm

File Type Description DeepOCR MinerU Chemical
zip Full package (all results) - Yes -
json JSON format Yes Yes Yes
markdown Markdown (no images) Yes Yes Yes
md-figure Markdown (with images) Yes Yes Yes
figure Image files Yes Yes Yes
formula Formula files Yes - Yes
table Table files Yes - Yes

Interface Scene

The multiPartMerge request carries an interfaceScene code identifying the caller scene. Set it via interface_scene on start() / merge_parts() (defaults to 1, open_api):

Code Scene
0 web_page
1 open_api (default)
2 batch_import
3 zeroxlab
result = parser.start(
    file_name="document.pdf",
    file_path="./document.pdf",
    interface_scene=2,  # batch_import
)

Advanced Usage

Logging Configuration

The SDK automatically initializes logging when creating a ParserPDF instance. By default, logs are written to both console and a rotating file (logs/pdf_parser.log).

Customize log level via constructor:

import logging

parser = ParserPDF(
    ak=config.AK,
    sk=config.SK,
    presigned_url=config.PRESIGNED_URL,
    log_level=logging.DEBUG,       # or "DEBUG" string
    log_dir="custom_logs",
    log_file="my_app.log",
)

Customize log level via environment variable (in .env):

LOG_LEVEL=DEBUG
LOG_DIR=logs
LOG_FILE=pdf_parser.log

Valid log levels: DEBUG, INFO, WARNING, ERROR, CRITICAL.

Customize log level programmatically:

from hn_deepocr_client import init_logger

init_logger(log_level="DEBUG", log_dir="logs", log_file="app.log")

Custom Configuration

from hn_deepocr_client import ParserPDF, load_config, ConfigurationError

try:
    config = load_config()
    parser = ParserPDF(ak=config.AK, sk=config.SK, presigned_url=config.PRESIGNED_URL)
    data_id = parser.start("document.pdf", "./document.pdf")
except ConfigurationError as e:
    print(f"Configuration error: {e}")
except UploadError as e:
    print(f"Upload failed: {e}")
except ParseError as e:
    print(f"Parse failed: {e}")

Generate Specific Formats Only

# DeepOCR with custom file types
result = parser.start(
    "document.pdf", "./document.pdf",
    file_types=["json", "markdown"],
)

# MinerU with custom file types
result = parser.start(
    "document.pdf", "./document.pdf",
    algorithm="mineru",
    file_types=["zip", "markdown"],
)

File Upload Validation

The SDK validates files before upload:

  • file_name must match the basename of file_path (e.g., file_name="doc.pdf" with file_path="./doc.pdf")
  • Empty files (0 bytes) are rejected with a clear error message
  • No client-side extension restriction; unsupported formats are rejected by the server
  • part_size must be between 1MB and 100MB (configurable via HN_DEEP_OCR_PART_SIZE)

Direct API Usage

from hn_deepocr_client import create_token

token = create_token(
    ak="your_ak",
    sk="your_sk",
    url="https://api.example.com/upload",
    request_body='{"file":"test.pdf"}',
)

API Reference

ParserPDF

Main class for PDF parsing operations.

Methods

  • __init__(ak, sk, presigned_url, config=None, http_client=None, log_level=None, log_dir="logs", log_file="pdf_parser.log") - Initialize parser with optional logging configuration
  • start(file_name: str, file_path: str, generate_all_formats: bool = True, file_types: list[str] | None = None, parse_timeout: int = 1800, algorithm: str = "deepocr", interface_scene: int = 1) -> dict - Start full parsing workflow, returns {"data_id": ..., "download_urls": {...}}. algorithm accepts "deepocr" (default), "mineru", or "chemical". interface_scene is the caller scene code sent as interfaceScene in multiPartMerge: 0=web_page, 1=open_api (default), 2=batch_import, 3=zeroxlab.
  • get_parsing_status(data_id: str) -> int - Query parsing status (1=parsing, 2=success, 3=failed)
  • generate_zips(data_id: str, file_type: str) -> str - Generate download link for specific format
  • calc_part_count(file_path: str) -> int - Calculate number of chunks for a file

Exception Classes

  • HNDeepOCRError - Base exception class
  • ConfigurationError - Configuration related errors
  • AuthenticationError - Authentication failures
  • UploadError - File upload failures
  • ParseError - Parsing failures

Configuration

Configuration is managed via Pydantic Settings. Available options:

Core Configuration

Option Type Default Description
AK str (required) Access Key
SK str (required) Secret Key
PRESIGNED_URL str https://open-sci-datahub.zero2x.org API base URL
LOG_LEVEL str INFO Log level (DEBUG/INFO/WARNING/ERROR/CRITICAL)
LOG_DIR str logs Log directory
LOG_FILE str pdf_parser.log Log filename

Network Configuration

Option Type Default Description
HN_DEEP_OCR_TIMEOUT_API int 30 API call timeout (seconds)
HN_DEEP_OCR_TIMEOUT_UPLOAD int 300 File upload timeout (seconds)
HN_DEEP_OCR_TIMEOUT_STATUS int 10 Status query timeout (seconds)
HN_DEEP_OCR_TIMEOUT_MERGE int 60 Merge operation timeout (seconds)
HN_DEEP_OCR_MAX_RETRIES_DEFAULT int 3 Default retry count
HN_DEEP_OCR_MAX_RETRIES_UPLOAD int 5 Upload retry count
HN_DEEP_OCR_BASE_DELAY float 1.0 Base delay for retry backoff
HN_DEEP_OCR_CONNECTION_POOL_SIZE int 10 Connection pool size
HN_DEEP_OCR_CONNECTION_MAXSIZE int 20 Maximum connections per pool
HN_DEEP_OCR_PART_SIZE int 10485760 File chunk size in bytes (10MB, valid range: 1MB~100MB)

All network configuration options can be set via environment variables with the HN_DEEP_OCR_ prefix or in your .env file.

Development

Installation

git clone https://github.com/yourusername/hn-deepocr-client.git
cd hn-deepocr-client
uv sync

Running Tests

uv run pytest

With coverage:

uv run pytest --cov=hn_deepocr_client

Code Quality

# Format code
uv run ruff format .

# Lint code
uv run ruff check .

# Fix linting issues
uv run ruff check --fix .

# Type check
uv run mypy .

Examples

See the examples/ directory for usage examples:

  • basic_usage.py - Basic usage example
  • advanced_usage.py - Advanced features and error handling

Documentation

For more detailed documentation, see:

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Support

Acknowledgments

Metadata

Release files for hn-deepocr-client 0.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for hn-deepocr-client 0.5.0
File Size Uploaded Provenance Actions
hn_deepocr_client-0.5.0.tar.gz 2.5 MB Without provenance Details for hn_deepocr_client-0.5.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for hn-deepocr-client 0.5.0
File Interpreter ABI Platform Actions
hn_deepocr_client-0.5.0-py3-none-any.whl Python 3 none any Details for hn_deepocr_client-0.5.0-py3-none-any.whl

Total release size: 2.5 MB

Release files / hn_deepocr_client-0.5.0.tar.gz

Download URL hn_deepocr_client-0.5.0.tar.gz
Size 2.5 MB
Tags Source
SHA-256 checksum
How to use checksums
27117e08bd541baec41b5e06d7889fc748aa0bf0f0bed80898815df1d0a34c27
BLAKE2b-256 checksum
How to use checksums
8037c5e0e34cd77718cc6ab3388ea3012a953e56a4d752455761ad5f333945e1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.28 {"installer":{"name":"uv","version":"0.9.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / hn_deepocr_client-0.5.0-py3-none-any.whl

Download URL hn_deepocr_client-0.5.0-py3-none-any.whl
Size 18.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
43f2368e5a6e36ab80d606aa81202f37243ca1c7d555ff412674e08c888b2996
BLAKE2b-256 checksum
How to use checksums
59b7a5eb81a4a2db2da8eff861cacd59d724210aedc3cda2d9c5859d263ded07
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.28 {"installer":{"name":"uv","version":"0.9.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for hn-deepocr-client 0.5.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page