|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import TYPE_CHECKING, Optional |
| 4 | + |
| 5 | +import httpx |
| 6 | + |
| 7 | +from zai.core import ( |
| 8 | + BaseAPI, |
| 9 | + NOT_GIVEN, |
| 10 | + Body, |
| 11 | + Headers, |
| 12 | + NotGiven, |
| 13 | + deepcopy_minimal, |
| 14 | + make_request_options, |
| 15 | +) |
| 16 | +from zai.types.ocr.layout_parsing_resp import LayoutParsingResp |
| 17 | + |
| 18 | +if TYPE_CHECKING: |
| 19 | + from zai._client import ZaiClient |
| 20 | + |
| 21 | +__all__ = ["LayoutParsing"] |
| 22 | + |
| 23 | + |
| 24 | +class LayoutParsing(BaseAPI): |
| 25 | + """ |
| 26 | + Layout parsing API resource for document/image OCR with layout detection. |
| 27 | + |
| 28 | + This API supports parsing images and PDF documents to extract text content |
| 29 | + with detailed layout information. |
| 30 | + """ |
| 31 | + |
| 32 | + def __init__(self, client: "ZaiClient") -> None: |
| 33 | + super().__init__(client) |
| 34 | + |
| 35 | + def create( |
| 36 | + self, |
| 37 | + *, |
| 38 | + model: str, |
| 39 | + file: str, |
| 40 | + return_crop_images: Optional[bool] | NotGiven = NOT_GIVEN, |
| 41 | + need_layout_visualization: Optional[bool] | NotGiven = NOT_GIVEN, |
| 42 | + start_page_id: Optional[int] | NotGiven = NOT_GIVEN, |
| 43 | + end_page_id: Optional[int] | NotGiven = NOT_GIVEN, |
| 44 | + request_id: Optional[str] | NotGiven = NOT_GIVEN, |
| 45 | + user_id: Optional[str] | NotGiven = NOT_GIVEN, |
| 46 | + extra_headers: Headers | None = None, |
| 47 | + extra_body: Body | None = None, |
| 48 | + timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN, |
| 49 | + ) -> LayoutParsingResp: |
| 50 | + """ |
| 51 | + Parse document or image layout and extract text content. |
| 52 | +
|
| 53 | + Arguments: |
| 54 | + model (str): Model code, e.g., 'GLM-OCR' or 'glm-ocr' |
| 55 | + file (str): URL or base64 encoded image/PDF to parse. |
| 56 | + Supported formats: PDF, JPG, PNG. |
| 57 | + Size limits: Image ≤ 10MB, PDF ≤ 50MB, max 100 pages. |
| 58 | + return_crop_images (Optional[bool]): Whether to return crop images. |
| 59 | + Defaults to False. When True, returns cropped image information. |
| 60 | + need_layout_visualization (Optional[bool]): Whether to return detailed layout visualization results. |
| 61 | + Defaults to False. When True, returns detailed layout image result information. |
| 62 | + start_page_id (Optional[int]): Starting page number for PDF parsing. |
| 63 | + end_page_id (Optional[int]): Ending page number for PDF parsing. |
| 64 | + request_id (Optional[str]): Unique request identifier. Auto-generated if not provided. |
| 65 | + user_id (Optional[str]): End user ID for abuse monitoring. |
| 66 | + Length: 6-128 characters. |
| 67 | + extra_headers (Headers): Additional HTTP headers. |
| 68 | + extra_body (Body): Additional request body parameters. |
| 69 | + timeout (float | httpx.Timeout): Request timeout. |
| 70 | +
|
| 71 | + Returns: |
| 72 | + LayoutParsingResp: Parsed layout result including: |
| 73 | + - id: Task ID |
| 74 | + - created: Unix timestamp |
| 75 | + - model: Model name |
| 76 | + - md_results: Markdown formatted recognition result |
| 77 | + - crop_images: Cropped image information (if return_crop_images=True) |
| 78 | + - layout_visualization: Detailed layout visualization information (if need_layout_visualization=True) |
| 79 | + - data_info: Document metadata (page count, dimensions) |
| 80 | + """ |
| 81 | + if not model: |
| 82 | + raise ValueError("`model` must be provided.") |
| 83 | + if not file: |
| 84 | + raise ValueError("`file` must be provided.") |
| 85 | + |
| 86 | + body = deepcopy_minimal( |
| 87 | + { |
| 88 | + "model": model, |
| 89 | + "file": file, |
| 90 | + "return_crop_images": return_crop_images, |
| 91 | + "need_layout_visualization": need_layout_visualization, |
| 92 | + "start_page_id": start_page_id, |
| 93 | + "end_page_id": end_page_id, |
| 94 | + "request_id": request_id, |
| 95 | + "user_id": user_id, |
| 96 | + } |
| 97 | + ) |
| 98 | + |
| 99 | + return self._post( |
| 100 | + "/layout_parsing", |
| 101 | + body=body, |
| 102 | + options=make_request_options( |
| 103 | + extra_headers=extra_headers, extra_body=extra_body, timeout=timeout |
| 104 | + ), |
| 105 | + cast_type=LayoutParsingResp, |
| 106 | + ) |
0 commit comments