m8chaa commited on
Commit
7f8975b
·
verified ·
1 Parent(s): 5491224

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -26
app.py CHANGED
@@ -443,10 +443,23 @@ async def check_user_doc(user_id: str = Form(...)):
443
  # await move_file_to_folder(image_id, parent_folders, user_data['gDrive_metadata']['error'])
444
  # return {"error": str(e)}
445
 
446
- async def update_user_sheets_and_folders(user_data, image_id, found_receipt_no, found_business_cards_no, data, transaction_ref):
447
- new_folders = []
448
  try:
449
- # Process receipts and business cards
 
 
 
 
 
 
 
 
 
 
 
 
 
 
450
  if found_receipt_no > 0:
451
  new_folders.append(user_data['gDrive_metadata']['receipts'])
452
  receipts_data = convert_dicts_to_list(
@@ -456,8 +469,8 @@ async def update_user_sheets_and_folders(user_data, image_id, found_receipt_no,
456
  link_text="바로가기",
457
  link_position=1 # Link third
458
  )
 
459
  await update_user_sheet(user_data['gDrive_metadata']['spreadsheet'], receipts_ss, "영수증", receipts_data)
460
-
461
  if found_business_cards_no > 0:
462
  new_folders.append(user_data['gDrive_metadata']['business_cards'])
463
  business_cards_data = convert_dicts_to_list(
@@ -468,16 +481,27 @@ async def update_user_sheets_and_folders(user_data, image_id, found_receipt_no,
468
  link_position=-1 # Link at the end
469
  )
470
  await update_user_sheet(user_data['gDrive_metadata']['spreadsheet'], business_cards_ss, "명함", business_cards_data)
471
-
472
- await move_file_to_folder(image_id, parent_folders, new_folders)
473
- await update_fb_transaction(
 
474
  transaction_ref,
475
  no_receipts=found_receipt_no,
476
  no_business_cards=found_business_cards_no
477
  )
 
 
 
 
 
 
 
 
478
  except Exception as e:
479
- logging.error(f"An error occurred in background task: {e}")
 
480
  await move_file_to_folder(image_id, parent_folders, user_data['gDrive_metadata']['error'])
 
481
  @app.post("/process-image")
482
  async def process_photo(background_tasks: BackgroundTasks, image_id: str = Form(...), user_id: str = Form(...)):
483
  transaction_ref = transactions_collection_ref.document(user_id)
@@ -505,24 +529,7 @@ async def process_photo(background_tasks: BackgroundTasks, image_id: str = Form(
505
  user = user_ref.get()
506
  user_data = user.to_dict()
507
 
508
- try:
509
- parent_folders = [user_data['gDrive_metadata']['yungsoogi'], user_data['gDrive_metadata']['uploaded']]
510
- dataJson = await request_gpt4o_completion(image_id, transaction_data['purchased_credit']['image_detection'])
511
- if dataJson is None:
512
- return {"error": "An error occurred while processing the image"}
513
-
514
- data = json.loads(dataJson)
515
- found_receipt_no = len(data['receipts']) if 'receipts' in data else 0
516
- found_business_cards_no = len(data['busi_cards']) if 'busi_cards' in data else 0
517
-
518
- # Schedule background tasks
519
- background_tasks.add_task(update_user_sheets_and_folders, user_data, image_id, found_receipt_no, found_business_cards_no, data, transaction_ref)
520
-
521
- return {"success": True}
522
- except Exception as e:
523
- logging.error(f"An error occurred: {e}")
524
- await move_file_to_folder(image_id, parent_folders, user_data['gDrive_metadata']['error'])
525
- return {"error": str(e)}
526
 
527
 
528
  @app.post("/sync-data")
 
443
  # await move_file_to_folder(image_id, parent_folders, user_data['gDrive_metadata']['error'])
444
  # return {"error": str(e)}
445
 
446
+ async def update_user_sheets_and_folders(user_data, transaction_ref, image_id):
 
447
  try:
448
+ parent_folders = [user_data['gDrive_metadata']['yungsoogi'], user_data['gDrive_metadata']['uploaded']]
449
+ dataJson = await request_gpt4o_completion(image_id, transaction_data['purchased_credit']['image_detection'])
450
+ if dataJson is None:
451
+ return {"error": "An error occurred while processing the image"}
452
+
453
+ data = json.loads(dataJson)
454
+ # Check if 'receipts' key exists and the length
455
+ found_receipt_no = len(data['receipts']) if 'receipts' in data else 0
456
+ found_business_cards_no = len(data['busi_cards']) if 'busi_cards' in data else 0
457
+ new_folders = []
458
+ timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
459
+ # https://drive.google.com/file/d/1cFIA09PBqFjM8YvIz1D60p7HMGk1uBvo/view?usp=sharing
460
+ download_image_url = f"https://drive.google.com/uc?id={image_id}&export=download"
461
+ image_url = f"https://drive.google.com/file/d/{image_id}/view?usp=sharing"
462
+ # 이미지를 보여주어야 할때 =image({image_url})를 사용하고 갈 수 있는 경로를 만들때는 =HYPERLINK({image_url}, '바로가기')를 사용하세요.
463
  if found_receipt_no > 0:
464
  new_folders.append(user_data['gDrive_metadata']['receipts'])
465
  receipts_data = convert_dicts_to_list(
 
469
  link_text="바로가기",
470
  link_position=1 # Link third
471
  )
472
+ print(receipts_data)
473
  await update_user_sheet(user_data['gDrive_metadata']['spreadsheet'], receipts_ss, "영수증", receipts_data)
 
474
  if found_business_cards_no > 0:
475
  new_folders.append(user_data['gDrive_metadata']['business_cards'])
476
  business_cards_data = convert_dicts_to_list(
 
481
  link_position=-1 # Link at the end
482
  )
483
  await update_user_sheet(user_data['gDrive_metadata']['spreadsheet'], business_cards_ss, "명함", business_cards_data)
484
+
485
+ print(f"{found_receipt_no}, {found_business_cards_no}")
486
+
487
+ status = await update_fb_transaction(
488
  transaction_ref,
489
  no_receipts=found_receipt_no,
490
  no_business_cards=found_business_cards_no
491
  )
492
+
493
+
494
+ if status['status'] == "Success":
495
+ await move_file_to_folder(image_id, parent_folders, new_folders)
496
+ return {"success": True}
497
+ else:
498
+ logging.error(f"Transaction update failed with status: {status['status']}")
499
+ return {"error": "An error has occurred while updating the transaction"}
500
  except Exception as e:
501
+ logging.error(f"An error occurred: {e}")
502
+ # Move the image file to the error folder
503
  await move_file_to_folder(image_id, parent_folders, user_data['gDrive_metadata']['error'])
504
+
505
  @app.post("/process-image")
506
  async def process_photo(background_tasks: BackgroundTasks, image_id: str = Form(...), user_id: str = Form(...)):
507
  transaction_ref = transactions_collection_ref.document(user_id)
 
529
  user = user_ref.get()
530
  user_data = user.to_dict()
531
 
532
+ background_tasks.add_task(update_user_sheets_and_folders, user_data, transaction_ref, image_id)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
533
 
534
 
535
  @app.post("/sync-data")