m8chaa commited on
Commit
07e8346
·
verified ·
1 Parent(s): 46fabd3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -50
app.py CHANGED
@@ -94,14 +94,16 @@ async def record_failure(s3_original_url, failed_task, sheetID):
94
 
95
  # post the processed image to google apps script
96
  async def post_to_google_apps_script(appsScriptsURL, body, s3_orginal_url):
97
- async with httpx.AsyncClient(follow_redirects=True) as client:
98
  response = await client.post(appsScriptsURL, json=body)
99
  if response.status_code != 200:
100
  logging.error(f"Unexpected response from Google Apps Script API: {response.status_code} - {response.text}")
101
  await record_failure(s3_orginal_url, 'Failed to queue processed image.', body.sheetID)
 
 
102
 
103
  # background worker
104
- async def background_worker(sheeturl, image_contents, filename, content_type, appsScriptsURL, user_ref, doc_type):
105
  sheetID = sheeturl.split('/')[-2]
106
  user_doc = users_ref.document(user_ref)
107
  # get the 'total_uploaded' field from the user's document
@@ -113,14 +115,6 @@ async def background_worker(sheeturl, image_contents, filename, content_type, ap
113
  await record_failure('', 'User not found.', sheetID)
114
  return {"message": "User not found."}
115
 
116
- if doc_type == 'receipt':
117
- total_uploaded = user_data.get('receipts_uploaded', 0)
118
- current_upload = 'receipts_uploaded'
119
- elif doc_type == 'business_card':
120
- total_uploaded = user_data.get('business_cards_uploaded', 0)
121
- current_upload = 'business_cards_uploaded'
122
-
123
-
124
  image_data = {
125
  'sheetID': sheetID,
126
  'fileName': filename,
@@ -141,19 +135,14 @@ async def background_worker(sheeturl, image_contents, filename, content_type, ap
141
  await record_failure(s3_orginal_url, 'No valid receipts found.', sheetID)
142
  return {"message": "No valid receipts found."}
143
  else:
144
- # get the number of total cut photos
145
- receipt_count = cut_images.__len__()
146
- # set the user's total_uploaded field to the current total_uploaded + receipt_count
147
- user_doc.update({current_upload: total_uploaded + receipt_count})
148
-
149
  for i, base64_str in enumerate(cut_images, start=1):
150
  image_bytes = base64.b64decode(base64_str)
151
- object_name = f"{object_name}_processed_{i}.png"
152
  response = s3_client.put_object(
153
  Bucket=bucket_name,
154
  Key=object_name,
155
  Body=image_bytes,
156
- ContentType='image/png'
157
  )
158
  status = response.get("ResponseMetadata", {}).get("HTTPStatusCode")
159
  if status != 200:
@@ -167,12 +156,19 @@ async def background_worker(sheeturl, image_contents, filename, content_type, ap
167
  "queued": queued,
168
  "mode": "mobile",
169
  "sheetID": sheetID,
170
- "docType": doc_type,
171
  }
172
-
173
- print(body)
174
  logging.info(body)
175
- await post_to_google_apps_script(appsScriptsURL, body, s3_orginal_url)
 
 
 
 
 
 
 
 
 
 
176
  return {"message": "Image processing completed."}
177
 
178
  # send the image to dezgo
@@ -284,29 +280,31 @@ async def redirect_to_google_apps_script(request: Request):
284
 
285
  # receive a sheetID and an image as multipart form data
286
  @app.post('/mobile-process')
287
- async def mobile_process_receipts(background_tasks: BackgroundTasks, sheeturl: str = Form(...), user_ref: str = Form(...), doc_type: str = Form(...), image: UploadFile = File(...)):
288
  image_contents = await image.read()
289
  uuid = str(uuid4())
290
- timestamp = str(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
291
  sheetID = sheeturl.split('/')[-2]
292
-
293
- message = "영수기 AI가 데이터 분석 중입니다. 잠시만 기다려주세요."
294
- data = [timestamp, uuid, message]
295
  body = {
296
  "sheetID": sheetID,
297
- "data": data,
298
  "mode": "alert-user",
299
- "docType": doc_type,
300
  }
301
  async with httpx.AsyncClient(follow_redirects=True) as client:
302
  response = await client.post(appsScriptsURL, json=body)
303
  if response.status_code != 200:
304
  logging.error(f"Unexpected response from Google Apps Script API: {response.status_code} - {response.text}")
305
- raise HTTPException(status_code=500, detail="Error from external API.")
306
 
307
- background_tasks.add_task(background_worker, sheeturl, image_contents, uuid, image.content_type, appsScriptsURL, user_ref, doc_type)
 
 
 
 
 
308
 
309
- return {"message": "Image processing completed."}
310
 
311
 
312
  @app.post('/process')
@@ -317,23 +315,6 @@ async def mobile_process_receipts(background_tasks: BackgroundTasks, request: Re
317
 
318
  content_type = data['mimeType']
319
  sheeturl = data['sheetID']
320
- doc_type = data['docType']
321
-
322
- checkPhoto = {
323
- 'sheetID': sheeturl.split('/')[-2],
324
- 'mode': "check",
325
- 'photo': base64_str,
326
- 'mimeType': content_type,
327
- 'fileName': 'test.png',
328
- 'docType': doc_type,
329
- }
330
- async with httpx.AsyncClient(timeout=300, follow_redirects=True) as client:
331
- response = await client.post(appsScriptsURL, json=checkPhoto)
332
- if response.status_code != 200:
333
- logging.error(f"Unexpected response from Google Apps Script API: {response.status_code} - {response.text}")
334
- raise HTTPException(status_code=500, detail="Error from external API.")
335
- if response.json()['status'] == 'invalid':
336
- return {"message": "Invalid photo."}
337
 
338
  image_contents = base64.b64decode(base64_str)
339
  uuid = str(uuid4())
@@ -350,7 +331,7 @@ async def mobile_process_receipts(background_tasks: BackgroundTasks, request: Re
350
  if response.status_code != 200:
351
  logging.error(f"Unexpected response from Google Apps Script API: {response.status_code} - {response.text}")
352
  raise HTTPException(status_code=500, detail="Error from external API.")
353
-
354
  # before continuing on, search db for user via sheetID
355
  sheetID = sheeturl.split('/')[-2]
356
  # Query the database for documents where 'sheetID' field is equal to sheetID
@@ -364,7 +345,7 @@ async def mobile_process_receipts(background_tasks: BackgroundTasks, request: Re
364
 
365
  user_ref = user_docs[0].id
366
 
367
- background_tasks.add_task(background_worker, sheeturl, image_contents, uuid, content_type, appsScriptsURL, user_ref, doc_type)
368
 
369
  return {"message": "Image processing completed."}
370
 
 
94
 
95
  # post the processed image to google apps script
96
  async def post_to_google_apps_script(appsScriptsURL, body, s3_orginal_url):
97
+ async with httpx.AsyncClient(follow_redirects=True, timeout=timeout) as client:
98
  response = await client.post(appsScriptsURL, json=body)
99
  if response.status_code != 200:
100
  logging.error(f"Unexpected response from Google Apps Script API: {response.status_code} - {response.text}")
101
  await record_failure(s3_orginal_url, 'Failed to queue processed image.', body.sheetID)
102
+ raise HTTPException(status_code=500, detail="Error from external API.")
103
+ return response.json()
104
 
105
  # background worker
106
+ async def background_worker(sheeturl, image_contents, filename, content_type, appsScriptsURL, user_ref):
107
  sheetID = sheeturl.split('/')[-2]
108
  user_doc = users_ref.document(user_ref)
109
  # get the 'total_uploaded' field from the user's document
 
115
  await record_failure('', 'User not found.', sheetID)
116
  return {"message": "User not found."}
117
 
 
 
 
 
 
 
 
 
118
  image_data = {
119
  'sheetID': sheetID,
120
  'fileName': filename,
 
135
  await record_failure(s3_orginal_url, 'No valid receipts found.', sheetID)
136
  return {"message": "No valid receipts found."}
137
  else:
 
 
 
 
 
138
  for i, base64_str in enumerate(cut_images, start=1):
139
  image_bytes = base64.b64decode(base64_str)
140
+ object_name = f"{object_name}_processed_{i}.jpg"
141
  response = s3_client.put_object(
142
  Bucket=bucket_name,
143
  Key=object_name,
144
  Body=image_bytes,
145
+ ContentType='image/jepg'
146
  )
147
  status = response.get("ResponseMetadata", {}).get("HTTPStatusCode")
148
  if status != 200:
 
156
  "queued": queued,
157
  "mode": "mobile",
158
  "sheetID": sheetID,
 
159
  }
 
 
160
  logging.info(body)
161
+ countedDoc = await post_to_google_apps_script(appsScriptsURL, body, s3_orginal_url)
162
+
163
+ if countedDoc['num_receipts'] > 0:
164
+ total_uploaded = user_data.get('receipts_uploaded', 0)
165
+ current_upload = 'receipts_uploaded'
166
+ await user_doc.update({current_upload: user_data[current_upload] + countedDoc['num_receipts']})
167
+ elif countedDoc['num_business_cards'] > 0:
168
+ total_uploaded = user_data.get('business_cards_uploaded', 0)
169
+ current_upload = 'business_cards_uploaded'
170
+ await user_doc.update({current_upload: user_data[current_upload] + countedDoc['num_business_cards']})
171
+
172
  return {"message": "Image processing completed."}
173
 
174
  # send the image to dezgo
 
280
 
281
  # receive a sheetID and an image as multipart form data
282
  @app.post('/mobile-process')
283
+ async def mobile_process_receipts(background_tasks: BackgroundTasks, sheeturl: str = Form(...), user_ref: str = Form(...), image: UploadFile = File(...)):
284
  image_contents = await image.read()
285
  uuid = str(uuid4())
 
286
  sheetID = sheeturl.split('/')[-2]
287
+ base_64_image = base64.b64encode(image_contents).decode('utf-8')
 
 
288
  body = {
289
  "sheetID": sheetID,
290
+ "base64Image": base_64_image,
291
  "mode": "alert-user",
292
+ "mimeType": image.content_type,
293
  }
294
  async with httpx.AsyncClient(follow_redirects=True) as client:
295
  response = await client.post(appsScriptsURL, json=body)
296
  if response.status_code != 200:
297
  logging.error(f"Unexpected response from Google Apps Script API: {response.status_code} - {response.text}")
298
+ raise HTTPException(status_code=500, detail="Error in trying to connect to your sheet. Please try again.")
299
 
300
+ foundList = response.json().get('foundList')
301
+ if not foundList:
302
+ # alert user that no documents were found
303
+ return {"message": "No documents were found."}
304
+
305
+ background_tasks.add_task(background_worker, sheeturl, image_contents, uuid, image.content_type, appsScriptsURL, user_ref)
306
 
307
+ return {"message": "Your image has been queued. It may take up to 5 minutes."}
308
 
309
 
310
  @app.post('/process')
 
315
 
316
  content_type = data['mimeType']
317
  sheeturl = data['sheetID']
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
318
 
319
  image_contents = base64.b64decode(base64_str)
320
  uuid = str(uuid4())
 
331
  if response.status_code != 200:
332
  logging.error(f"Unexpected response from Google Apps Script API: {response.status_code} - {response.text}")
333
  raise HTTPException(status_code=500, detail="Error from external API.")
334
+
335
  # before continuing on, search db for user via sheetID
336
  sheetID = sheeturl.split('/')[-2]
337
  # Query the database for documents where 'sheetID' field is equal to sheetID
 
345
 
346
  user_ref = user_docs[0].id
347
 
348
+ background_tasks.add_task(background_worker, sheeturl, image_contents, uuid, content_type, appsScriptsURL, user_ref)
349
 
350
  return {"message": "Image processing completed."}
351