-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathclassification.py
More file actions
115 lines (92 loc) · 2.6 KB
/
classification.py
File metadata and controls
115 lines (92 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
from typing import Any, Dict, List, Union, cast
from typing_extensions import Literal, NotRequired, TypedDict
from ._config import ClientConfig
from ._types import BaseResponse
from .async_request import AsyncRequest, AsyncRequestConfig
from .request import Request, RequestConfig
class DatasetItem(TypedDict):
type: Literal["text", "image"]
"""
Type of the dataset item: text
"""
value: str
"""
Value of the dataset item
"""
class LabelItem(TypedDict):
key: NotRequired[str]
"""
Optional key for the label
"""
type: Literal["text", "image"]
"""
Type of the label: text
"""
value: str
"""
Value of the label
"""
class ClassificationParams(TypedDict):
dataset: List[DatasetItem]
"""
List of text dataset items to classify
"""
labels: List[LabelItem]
"""
List of text labels for classification
"""
multiple_labels: NotRequired[bool]
"""
Whether to allow multiple labels per item
"""
class ClassificationResponse(BaseResponse):
predictions: List[Union[str, List[str]]]
"""
Classification predictions - single labels or multiple labels per item
"""
class Classification(ClientConfig):
config: RequestConfig
def __init__(
self,
api_key: str,
base_url: str,
headers: Union[Dict[str, str], None] = None,
):
super().__init__(api_key, base_url, headers)
self.config = RequestConfig(
base_url=base_url,
api_key=api_key,
headers=headers,
)
def classify(self, params: ClassificationParams) -> ClassificationResponse:
path = "/classification"
resp = Request(
config=self.config,
path=path,
params=cast(Dict[Any, Any], params),
verb="post",
).perform_with_content()
return resp
class AsyncClassification(ClientConfig):
config: AsyncRequestConfig
def __init__(
self,
api_key: str,
base_url: str,
headers: Union[Dict[str, str], None] = None,
):
super().__init__(api_key, base_url, headers)
self.config = AsyncRequestConfig(
base_url=base_url,
api_key=api_key,
headers=headers,
)
async def classify(self, params: ClassificationParams) -> ClassificationResponse:
path = "/classification"
resp = await AsyncRequest(
config=self.config,
path=path,
params=cast(Dict[Any, Any], params),
verb="post",
).perform_with_content()
return resp