Skip to content

Commit 8b5cdaa

Browse files
code-c-lightmengqian
andauthored
feat: support OCR handwriting recognition feature (#50)
Co-authored-by: mengqian <[email protected]>
1 parent fec2f2b commit 8b5cdaa

File tree

12 files changed

+178
-24
lines changed

12 files changed

+178
-24
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from zai import ZaiClient
2+
3+
client = ZaiClient(
4+
base_url="",
5+
api_key=""
6+
)
7+
8+
9+
def handwriting_ocr_example():
10+
"""
11+
Full Example: Submit image for recognition and wait for the result to be returned.
12+
"""
13+
# Create recognition task
14+
# Please modify the local file path
15+
file_path = 'Your image path'
16+
with open(file_path, 'rb') as f:
17+
print("Submitting a handwriting recognition task ...")
18+
response = client.ocr.handwriting_ocr(
19+
file=f,
20+
tool_type="hand_write",
21+
probability=True
22+
)
23+
print("Task created successfully. Response:")
24+
print(response)
25+
26+
print("Handwriting OCR demo completed.")
27+
28+
29+
if __name__ == "__main__":
30+
print("=== Handwriting recognition quick demo ===\n")
31+
handwriting_ocr_example()

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "zai-sdk"
3-
version = "0.0.4.2"
3+
version = "0.0.4.3"
44
description = "A SDK library for accessing big model apis from Z.ai"
55
authors = ["Z.ai"]
66
readme = "README.md"

src/__init__.py

Whitespace-only changes.

src/zai/_client.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from zai.api_resource.web_search import WebSearchApi
2525
from zai.api_resource.web_reader import WebReaderApi
2626
from zai.api_resource.file_parser import FileParser
27+
from zai.api_resource.ocr import HandwritingOCR
2728

2829
from .core import (
2930
NOT_GIVEN,
@@ -200,6 +201,11 @@ def file_parser(self) -> FileParser:
200201
from zai.api_resource.file_parser import FileParser
201202
return FileParser(self)
202203

204+
@cached_property
205+
def ocr(self) -> HandwritingOCR:
206+
from zai.api_resource.ocr import HandwritingOCR
207+
return HandwritingOCR(self)
208+
203209
@property
204210
@override
205211
def auth_headers(self) -> dict[str, str]:
@@ -208,7 +214,7 @@ def auth_headers(self) -> dict[str, str]:
208214
if self.disable_token_cache:
209215
return {
210216
'Authorization': f'Bearer {api_key}',
211-
'x-source-channel': source_channel,
217+
'x-source-channel': source_channel,
212218
}
213219
else:
214220
return {

src/zai/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
__title__ = 'Z.ai'
2-
__version__ = '0.0.4.2'
2+
__version__ = '0.0.4.3'

src/zai/api_resource/__init__.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,35 @@
1010
Completions,
1111
)
1212
from .embeddings import Embeddings
13+
from .file_parser import FileParser
1314
from .files import Files, FilesWithRawResponse
1415
from .images import Images
1516
from .moderations import Moderations
17+
from .ocr import HandwritingOCR
1618
from .tools import Tools
1719
from .videos import (
1820
Videos,
1921
)
20-
from .web_search import WebSearchApi
2122
from .web_reader import WebReaderApi
22-
from .file_parser import FileParser
23-
23+
from .web_search import WebSearchApi
2424

2525
__all__ = [
26-
'Videos',
27-
'AsyncCompletions',
28-
'Chat',
29-
'Completions',
30-
'Images',
31-
'Embeddings',
32-
'Files',
33-
'FilesWithRawResponse',
34-
'Batches',
35-
'Tools',
36-
'Assistant',
37-
'Audio',
38-
'Moderations',
39-
'WebSearchApi',
40-
'WebReaderApi',
41-
'Agents',
42-
'FileParser',
26+
'Videos',
27+
'AsyncCompletions',
28+
'Chat',
29+
'Completions',
30+
'Images',
31+
'Embeddings',
32+
'Files',
33+
'FilesWithRawResponse',
34+
'Batches',
35+
'Tools',
36+
'Assistant',
37+
'Audio',
38+
'Moderations',
39+
'WebSearchApi',
40+
'WebReaderApi',
41+
'Agents',
42+
'FileParser',
43+
'HandwritingOCR'
4344
]
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
from .file_parser import FileParser
22

3-
__all__ = ['FileParser']
3+
__all__ = ['FileParser']
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from .handwriting_ocr import HandwritingOCR
2+
3+
__all__ = ["HandwritingOCR"]
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
from __future__ import annotations
2+
3+
from typing import TYPE_CHECKING, Mapping, cast
4+
5+
import httpx
6+
from typing_extensions import Literal
7+
from zai.core import (
8+
BaseAPI,
9+
maybe_transform,
10+
NOT_GIVEN,
11+
Body,
12+
Headers,
13+
NotGiven,
14+
FileTypes,
15+
deepcopy_minimal,
16+
extract_files,
17+
make_request_options
18+
)
19+
from zai.types.ocr.handwriting_ocr_params import HandwritingOCRParams
20+
from zai.types.ocr.handwriting_ocr_resp import HandwritingOCRResp
21+
22+
if TYPE_CHECKING:
23+
from zai._client import ZaiClient
24+
25+
__all__ = ["HandwritingOCR"]
26+
27+
28+
class HandwritingOCR(BaseAPI):
29+
30+
def __init__(self, client: "ZaiClient") -> None:
31+
super().__init__(client)
32+
33+
def handwriting_ocr(
34+
self,
35+
*,
36+
file: FileTypes,
37+
tool_type: Literal["hand_write"],
38+
language_type: str = None, # optional,
39+
probability: bool = None,
40+
extra_headers: Headers | None = None,
41+
extra_body: Body | None = None,
42+
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
43+
) -> HandwritingOCRResp:
44+
if not file:
45+
raise ValueError("`file` must be provided.")
46+
if not tool_type:
47+
raise ValueError("`tool_type` must be provided.")
48+
body = deepcopy_minimal(
49+
{
50+
"file": file,
51+
"tool_type": tool_type,
52+
"language_type": language_type,
53+
"probability": probability
54+
}
55+
)
56+
files = extract_files(cast(Mapping[str, object], body), paths=[["file"]])
57+
if files:
58+
extra_headers = {"Content-Type": "multipart/form-data", **(extra_headers or {})}
59+
return self._post(
60+
"/files/ocr",
61+
body=maybe_transform(body, HandwritingOCRParams),
62+
files=files,
63+
options=make_request_options(
64+
extra_headers=extra_headers, extra_body=extra_body, timeout=timeout
65+
),
66+
cast_type=HandwritingOCRResp,
67+
)

src/zai/types/ocr/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)