Skip to content

Commit c6e76b0

Browse files
authored
Merge pull request #50 from veryfi/add-classify-document-api
Add support for Classify Document API
2 parents ebe324a + d09ba2e commit c6e76b0

9 files changed

Lines changed: 123 additions & 7 deletions

File tree

tests/test_bank_statements.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
from veryfi import Client
44

5-
65
MOCK = {
76
"transactions": [
87
{

tests/test_bussines_cards.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
from veryfi import Client
44

5-
65
MOCK = {
76
"text": "Dmitry Birulia\ndmitry@veryfi.com\nVERYFI\twww.veryfi.com",
87
"id": 4662609,

tests/test_checks.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
from veryfi import Client
44

5-
65
MOCK = {
76
"pdf_url": "https://scdn.veryfi.com/checks/919ba4778c039560/78535c60-e371-40fd-ae78-6d0c019b2c35/067e7057-e38b-4c32-9d87-720e0a8e232f.pdf?Expires=1730233783&Signature=fbR-arwLoH1YC8GVK52SvvilH59eHWIYp1o2WXw5UWr0s0CjKkrJ1Bx-PORKVzLbnJHOoYJDC4lU1iiqdq4~yDyz~-ygRHTNxyT9BJovFSzCBAc3Gnzv7uWMNIp-9mdV0QGk-Fu25eZfwd56Dfd2ZhG-EzreCRfh66r6338UF4EaHK5SG5b4i-NwkDaZ~qRZC6jNzYUJOGbXexYPbQxF5tMinc97ok~~fLQ--r0HWr7SvQyJisUqDnKS0DMTQujDz-7lStMJmvvlQX0jmpdcsq8DBIR6SnWZxHA7tM-ydD27Jt8l753X3uNtZuao61CeGSpQP09CnWlctTewm4IHKQ__&Key-Pair-Id=APKAJCILBXEJFZF4DCHQ",
87
"id": 4662680,

tests/test_classify.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import responses
2+
3+
from veryfi import Client
4+
5+
MOCK = {
6+
"id": 12345,
7+
"document_type": {
8+
"score": 0.98,
9+
"value": "receipt",
10+
},
11+
}
12+
13+
14+
@responses.activate
15+
def test_classify_document_url():
16+
client = Client(client_id="v", client_secret="w", username="o", api_key="c")
17+
responses.add(
18+
responses.POST,
19+
f"{client.versioned_url}/partner/classify/",
20+
json=MOCK,
21+
status=200,
22+
)
23+
d = client.classify_document_url(file_url="https://cdn.example.com/receipt.jpg")
24+
assert d == MOCK
25+
26+
27+
@responses.activate
28+
def test_classify_document():
29+
client = Client(client_id="v", client_secret="w", username="o", api_key="c")
30+
responses.add(
31+
responses.POST,
32+
f"{client.versioned_url}/partner/classify/",
33+
json=MOCK,
34+
status=200,
35+
)
36+
d = client.classify_document(file_path="tests/assets/receipt_public.jpg")
37+
assert d == MOCK
38+
39+
40+
@responses.activate
41+
def test_classify_document_url_with_document_types():
42+
client = Client(client_id="v", client_secret="w", username="o", api_key="c")
43+
responses.add(
44+
responses.POST,
45+
f"{client.versioned_url}/partner/classify/",
46+
json=MOCK,
47+
status=200,
48+
)
49+
d = client.classify_document_url(
50+
file_url="https://cdn.example.com/receipt.jpg",
51+
document_types=["receipt", "invoice", "check"],
52+
)
53+
assert d == MOCK

tests/test_w2s.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
from veryfi import Client
44

5-
65
MOCK = {
76
"pdf_url": None,
87
"id": 4559395,

tests/test_w8s.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
from veryfi import Client
44

5-
65
MOCK = {
76
"pdf_url": "https://scdn.veryfi.com/w8s/919ba4778c039560/64ea51e9-4293-4a0e-99a6-8c3cd0a4f7ab/8468d271-2943-46e7-b9a2-3dd0372d3648.pdf?Expires=1730234368&Signature=e1bOil~yK4P4uKYLxX1NPfHA3PxiTxd-Ds4HVnjNXxY22D-ng2NGJQQrWAst5E0ionsdkkFPc7mKy0fp6MkmwnZF~I-j8e1P9fhbI-T-0NhiDji4gp6xt4~vm-i9MG34K~Xa3TWPA~kMbQ~Hj2gjiMMniXsH6HeqH99yfl-Vt2ZWEMdWl3~ZlMWEpnPVIzDXdDBc~uRYCOS0KiLD2pfNAORYwp1ayNiuhiJzucJPAfRuK00y0BoUEPBmBS-aLa62VhNYVKmUPtVNobS2MjcGcnqnBhOZlbw0B5VTLNqrSIgKSVy6I6Co4zAwLjgviQyoPArVtgmJR8UNdFRk9LuDqw__&Key-Pair-Id=APKAJCILBXEJFZF4DCHQ",
87
"id": 4662698,

tests/test_w9s.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
from veryfi import Client
44

5-
65
MOCK = {
76
"account_numbers": "",
87
"address1": "28 E 3rd Ave, Suite 201",

veryfi/classify.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import os
2+
import base64
3+
from typing import Dict, List, Optional
4+
5+
from veryfi.client_base import Client
6+
7+
8+
class Classify:
9+
def __init__(self, client: Client):
10+
self.client = client
11+
12+
def classify_document(
13+
self,
14+
file_path: str,
15+
document_types: Optional[List] = None,
16+
**kwargs,
17+
) -> Dict:
18+
"""
19+
Classify a document from a file path.
20+
https://docs.veryfi.com/api/classify/classify-a-document/
21+
22+
:param file_path: Path on disk to a file to submit for classification
23+
:param document_types: Optional list of document types to classify into. If omitted, a preset set of types will be used.
24+
:param kwargs: Additional body parameters
25+
:return: Classification result with document type prediction
26+
"""
27+
endpoint_name = "/classify/"
28+
file_name = os.path.basename(file_path)
29+
with open(file_path, "rb") as image_file:
30+
base64_encoded_string = base64.b64encode(image_file.read()).decode("utf-8")
31+
request_arguments = {
32+
"file_name": file_name,
33+
"file_data": base64_encoded_string,
34+
}
35+
if document_types is not None:
36+
request_arguments["document_types"] = document_types
37+
request_arguments.update(kwargs)
38+
return self.client._request("POST", endpoint_name, request_arguments)
39+
40+
def classify_document_url(
41+
self,
42+
file_url: Optional[str] = None,
43+
file_urls: Optional[List[str]] = None,
44+
document_types: Optional[List] = None,
45+
**kwargs,
46+
) -> Dict:
47+
"""
48+
Classify a document from a URL.
49+
https://docs.veryfi.com/api/classify/classify-a-document/
50+
51+
:param file_url: Required if file_urls isn't specified. Publicly accessible URL to a file, e.g. "https://cdn.example.com/receipt.jpg".
52+
:param file_urls: Required if file_url isn't specified. List of publicly accessible URLs to multiple files.
53+
:param document_types: Optional list of document types to classify into. If omitted, a preset set of types will be used.
54+
:param kwargs: Additional body parameters
55+
:return: Classification result with document type prediction
56+
"""
57+
endpoint_name = "/classify/"
58+
request_arguments = {
59+
"file_url": file_url,
60+
"file_urls": file_urls,
61+
}
62+
if document_types is not None:
63+
request_arguments["document_types"] = document_types
64+
request_arguments.update(kwargs)
65+
return self.client._request("POST", endpoint_name, request_arguments)

veryfi/client.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44
from veryfi.bank_statements import BankStatements
55
from veryfi.bussines_cards import BussinesCards
66
from veryfi.checks import Checks
7+
from veryfi.classify import Classify
78
from veryfi.documents import Documents
89
from veryfi.w2s import W2s
910
from veryfi.w8s import W8s
1011
from veryfi.w9s import W9s
1112

1213

13-
class Client(ClientBase, ADocs, BankStatements, BussinesCards, Checks, Documents, W2s, W8s, W9s):
14+
class Client(
15+
ClientBase, ADocs, BankStatements, BussinesCards, Checks, Classify, Documents, W2s, W8s, W9s
16+
):
1417
def __init__(
1518
self,
1619
client_id: str,
@@ -34,6 +37,7 @@ def __init__(
3437
BankStatements.__init__(self, super())
3538
BussinesCards.__init__(self, super())
3639
Checks.__init__(self, super())
40+
Classify.__init__(self, super())
3741
Documents.__init__(self, super())
3842
W2s.__init__(self, super())
3943
W8s.__init__(self, super())

0 commit comments

Comments
 (0)