Spaces:
Running
Running
Update extract_and_store_supabase.py
Browse files- extract_and_store_supabase.py +238 -116
extract_and_store_supabase.py
CHANGED
@@ -11,135 +11,257 @@ def extract_structure_store_message(user_id:str,message_id:str , attachment_id:s
|
|
11 |
project_id = os.getenv('PROJECT_ID')
|
12 |
processor_id = os.getenv('PROCESSOR_ID')
|
13 |
document_entities = {}
|
14 |
-
|
15 |
file_name = f"{message_id}_{attachment_id}.{attachment_extension}"
|
16 |
print(f"file_name: {file_name}")
|
17 |
supabase = Supabase_Client().instance
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
"mimeType": f"application/{attachment_extension}",
|
28 |
-
"content": base64_data
|
29 |
-
}
|
30 |
-
}
|
31 |
-
|
32 |
-
access_token = get_access_token_v1()
|
33 |
-
print(access_token)
|
34 |
-
|
35 |
-
headers = {
|
36 |
-
'Authorization': f'Bearer {access_token}',
|
37 |
-
'Content-Type': 'application/json; charset=utf-8'
|
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 |
-
|
|
|
11 |
project_id = os.getenv('PROJECT_ID')
|
12 |
processor_id = os.getenv('PROCESSOR_ID')
|
13 |
document_entities = {}
|
14 |
+
|
15 |
file_name = f"{message_id}_{attachment_id}.{attachment_extension}"
|
16 |
print(f"file_name: {file_name}")
|
17 |
supabase = Supabase_Client().instance
|
18 |
+
|
19 |
+
response = supabase.storage.from_("receipt_radar").download(file_name)
|
20 |
+
base64_data = urlsafe_b64encode(response).decode('utf-8')
|
21 |
+
|
22 |
+
payload = {
|
23 |
+
"skipHumanReview": True,
|
24 |
+
"rawDocument": {
|
25 |
+
"mimeType": f"application/{attachment_extension}",
|
26 |
+
"content": base64_data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
}
|
28 |
+
}
|
29 |
+
|
30 |
+
access_token = get_access_token_v1()
|
31 |
+
print(access_token)
|
32 |
+
|
33 |
+
headers = {
|
34 |
+
'Authorization': f'Bearer {access_token}',
|
35 |
+
'Content-Type': 'application/json; charset=utf-8'
|
36 |
+
}
|
37 |
+
|
38 |
+
response = requests.post(
|
39 |
+
f'https://us-documentai.googleapis.com/v1/projects/{project_id}/locations/us/processors/{processor_id}:process',
|
40 |
+
headers=headers,
|
41 |
+
json=payload
|
42 |
+
)
|
43 |
+
response_json = response.json()
|
44 |
+
allowed_entities = [
|
45 |
+
"due_date",
|
46 |
+
"invoice_date",
|
47 |
+
"total_amount",
|
48 |
+
"total_tax_amount",
|
49 |
+
"receiver_name",
|
50 |
+
"invoice_id",
|
51 |
+
"currency",
|
52 |
+
"receiver_address",
|
53 |
+
"invoice_type",
|
54 |
+
"supplier_name",
|
55 |
+
"payment_terms",
|
56 |
+
"line_item",
|
57 |
+
"line_item/description",
|
58 |
+
"line_item/quantity",
|
59 |
+
"line_item/amount",
|
60 |
+
"line_item/unit_price"
|
61 |
+
]
|
62 |
+
raw_text = response_json.get('document').get('text', None)
|
63 |
+
entities = response_json.get('document').get('entities', None)
|
64 |
+
document_entities['user_id'] = user_id
|
65 |
+
insert_ocr_data_response = (
|
66 |
+
supabase.table("receipt_ocr_data")
|
67 |
+
.insert({'user_id': user_id, 'message_id': message_id, 'receipt_text': raw_text, 'email': email, 'file_type': attachment_extension})
|
68 |
+
.execute()
|
69 |
+
)
|
70 |
+
|
71 |
+
print('Printing entities')
|
72 |
+
print(entities)
|
73 |
+
|
74 |
+
if entities is not None:
|
75 |
+
for ent in entities:
|
76 |
+
if ent.get('type') is not None:
|
77 |
+
entity_type = ent.get('type') or ""
|
78 |
+
|
79 |
+
if entity_type in allowed_entities:
|
80 |
+
mention_text = ent.get('mentionText') or ""
|
81 |
+
normalized_values = ent.get('normalizedValue') or ""
|
82 |
+
|
83 |
+
if entity_type not in document_entities:
|
84 |
+
document_entities[entity_type] = []
|
85 |
+
|
86 |
+
document_entities[entity_type].append({
|
87 |
+
"mention_text": mention_text,
|
88 |
+
"normalizedValue": normalized_values
|
89 |
+
})
|
90 |
+
|
91 |
+
if entity_type == 'line_item' and 'properties' in ent:
|
92 |
+
for prop in ent['properties']:
|
93 |
+
prop_type = prop.get('type') or ""
|
94 |
+
if prop_type in allowed_entities:
|
95 |
+
mention_text = prop.get('mentionText') or ""
|
96 |
+
normalized_values = prop.get('normalizedValue') or ""
|
97 |
+
|
98 |
+
if prop_type not in document_entities:
|
99 |
+
document_entities[prop_type] = []
|
100 |
+
|
101 |
+
document_entities[prop_type].append({
|
102 |
+
"mention_text": mention_text,
|
103 |
+
"normalizedValue": normalized_values
|
104 |
+
})
|
105 |
+
|
106 |
+
if 'line_item/description' in document_entities:
|
107 |
+
document_entities['line_item_description'] = document_entities['line_item/description']
|
108 |
+
document_entities.pop('line_item/description', None)
|
109 |
+
|
110 |
+
if 'line_item/quantity' in document_entities:
|
111 |
+
document_entities['line_item_quantity'] = document_entities['line_item/quantity']
|
112 |
+
document_entities.pop('line_item/quantity', None)
|
113 |
+
|
114 |
+
if 'line_item/amount' in document_entities:
|
115 |
+
document_entities['line_item_amount'] = document_entities['line_item/amount']
|
116 |
+
document_entities.pop('line_item/amount', None)
|
117 |
+
|
118 |
+
if 'line_item/unit_price' in document_entities:
|
119 |
+
document_entities['line_item_unit_price'] = document_entities['line_item/unit_price']
|
120 |
+
document_entities.pop('line_item/unit_price', None)
|
121 |
+
|
122 |
+
document_entities['email'] = email
|
123 |
+
document_entities['message_id'] = message_id
|
124 |
+
print(document_entities)
|
125 |
+
insert_data_response = (
|
126 |
+
supabase.table("document_ai_entities")
|
127 |
+
.insert(document_entities)
|
128 |
+
.execute()
|
129 |
+
)
|
130 |
+
print(insert_data_response)
|
131 |
+
|
132 |
+
# if attachment_id and message_id:
|
133 |
+
# project_id = os.getenv('PROJECT_ID')
|
134 |
+
# processor_id = os.getenv('PROCESSOR_ID')
|
135 |
+
# document_entities = {}
|
136 |
+
|
137 |
+
# file_name = f"{message_id}_{attachment_id}.{attachment_extension}"
|
138 |
+
# print(f"file_name: {file_name}")
|
139 |
+
# supabase = Supabase_Client().instance
|
140 |
+
# try:
|
141 |
+
# response = supabase.storage.from_("receipt_radar").download(
|
142 |
+
# file_name
|
143 |
+
# )
|
144 |
+
# base64_data = urlsafe_b64encode(response).decode('utf-8')
|
145 |
+
|
146 |
+
# payload = {
|
147 |
+
# "skipHumanReview": True,
|
148 |
+
# "rawDocument": {
|
149 |
+
# "mimeType": f"application/{attachment_extension}",
|
150 |
+
# "content": base64_data
|
151 |
+
# }
|
152 |
+
# }
|
153 |
+
|
154 |
+
# access_token = get_access_token_v1()
|
155 |
+
# print(access_token)
|
156 |
+
|
157 |
+
# headers = {
|
158 |
+
# 'Authorization': f'Bearer {access_token}',
|
159 |
+
# 'Content-Type': 'application/json; charset=utf-8'
|
160 |
+
# }
|
161 |
|
162 |
+
# response = requests.post(
|
163 |
+
# f'https://us-documentai.googleapis.com/v1/projects/{project_id}/locations/us/processors/{processor_id}:process',
|
164 |
+
# headers=headers,
|
165 |
+
# json=payload
|
166 |
+
# )
|
167 |
+
# response_json = response.json()
|
168 |
+
# allowed_entities = [
|
169 |
+
# "due_date",
|
170 |
+
# "invoice_date",
|
171 |
+
# "total_amount",
|
172 |
+
# "total_tax_amount",
|
173 |
+
# "receiver_name",
|
174 |
+
# "invoice_id",
|
175 |
+
# "currency",
|
176 |
+
# "receiver_address",
|
177 |
+
# "invoice_type",
|
178 |
+
# "supplier_name",
|
179 |
+
# "payment_terms",
|
180 |
+
# "line_item",
|
181 |
+
# "line_item/description",
|
182 |
+
# "line_item/quantity",
|
183 |
+
# "line_item/amount",
|
184 |
+
# "line_item/unit_price"
|
185 |
+
# ]
|
186 |
+
# raw_text = response_json.get('document').get('text' , None)
|
187 |
+
# entities = response_json.get('document').get('entities' , None)
|
188 |
+
# document_entities['user_id'] = user_id
|
189 |
+
# insert_ocr_data_response = (
|
190 |
+
# supabase.table("receipt_ocr_data")
|
191 |
+
# .insert({'user_id':user_id , 'message_id':message_id,'receipt_text':raw_text ,'email':email,'file_type':attachment_extension})
|
192 |
+
# .execute()
|
193 |
+
# )
|
194 |
|
195 |
+
# print('Printing entities')
|
196 |
+
# print(entities)
|
197 |
+
# # if entities is not None:
|
198 |
+
# # for ent in entities:
|
199 |
+
# # if ent.get('type') is not None:
|
200 |
+
# # if ent.get('type') in allowed_entities:
|
201 |
+
# # mention_text = ent.get('mentionText')
|
202 |
+
# # normalised_values = ent.get('normalizedValue') if 'normalizedValue' in ent else None
|
203 |
+
# # document_entities[ent.get('type')] = {"mention_text":mention_text,"normalizedValue":normalised_values}
|
204 |
+
# if entities is not None:
|
205 |
+
# for ent in entities:
|
206 |
+
# if ent.get('type') is not None:
|
207 |
+
# entity_type = ent.get('type') or ""
|
208 |
|
209 |
+
# # Check if the entity type is in the allowed list
|
210 |
+
# if entity_type in allowed_entities:
|
211 |
+
# mention_text = ent.get('mentionText') or ""
|
212 |
+
# normalized_values = ent.get('normalizedValue') or ""
|
213 |
|
214 |
+
# # Initialize a list for the entity type if not already present
|
215 |
+
# if entity_type not in document_entities:
|
216 |
+
# document_entities[entity_type] = []
|
217 |
|
218 |
+
# # Append the entity data to the list
|
219 |
+
# document_entities[entity_type].append({
|
220 |
+
# "mention_text": mention_text,
|
221 |
+
# "normalizedValue": normalized_values
|
222 |
+
# })
|
223 |
|
224 |
+
# # Handling 'line_item' and its properties (line_item/description, line_item/quantity, etc.)
|
225 |
+
# if entity_type == 'line_item' and 'properties' in ent:
|
226 |
+
# for prop in ent['properties']:
|
227 |
+
# prop_type = prop.get('type') or ""
|
228 |
+
# if prop_type in allowed_entities:
|
229 |
+
# mention_text = prop.get('mentionText') or ""
|
230 |
+
# normalized_values = prop.get('normalizedValue') or ""
|
231 |
|
232 |
+
# # Initialize a list for the property type if not already present
|
233 |
+
# if prop_type not in document_entities:
|
234 |
+
# document_entities[prop_type] = []
|
235 |
|
236 |
+
# # Append the property data to the list
|
237 |
+
# document_entities[prop_type].append({
|
238 |
+
# "mention_text": mention_text,
|
239 |
+
# "normalizedValue": normalized_values
|
240 |
+
# })
|
241 |
+
# if 'line_item/description' in document_entities:
|
242 |
+
# document_entities['line_item_description'] = document_entities['line_item/description']
|
243 |
+
# document_entities.pop('line_item/description', None)
|
244 |
|
245 |
+
# if 'line_item/quantity' in document_entities:
|
246 |
+
# document_entities['line_item_quantity'] = document_entities['line_item/quantity']
|
247 |
+
# document_entities.pop('line_item/quantity', None)
|
248 |
|
249 |
+
# if 'line_item/amount' in document_entities:
|
250 |
+
# document_entities['line_item_amount'] = document_entities['line_item/amount']
|
251 |
+
# document_entities.pop('line_item/amount', None)
|
252 |
|
253 |
+
# if 'line_item/unit_price' in document_entities:
|
254 |
+
# document_entities['line_item_unit_price'] = document_entities['line_item/unit_price']
|
255 |
+
# document_entities.pop('line_item/unit_price', None)
|
256 |
+
# document_entities['email'] = email
|
257 |
+
# document_entities['message_id'] = message_id
|
258 |
+
# print(document_entities)
|
259 |
+
# insert_data_response = (
|
260 |
+
# supabase.table("document_ai_entities")
|
261 |
+
# .insert(document_entities)
|
262 |
+
# .execute()
|
263 |
+
# )
|
264 |
+
# print(insert_data_response)
|
265 |
|
266 |
+
# except Exception as e:
|
267 |
+
# print(f"Error downloading or encoding file: {e}")
|