Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,58 +1,22 @@
|
|
| 1 |
-
from fastapi import FastAPI, HTTPException,Query,Request
|
| 2 |
import json
|
| 3 |
from authenticate import get_access_token,get_access_token_v1
|
| 4 |
import os
|
| 5 |
import requests
|
| 6 |
-
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
|
| 10 |
@app.post("/process_document")
|
| 11 |
async def process_document_base64(request: Request):
|
| 12 |
-
project_id = os.getenv('PROJECT_ID')
|
| 13 |
-
processor_id = os.getenv('PROCESSOR_ID')
|
| 14 |
-
document_entities = {}
|
| 15 |
request_data = await request.json()
|
| 16 |
# print(request_data)
|
| 17 |
-
if request_data.get('
|
| 18 |
-
return {"error":"
|
| 19 |
message_id = request_data.get('message_id')
|
| 20 |
-
filename = request_data.get('
|
| 21 |
-
file_type = filename.split('.')
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
payload = {
|
| 25 |
-
"skipHumanReview": True,
|
| 26 |
-
"rawDocument": {
|
| 27 |
-
"mimeType": "application/pdf",
|
| 28 |
-
"content": request_data.get('base64_content')
|
| 29 |
-
}
|
| 30 |
-
}
|
| 31 |
-
|
| 32 |
-
access_token = get_access_token_v1()
|
| 33 |
-
print("printing access_token")
|
| 34 |
-
print(access_token)
|
| 35 |
-
|
| 36 |
-
headers = {
|
| 37 |
-
'Authorization': f'Bearer {access_token}',
|
| 38 |
-
'Content-Type': 'application/json; charset=utf-8'
|
| 39 |
-
}
|
| 40 |
|
| 41 |
-
|
| 42 |
-
f'https://us-documentai.googleapis.com/v1/projects/{project_id}/locations/us/processors/{processor_id}:process',
|
| 43 |
-
headers=headers,
|
| 44 |
-
json=payload
|
| 45 |
-
)
|
| 46 |
-
response_json = response.json()
|
| 47 |
-
entities = response_json.get('document').get('entities' , None)
|
| 48 |
-
print('Printing entities')
|
| 49 |
-
print(entities)
|
| 50 |
-
if entities is not None:
|
| 51 |
-
for ent in entities:
|
| 52 |
-
if ent.get('type') is not None:
|
| 53 |
-
mention_text = ent.get('mentionText')
|
| 54 |
-
normalised_values = ent.get('normalizedValue') if 'normalizedValue' in ent else None
|
| 55 |
-
document_entities[ent.get('type')] = {"mention_text":mention_text,"normalizedValue":normalised_values}
|
| 56 |
|
| 57 |
-
return {"
|
| 58 |
-
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException,Query,Request,BackgroundTasks
|
| 2 |
import json
|
| 3 |
from authenticate import get_access_token,get_access_token_v1
|
| 4 |
import os
|
| 5 |
import requests
|
| 6 |
+
from extract_and_store_supabase import store_message_data
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
|
| 10 |
@app.post("/process_document")
|
| 11 |
async def process_document_base64(request: Request):
|
|
|
|
|
|
|
|
|
|
| 12 |
request_data = await request.json()
|
| 13 |
# print(request_data)
|
| 14 |
+
if request_data.get('user_id') and request_data.get('message_id') and request_data.get('attachment_id') is None:
|
| 15 |
+
return {"error":"attachment not present"}
|
| 16 |
message_id = request_data.get('message_id')
|
| 17 |
+
filename = request_data.get('attachment_id')
|
| 18 |
+
file_type = filename.split('.')[1]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
background_tasks.add_task(extract_structure_store_message,user_id,message_id,attachment_id)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
return {"status":'Processing and Updating the data to db!'}
|
|
|