-
Notifications
You must be signed in to change notification settings - Fork 261
Expand file tree
/
Copy pathcontent_understanding_client.py
More file actions
403 lines (344 loc) · 15.5 KB
/
content_understanding_client.py
File metadata and controls
403 lines (344 loc) · 15.5 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
import requests
from requests.models import Response
import logging
import json
import time
from pathlib import Path
# Module-level constant: corrupted control-char → intended Unicode mapping.
# The CU analyzeBinary API (v2025-11-01) intermittently strips the high byte
# from Unicode characters (e.g. U+2019 → U+0019). This dict maps each known
# corrupted control character back to its intended equivalent.
_CU_REPLACEMENTS = {
'\u0014': '\u2014', # em dash
'\u0019': '\u2019', # right single quotation mark
'\u001a': '\u201a', # single low-9 quotation mark
'\u001c': '\u201c', # left double quotation mark
'\u001d': '\u201d', # right double quotation mark
'\u001e': '\u201e', # double low-9 quotation mark
}
_CU_BAD_CHARS = set(_CU_REPLACEMENTS.keys())
def sanitize_cu_output(text):
"""Replace corrupted control characters that CU may emit.
Returns *text* unchanged (no-op) when none of the known corrupted
characters are present. The replacement mapping is allocated once
at module level to avoid per-call overhead.
"""
if not text or _CU_BAD_CHARS.isdisjoint(text):
return text
for bad, good in _CU_REPLACEMENTS.items():
text = text.replace(bad, good)
return text
class AzureContentUnderstandingClient:
def __init__(
self,
endpoint: str,
api_version: str,
subscription_key: str = None,
token_provider: callable = None,
x_ms_useragent: str = "cu-sample-code",
):
if not subscription_key and not token_provider:
raise ValueError(
"Either subscription key or token provider must be provided."
)
if not api_version:
raise ValueError("API version must be provided.")
if not endpoint:
raise ValueError("Endpoint must be provided.")
self._endpoint = endpoint.rstrip("/")
self._api_version = api_version
self._logger = logging.getLogger(__name__)
self._headers = self._get_headers(
subscription_key, token_provider(), x_ms_useragent
)
def _get_analyzer_url(self, endpoint, api_version, analyzer_id):
return f"{endpoint}/contentunderstanding/analyzers/{analyzer_id}?api-version={api_version}"
def _get_analyzer_list_url(self, endpoint, api_version):
return f"{endpoint}/contentunderstanding/analyzers?api-version={api_version}"
def _get_analyze_url(self, endpoint, api_version, analyzer_id):
return f"{endpoint}/contentunderstanding/analyzers/{analyzer_id}:analyze?api-version={api_version}"
def _get_analyze_binary_url(self, endpoint, api_version, analyzer_id):
return f"{endpoint}/contentunderstanding/analyzers/{analyzer_id}:analyzeBinary?api-version={api_version}"
def _get_defaults_url(self, endpoint, api_version):
return f"{endpoint}/contentunderstanding/defaults?api-version={api_version}"
def _get_training_data_config(
self, storage_container_sas_url, storage_container_path_prefix
):
return {
"containerUrl": storage_container_sas_url,
"kind": "blob",
"prefix": storage_container_path_prefix,
}
def _get_headers(self, subscription_key, api_token, x_ms_useragent):
"""Returns the headers for the HTTP requests.
Args:
subscription_key (str): The subscription key for the service.
api_token (str): The API token for the service.
enable_face_identification (bool): A flag to enable face identification.
Returns:
dict: A dictionary containing the headers for the HTTP requests.
"""
headers = (
{"Ocp-Apim-Subscription-Key": subscription_key}
if subscription_key
else {"Authorization": f"Bearer {api_token}"}
)
headers["x-ms-useragent"] = x_ms_useragent
return headers
def get_all_analyzers(self):
"""
Retrieves a list of all available analyzers from the content understanding service.
This method sends a GET request to the service endpoint to fetch the list of analyzers.
It raises an HTTPError if the request fails.
Returns:
dict: A dictionary containing the JSON response from the service, which includes
the list of available analyzers.
Raises:
requests.exceptions.HTTPError: If the HTTP request returned an unsuccessful status code.
"""
response = requests.get(
url=self._get_analyzer_list_url(self._endpoint, self._api_version),
headers=self._headers,
)
response.raise_for_status()
return response.json()
def get_analyzer_detail_by_id(self, analyzer_id):
"""
Retrieves a specific analyzer detail through analyzerid from the content understanding service.
This method sends a GET request to the service endpoint to get the analyzer detail.
Args:
analyzer_id (str): The unique identifier for the analyzer.
Returns:
dict: A dictionary containing the JSON response from the service, which includes the target analyzer detail.
Raises:
HTTPError: If the request fails.
"""
response = requests.get(
url=self._get_analyzer_url(self._endpoint, self._api_version, analyzer_id),
headers=self._headers,
)
response.raise_for_status()
return response.json()
def begin_create_analyzer(
self,
analyzer_id: str,
analyzer_template: dict = None,
analyzer_template_path: str = "",
training_storage_container_sas_url: str = "",
training_storage_container_path_prefix: str = "",
completion_model: str = "",
embedding_model: str = "",
):
"""
Initiates the creation of an analyzer with the given ID and schema.
Args:
analyzer_id (str): The unique identifier for the analyzer.
analyzer_template (dict, optional): The schema definition for the analyzer. Defaults to None.
analyzer_template_path (str, optional): The file path to the analyzer schema JSON file. Defaults to "".
training_storage_container_sas_url (str, optional): The SAS URL for the training storage container. Defaults to "".
training_storage_container_path_prefix (str, optional): The path prefix within the training storage container. Defaults to "".
completion_model (str, optional): The completion model deployment name for the analyzer. Defaults to "".
embedding_model (str, optional): The embedding model deployment name for the analyzer. Defaults to "".
Raises:
ValueError: If neither `analyzer_template` nor `analyzer_template_path` is provided.
requests.exceptions.HTTPError: If the HTTP request to create the analyzer fails.
Returns:
requests.Response: The response object from the HTTP request.
"""
if analyzer_template_path and Path(analyzer_template_path).exists():
with open(analyzer_template_path, "r") as file:
analyzer_template = json.load(file)
if not analyzer_template:
raise ValueError("Analyzer schema must be provided.")
if (
training_storage_container_sas_url
and training_storage_container_path_prefix
):
analyzer_template["knowledgeSources"] = self._get_training_data_config(
training_storage_container_sas_url,
training_storage_container_path_prefix,
)
if completion_model and embedding_model:
analyzer_template["models"] = {
"completion": completion_model,
"embedding": embedding_model,
}
headers = {"Content-Type": "application/json"}
headers.update(self._headers)
response = requests.put(
url=self._get_analyzer_url(self._endpoint, self._api_version, analyzer_id),
headers=headers,
json=analyzer_template,
)
response.raise_for_status()
self._logger.info(f"Analyzer {analyzer_id} create request accepted.")
return response
def delete_analyzer(self, analyzer_id: str):
"""
Deletes an analyzer with the specified analyzer ID.
Args:
analyzer_id (str): The ID of the analyzer to be deleted.
Returns:
response: The response object from the delete request.
Raises:
HTTPError: If the delete request fails.
"""
response = requests.delete(
url=self._get_analyzer_url(self._endpoint, self._api_version, analyzer_id),
headers=self._headers,
)
response.raise_for_status()
self._logger.info(f"Analyzer {analyzer_id} deleted.")
return response
def begin_analyze(self, analyzer_id: str, file_location: str, file_data):
"""
Begins the analysis of a file or URL using the specified analyzer.
Args:
analyzer_id (str): The ID of the analyzer to use.
file_location (str): The path to the file or the URL to analyze.
Returns:
Response: The response from the analysis request.
Raises:
ValueError: If the file location is not a valid path or URL.
HTTPError: If the HTTP request returned an unsuccessful status code.
"""
if file_data:
data = file_data
headers = {"Content-Type": "application/octet-stream"}
else:
data = None
if Path(file_location).exists():
with open(file_location, "rb") as file:
data = file.read()
headers = {"Content-Type": "application/octet-stream"}
elif "https://" in file_location or "http://" in file_location:
data = {"inputs": [{"url": file_location}]}
headers = {"Content-Type": "application/json"}
else:
raise ValueError("File location must be a valid path or URL.")
headers.update(self._headers)
if isinstance(data, dict):
response = requests.post(
url=self._get_analyze_url(
self._endpoint, self._api_version, analyzer_id
),
headers=headers,
json=data,
)
else:
response = requests.post(
url=self._get_analyze_binary_url(
self._endpoint, self._api_version, analyzer_id
),
headers=headers,
data=data,
)
response.raise_for_status()
self._logger.info(
f"Analyzing file {file_location} with analyzer: {analyzer_id}"
)
return response
def get_file_from_analyze_operation(
self, analyze_response: Response, file_path: str
):
"""Retrieves a file from the analyze operation using the file path.
Args:
analyze_response (Response): The response object from the analyze operation.
file_path (str): The path of the file to retrieve.
Returns:
bytes: The file content as a byte string.
"""
operation_location = analyze_response.headers.get("operation-location", "")
if not operation_location:
raise ValueError(
"Operation location not found in the analyzer response header."
)
operation_location = operation_location.split("?api-version")[0]
file_retrieval_url = (
f"{operation_location}/files/{file_path}?api-version={self._api_version}"
)
try:
response = requests.get(url=file_retrieval_url, headers=self._headers)
response.raise_for_status()
assert response.headers.get("Content-Type") == "image/jpeg"
return response.content
except requests.exceptions.RequestException as e:
print(f"HTTP request failed: {e}")
return None
def poll_result(
self,
response: Response,
timeout_seconds: int = 120,
polling_interval_seconds: int = 2,
):
"""
Polls the result of an asynchronous operation until it completes or times out.
Args:
response (Response): The initial response object containing the operation location.
timeout_seconds (int, optional): The maximum number of seconds to wait for the operation to complete. Defaults to 120.
polling_interval_seconds (int, optional): The number of seconds to wait between polling attempts. Defaults to 2.
Raises:
ValueError: If the operation location is not found in the response headers.
TimeoutError: If the operation does not complete within the specified timeout.
RuntimeError: If the operation fails.
Returns:
dict: The JSON response of the completed operation if it succeeds.
"""
operation_location = response.headers.get("operation-location", "")
if not operation_location:
raise ValueError("Operation location not found in response headers.")
headers = {"Content-Type": "application/json"}
headers.update(self._headers)
start_time = time.time()
while True:
elapsed_time = time.time() - start_time
if elapsed_time > timeout_seconds:
raise TimeoutError(
f"Operation timed out after {timeout_seconds:.2f} seconds."
)
response = requests.get(operation_location, headers=self._headers)
response.raise_for_status()
status = response.json().get("status").lower()
if status == "succeeded":
self._logger.info(
f"Request result is ready after {elapsed_time:.2f} seconds."
)
return response.json()
elif status == "failed":
self._logger.error(f"Request failed. Reason: {response.json()}")
raise RuntimeError("Request failed.")
else:
self._logger.info(
f"Request {operation_location.split('/')[-1].split('?')[0]} in progress ..."
)
time.sleep(polling_interval_seconds)
def set_defaults(self, deployment_model: str, embedding_model: str):
"""
Sets the default model deployments for the Content Understanding resource.
Must be called before creating analyzers in GA API.
Args:
deployment_model (str): The deployment name for the generative model.
embedding_model (str): The deployment name for the embedding model.
Returns:
requests.Response: The response object from the PATCH request.
Raises:
requests.exceptions.HTTPError: If the HTTP request fails.
"""
headers = {"Content-Type": "application/merge-patch+json"}
headers.update(self._headers)
payload = {
"modelDeployments": {
deployment_model: deployment_model,
embedding_model: embedding_model,
}
}
response = requests.patch(
url=self._get_defaults_url(self._endpoint, self._api_version),
headers=headers,
json=payload,
)
response.raise_for_status()
self._logger.info(
f"Defaults set: deployment_model={deployment_model}, embedding_model={embedding_model}"
)
return response