Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -355,11 +355,134 @@ async def check_user_doc(user_id: str = Form(...)):
|
|
| 355 |
# update user sheet
|
| 356 |
# update firebase transaction
|
| 357 |
# privatize the image and move the image to appropriate folder(s)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 358 |
@app.post("/process-image")
|
| 359 |
-
async def process_photo(image_id: str
|
| 360 |
transaction_ref = transactions_collection_ref.document(user_id)
|
| 361 |
transaction = transaction_ref.get()
|
| 362 |
-
|
| 363 |
if not transaction.exists:
|
| 364 |
transaction_ref.set({
|
| 365 |
"no_sms": 0,
|
|
@@ -372,17 +495,15 @@ async def process_photo(image_id: str = Form(...), user_id: str = Form(...)):
|
|
| 372 |
}
|
| 373 |
})
|
| 374 |
transaction = transaction_ref.get()
|
| 375 |
-
|
| 376 |
transaction_data = transaction.to_dict()
|
| 377 |
|
| 378 |
-
# check if any of the limits have been reached
|
| 379 |
if transaction_data['no_receipts'] + transaction_data['no_business_cards'] >= transaction_data['purchased_credit']['image_detection']:
|
| 380 |
return {"error": "You have reached the limit of the number of items you can process. Please upgrade your plan."}
|
| 381 |
|
| 382 |
user_ref = users_collection_ref.document(user_id)
|
| 383 |
user = user_ref.get()
|
| 384 |
user_data = user.to_dict()
|
| 385 |
-
|
| 386 |
|
| 387 |
try:
|
| 388 |
parent_folders = [user_data['gDrive_metadata']['yungsoogi'], user_data['gDrive_metadata']['uploaded']]
|
|
@@ -391,57 +512,18 @@ async def process_photo(image_id: str = Form(...), user_id: str = Form(...)):
|
|
| 391 |
return {"error": "An error occurred while processing the image"}
|
| 392 |
|
| 393 |
data = json.loads(dataJson)
|
| 394 |
-
# Check if 'receipts' key exists and the length
|
| 395 |
found_receipt_no = len(data['receipts']) if 'receipts' in data else 0
|
| 396 |
found_business_cards_no = len(data['busi_cards']) if 'busi_cards' in data else 0
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
# 이미지를 보여주어야 할때 =image({image_url})를 사용하고 갈 수 있는 경로를 만들때는 =HYPERLINK({image_url}, '바로가기')를 사용하세요.
|
| 403 |
-
if found_receipt_no > 0:
|
| 404 |
-
new_folders.append(user_data['gDrive_metadata']['receipts'])
|
| 405 |
-
receipts_data = convert_dicts_to_list(
|
| 406 |
-
data['receipts'],
|
| 407 |
-
add_link=True,
|
| 408 |
-
image_id=image_id,
|
| 409 |
-
link_text="바로가기",
|
| 410 |
-
link_position=1 # Link third
|
| 411 |
-
)
|
| 412 |
-
print(receipts_data)
|
| 413 |
-
await update_user_sheet(user_data['gDrive_metadata']['spreadsheet'], receipts_ss, "영수증", receipts_data)
|
| 414 |
-
if found_business_cards_no > 0:
|
| 415 |
-
new_folders.append(user_data['gDrive_metadata']['business_cards'])
|
| 416 |
-
business_cards_data = convert_dicts_to_list(
|
| 417 |
-
data['busi_cards'],
|
| 418 |
-
add_link=True,
|
| 419 |
-
image_id=image_id,
|
| 420 |
-
link_text="명함보기",
|
| 421 |
-
link_position=-1 # Link at the end
|
| 422 |
-
)
|
| 423 |
-
await update_user_sheet(user_data['gDrive_metadata']['spreadsheet'], business_cards_ss, "명함", business_cards_data)
|
| 424 |
-
|
| 425 |
-
print(f"{found_receipt_no}, {found_business_cards_no}")
|
| 426 |
-
|
| 427 |
-
status = await update_fb_transaction(
|
| 428 |
-
transaction_ref,
|
| 429 |
-
no_receipts=found_receipt_no,
|
| 430 |
-
no_business_cards=found_business_cards_no
|
| 431 |
-
)
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
if status['status'] == "Success":
|
| 435 |
-
await move_file_to_folder(image_id, parent_folders, new_folders)
|
| 436 |
-
return {"success": True}
|
| 437 |
-
else:
|
| 438 |
-
logging.error(f"Transaction update failed with status: {status['status']}")
|
| 439 |
-
return {"error": "An error has occurred while updating the transaction"}
|
| 440 |
except Exception as e:
|
| 441 |
logging.error(f"An error occurred: {e}")
|
| 442 |
-
# Move the image file to the error folder
|
| 443 |
await move_file_to_folder(image_id, parent_folders, user_data['gDrive_metadata']['error'])
|
| 444 |
return {"error": str(e)}
|
|
|
|
| 445 |
|
| 446 |
@app.post("/sync-data")
|
| 447 |
async def process_photo(data_json: str = Form(...), user_id: str = Form(...)):
|
|
|
|
| 355 |
# update user sheet
|
| 356 |
# update firebase transaction
|
| 357 |
# privatize the image and move the image to appropriate folder(s)
|
| 358 |
+
# @app.post("/process-image")
|
| 359 |
+
# async def process_photo(image_id: str = Form(...), user_id: str = Form(...)):
|
| 360 |
+
# transaction_ref = transactions_collection_ref.document(user_id)
|
| 361 |
+
# transaction = transaction_ref.get()
|
| 362 |
+
# # If transaction does not exist, create a new one
|
| 363 |
+
# if not transaction.exists:
|
| 364 |
+
# transaction_ref.set({
|
| 365 |
+
# "no_sms": 0,
|
| 366 |
+
# "no_contacts": 0,
|
| 367 |
+
# "no_receipts": 0,
|
| 368 |
+
# "no_business_cards": 0,
|
| 369 |
+
# "purchased_credit": {
|
| 370 |
+
# "image_detection": 100,
|
| 371 |
+
# "sync_data": 500,
|
| 372 |
+
# }
|
| 373 |
+
# })
|
| 374 |
+
# transaction = transaction_ref.get()
|
| 375 |
+
|
| 376 |
+
# transaction_data = transaction.to_dict()
|
| 377 |
+
|
| 378 |
+
# # check if any of the limits have been reached
|
| 379 |
+
# if transaction_data['no_receipts'] + transaction_data['no_business_cards'] >= transaction_data['purchased_credit']['image_detection']:
|
| 380 |
+
# return {"error": "You have reached the limit of the number of items you can process. Please upgrade your plan."}
|
| 381 |
+
|
| 382 |
+
# user_ref = users_collection_ref.document(user_id)
|
| 383 |
+
# user = user_ref.get()
|
| 384 |
+
# user_data = user.to_dict()
|
| 385 |
+
|
| 386 |
+
|
| 387 |
+
# try:
|
| 388 |
+
# parent_folders = [user_data['gDrive_metadata']['yungsoogi'], user_data['gDrive_metadata']['uploaded']]
|
| 389 |
+
# dataJson = await request_gpt4o_completion(image_id, transaction_data['purchased_credit']['image_detection'])
|
| 390 |
+
# if dataJson is None:
|
| 391 |
+
# return {"error": "An error occurred while processing the image"}
|
| 392 |
+
|
| 393 |
+
# data = json.loads(dataJson)
|
| 394 |
+
# # Check if 'receipts' key exists and the length
|
| 395 |
+
# found_receipt_no = len(data['receipts']) if 'receipts' in data else 0
|
| 396 |
+
# found_business_cards_no = len(data['busi_cards']) if 'busi_cards' in data else 0
|
| 397 |
+
# new_folders = []
|
| 398 |
+
# timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 399 |
+
# # https://drive.google.com/file/d/1cFIA09PBqFjM8YvIz1D60p7HMGk1uBvo/view?usp=sharing
|
| 400 |
+
# download_image_url = f"https://drive.google.com/uc?id={image_id}&export=download"
|
| 401 |
+
# image_url = f"https://drive.google.com/file/d/{image_id}/view?usp=sharing"
|
| 402 |
+
# # 이미지를 보여주어야 할때 =image({image_url})를 사용하고 갈 수 있는 경로를 만들때는 =HYPERLINK({image_url}, '바로가기')를 사용하세요.
|
| 403 |
+
# if found_receipt_no > 0:
|
| 404 |
+
# new_folders.append(user_data['gDrive_metadata']['receipts'])
|
| 405 |
+
# receipts_data = convert_dicts_to_list(
|
| 406 |
+
# data['receipts'],
|
| 407 |
+
# add_link=True,
|
| 408 |
+
# image_id=image_id,
|
| 409 |
+
# link_text="바로가기",
|
| 410 |
+
# link_position=1 # Link third
|
| 411 |
+
# )
|
| 412 |
+
# print(receipts_data)
|
| 413 |
+
# await update_user_sheet(user_data['gDrive_metadata']['spreadsheet'], receipts_ss, "영수증", receipts_data)
|
| 414 |
+
# if found_business_cards_no > 0:
|
| 415 |
+
# new_folders.append(user_data['gDrive_metadata']['business_cards'])
|
| 416 |
+
# business_cards_data = convert_dicts_to_list(
|
| 417 |
+
# data['busi_cards'],
|
| 418 |
+
# add_link=True,
|
| 419 |
+
# image_id=image_id,
|
| 420 |
+
# link_text="명함보기",
|
| 421 |
+
# link_position=-1 # Link at the end
|
| 422 |
+
# )
|
| 423 |
+
# await update_user_sheet(user_data['gDrive_metadata']['spreadsheet'], business_cards_ss, "명함", business_cards_data)
|
| 424 |
+
|
| 425 |
+
# print(f"{found_receipt_no}, {found_business_cards_no}")
|
| 426 |
+
|
| 427 |
+
# status = await update_fb_transaction(
|
| 428 |
+
# transaction_ref,
|
| 429 |
+
# no_receipts=found_receipt_no,
|
| 430 |
+
# no_business_cards=found_business_cards_no
|
| 431 |
+
# )
|
| 432 |
+
|
| 433 |
+
|
| 434 |
+
# if status['status'] == "Success":
|
| 435 |
+
# await move_file_to_folder(image_id, parent_folders, new_folders)
|
| 436 |
+
# return {"success": True}
|
| 437 |
+
# else:
|
| 438 |
+
# logging.error(f"Transaction update failed with status: {status['status']}")
|
| 439 |
+
# return {"error": "An error has occurred while updating the transaction"}
|
| 440 |
+
# except Exception as e:
|
| 441 |
+
# logging.error(f"An error occurred: {e}")
|
| 442 |
+
# # Move the image file to the error folder
|
| 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(
|
| 453 |
+
data['receipts'],
|
| 454 |
+
add_link=True,
|
| 455 |
+
image_id=image_id,
|
| 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(
|
| 464 |
+
data['busi_cards'],
|
| 465 |
+
add_link=True,
|
| 466 |
+
image_id=image_id,
|
| 467 |
+
link_text="명함보기",
|
| 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(image_id: str = Form(...), user_id: str = Form(...), background_tasks: BackgroundTasks):
|
| 483 |
transaction_ref = transactions_collection_ref.document(user_id)
|
| 484 |
transaction = transaction_ref.get()
|
| 485 |
+
|
| 486 |
if not transaction.exists:
|
| 487 |
transaction_ref.set({
|
| 488 |
"no_sms": 0,
|
|
|
|
| 495 |
}
|
| 496 |
})
|
| 497 |
transaction = transaction_ref.get()
|
| 498 |
+
|
| 499 |
transaction_data = transaction.to_dict()
|
| 500 |
|
|
|
|
| 501 |
if transaction_data['no_receipts'] + transaction_data['no_business_cards'] >= transaction_data['purchased_credit']['image_detection']:
|
| 502 |
return {"error": "You have reached the limit of the number of items you can process. Please upgrade your plan."}
|
| 503 |
|
| 504 |
user_ref = users_collection_ref.document(user_id)
|
| 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']]
|
|
|
|
| 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")
|
| 529 |
async def process_photo(data_json: str = Form(...), user_id: str = Form(...)):
|