Back to Docling

Docling Service Client With A File

docs/examples/service_client/docling_service_client_file_quickstart.ipynb

2.92.01.4 KB
Original Source

Docling Service Client With A File

Experimental: docling.service_client is experimental and may change in future releases without notice.

This notebook uses docling.service_client against an already running docling-serve instance. It does not start the service.

Set DOCLING_SERVICE_URL before running the cells. Set DOCLING_SERVICE_API_KEY if your service requires authentication.

python
%pip install -qU docling
python
import os
from pathlib import Path

from docling.service_client import DoclingServiceClient

SERVICE_URL = os.environ["DOCLING_SERVICE_URL"]
API_KEY = os.environ.get("DOCLING_SERVICE_API_KEY")
SOURCE_URL = "https://arxiv.org/pdf/2311.18481"
LOCAL_FILE_PATH = os.environ.get("DOCLING_SAMPLE_FILE")

1. Convert a file from a URL

python
with DoclingServiceClient(url=SERVICE_URL, api_key=API_KEY) as client:
    result = client.convert(source=SOURCE_URL)

print(result.document.export_to_markdown())

2. Convert a single local file

Set DOCLING_SAMPLE_FILE to a local file path before running this cell.

python
if LOCAL_FILE_PATH is None:
    raise RuntimeError("Set DOCLING_SAMPLE_FILE to a local file path to run this cell.")

local_file = Path(LOCAL_FILE_PATH)

with DoclingServiceClient(url=SERVICE_URL, api_key=API_KEY) as client:
    local_result = client.convert(source=local_file)

print(local_result.document.export_to_markdown())