Skip to content

Commit db70bb0

Browse files
committed
♻️ refactor Endpoints and document building
1 parent 702b6b4 commit db70bb0

File tree

15 files changed

+135
-432
lines changed

15 files changed

+135
-432
lines changed

mindee/client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from mindee.response import format_response, DocumentResponse
1313
from mindee.http import (
1414
HTTPException,
15-
Endpoint,
15+
CustomEndpoint,
1616
InvoiceEndpoint,
1717
ReceiptEndpoint,
1818
PassportEndpoint,
@@ -149,7 +149,7 @@ def config_custom_doc(
149149
plural_name=plural_name,
150150
constructor=CustomDocument,
151151
endpoints=[
152-
Endpoint(
152+
CustomEndpoint(
153153
owner=account_name,
154154
url_name=document_type,
155155
version=version,

mindee/documents/base.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,43 @@
1-
from typing import List, Type
1+
from typing import List, Type, Optional
22

33
# from mindee.inputs import InputDocument
44
from mindee.http import Endpoint
55

66

77
class Document:
88
type: str
9+
checklist: dict = {}
10+
filepath: Optional[str] = None
11+
filename: Optional[str] = None
12+
file_extension: Optional[str] = None
913

1014
def __init__(
11-
self, input_file, document_type: str, api_prediction, page_n=0
12-
): # pylint: disable=unused-argument
13-
self.filepath = None
14-
self.filename = None
15-
self.file_extension = None
16-
17-
if input_file is not None:
15+
self,
16+
input_file,
17+
document_type: str,
18+
api_prediction,
19+
page_n: Optional[int] = None,
20+
):
21+
if input_file:
1822
self.filepath = input_file.filepath
1923
self.filename = input_file.filename
2024
self.file_extension = input_file.file_extension
2125

2226
self.type = document_type
23-
self.checklist: dict = {}
27+
28+
self.build_from_api_prediction(api_prediction, page_n=page_n)
29+
self._checklist()
30+
self._reconstruct()
2431

2532
@staticmethod
2633
def request(endpoints: List[Endpoint], input_file, include_words: bool = False):
2734
"""Make request to the product endpoint"""
2835
raise NotImplementedError()
2936

37+
def build_from_api_prediction(self, api_prediction: dict, page_n):
38+
"""Build the document from an API response JSON"""
39+
raise NotImplementedError()
40+
3041
def _checklist(self, *args):
3142
raise NotImplementedError()
3243

mindee/documents/custom_document.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
from typing import Dict, List
22

33
from mindee.documents.base import Document
4-
from mindee.http import make_api_request, Endpoint
4+
from mindee.http import Endpoint
55

66

77
class CustomDocument(Document):
88
fields: Dict[str, dict] = {}
99

1010
def __init__(
1111
self,
12-
document_type: str = "",
12+
document_type: str,
1313
api_prediction=None,
1414
input_file=None,
1515
page_n: int = 0,
@@ -20,14 +20,12 @@ def __init__(
2020
:param input_file: Input object
2121
:param page_n: Page number for multi pages pdf input
2222
"""
23-
# Invoke Document constructor
2423
super().__init__(
2524
input_file=input_file,
2625
document_type=document_type,
2726
api_prediction=api_prediction,
2827
page_n=page_n,
2928
)
30-
self.build_from_api_prediction(api_prediction, page_n=page_n)
3129

3230
def build_from_api_prediction(self, api_prediction, page_n: int = 0):
3331
"""
@@ -62,9 +60,7 @@ def request(endpoints: List[Endpoint], input_file, include_words: bool = False):
6260
:param input_file: Input object
6361
:param endpoints: Endpoints config
6462
"""
65-
return make_api_request(
66-
endpoints[0].predict_url, input_file, endpoints[0].api_key
67-
)
63+
return endpoints[0].predict_request(input_file, include_words)
6864

6965
def _checklist(self):
7066
return {}

mindee/documents/financial_document.py

Lines changed: 13 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
from typing import List
22

3-
from mindee.fields.amount import Amount
4-
from mindee.fields.date import Date
5-
from mindee.fields.locale import Locale
6-
from mindee.fields.orientation import Orientation
7-
from mindee.fields.tax import Tax
83
from mindee.fields import Field
9-
from mindee.http import make_api_request, Endpoint
4+
from mindee.http import Endpoint
105
from mindee.documents.base import Document
116
from mindee.documents.invoice import Invoice
127
from mindee.documents.receipt import Receipt
@@ -17,50 +12,14 @@ def __init__(
1712
self,
1813
api_prediction=None,
1914
input_file=None,
20-
locale=None,
21-
total_incl=None,
22-
total_excl=None,
23-
date=None,
24-
invoice_number=None,
25-
due_date=None,
26-
taxes=None,
27-
merchant_name=None,
28-
payment_details=None,
29-
company_number=None,
30-
vat_number=None,
31-
orientation=None,
32-
total_tax=None,
33-
time=None,
3415
page_n=0,
3516
document_type="financial_doc",
3617
):
3718
"""
3819
:param api_prediction: Raw prediction from HTTP response
3920
:param input_file: Input object
40-
:param locale: locale value for creating FinancialDocument object from scratch
41-
:param total_incl: total_incl value for creating FinancialDocument object from scratch
42-
:param total_excl: total_excl value for creating FinancialDocument object from scratch
43-
:param date: date value for creating FinancialDocument object from scratch
44-
:param invoice_number: invoice_number value for creating FinancialDocument object from scratch
45-
:param due_date: due_date value for creating FinancialDocument object from scratch
46-
:param taxes: taxes value for creating FinancialDocument object from scratch
47-
:param merchant_name: merchant_name value for creating FinancialDocument object from scratch
48-
:param payment_details: payment_details value for creating FinancialDocument object from scratch
49-
:param company_number: company_number value for creating FinancialDocument object from scratch
50-
:param vat_number: vat_number value for creating FinancialDocument object from scratch
51-
:param orientation: orientation value for creating FinancialDocument object from scratch
52-
:param total_tax: total_tax value for creating FinancialDocument object from scratch
53-
:param time: time value for creating FinancialDocument object from scratch
5421
:param page_n: Page number for multi pages pdf input
5522
"""
56-
# Invoke Document constructor
57-
super().__init__(
58-
input_file=input_file,
59-
document_type=document_type,
60-
api_prediction=api_prediction,
61-
page_n=page_n,
62-
)
63-
6423
self.locale = None
6524
self.total_incl = None
6625
self.total_excl = None
@@ -76,66 +35,24 @@ def __init__(
7635
self.total_tax = None
7736
self.time = None
7837

79-
if api_prediction is not None:
80-
self.build_from_api_prediction(api_prediction, input_file, page_n=page_n)
81-
else:
82-
self.orientation = Orientation(
83-
{"value": orientation}, value_key="value", page_n=page_n
84-
)
85-
self.locale = Locale({"value": locale}, value_key="value", page_n=page_n)
86-
self.date = Date({"value": date}, value_key="value", page_n=page_n)
87-
self.due_date = Date({"value": due_date}, value_key="value", page_n=page_n)
88-
if taxes is not None:
89-
self.taxes = [
90-
Tax(
91-
{"value": t[0], "rate": t[1]},
92-
page_n=page_n,
93-
value_key="value",
94-
rate_key="rate",
95-
)
96-
for t in taxes
97-
]
98-
self.total_incl = Amount(
99-
{"value": total_incl}, value_key="value", page_n=page_n
100-
)
101-
self.total_excl = Amount(
102-
{"value": total_excl}, value_key="value", page_n=page_n
103-
)
104-
self.merchant_name = Field(
105-
{"value": merchant_name}, value_key="value", page_n=page_n
106-
)
107-
self.time = Field({"value": time}, value_key="value", page_n=page_n)
108-
self.total_tax = Amount(
109-
{"value": total_tax}, value_key="value", page_n=page_n
110-
)
111-
self.vat_number = Field(
112-
{"value": vat_number}, value_key="value", page_n=page_n
113-
)
114-
self.invoice_number = Field(
115-
{"value": invoice_number}, value_key="value", page_n=page_n
116-
)
117-
self.payment_details = Field(
118-
{"value": payment_details}, value_key="value", page_n=page_n
119-
)
120-
self.company_number = Field(
121-
{"value": company_number}, value_key="value", page_n=page_n
122-
)
123-
124-
# Run checks
125-
self._checklist()
38+
# need this for building from prediction
39+
self.input_file = input_file
12640

127-
# Reconstruct extra fields
128-
self._reconstruct()
41+
super().__init__(
42+
input_file=input_file,
43+
document_type=document_type,
44+
api_prediction=api_prediction,
45+
page_n=page_n,
46+
)
12947

130-
def build_from_api_prediction(self, api_prediction, input_file, page_n=0):
48+
def build_from_api_prediction(self, api_prediction, page_n=0):
13149
"""
13250
:param api_prediction: Raw prediction from HTTP response
133-
:param input_file: Input object
13451
:param page_n: Page number for multi pages pdf input
13552
:return: (void) set the object attributes with api prediction values
13653
"""
13754
if "invoice_number" in api_prediction.keys():
138-
invoice = Invoice(api_prediction, input_file, page_n=page_n)
55+
invoice = Invoice(api_prediction, self.input_file, page_n=page_n)
13956
self.locale = invoice.locale
14057
self.total_incl = invoice.total_incl
14158
self.total_excl = invoice.total_excl
@@ -150,7 +67,7 @@ def build_from_api_prediction(self, api_prediction, input_file, page_n=0):
15067
self.total_tax = invoice.total_tax
15168
self.time = Field({"value": None, "confidence": 0.0})
15269
else:
153-
receipt = Receipt(api_prediction, input_file, page_n=page_n)
70+
receipt = Receipt(api_prediction, self.input_file, page_n=page_n)
15471
self.orientation = receipt.orientation
15572
self.date = receipt.date
15673
self.due_date = receipt.date
@@ -204,12 +121,7 @@ def request(endpoints: List[Endpoint], input_file, include_words=False):
204121
index = 0
205122
else:
206123
index = 1
207-
return make_api_request(
208-
endpoints[index].predict_url,
209-
input_file,
210-
endpoints[index].api_key,
211-
include_words,
212-
)
124+
return endpoints[index].predict_request(input_file, include_words)
213125

214126
def _checklist(self):
215127
"""

0 commit comments

Comments
 (0)