-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract_data.py
35 lines (28 loc) · 1.19 KB
/
extract_data.py
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
import os
import time
import requests
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
FILE_NAME = 'circleci1.pdf'
file_path = os.path.join(BASE_DIR, 'examples', 'files', FILE_NAME)
with open(file_path, 'rb') as invoice_file:
files = {
"file": (FILE_NAME, invoice_file.read(),),
}
request_data = {
"document_type_name": 'pre-learning-example',
"customer": 'me'
}
start = time.time()
response = requests.post(
f'https://developers.typless.com/api/document-types/extract-data/',
files=files,
data=request_data,
headers={'Authorization': f'Token {os.getenv("API_KEY")}'}
)
fields = response.json()['extracted_fields']
supplier = [field for field in fields if field['name'] == 'supplier'][0]['values'][0]['value']
invoice_number = [field for field in fields if field['name'] == 'invoice_number'][0]['values'][0]['value']
issue_date = [field for field in fields if field['name'] == 'issue_date'][0]['values'][0]['value']
total_amount = [field for field in fields if field['name'] == 'total_amount'][0]['values'][0]['value']
data = f'Extracted data - supplier: {supplier}, invoice_number: {invoice_number}, issue_date: {issue_date}, total_amount: {total_amount}'
print(data)