forked from Ecalzo/my_app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
162 lines (80 loc) · 3.03 KB
/
app.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
from flask import Flask, render_template,request
import boto3
import re
import requests
from smart_open import open
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
#@app.route('/uploadFile')
def upload_files(req):
fileLocation ="Users/"+str(req['phonenumber'])+"/"+str(req["year"])+"/"+str(req["month"])
response = {}
for _ in req["purchaseInvoices"]:
response = requests.get(_['inv_location'], stream=True)
s3url = 's3://invoice-ocr-poc.22/' + fileLocation+"/purchaseInvoices/"+_['inv_name']
with open(s3url, 'wb') as fout:
fout.write(response.content)
for _ in req["saleInvoices"]:
response = requests.get(_['inv_location'], stream=True)
s3url = 's3://invoice-ocr-poc.22/' + fileLocation+"/saleInvoices/"+_['inv_name']
with open(s3url, 'wb') as fout:
fout.write(response.content)
@app.route('/ocrApi')
def invoices():
req = request.json
phoneNumber = req["phonenumber"]
if(req['uploadToS3']):
upload_files(req)
s3 = boto3.resource('s3')
bucket = s3.Bucket('invoice-ocr-poc.22')
folderList = ["purchaseInvoices","saleInvoices"]
file_list = []
for _ in folderList:
file_list_ = [obj.key for obj in bucket.objects.filter(Prefix="Users/"+phoneNumber+'/'+req['year'] +'/'+req['month']+'/'+_)]
file_list.extend(file_list_)
result = { _.split("/")[-1] :ocr_api(_) for _ in file_list }
return result
def ocr_api(doc_name):
try:
textract = boto3.client(service_name='textract')
response = textract.analyze_expense(Document={"S3Object": {"Bucket": "invoice-ocr-poc.22","Name": doc_name}})
keys= []
val = []
for _ in response["ExpenseDocuments"][0]["SummaryFields"]:
x,y = get_val(_)
if(x!=None):
keys.append(x)
val.append(y)
final_res = dict()
invoice_no_added = False
total_added = False
for _ in range(len(keys)):
if("invoice" in keys[_].lower()):
amount = val[_]
digits = re.sub(r"\D",'',amount)
chars = re.sub(r"\d",'',amount)
if(chars[-1]=='.'):
digits = digits/100
final_res["invoiceNumber"] = digits
invoice_no_added = True
if("total" in keys[_].lower()):
final_res["totalAmount"] = val[_]
total_added = True
if(not invoice_no_added):
final_res["invoiceNumber"] = -1
if(not total_added):
final_res["totalAmount"] = -1
return final_res
except Exception:
return {"invoiceNumber":"N/A","totalAmount":"N/A"}
def get_val(x):
try:
key = x["LabelDetection"]['Text']
val = x["ValueDetection"]['Text']
return key,val
except Exception:
return None,None
if __name__ == '__main__':
app.run(port=8000)