Omkar008 commited on
Commit
36a270b
·
verified ·
1 Parent(s): 7535d0e

Update extract_and_store_supabase.py

Browse files
Files changed (1) hide show
  1. extract_and_store_supabase.py +69 -5
extract_and_store_supabase.py CHANGED
@@ -1,17 +1,81 @@
1
  from supabase_models import Supabase_Client
2
 
3
 
4
- def store_message_data(message_id:str , attachment_id:str):
 
5
  if attachment_id and message_id:
 
 
 
6
  extension = attachment.filename.split(".")[-1]
7
  file_name = f"{message.id}_{attachment.attachment_id}.{extension}"
8
  print(f"file_name: {file_name}")
9
  supabase = Supabase_Client().instance
10
  try:
11
- response = supabase.storage.from_("receipt_radar").download(
12
- file_name
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  )
14
- base64_data = urlsafe_b64encode(response).decode('utf-8')
15
- return base64_data
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  except Exception as e:
17
  print(f"Error downloading or encoding file: {e}")
 
1
  from supabase_models import Supabase_Client
2
 
3
 
4
+ def extract_structure_store_message(user_id:str,message_id:str , attachment_id:str):
5
+
6
  if attachment_id and message_id:
7
+ project_id = os.getenv('PROJECT_ID')
8
+ processor_id = os.getenv('PROCESSOR_ID')
9
+ document_entities = {}
10
  extension = attachment.filename.split(".")[-1]
11
  file_name = f"{message.id}_{attachment.attachment_id}.{extension}"
12
  print(f"file_name: {file_name}")
13
  supabase = Supabase_Client().instance
14
  try:
15
+ response = supabase.storage.from_("receipt_radar").download(
16
+ file_name
17
+ )
18
+ base64_data = urlsafe_b64encode(response).decode('utf-8')
19
+
20
+ payload = {
21
+ "skipHumanReview": True,
22
+ "rawDocument": {
23
+ "mimeType": f"application/{file_type}",
24
+ "content": base64_content
25
+ }
26
+ }
27
+
28
+ access_token = get_access_token_v1()
29
+
30
+
31
+ headers = {
32
+ 'Authorization': f'Bearer {access_token}',
33
+ 'Content-Type': 'application/json; charset=utf-8'
34
+ }
35
+
36
+ response = requests.post(
37
+ f'https://us-documentai.googleapis.com/v1/projects/{project_id}/locations/us/processors/{processor_id}:process',
38
+ headers=headers,
39
+ json=payload
40
  )
41
+ response_json = response.json()
42
+ allowed_entities = [
43
+ "due_date",
44
+ "invoice_date",
45
+ "total_amount",
46
+ "total_tax_amount",
47
+ "receiver_name",
48
+ "invoice_id",
49
+ "currency",
50
+ "receiver_address",
51
+ "invoice_type",
52
+ "supplier_name",
53
+ "payment_terms",
54
+ "line_item",
55
+ "line_item/description",
56
+ "line_item/quantity",
57
+ "line_item/amount",
58
+ "line_item/unit_price"
59
+ ]
60
+ raw_text = response_json.get('document').get('text' , None)
61
+ entities = response_json.get('document').get('entities' , None)
62
+ document_entities['user_id'] = user_id
63
+ document_entities['raw_text'] = raw_text
64
+ print('Printing entities')
65
+ print(entities)
66
+ if entities is not None:
67
+ for ent in entities:
68
+ if ent.get('type') is not None:
69
+ if entity_type in allowed_entities:
70
+ mention_text = ent.get('mentionText')
71
+ normalised_values = ent.get('normalizedValue') if 'normalizedValue' in ent else None
72
+ document_entities[ent.get('type')] = {"mention_text":mention_text,"normalizedValue":normalised_values}
73
+
74
+ print(document_entities)
75
+ insert_data_response = response = (
76
+ supabase.table("countries")
77
+ .insert(document_entities)
78
+ .execute()
79
+ )
80
  except Exception as e:
81
  print(f"Error downloading or encoding file: {e}")