22
22
import logging
23
23
import os
24
24
import time
25
- from typing import IO
25
+ from collections .abc import Generator
26
+ from typing import IO , Any
26
27
27
28
import requests
28
29
@@ -41,13 +42,25 @@ class LLMWhispererClientException(Exception):
41
42
status_code (int, optional): HTTP status code returned by the LLMWhisperer API. Defaults to None.
42
43
"""
43
44
44
- def __init__ (self , value ):
45
+ def __init__ (self , value : str , status_code : int | None = None ) -> None :
46
+ """Initialize the LLMWhispererClientException.
47
+
48
+ Args:
49
+ value: The error message or value.
50
+ status_code: The HTTP status code returned by the LLMWhisperer API.
51
+ """
45
52
self .value = value
53
+ self .status_code = status_code
46
54
47
- def __str__ (self ):
55
+ def __str__ (self ) -> str :
56
+ """Return string representation of the exception.
57
+
58
+ Returns:
59
+ String representation of the error value.
60
+ """
48
61
return repr (self .value )
49
62
50
- def error_message (self ):
63
+ def error_message (self ) -> str :
51
64
return self .value
52
65
53
66
@@ -67,17 +80,17 @@ class LLMWhispererClientV2:
67
80
log_stream_handler .setFormatter (formatter )
68
81
logger .addHandler (log_stream_handler )
69
82
70
- api_key = ""
71
- base_url = ""
72
- api_timeout = 120
83
+ api_key : str
84
+ base_url : str
85
+ api_timeout : int
73
86
74
87
def __init__ (
75
88
self ,
76
89
base_url : str = "" ,
77
90
api_key : str = "" ,
78
91
logging_level : str = "" ,
79
92
custom_headers : dict [str , str ] | None = None ,
80
- ):
93
+ ) -> None :
81
94
"""Initializes the LLMWhispererClient with the given parameters.
82
95
83
96
Args:
@@ -136,15 +149,15 @@ def __init__(
136
149
# "Start-Date": "2024-07-09",
137
150
# }
138
151
139
- def get_usage_info (self ) -> dict :
152
+ def get_usage_info (self ) -> Any :
140
153
"""Retrieves the usage information of the LLMWhisperer API.
141
154
142
155
This method sends a GET request to the '/get-usage-info' endpoint of the LLMWhisperer API.
143
156
The response is a JSON object containing the usage information.
144
157
Refer to https://docs.unstract.com/llm_whisperer/apis/llm_whisperer_usage_api
145
158
146
159
Returns:
147
- dict : A dictionary containing the usage information.
160
+ Dict[Any, Any] : A dictionary containing the usage information.
148
161
149
162
Raises:
150
163
LLMWhispererClientException: If the API request fails, it raises an exception with
@@ -163,7 +176,7 @@ def get_usage_info(self) -> dict:
163
176
raise LLMWhispererClientException (err )
164
177
return json .loads (response .text )
165
178
166
- def get_highlight_data (self , whisper_hash : str , lines : str , extract_all_lines : bool = False ) -> dict :
179
+ def get_highlight_data (self , whisper_hash : str , lines : str , extract_all_lines : bool = False ) -> Any :
167
180
"""Retrieves the highlight information of the LLMWhisperer API.
168
181
169
182
This method sends a GET request to the '/highlights' endpoint of the LLMWhisperer API.
@@ -178,7 +191,7 @@ def get_highlight_data(self, whisper_hash: str, lines: str, extract_all_lines: b
178
191
till the last line meta data.
179
192
180
193
Returns:
181
- dict : A dictionary containing the highlight information.
194
+ Dict[Any, Any] : A dictionary containing the highlight information.
182
195
183
196
Raises:
184
197
LLMWhispererClientException: If the API request fails, it raises an exception with
@@ -205,7 +218,7 @@ def get_highlight_data(self, whisper_hash: str, lines: str, extract_all_lines: b
205
218
def whisper (
206
219
self ,
207
220
file_path : str = "" ,
208
- stream : IO [bytes ] = None ,
221
+ stream : IO [bytes ] | None = None ,
209
222
url : str = "" ,
210
223
mode : str = "form" ,
211
224
output_mode : str = "layout_preserving" ,
@@ -219,17 +232,17 @@ def whisper(
219
232
mark_horizontal_lines : bool = False ,
220
233
line_spitter_strategy : str = "left-priority" ,
221
234
add_line_nos : bool = False ,
222
- lang = "eng" ,
223
- tag = "default" ,
224
- filename = "" ,
225
- webhook_metadata = "" ,
226
- use_webhook = "" ,
227
- wait_for_completion = False ,
228
- wait_timeout = 180 ,
235
+ lang : str = "eng" ,
236
+ tag : str = "default" ,
237
+ filename : str = "" ,
238
+ webhook_metadata : str = "" ,
239
+ use_webhook : str = "" ,
240
+ wait_for_completion : bool = False ,
241
+ wait_timeout : int = 180 ,
229
242
encoding : str = "utf-8" ,
230
- ) -> dict :
243
+ ) -> Any :
231
244
"""Sends a request to the LLMWhisperer API to process a document.
232
- Refer to https://docs.unstract.com/llm_whisperer/apis/llm_whisperer_text_extraction_api
245
+ Refer to https://docs.unstract.com/llm_whisperer/apis/llm_whisperer_text_extraction_api.
233
246
234
247
Args:
235
248
file_path (str, optional): The path to the file to be processed. Defaults to "".
@@ -264,7 +277,7 @@ def whisper(
264
277
encoding (str): The character encoding to use for processing the text. Defaults to "utf-8".
265
278
266
279
Returns:
267
- dict : The response from the API as a dictionary.
280
+ Dict[Any, Any] : The response from the API as a dictionary.
268
281
269
282
Raises:
270
283
LLMWhispererClientException: If the API request fails, it raises an exception with
@@ -296,28 +309,22 @@ def whisper(
296
309
self .logger .debug ("params: %s" , params )
297
310
298
311
if use_webhook != "" and wait_for_completion :
299
- raise LLMWhispererClientException (
300
- {
301
- "status_code" : - 1 ,
302
- "message" : "Cannot wait for completion when using webhook" ,
303
- }
304
- )
312
+ raise LLMWhispererClientException ("Cannot wait for completion when using webhook" , 1 )
305
313
306
314
if url == "" and file_path == "" and stream is None :
307
315
raise LLMWhispererClientException (
308
- {
309
- "status_code" : - 1 ,
310
- "message" : "Either url, stream or file_path must be provided" ,
311
- }
316
+ "Either url, stream or file_path must be provided" ,
317
+ 1 ,
312
318
)
313
319
314
320
should_stream = False
315
321
if url == "" :
316
322
if stream is not None :
317
323
should_stream = True
318
324
319
- def generate ():
320
- yield from stream
325
+ def generate () -> Generator [bytes , None , None ]:
326
+ if stream is not None : # Add explicit type check
327
+ yield from stream
321
328
322
329
req = requests .Request (
323
330
"POST" ,
@@ -410,7 +417,7 @@ def generate():
410
417
message ["status_code" ] = response .status_code
411
418
return message
412
419
413
- def whisper_status (self , whisper_hash : str ) -> dict :
420
+ def whisper_status (self , whisper_hash : str ) -> Any :
414
421
"""Retrieves the status of the whisper operation from the LLMWhisperer
415
422
API.
416
423
@@ -446,7 +453,7 @@ def whisper_status(self, whisper_hash: str) -> dict:
446
453
message ["status_code" ] = response .status_code
447
454
return message
448
455
449
- def whisper_retrieve (self , whisper_hash : str , encoding : str = "utf-8" ) -> dict :
456
+ def whisper_retrieve (self , whisper_hash : str , encoding : str = "utf-8" ) -> Any :
450
457
"""Retrieves the result of the whisper operation from the LLMWhisperer
451
458
API.
452
459
@@ -485,7 +492,7 @@ def whisper_retrieve(self, whisper_hash: str, encoding: str = "utf-8") -> dict:
485
492
"extraction" : json .loads (response .text ),
486
493
}
487
494
488
- def register_webhook (self , url : str , auth_token : str , webhook_name : str ) -> dict :
495
+ def register_webhook (self , url : str , auth_token : str , webhook_name : str ) -> Any :
489
496
"""Registers a webhook with the LLMWhisperer API.
490
497
491
498
This method sends a POST request to the '/whisper-manage-callback' endpoint of the LLMWhisperer API.
@@ -499,7 +506,7 @@ def register_webhook(self, url: str, auth_token: str, webhook_name: str) -> dict
499
506
webhook_name (str): The name of the webhook.
500
507
501
508
Returns:
502
- dict : A dictionary containing the status code and the response from the API.
509
+ Any : A dictionary containing the status code and the response from the API.
503
510
504
511
Raises:
505
512
LLMWhispererClientException: If the API request fails, it raises an exception with
@@ -521,7 +528,7 @@ def register_webhook(self, url: str, auth_token: str, webhook_name: str) -> dict
521
528
raise LLMWhispererClientException (err )
522
529
return json .loads (response .text )
523
530
524
- def update_webhook_details (self , webhook_name : str , url : str , auth_token : str ) -> dict :
531
+ def update_webhook_details (self , webhook_name : str , url : str , auth_token : str ) -> Any :
525
532
"""Updates the details of a webhook from the LLMWhisperer API.
526
533
527
534
This method sends a PUT request to the '/whisper-manage-callback' endpoint of the LLMWhisperer API.
@@ -557,7 +564,7 @@ def update_webhook_details(self, webhook_name: str, url: str, auth_token: str) -
557
564
raise LLMWhispererClientException (err )
558
565
return json .loads (response .text )
559
566
560
- def get_webhook_details (self , webhook_name : str ) -> dict :
567
+ def get_webhook_details (self , webhook_name : str ) -> Any :
561
568
"""Retrieves the details of a webhook from the LLMWhisperer API.
562
569
563
570
This method sends a GET request to the '/whisper-manage-callback' endpoint of the LLMWhisperer API.
@@ -587,7 +594,7 @@ def get_webhook_details(self, webhook_name: str) -> dict:
587
594
raise LLMWhispererClientException (err )
588
595
return json .loads (response .text )
589
596
590
- def delete_webhook (self , webhook_name : str ) -> dict :
597
+ def delete_webhook (self , webhook_name : str ) -> Any :
591
598
"""Deletes a webhook from the LLMWhisperer API.
592
599
593
600
This method sends a DELETE request to the '/whisper-manage-callback' endpoint of the LLMWhisperer API.
@@ -624,7 +631,7 @@ def get_highlight_rect(
624
631
target_height : int ,
625
632
) -> tuple [int , int , int , int , int ]:
626
633
"""Given the line metadata and the line number, this function returns
627
- the bounding box of the line in the format (page,x1,y1,x2,y2)
634
+ the bounding box of the line in the format (page,x1,y1,x2,y2).
628
635
629
636
Args:
630
637
line_metadata (list[int]): The line metadata returned by the LLMWhisperer API.
0 commit comments