문서 파싱(Document Parsing) - 퀵스타트

문서 파싱(Document Parsing) - 퀵스타트

Cohere의 Parse 모델(v2 API)로 문서를 파싱하는 퀵스타트 가이드예요.

출처: 문서

본문

Parse API란

Cohere의 Parse 모델은 비정형 기업 문서(PDF, 이미지, 슬라이드)를 구조화된 Markdown 출력으로 변환해 줘요. 텍스트, 표, 목록, 양식, 이미지, 캡션, 바운딩 박스 좌표까지 추출하죠.

이 퀵스타트 가이드는 Parse 엔드포인트로 문서 이미지를 파싱하는 방법을 보여드려요.

설정

먼저 다음 명령으로 Cohere Python SDK를 설치해요.

pip install -U cohere

다음으로 라이브러리를 import하고 클라이언트를 만들어요.

Cohere Platform

PYTHON

import cohere

co = cohere.ClientV2(
    "COHERE_API_KEY"
)  # Get your free API key here: https://dashboard.cohere.com/api-keys

Private Deployment

PYTHON

import cohere

co = cohere.ClientV2(
    api_key="",  # Leave this blank
    base_url="<YOUR_DEPLOYMENT_URL>",
)

SageMaker

PYTHON

import cohere

co = cohere.SagemakerClientV2(
    aws_region="AWS_REGION",
    aws_access_key="AWS_ACCESS_KEY_ID",
    aws_secret_key="AWS_SECRET_ACCESS_KEY",
    aws_session_token="AWS_SESSION_TOKEN",
)

문서 준비하기

Parse는 문서를 base64로 인코딩된 data URI로 받아들여요. 이미지를 data URI로 변환하세요.

PYTHON

import base64

with open("document.png", "rb") as f:
    b64 = base64.b64encode(f.read()).decode("utf-8")

data_uri = f"data:image/png;base64,{b64}"

문서 파싱하기

문서를 Parse 엔드포인트에 전달해요. 기본적으로 응답에는 Markdown 출력이 들어 있어요.

Cohere Platform

PYTHON

response = co.parse(
    model="parse-v5.0",
    document={"type": "image_url", "image_url": data_uri},
)

for page in response.pages:
    print(page.markdown.content)

Private Deployment

PYTHON

response = co.parse(
    model="parse-v5.0",
    document={"type": "image_url", "image_url": data_uri},
)

for page in response.pages:
    print(page.markdown.content)

SageMaker

PYTHON

response = co.parse(
    model="YOUR_ENDPOINT_NAME",
    document={"type": "image_url", "image_url": data_uri},
)

for page in response.pages:
    print(page.markdown.content)

블록 출력(Blocks Output)

구조화된 콘텐츠 블록을 얻으려면 output_format을 "blocks"로 설정해요. 각 블록에는 type(예: text, table)이 있고, 표에는 바운딩 박스(bounding box)를 포함한 타입별 필드가 붙어요.

PYTHON

response = co.parse(
    model="parse-v5.0",
    document={"type": "image_url", "image_url": data_uri},
    output_format="blocks",
)

for page in response.pages:
    for block in page.blocks:
        if block.type == "text":
            print(block.text.content)
        elif block.type == "table":
            print(f"[Table] bbox={block.table.bounding_box}")
            print(block.table.html)
            print(block.table.description)
        print()

더 보기(Further Resources)

더 알아보기 (Learn more)