문서 파싱(Document Parsing) - 베스트 프랙티스
문서 파싱(Document Parsing) - 베스트 프랙티스
Cohere Parse API를 사용할 때 이미지 형식, 해상도, 처리량(throughput)에 관한 베스트 프랙티스예요.
출처: 문서
본문
빠른 권장사항
| 사용 사례 | 형식 | 리사이즈 | 참고 |
|---|---|---|---|
| 일반적인 파싱(권장) | WebP 90 | 2048/1536px 긴 변 | 대부분의 워크로드에서 좋은 균형을 제공해요 |
| 표, 금융, 고정밀 문서 | PNG 또는 JPEG 95 | 2048px 긴 변 | 가는 선과 셀 경계를 보존해요 |
처리량을 위해: 보내기 전에 긴 변을 1536px로 리사이즈하세요. 파싱 품질 손실은 최소이면서 처리량은 크게 개선돼요.
리사이즈 및 변환
핵심 작업은 thumbnail인데, 이는 가로세로 비율을 유지하면서 제자리에서 리사이즈해 줘요:
PYTHON
from PIL import Image
IMG_MAX_SIZE = 2048
with Image.open("page.png") as img:
img.thumbnail(
(IMG_MAX_SIZE, IMG_MAX_SIZE), Image.Resampling.LANCZOS
)
if img.mode != "RGB":
img = img.convert("RGB")
img.save("page.webp", format="WEBP", quality=90)
Parse 요청 보내기
PYTHON
import os
import base64
import cohere
co = cohere.ClientV2(
"COHERE_API_KEY"
) # Get your free API key here: https://dashboard.cohere.com/api-keys
with open("page.webp", "rb") as f:
b64 = base64.b64encode(f.read()).decode()
data_uri = f"data:image/webp;base64,{b64}"
response = co.parse(
model="parse-v5.0",
document={"type": "image_url", "image_url": data_uri},
)
for page in response.pages:
print(page.markdown.content)