1010from mindee .documents .passport import Passport
1111from mindee .benchmark import Benchmark
1212
13+ DOCUMENT_CLASSES = {
14+ "receipt" : Receipt ,
15+ "invoice" : Invoice ,
16+ "financial_document" : FinancialDocument ,
17+ "passport" : Passport ,
18+ "license_plate" : CarPlate
19+ }
20+
1321
1422class Client (object ):
1523 def __init__ (
@@ -29,7 +37,7 @@ def __init__(
2937 """
3038 assert type (raise_on_error ) == bool
3139 self .raise_on_error = raise_on_error
32- self .base_url = "https://api.mindee.net/products/"
40+ self .base_url = "https://api.mindee.net/v1/ products/mindee /"
3341 self .expense_receipt_token = expense_receipt_token
3442 self .invoice_token = invoice_token
3543 self .passport_token = passport_token
@@ -79,10 +87,11 @@ def _wrap_response(
7987 :return: Full response object
8088 """
8189 dict_response = response .json ()
82- if response .status_code != 200 and self .raise_on_error :
90+
91+ if response .status_code > 201 and self .raise_on_error :
8392 raise HTTPException (
8493 "Receipt API %s HTTP error: %s" % (response .status_code , json .dumps (dict_response )))
85- elif response .status_code != 200 :
94+ elif response .status_code > 201 :
8695 return Response (
8796 http_response = dict_response ,
8897 pages = [],
@@ -288,55 +297,30 @@ def format_response(json_response, document_type, input_file):
288297 json_response ["filepath" ] = input_file .filepath
289298 json_response ["file_extension" ] = input_file .file_extension
290299 pages = []
291- for page_n , page_prediction in enumerate (json_response ["predictions" ]):
292- if document_type == "receipt" :
293- pages .append (
294- Receipt (
295- api_prediction = page_prediction ,
296- input_file = input_file ,
297- page_n = page_n
298- )
299- )
300- elif document_type == "invoice" :
301- pages .append (
302- Invoice (
303- api_prediction = page_prediction ,
304- input_file = input_file ,
305- page_n = page_n
306- )
307- )
308- elif document_type == "financial_document" :
309- pages .append (
310- FinancialDocument (
311- api_prediction = page_prediction ,
312- input_file = input_file ,
313- page_n = page_n
314- )
315- )
316- elif document_type == "passport" :
317- pages .append (
318- Passport (
319- api_prediction = page_prediction ,
320- input_file = input_file ,
321- page_n = page_n
322- )
323- )
324- elif document_type == "license_plate" :
325- pages .append (
326- CarPlate (
327- api_prediction = page_prediction ,
328- input_file = input_file ,
329- page_n = page_n
330- )
300+
301+ if document_type not in DOCUMENT_CLASSES .keys ():
302+ raise Exception ("Document type not supported." )
303+
304+ # Create page level objects
305+ for page_n , page_prediction in enumerate (json_response ["document" ]["inference" ]["pages" ]):
306+ pages .append (
307+ DOCUMENT_CLASSES [document_type ](
308+ api_prediction = page_prediction ["prediction" ],
309+ input_file = input_file ,
310+ page_n = page_prediction ["id" ]
331311 )
332- else :
333- raise Exception ("Document type not supported." )
312+ )
334313
335- document = Document .merge_pages (pages )
314+ # Create the document level object
315+ document_level = DOCUMENT_CLASSES [document_type ](
316+ api_prediction = json_response ["document" ]["inference" ]["prediction" ],
317+ input_file = input_file ,
318+ page_n = "-1"
319+ )
336320
337321 return Response (
338322 http_response = json_response ,
339323 pages = pages ,
340- document = document ,
324+ document = document_level ,
341325 document_type = document_type
342326 )
0 commit comments