Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -443,6 +443,81 @@ 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, transaction_ref, transaction_data, image_id):
|
| 447 |
try:
|
| 448 |
parent_folders = [user_data['gDrive_metadata']['yungsoogi'], user_data['gDrive_metadata']['uploaded']]
|
|
@@ -454,34 +529,41 @@ async def update_user_sheets_and_folders(user_data, transaction_ref, transaction
|
|
| 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 |
-
|
|
|
|
| 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 |
-
#
|
|
|
|
|
|
|
| 463 |
if found_receipt_no > 0:
|
| 464 |
-
|
| 465 |
receipts_data = convert_dicts_to_list(
|
| 466 |
data['receipts'],
|
| 467 |
add_link=True,
|
| 468 |
image_id=image_id,
|
| 469 |
link_text="바로가기",
|
| 470 |
-
link_position=1
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
| 476 |
business_cards_data = convert_dicts_to_list(
|
| 477 |
data['busi_cards'],
|
| 478 |
add_link=True,
|
| 479 |
image_id=image_id,
|
| 480 |
link_text="명함보기",
|
| 481 |
-
link_position=-1
|
| 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(
|
|
@@ -490,9 +572,11 @@ async def update_user_sheets_and_folders(user_data, transaction_ref, transaction
|
|
| 490 |
no_business_cards=found_business_cards_no
|
| 491 |
)
|
| 492 |
|
| 493 |
-
|
| 494 |
if status['status'] == "Success":
|
| 495 |
-
|
|
|
|
|
|
|
|
|
|
| 496 |
return {"success": True}
|
| 497 |
else:
|
| 498 |
logging.error(f"Transaction update failed with status: {status['status']}")
|
|
@@ -500,7 +584,7 @@ async def update_user_sheets_and_folders(user_data, transaction_ref, 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 |
return {"error": str(e)}
|
| 505 |
|
| 506 |
@app.post("/process-image")
|
|
|
|
| 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, transaction_data, 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(
|
| 466 |
+
# data['receipts'],
|
| 467 |
+
# add_link=True,
|
| 468 |
+
# image_id=image_id,
|
| 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(
|
| 477 |
+
# data['busi_cards'],
|
| 478 |
+
# add_link=True,
|
| 479 |
+
# image_id=image_id,
|
| 480 |
+
# link_text="명함보기",
|
| 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 |
+
# return {"error": str(e)}
|
| 505 |
+
async def create_shortcut(file_id, parent_folder_id, name):
|
| 506 |
+
try:
|
| 507 |
+
file_metadata = {
|
| 508 |
+
'name': name,
|
| 509 |
+
'mimeType': 'application/vnd.google-apps.shortcut',
|
| 510 |
+
'parents': [parent_folder_id],
|
| 511 |
+
'shortcutDetails': {
|
| 512 |
+
'targetId': file_id
|
| 513 |
+
}
|
| 514 |
+
}
|
| 515 |
+
shortcut = drive_service.files().create(body=file_metadata, fields='id').execute()
|
| 516 |
+
return shortcut.get('id')
|
| 517 |
+
except Exception as e:
|
| 518 |
+
logging.error(f"Failed to create shortcut: {e}")
|
| 519 |
+
return None
|
| 520 |
+
|
| 521 |
async def update_user_sheets_and_folders(user_data, transaction_ref, transaction_data, image_id):
|
| 522 |
try:
|
| 523 |
parent_folders = [user_data['gDrive_metadata']['yungsoogi'], user_data['gDrive_metadata']['uploaded']]
|
|
|
|
| 529 |
# Check if 'receipts' key exists and the length
|
| 530 |
found_receipt_no = len(data['receipts']) if 'receipts' in data else 0
|
| 531 |
found_business_cards_no = len(data['busi_cards']) if 'busi_cards' in data else 0
|
| 532 |
+
new_folder = None
|
| 533 |
+
secondary_folder = None
|
| 534 |
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 535 |
# https://drive.google.com/file/d/1cFIA09PBqFjM8YvIz1D60p7HMGk1uBvo/view?usp=sharing
|
| 536 |
download_image_url = f"https://drive.google.com/uc?id={image_id}&export=download"
|
| 537 |
image_url = f"https://drive.google.com/file/d/{image_id}/view?usp=sharing"
|
| 538 |
+
# 이미지를 보여주어야 할때 =image({image_url})를 사용하고 갈 수 있는 경로를 만들때는 =HYPERLINK({image_url}, '바로가기')를 사용하세요.
|
| 539 |
+
|
| 540 |
+
# Determine the primary folder to move the image to and secondary folder for shortcut
|
| 541 |
if found_receipt_no > 0:
|
| 542 |
+
new_folder = user_data['gDrive_metadata']['receipts']
|
| 543 |
receipts_data = convert_dicts_to_list(
|
| 544 |
data['receipts'],
|
| 545 |
add_link=True,
|
| 546 |
image_id=image_id,
|
| 547 |
link_text="바로가기",
|
| 548 |
+
link_position=1 # Link third
|
| 549 |
)
|
| 550 |
print(receipts_data)
|
| 551 |
await update_user_sheet(user_data['gDrive_metadata']['spreadsheet'], receipts_ss, "영수증", receipts_data)
|
| 552 |
+
|
| 553 |
if found_business_cards_no > 0:
|
| 554 |
+
if new_folder is None:
|
| 555 |
+
new_folder = user_data['gDrive_metadata']['business_cards']
|
| 556 |
+
else:
|
| 557 |
+
secondary_folder = user_data['gDrive_metadata']['business_cards']
|
| 558 |
business_cards_data = convert_dicts_to_list(
|
| 559 |
data['busi_cards'],
|
| 560 |
add_link=True,
|
| 561 |
image_id=image_id,
|
| 562 |
link_text="명함보기",
|
| 563 |
+
link_position=-1 # Link at the end
|
| 564 |
)
|
| 565 |
await update_user_sheet(user_data['gDrive_metadata']['spreadsheet'], business_cards_ss, "명함", business_cards_data)
|
| 566 |
+
|
| 567 |
print(f"{found_receipt_no}, {found_business_cards_no}")
|
| 568 |
|
| 569 |
status = await update_fb_transaction(
|
|
|
|
| 572 |
no_business_cards=found_business_cards_no
|
| 573 |
)
|
| 574 |
|
|
|
|
| 575 |
if status['status'] == "Success":
|
| 576 |
+
if new_folder:
|
| 577 |
+
await move_file_to_folder(image_id, parent_folders, [new_folder])
|
| 578 |
+
if secondary_folder:
|
| 579 |
+
await create_shortcut(image_id, secondary_folder, f"Shortcut to {image_id}")
|
| 580 |
return {"success": True}
|
| 581 |
else:
|
| 582 |
logging.error(f"Transaction update failed with status: {status['status']}")
|
|
|
|
| 584 |
except Exception as e:
|
| 585 |
logging.error(f"An error occurred: {e}")
|
| 586 |
# Move the image file to the error folder
|
| 587 |
+
await move_file_to_folder(image_id, parent_folders, [user_data['gDrive_metadata']['error']])
|
| 588 |
return {"error": str(e)}
|
| 589 |
|
| 590 |
@app.post("/process-image")
|