m8chaa commited on
Commit
24369dc
โ€ข
1 Parent(s): bbd73a4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -155
app.py CHANGED
@@ -16,6 +16,8 @@ from typing import List, Optional, Dict
16
  from google.oauth2 import service_account
17
  from googleapiclient.discovery import build
18
 
 
 
19
  # start application
20
  app = FastAPI()
21
 
@@ -60,6 +62,27 @@ APICredential = service_account.Credentials.from_service_account_file(
60
  sheet_service = build('sheets', 'v4', credentials=APICredential)
61
  drive_service = build('drive', 'v3', credentials=APICredential)
62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  # first three are "๋“ฑ๋ก์ผ์‹œ", "์ธ๋„ค์ผ", "๋ฐ”๋กœ๊ฐ€๊ธฐ",
64
  receipt_sheet_headers = ['๋ฐœํ–‰์ผ', '์ƒํ˜ธ', '์‚ฌ์—…์ž๋ฒˆํ˜ธ', 'ํ•ฉ๊ณ„๊ธˆ์•ก', '๋‚ด์—ญ', '์นด๋“œ๋ฒˆํ˜ธ', '์นด๋“œ์ข…๋ฅ˜', '์Šน์ธ๋ฒˆํ˜ธ', '๊ธฐํƒ€']
65
  # at index 1 '๋“ฑ๋ก ์ผ์‹œ', at last index '๋ช…ํ•จ๋ณด๊ธฐ'
@@ -464,161 +487,6 @@ async def edit_spreadsheet(target_sheet_id: str = Form(...)):
464
  logging.error(f"Failed to copy sheets: {e}")
465
  return {"error": str(e)}
466
 
467
- # image upload
468
- # receive image id and user_id
469
- # check if they have enough credits
470
- # post to gpt4o with the user credit info
471
- # identify the sheet_id to update by checking in user_id
472
- # update user sheet
473
- # update firebase transaction
474
- # privatize the image and move the image to appropriate folder(s)
475
- # @app.post("/process-image")
476
- # async def process_photo(image_id: str = Form(...), user_id: str = Form(...)):
477
- # transaction_ref = transactions_collection_ref.document(user_id)
478
- # transaction = transaction_ref.get()
479
- # # If transaction does not exist, create a new one
480
- # if not transaction.exists:
481
- # transaction_ref.set({
482
- # "no_sms": 0,
483
- # "no_contacts": 0,
484
- # "no_receipts": 0,
485
- # "no_business_cards": 0,
486
- # "purchased_credit": {
487
- # "image_detection": 100,
488
- # "sync_data": 500,
489
- # }
490
- # })
491
- # transaction = transaction_ref.get()
492
-
493
- # transaction_data = transaction.to_dict()
494
-
495
- # # check if any of the limits have been reached
496
- # if transaction_data['no_receipts'] + transaction_data['no_business_cards'] >= transaction_data['purchased_credit']['image_detection']:
497
- # return {"error": "You have reached the limit of the number of items you can process. Please upgrade your plan."}
498
-
499
- # user_ref = users_collection_ref.document(user_id)
500
- # user = user_ref.get()
501
- # user_data = user.to_dict()
502
-
503
-
504
- # try:
505
- # parent_folders = [user_data['gDrive_metadata']['yungsoogi'], user_data['gDrive_metadata']['uploaded']]
506
- # dataJson = await request_gpt4o_completion(image_id, transaction_data['purchased_credit']['image_detection'])
507
- # if dataJson is None:
508
- # return {"error": "An error occurred while processing the image"}
509
-
510
- # data = json.loads(dataJson)
511
- # # Check if 'receipts' key exists and the length
512
- # found_receipt_no = len(data['receipts']) if 'receipts' in data else 0
513
- # found_business_cards_no = len(data['busi_cards']) if 'busi_cards' in data else 0
514
- # new_folders = []
515
- # timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
516
- # # https://drive.google.com/file/d/1cFIA09PBqFjM8YvIz1D60p7HMGk1uBvo/view?usp=sharing
517
- # download_image_url = f"https://drive.google.com/uc?id={image_id}&export=download"
518
- # image_url = f"https://drive.google.com/file/d/{image_id}/view?usp=sharing"
519
- # # ์ด๋ฏธ์ง€๋ฅผ ๋ณด์—ฌ์ฃผ์–ด์•ผ ํ• ๋•Œ =image({image_url})๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ๋กœ๋ฅผ ๋งŒ๋“ค๋•Œ๋Š” =HYPERLINK({image_url}, '๋ฐ”๋กœ๊ฐ€๊ธฐ')๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”.
520
- # if found_receipt_no > 0:
521
- # new_folders.append(user_data['gDrive_metadata']['receipts'])
522
- # receipts_data = convert_dicts_to_list(
523
- # data['receipts'],
524
- # add_link=True,
525
- # image_id=image_id,
526
- # link_text="๋ฐ”๋กœ๊ฐ€๊ธฐ",
527
- # link_position=1 # Link third
528
- # )
529
- # print(receipts_data)
530
- # await update_user_sheet(user_data['gDrive_metadata']['spreadsheet'], receipts_ss, "์˜์ˆ˜์ฆ", receipts_data)
531
- # if found_business_cards_no > 0:
532
- # new_folders.append(user_data['gDrive_metadata']['business_cards'])
533
- # business_cards_data = convert_dicts_to_list(
534
- # data['busi_cards'],
535
- # add_link=True,
536
- # image_id=image_id,
537
- # link_text="๋ช…ํ•จ๋ณด๊ธฐ",
538
- # link_position=-1 # Link at the end
539
- # )
540
- # await update_user_sheet(user_data['gDrive_metadata']['spreadsheet'], business_cards_ss, "๋ช…ํ•จ", business_cards_data)
541
-
542
- # print(f"{found_receipt_no}, {found_business_cards_no}")
543
-
544
- # status = await update_fb_transaction(
545
- # transaction_ref,
546
- # no_receipts=found_receipt_no,
547
- # no_business_cards=found_business_cards_no
548
- # )
549
-
550
-
551
- # if status['status'] == "Success":
552
- # await move_file_to_folder(image_id, parent_folders, new_folders)
553
- # return {"success": True}
554
- # else:
555
- # logging.error(f"Transaction update failed with status: {status['status']}")
556
- # return {"error": "An error has occurred while updating the transaction"}
557
- # except Exception as e:
558
- # logging.error(f"An error occurred: {e}")
559
- # # Move the image file to the error folder
560
- # await move_file_to_folder(image_id, parent_folders, user_data['gDrive_metadata']['error'])
561
- # return {"error": str(e)}
562
-
563
- # async def update_user_sheets_and_folders(user_data, transaction_ref, transaction_data, image_id):
564
- # try:
565
- # parent_folders = [user_data['gDrive_metadata']['yungsoogi'], user_data['gDrive_metadata']['uploaded']]
566
- # dataJson = await request_gpt4o_completion(image_id, transaction_data['purchased_credit']['image_detection'])
567
- # if dataJson is None:
568
- # return {"error": "An error occurred while processing the image"}
569
-
570
- # data = json.loads(dataJson)
571
- # # Check if 'receipts' key exists and the length
572
- # found_receipt_no = len(data['receipts']) if 'receipts' in data else 0
573
- # found_business_cards_no = len(data['busi_cards']) if 'busi_cards' in data else 0
574
- # new_folders = []
575
- # timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
576
- # # https://drive.google.com/file/d/1cFIA09PBqFjM8YvIz1D60p7HMGk1uBvo/view?usp=sharing
577
- # download_image_url = f"https://drive.google.com/uc?id={image_id}&export=download"
578
- # image_url = f"https://drive.google.com/file/d/{image_id}/view?usp=sharing"
579
- # # ์ด๋ฏธ์ง€๋ฅผ ๋ณด์—ฌ์ฃผ์–ด์•ผ ํ• ๋•Œ =image({image_url})๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ๋กœ๋ฅผ ๋งŒ๋“ค๋•Œ๋Š” =HYPERLINK({image_url}, '๋ฐ”๋กœ๊ฐ€๊ธฐ')๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”.
580
- # if found_receipt_no > 0:
581
- # new_folders.append(user_data['gDrive_metadata']['receipts'])
582
- # receipts_data = convert_dicts_to_list(
583
- # data['receipts'],
584
- # add_link=True,
585
- # image_id=image_id,
586
- # link_text="๋ฐ”๋กœ๊ฐ€๊ธฐ",
587
- # link_position=1 # Link third
588
- # )
589
- # print(receipts_data)
590
- # await update_user_sheet(user_data['gDrive_metadata']['spreadsheet'], receipts_ss, "์˜์ˆ˜์ฆ", receipts_data)
591
- # if found_business_cards_no > 0:
592
- # new_folders.append(user_data['gDrive_metadata']['business_cards'])
593
- # business_cards_data = convert_dicts_to_list(
594
- # data['busi_cards'],
595
- # add_link=True,
596
- # image_id=image_id,
597
- # link_text="๋ช…ํ•จ๋ณด๊ธฐ",
598
- # link_position=-1 # Link at the end
599
- # )
600
- # await update_user_sheet(user_data['gDrive_metadata']['spreadsheet'], business_cards_ss, "๋ช…ํ•จ", business_cards_data)
601
-
602
- # print(f"{found_receipt_no}, {found_business_cards_no}")
603
-
604
- # status = await update_fb_transaction(
605
- # transaction_ref,
606
- # no_receipts=found_receipt_no,
607
- # no_business_cards=found_business_cards_no
608
- # )
609
-
610
-
611
- # if status['status'] == "Success":
612
- # await move_file_to_folder(image_id, parent_folders, new_folders)
613
- # return {"success": True}
614
- # else:
615
- # logging.error(f"Transaction update failed with status: {status['status']}")
616
- # return {"error": "An error has occurred while updating the transaction"}
617
- # except Exception as e:
618
- # logging.error(f"An error occurred: {e}")
619
- # # Move the image file to the error folder
620
- # await move_file_to_folder(image_id, parent_folders, user_data['gDrive_metadata']['error'])
621
- # return {"error": str(e)}
622
  async def create_shortcut(file_id, parent_folder_id, name):
623
  try:
624
  file_metadata = {
 
16
  from google.oauth2 import service_account
17
  from googleapiclient.discovery import build
18
 
19
+ import google.generativeai as genai
20
+
21
  # start application
22
  app = FastAPI()
23
 
 
62
  sheet_service = build('sheets', 'v4', credentials=APICredential)
63
  drive_service = build('drive', 'v3', credentials=APICredential)
64
 
65
+ with open('/app/secrets/gemini.txt', 'r') as f:
66
+ api_key = f.read()
67
+
68
+ genai.configure(api_key=api_key)
69
+
70
+ # Create the model
71
+ generation_config = {
72
+ "temperature": 1,
73
+ "top_p": 0.95,
74
+ "top_k": 64,
75
+ "max_output_tokens": 8192,
76
+ "response_mime_type": "application/json",
77
+ }
78
+
79
+ model = genai.GenerativeModel(
80
+ model_name="gemini-1.5-flash",
81
+ generation_config=generation_config,
82
+ # safety_settings = Adjust safety settings
83
+ # See https://ai.google.dev/gemini-api/docs/safety-settings
84
+ )
85
+
86
  # first three are "๋“ฑ๋ก์ผ์‹œ", "์ธ๋„ค์ผ", "๋ฐ”๋กœ๊ฐ€๊ธฐ",
87
  receipt_sheet_headers = ['๋ฐœํ–‰์ผ', '์ƒํ˜ธ', '์‚ฌ์—…์ž๋ฒˆํ˜ธ', 'ํ•ฉ๊ณ„๊ธˆ์•ก', '๋‚ด์—ญ', '์นด๋“œ๋ฒˆํ˜ธ', '์นด๋“œ์ข…๋ฅ˜', '์Šน์ธ๋ฒˆํ˜ธ', '๊ธฐํƒ€']
88
  # at index 1 '๋“ฑ๋ก ์ผ์‹œ', at last index '๋ช…ํ•จ๋ณด๊ธฐ'
 
487
  logging.error(f"Failed to copy sheets: {e}")
488
  return {"error": str(e)}
489
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
490
  async def create_shortcut(file_id, parent_folder_id, name):
491
  try:
492
  file_metadata = {