Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,6 +12,10 @@ import logging
|
|
| 12 |
import boto3
|
| 13 |
import base64
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
app = FastAPI()
|
| 16 |
|
| 17 |
app.add_middleware(
|
|
@@ -27,6 +31,32 @@ timeout = Timeout(100.0, connect=600.0)
|
|
| 27 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 28 |
logging.info("Application starting up")
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
class ImageData(BaseModel):
|
| 31 |
sheetID: str
|
| 32 |
base64Image: str
|
|
@@ -52,12 +82,7 @@ async def redirect_to_google_apps_script(request: Request):
|
|
| 52 |
raise HTTPException(status_code=500, detail="Error from external API.")
|
| 53 |
return response.json()
|
| 54 |
|
| 55 |
-
appsScriptsURL = 'https://script.google.com/macros/s/AKfycbw9_31pMwQVhJPezJ9qlMRCAamQKRREnun-tfT_TnS5TOwOVcfdgU1Y9xTpAv6bZuiKOA/exec'
|
| 56 |
-
dezgo_api_key = 'DEZGO-AA84DEB300562DA089E56CEA6081AE7DB601EF2D3EE4C693910E1C25E59A4FF8023D4932'
|
| 57 |
|
| 58 |
-
dezgo_headers = {
|
| 59 |
-
'X-Dezgo-Key': dezgo_api_key,
|
| 60 |
-
}
|
| 61 |
|
| 62 |
# update the google sheet with failure
|
| 63 |
async def record_failure(s3_original_url, failed_task, sheetID):
|
|
@@ -81,7 +106,16 @@ async def post_to_google_apps_script(appsScriptsURL, body, s3_orginal_url):
|
|
| 81 |
await record_failure(s3_orginal_url, 'Failed to queue processed image.', body.sheetID)
|
| 82 |
|
| 83 |
async def background_worker(sheeturl, image_contents, filename, content_type, appsScriptsURL):
|
|
|
|
| 84 |
sheetID = sheeturl.split('/')[-2]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
image_data = {
|
| 86 |
'sheetID': sheetID,
|
| 87 |
'fileName': filename,
|
|
@@ -102,6 +136,11 @@ async def background_worker(sheeturl, image_contents, filename, content_type, ap
|
|
| 102 |
await record_failure(s3_orginal_url, 'No valid receipts found.', sheetID)
|
| 103 |
return {"message": "No valid receipts found."}
|
| 104 |
else:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
for i, base64_str in enumerate(cut_images, start=1):
|
| 106 |
image_bytes = base64.b64decode(base64_str)
|
| 107 |
object_name = f"{object_name}_processed_{i}.png"
|
|
@@ -323,4 +362,4 @@ async def mobile_process_receipts(background_tasks: BackgroundTasks, sheeturl: s
|
|
| 323 |
logging.error(f"Unexpected response from Google Apps Script API: {response.status_code} - {response.text}")
|
| 324 |
raise HTTPException(status_code=500, detail="Error from external API.")
|
| 325 |
background_tasks.add_task(background_worker, sheeturl, image_contents, image.filename, image.content_type, appsScriptsURL)
|
| 326 |
-
return {"message": "Image processing completed."}
|
|
|
|
| 12 |
import boto3
|
| 13 |
import base64
|
| 14 |
|
| 15 |
+
import firebase_admin
|
| 16 |
+
from firebase_admin import credentials
|
| 17 |
+
from firebase_admin import firestore
|
| 18 |
+
|
| 19 |
app = FastAPI()
|
| 20 |
|
| 21 |
app.add_middleware(
|
|
|
|
| 31 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 32 |
logging.info("Application starting up")
|
| 33 |
|
| 34 |
+
appsScriptsURL = 'https://script.google.com/macros/s/AKfycbw9_31pMwQVhJPezJ9qlMRCAamQKRREnun-tfT_TnS5TOwOVcfdgU1Y9xTpAv6bZuiKOA/exec'
|
| 35 |
+
dezgo_api_key = 'DEZGO-AA84DEB300562DA089E56CEA6081AE7DB601EF2D3EE4C693910E1C25E59A4FF8023D4932'
|
| 36 |
+
|
| 37 |
+
dezgo_headers = {
|
| 38 |
+
'X-Dezgo-Key': dezgo_api_key,
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
firebase_config = {
|
| 42 |
+
"apiKey": "AIzaSyA5wGzaM1OB1qnn2q6wSPsgqvHfGZP-baA",
|
| 43 |
+
"authDomain": "resheet-k6x6vh.firebaseapp.com",
|
| 44 |
+
"projectId": "resheet-k6x6vh",
|
| 45 |
+
"storageBucket": "resheet-k6x6vh.appspot.com",
|
| 46 |
+
"messagingSenderId": "532354534433",
|
| 47 |
+
"appId": "1:532354534433:web:35eb1e4e5c1deaa916a760",
|
| 48 |
+
"measurementId": "G-38XY8HEDF4"
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
cred = credentials.Certificate('./resheet-k6x6vh-firebase-adminsdk-iivci-cb667b16a7.json')
|
| 52 |
+
firebase_admin.initialize_app(cred, {
|
| 53 |
+
'projectId': firebase_config['projectId'],
|
| 54 |
+
})
|
| 55 |
+
|
| 56 |
+
db = firestore.client()
|
| 57 |
+
users_ref = db.collection('users')
|
| 58 |
+
|
| 59 |
+
|
| 60 |
class ImageData(BaseModel):
|
| 61 |
sheetID: str
|
| 62 |
base64Image: str
|
|
|
|
| 82 |
raise HTTPException(status_code=500, detail="Error from external API.")
|
| 83 |
return response.json()
|
| 84 |
|
|
|
|
|
|
|
| 85 |
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
# update the google sheet with failure
|
| 88 |
async def record_failure(s3_original_url, failed_task, sheetID):
|
|
|
|
| 106 |
await record_failure(s3_orginal_url, 'Failed to queue processed image.', body.sheetID)
|
| 107 |
|
| 108 |
async def background_worker(sheeturl, image_contents, filename, content_type, appsScriptsURL):
|
| 109 |
+
|
| 110 |
sheetID = sheeturl.split('/')[-2]
|
| 111 |
+
user_doc = users_ref.document(sheetID)
|
| 112 |
+
# get the 'total_uploaded' field from the user's document
|
| 113 |
+
user_data = user_doc.get().to_dict()
|
| 114 |
+
if user_data is None:
|
| 115 |
+
await record_failure(s3_orginal_url, 'User not found.', sheetID)
|
| 116 |
+
return {"message": "User not found."}
|
| 117 |
+
total_uploaded = user_data.get('total_uploaded', 0)
|
| 118 |
+
|
| 119 |
image_data = {
|
| 120 |
'sheetID': sheetID,
|
| 121 |
'fileName': filename,
|
|
|
|
| 136 |
await record_failure(s3_orginal_url, 'No valid receipts found.', sheetID)
|
| 137 |
return {"message": "No valid receipts found."}
|
| 138 |
else:
|
| 139 |
+
# get the number of total cut photos
|
| 140 |
+
receipt_count = cut_images.__len__()
|
| 141 |
+
# set the user's total_uploaded field to the current total_uploaded + receipt_count
|
| 142 |
+
user_doc.update({'total_uploaded': total_uploaded + receipt_count})
|
| 143 |
+
# save the processed images to s3
|
| 144 |
for i, base64_str in enumerate(cut_images, start=1):
|
| 145 |
image_bytes = base64.b64decode(base64_str)
|
| 146 |
object_name = f"{object_name}_processed_{i}.png"
|
|
|
|
| 362 |
logging.error(f"Unexpected response from Google Apps Script API: {response.status_code} - {response.text}")
|
| 363 |
raise HTTPException(status_code=500, detail="Error from external API.")
|
| 364 |
background_tasks.add_task(background_worker, sheeturl, image_contents, image.filename, image.content_type, appsScriptsURL)
|
| 365 |
+
return {"message": "Image processing completed."}
|