m8chaa commited on
Commit
c394ef4
·
verified ·
1 Parent(s): 6ee09e4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -847
app.py CHANGED
@@ -1,885 +1,104 @@
1
- from fastapi import FastAPI, HTTPException, UploadFile, File, Request, BackgroundTasks, Form
2
  from fastapi.middleware.cors import CORSMiddleware
3
  from pydantic import BaseModel
4
  from httpx import Timeout
5
  import httpx
6
  import logging
7
- import base64
8
  import firebase_admin
9
- from firebase_admin import credentials
10
- from firebase_admin import firestore
11
- import json
12
- from datetime import datetime
13
- import pytz
14
-
15
- 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
 
22
- # allow all origins
23
  app.add_middleware(
24
  CORSMiddleware,
25
- allow_origins=["*"], # Allows all origins
26
- allow_credentials=True, # Allows credentials (such as cookies) to be sent with requests
27
- allow_methods=["*"], # Allows all methods
28
- allow_headers=["*"], # Allows all headers
29
  )
30
 
31
- # set up logging
32
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
33
  logging.info("Application starting up")
34
 
 
35
  request_timeout = Timeout(1000.0, connect=600.0)
36
 
37
- # set up firebase
38
-
39
-
40
- # Load Firebase configuration from a file
41
- with open('/app/secrets/fb_secret.json', 'r') as f:
42
- firebase_configuration = json.load(f)
43
-
44
- firebase_credentials = credentials.Certificate(firebase_configuration)
45
- firebase_admin.initialize_app(firebase_credentials, {
46
- 'projectId': firebase_configuration['project_id'],
47
- })
48
- firestore_db = firestore.client()
49
- users_collection_ref = firestore_db.collection('users')
50
- transactions_collection_ref = firestore_db.collection('transactions')
51
-
52
- SCOPES = ['https://www.googleapis.com/auth/spreadsheets','https://www.googleapis.com/auth/drive']
53
-
54
- with open('/app/secrets/openai_key.txt', 'r') as f:
55
- OPENAI_KEY = f.read()
56
-
57
- APICredential = service_account.Credentials.from_service_account_file(
58
- '/app/secrets/googleapi_secret.json', scopes=SCOPES)
59
-
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 '명함보기'
66
- business_card_sheet_headers = ['이름', '회사명', '소속 및 직위', '주소', '전화' ,'휴대폰', '팩스','e-mail','홈페이지']
67
- sms_sheet_headers = ['날짜', '계좌', '입금', '출금', '잔액', '문자']
68
- contact_sheet_headers = ['이름', '전화번호1', '전화번호2', '전화번호3', '이메일주소1', '이메일주소2', '이메일주소3', '그룹']
69
-
70
- receipts_ss = 1395967745
71
- business_cards_ss = 1733555840
72
- contacts_ss = 1729750548
73
- sms_ss = 1891574758
74
-
75
- kst = pytz.timezone('Asia/Seoul')
76
-
77
-
78
- class SheetUpdateRequest(BaseModel):
79
- target_sheet_id: int
80
- data: list
81
-
82
- def convert_dicts_to_list(dicts: List[Dict], headers: List[str], add_link: bool = False,
83
- image_id: str = '', link_text: str = '',
84
- link_position: int = -1) -> List[List[str]]:
85
- rows = []
86
- # timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
87
- timestamp = datetime.now(kst).strftime("%Y-%m-%d %H:%M:%S")
88
-
89
- for dict_item in dicts:
90
- # row = list(dict_item.values())
91
- # row = [str(value) for value in dict_item.values()]
92
- row = [str(dict_item.get(header, '')) for header in headers]
93
-
94
- # Always add timestamp to the first index
95
- row.insert(0, timestamp)
96
-
97
- if add_link:
98
- # Ensure the list length is at least 9
99
- while len(row) < 9:
100
- row.append('')
101
- if link_position == -1: # Append link at the end
102
- row.append(f'=HYPERLINK("https://drive.google.com/file/d/{image_id}/view?usp=sharing", "{link_text}")')
103
- else:
104
- row.insert(link_position, f'=HYPERLINK("https://drive.google.com/file/d/{image_id}/view?usp=sharing", "{link_text}")')
105
-
106
- rows.append(row)
107
- # print(rows)
108
- return rows
109
-
110
- async def request_gpt4o_completion(image_id: str, user_credit: int):
111
- openai_url = "https://api.openai.com/v1/chat/completions"
112
- headers = {"Authorization": f"Bearer {OPENAI_KEY}", "Content-Type": "application/json"}
113
- image_url = f"https://drive.usercontent.google.com/u/0/uc?id={image_id}&export=download"
114
- body = {
115
- "model": "gpt-4o-mini",
116
- "response_format": { "type": "json_object" },
117
- "messages": [
118
- {
119
- "role": "user",
120
- "content": [
121
- {
122
- "type": "text",
123
- "text": f'''
124
- 아래의 순서대로 실행해주세요:
125
- 1. {image_url} 이 링크에서 파일 다운로드가 완료될 때까지 기다리세요.
126
- 2. 이미지의 모든 영수증과 명함을 식별하고, 각각의 정보를 추출하세요.
127
- 3. 작성할 때 영수증과 명함 개수의 합이 {user_credit}을 넘지 않도록 하세요.
128
- 4. 추출한 정보를 {receipt_sheet_headers}란이 있는 영수증 시트 또는 {business_card_sheet_headers}란이 있는 명함 시트에 넣을 수 있도록 JSON으로 정리하세요.
129
- json.receipts.forEach() 또는 json.busi_cards.forEach()
130
- (item) => insert row where header field is item.[fieldName]
131
- '''
132
- },
133
- {
134
- "type": "image_url",
135
- "image_url": {
136
- "url": image_url,
137
- "detail": "high"
138
- }
139
- }
140
- ]
141
- }
142
- ],
143
- "max_tokens": 4096
144
- }
145
-
146
- logging.info(f"Request URL: {openai_url}")
147
- logging.info(f"Request Headers: {headers}")
148
- logging.info(f"Request Body: {json.dumps(body, indent=2)}")
149
-
150
- try:
151
- async with httpx.AsyncClient(follow_redirects=True, timeout=300.0) as client:
152
- response = await client.post(openai_url, headers=headers, json=body)
153
- response.raise_for_status() # Raise an error for bad status codes
154
- except httpx.RequestError as exc:
155
- logging.error(f"An error occurred while requesting {exc.request.url!r}.")
156
- logging.error(f"Exception: {str(exc)}")
157
- return None
158
- except httpx.HTTPStatusError as exc:
159
- logging.error(f"HTTP error occurred: {str(exc)}")
160
- logging.error(f"Response: {exc.response.text}")
161
- return None
162
-
163
- logging.info(f"Response Status Code: {response.status_code}")
164
- logging.info(f"Response Body: {response.text}")
165
-
166
- completion = response.json()
167
- if 'choices' in completion and len(completion['choices']) > 0:
168
- message_content = completion['choices'][0]['message']['content']
169
- print(message_content)
170
- return message_content
171
- else:
172
- print("No response received")
173
- return None
174
-
175
- async def move_file_to_folder(file_id, current_parents, new_parents):
176
- try:
177
-
178
- # Ensure current_parents is a list of parent IDs
179
- if not isinstance(current_parents, list):
180
- current_parents = [current_parents]
181
- if not isinstance(new_parents, list):
182
- new_parents = [new_parents]
183
-
184
- # Retrieve the existing parents to remove
185
- file = drive_service.files().get(fileId=file_id, fields='parents').execute()
186
- existing_parents = file.get('parents')
187
- logging.info(f"Existing parents of the file {file_id}: {existing_parents}")
188
-
189
- # Remove only the current parents
190
- parents_to_remove = [parent for parent in current_parents if parent in existing_parents]
191
- parents_to_add = [parent for parent in new_parents if parent not in existing_parents]
192
-
193
- previous_parents = ",".join(parents_to_remove)
194
- logging.info(f"Parents to remove: {parents_to_remove}")
195
- logging.info(f"Parents to add: {parents_to_add}")
196
 
197
- # Move the file to the new folders while keeping it in yungsoogi folder
198
- if parents_to_add:
199
- file = drive_service.files().update(
200
- fileId=file_id,
201
- addParents=",".join(parents_to_add),
202
- removeParents=previous_parents,
203
- fields='id, parents'
204
- ).execute()
205
- logging.info(f"File {file_id} moved to new folders {new_parents}.")
206
- else:
207
- logging.info(f"File {file_id} already in the target folders {new_parents}.")
208
- except Exception as e:
209
- logging.error(f"Failed to move file {file_id} to new folders: {e}")
210
 
211
-
212
- # Update the update_user_sheets_and_folders function for better error handling
213
- async def update_user_sheets_and_folders(user_data, transaction_ref, transaction_data, image_id):
 
 
214
  try:
215
- parent_folders = [user_data['gDrive_metadata']['yungsoogi'], user_data['gDrive_metadata']['uploaded']]
216
-
217
- # Request GPT-4 completion
218
- dataJson = await request_gpt4o_completion(image_id, transaction_data['purchased_credit']['image_detection'])
219
- if dataJson is None:
220
- raise Exception("Failed to process the image with GPT-4")
221
-
222
- data = json.loads(dataJson)
223
-
224
- # Process receipts and business cards
225
- found_receipt_no = len(data.get('receipts', []))
226
- found_business_cards_no = len(data.get('busi_cards', []))
227
 
228
- new_folder = None
229
- secondary_folder = None
230
- timestamp = datetime.now(kst).strftime("%Y-%m-%d %H:%M:%S")
231
-
232
- # Process receipts
233
- if found_receipt_no > 0:
234
- new_folder = user_data['gDrive_metadata']['receipts']
235
- receipts_data = convert_dicts_to_list(
236
- data['receipts'],
237
- receipt_sheet_headers,
238
- add_link=True,
239
- image_id=image_id,
240
- link_text="바로가기",
241
- link_position=1
242
- )
243
- try:
244
- await update_user_sheet(user_data['gDrive_metadata']['spreadsheet'], receipts_ss, "영수증", receipts_data)
245
- except HttpError as e:
246
- logging.error(f"Failed to update receipt sheet: {str(e)}")
247
- raise Exception("Failed to update receipt data in spreadsheet")
248
-
249
- # Process business cards
250
- if found_business_cards_no > 0:
251
- if new_folder is None:
252
- new_folder = user_data['gDrive_metadata']['business_cards']
253
- else:
254
- secondary_folder = user_data['gDrive_metadata']['business_cards']
255
- business_cards_data = convert_dicts_to_list(
256
- data['busi_cards'],
257
- business_card_sheet_headers,
258
- add_link=True,
259
- image_id=image_id,
260
- link_text="명함보기",
261
- link_position=-1
262
- )
263
- try:
264
- await update_user_sheet(user_data['gDrive_metadata']['spreadsheet'], business_cards_ss, "명함", business_cards_data)
265
- except HttpError as e:
266
- logging.error(f"Failed to update business card sheet: {str(e)}")
267
- raise Exception("Failed to update business card data in spreadsheet")
268
-
269
- # Update Firebase transaction
270
- status = await update_fb_transaction(
271
- transaction_ref,
272
- no_receipts=found_receipt_no,
273
- no_business_cards=found_business_cards_no
274
- )
275
-
276
- if status['status'] != "Success":
277
- logging.error(f"Transaction update failed with status: {status['status']}")
278
- raise Exception("Failed to update transaction in Firebase")
279
-
280
- # Move file to appropriate folder
281
- if new_folder:
282
- try:
283
- await move_file_to_folder(image_id, parent_folders, [new_folder])
284
- if secondary_folder:
285
- await create_shortcut(image_id, secondary_folder, f"Shortcut to {image_id}")
286
- except HttpError as e:
287
- logging.error(f"Failed to move file or create shortcut: {str(e)}")
288
- raise Exception("Failed to organize processed image in Google Drive")
289
-
290
- logging.info(f"Successfully processed image {image_id} for user {user_data['uid']}")
291
-
292
- except Exception as e:
293
- logging.error(f"Error processing image {image_id} for user {user_data['uid']}: {str(e)}")
294
- try:
295
- # Move the image file to the error folder
296
- await move_file_to_folder(image_id, parent_folders, [user_data['gDrive_metadata']['error']])
297
- logging.info(f"Moved image {image_id} to error folder for user {user_data['uid']}")
298
- except Exception as move_error:
299
- logging.error(f"Failed to move image {image_id} to error folder: {str(move_error)}")
300
 
301
- # You might want to update the transaction or user document to indicate the error
302
- # This depends on your error handling strategy
303
-
304
- async def copy_sms_sheet(phone_no: str, spreadsheet_id: str):
305
- requests = [
306
- {
307
- "duplicateSheet": {
308
- "sourceSheetId": sms_ss,
309
- "newSheetName": 'SMS ' + phone_no
310
- }
311
- }
312
- ]
313
 
314
- response = sheet_service.spreadsheets().batchUpdate(
315
- spreadsheetId=spreadsheet_id,
316
- body={
317
- 'requests': requests
318
- }
319
- ).execute()
320
- print(response)
321
- new_sheet_id = response['replies'][0]['duplicateSheet']['properties']['sheetId']
322
-
323
- unhide_request = [{
324
- "updateSheetProperties": {
325
- "properties": {
326
- "sheetId": new_sheet_id,
327
- "hidden": False
328
- },
329
- "fields": "hidden"
330
- }
331
- }]
332
-
333
- sheet_service.spreadsheets().batchUpdate(
334
- spreadsheetId=spreadsheet_id,
335
- body={
336
- 'requests': unhide_request
337
- }
338
- ).execute()
339
-
340
- return new_sheet_id
341
-
342
- async def update_fb_transaction(transaction_ref: firestore.DocumentReference, no_sms: Optional[int] = 0, no_contacts: Optional[int] = 0, no_receipts: Optional[int] = 0, no_business_cards: Optional[int] = 0):
343
- try:
344
- # Update the transaction
345
- transaction_ref.update({
346
- "no_sms": firestore.Increment(no_sms),
347
- "no_contacts": firestore.Increment(no_contacts),
348
- "no_receipts": firestore.Increment(no_receipts),
349
- "no_business_cards": firestore.Increment(no_business_cards)
350
- })
351
-
352
- return {"status": "Success"}
353
  except Exception as e:
354
- logging.error(f"Error updating transaction: {e}")
355
- return {"status": f"Error: {str(e)}"}
356
-
357
-
358
- @app.post("/check-userdoc")
359
- async def check_user_doc(user_id: str = Form(...)):
360
- user_ref = users_collection_ref.document(user_id)
361
- user = user_ref.get()
362
- if user.exists:
363
- return {"dbUserExists": True}
364
- transaction_ref = transactions_collection_ref.document(user_id)
365
- transaction = transaction_ref.get()
366
- if not transaction.exists:
367
- transaction_ref.set({
368
- "no_sms": 0,
369
- "no_contacts": 0,
370
- "no_receipts": 0,
371
- "no_business_cards": 0,
372
- "purchased_credit": {
373
- "image_detection": 100,
374
- "sync_data": 500,
375
- },
376
- "uid": user_id,
377
- })
378
- return {"dbUserExists": False}
379
-
380
- template_sheet_id = '1i5mrmlTs5sPWtx2mBtc_f-zm1D3x21r1T_77guki8_8'
381
 
382
- @app.post("/edit-spreadsheet")
383
- async def edit_spreadsheet(target_sheet_id: str = Form(...)):
 
 
384
  try:
385
- # Retrieve the target spreadsheet's metadata
386
- try:
387
- target_spreadsheet = sheet_service.spreadsheets().get(spreadsheetId=target_sheet_id).execute()
388
- except HttpError as e:
389
- if e.resp.status == 404:
390
- raise HTTPException(status_code=404, detail="Target spreadsheet not found")
391
- else:
392
- raise HTTPException(status_code=e.resp.status, detail=f"Error accessing target spreadsheet: {e.error_details}")
393
-
394
- # Collect all sheet IDs in the target spreadsheet
395
- target_sheet_ids = [sheet['properties']['sheetId'] for sheet in target_spreadsheet.get('sheets', [])]
396
-
397
- # Prepare batch update request to delete existing sheets
398
- requests = []
399
- for sheet_id in target_sheet_ids:
400
- requests.append({
401
- "deleteSheet": {
402
- "sheetId": sheet_id
403
- }
404
- })
405
-
406
- # Retrieve the template spreadsheet's metadata
407
- try:
408
- template_spreadsheet = sheet_service.spreadsheets().get(spreadsheetId=template_sheet_id).execute()
409
- except HttpError as e:
410
- if e.resp.status == 404:
411
- raise HTTPException(status_code=404, detail="Template spreadsheet not found")
412
- else:
413
- raise HTTPException(status_code=e.resp.status, detail=f"Error accessing template spreadsheet: {e.error_details}")
414
-
415
- # Iterate through each sheet in the template spreadsheet
416
- for sheet in template_spreadsheet.get('sheets', []):
417
- sheet_id = sheet['properties']['sheetId']
418
-
419
- try:
420
- # Use the copyTo method to copy the sheet to the target spreadsheet
421
- copy_request = sheet_service.spreadsheets().sheets().copyTo(
422
- spreadsheetId=template_sheet_id,
423
- sheetId=sheet_id,
424
- body={"destinationSpreadsheetId": target_sheet_id}
425
- )
426
- copy_response = copy_request.execute()
427
-
428
- # Get the copied sheet ID
429
- copied_sheet_id = copy_response['sheetId']
430
-
431
- # Rename the copied sheet to remove "Copy of"
432
- copied_sheet_title = sheet['properties']['title'].replace("Copy of ", "")
433
- rename_request = {
434
- "updateSheetProperties": {
435
- "properties": {
436
- "sheetId": copied_sheet_id,
437
- "title": copied_sheet_title
438
- },
439
- "fields": "title"
440
- }
441
- }
442
-
443
- # Execute rename request
444
- sheet_service.spreadsheets().batchUpdate(
445
- spreadsheetId=target_sheet_id,
446
- body={"requests": [rename_request]}
447
- ).execute()
448
-
449
- logging.info(f"Sheet {sheet['properties']['title']} copied and renamed to {copied_sheet_title} in {target_sheet_id}.")
450
- except HttpError as e:
451
- logging.error(f"Error copying or renaming sheet {sheet['properties']['title']}: {e}")
452
- raise HTTPException(status_code=e.resp.status, detail=f"Error copying or renaming sheet: {e.error_details}")
453
-
454
- if requests:
455
- try:
456
- # Execute batch update to delete sheets
457
- batch_update_request = {"requests": requests}
458
- sheet_service.spreadsheets().batchUpdate(
459
- spreadsheetId=target_sheet_id,
460
- body=batch_update_request
461
- ).execute()
462
- logging.info(f"Deleted existing sheets in target spreadsheet {target_sheet_id}.")
463
- except HttpError as e:
464
- logging.error(f"Error deleting existing sheets: {e}")
465
- raise HTTPException(status_code=e.resp.status, detail=f"Error deleting existing sheets: {e.error_details}")
466
-
467
- logging.info(f"All sheets from template {template_sheet_id} copied to {target_sheet_id}.")
468
- return {"message": "Spreadsheet copied successfully"}
469
-
470
- except HTTPException as he:
471
- # Re-raise HTTP exceptions
472
- raise he
473
- except Exception as e:
474
- logging.error(f"Unexpected error in edit_spreadsheet: {e}")
475
- raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {str(e)}")
476
-
477
- async def create_shortcut(file_id, parent_folder_id, name):
478
- try:
479
- file_metadata = {
480
- 'name': name,
481
- 'mimeType': 'application/vnd.google-apps.shortcut',
482
- 'parents': [parent_folder_id],
483
- 'shortcutDetails': {
484
- 'targetId': file_id
485
- }
486
- }
487
- shortcut = drive_service.files().create(body=file_metadata, fields='id').execute()
488
- return shortcut.get('id')
489
- except Exception as e:
490
- logging.error(f"Failed to create shortcut: {e}")
491
- return None
492
-
493
- async def update_user_sheets_and_folders(user_data, transaction_ref, transaction_data, image_id):
494
- try:
495
- parent_folders = [user_data['gDrive_metadata']['yungsoogi'], user_data['gDrive_metadata']['uploaded']]
496
- dataJson = await request_gpt4o_completion(image_id, transaction_data['purchased_credit']['image_detection'])
497
- if dataJson is None:
498
- return {"error": "An error occurred while processing the image"}
499
-
500
- data = json.loads(dataJson)
501
- # Check if 'receipts' key exists and the length
502
- found_receipt_no = len(data['receipts']) if 'receipts' in data else 0
503
- found_business_cards_no = len(data['busi_cards']) if 'busi_cards' in data else 0
504
- new_folder = None
505
- secondary_folder = None
506
- # timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
507
- timestamp = datetime.now(kst).strftime("%Y-%m-%d %H:%M:%S")
508
- # https://drive.google.com/file/d/1cFIA09PBqFjM8YvIz1D60p7HMGk1uBvo/view?usp=sharing
509
- download_image_url = f"https://drive.google.com/uc?id={image_id}&export=download"
510
- image_url = f"https://drive.google.com/file/d/{image_id}/view?usp=sharing"
511
- # 이미지를 보여주어야 할때 =image({image_url})를 사용하고 갈 수 있는 경로를 만들때는 =HYPERLINK({image_url}, '바로가기')를 사용하세요.
512
-
513
- # Determine the primary folder to move the image to and secondary folder for shortcut
514
- if found_receipt_no > 0:
515
- new_folder = user_data['gDrive_metadata']['receipts']
516
- receipts_data = convert_dicts_to_list(
517
- data['receipts'],
518
- receipt_sheet_headers,
519
- add_link=True,
520
- image_id=image_id,
521
- link_text="바로가기",
522
- link_position=1 # Link third
523
- )
524
- print(receipts_data)
525
- await update_user_sheet(user_data['gDrive_metadata']['spreadsheet'], receipts_ss, "영수증", receipts_data)
526
-
527
- if found_business_cards_no > 0:
528
- if new_folder is None:
529
- new_folder = user_data['gDrive_metadata']['business_cards']
530
- else:
531
- secondary_folder = user_data['gDrive_metadata']['business_cards']
532
- business_cards_data = convert_dicts_to_list(
533
- data['busi_cards'],
534
- business_card_sheet_headers,
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
- if status['status'] == "Success":
551
- if new_folder:
552
- await move_file_to_folder(image_id, parent_folders, [new_folder])
553
- if secondary_folder:
554
- await create_shortcut(image_id, secondary_folder, f"Shortcut to {image_id}")
555
- return {"success": True}
556
- else:
557
- logging.error(f"Transaction update failed with status: {status['status']}")
558
- return {"error": "An error has occurred while updating the transaction"}
559
- except Exception as e:
560
- logging.error(f"An error occurred: {e}")
561
- # Move the image file to the error folder
562
- await move_file_to_folder(image_id, parent_folders, [user_data['gDrive_metadata']['error']])
563
- return {"error": str(e)}
564
-
565
- @app.post("/process-image")
566
- async def process_photo(background_tasks: BackgroundTasks, image_id: str = Form(...), user_id: str = Form(...)):
567
- try:
568
- # Validate input
569
- if not image_id or not user_id:
570
- raise HTTPException(status_code=400, detail="Both image_id and user_id are required")
571
-
572
- # Get transaction data
573
- transaction_ref = transactions_collection_ref.document(user_id)
574
- transaction = transaction_ref.get()
575
-
576
- if not transaction.exists:
577
- # Initialize new transaction if it doesn't exist
578
- try:
579
- transaction_ref.set({
580
- "no_sms": 0,
581
- "no_contacts": 0,
582
- "no_receipts": 0,
583
- "no_business_cards": 0,
584
- "purchased_credit": {
585
- "image_detection": 100,
586
- "sync_data": 500,
587
- }
588
- })
589
- transaction = transaction_ref.get()
590
- except Exception as e:
591
- logging.error(f"Failed to initialize new transaction for user {user_id}: {str(e)}")
592
- raise HTTPException(status_code=500, detail="Failed to initialize user transaction")
593
-
594
- transaction_data = transaction.to_dict()
595
-
596
- # Check credit limit
597
- if transaction_data['no_receipts'] + transaction_data['no_business_cards'] >= transaction_data['purchased_credit']['image_detection']:
598
- raise HTTPException(status_code=403, detail="You have reached the limit of the number of items you can process. Please upgrade your plan.")
599
-
600
- # Get user data
601
- user_ref = users_collection_ref.document(user_id)
602
- user = user_ref.get()
603
- if not user.exists:
604
- raise HTTPException(status_code=404, detail="User not found")
605
- user_data = user.to_dict()
606
 
607
- # Validate user data
608
- if 'gDrive_metadata' not in user_data:
609
- raise HTTPException(status_code=400, detail="User Google Drive metadata not found")
 
610
 
611
- # Add task to background
612
- try:
613
- background_tasks.add_task(update_user_sheets_and_folders, user_data, transaction_ref, transaction_data, image_id)
614
- except Exception as e:
615
- logging.error(f"Failed to add background task for user {user_id}, image {image_id}: {str(e)}")
616
- raise HTTPException(status_code=500, detail="Failed to start image processing")
617
 
618
- return {"success": True, "message": "Image processing started"}
619
 
620
- except HTTPException as he:
621
- # Re-raise HTTP exceptions
622
- raise he
623
  except Exception as e:
624
- logging.error(f"Unexpected error in process_photo for user {user_id}, image {image_id}: {str(e)}")
625
- raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {str(e)}")
626
 
627
-
628
- async def update_user_sheet(spreadsheet_id: str, sheet_id: int, sheet_name: str, data: List[List[str]], is_reset: Optional[bool] = False):
 
 
629
  try:
630
- logging.info(f"Updating sheet '{sheet_name}' in spreadsheet {spreadsheet_id}")
631
-
632
- if not spreadsheet_id or not sheet_id or not sheet_name or not data:
633
- raise ValueError("Missing required parameters for updating user sheet")
634
-
635
- no_of_rows = len(data)
636
- no_of_cols = len(data[0]) if data else 0
637
-
638
- if is_reset:
639
- sheet_name = f"{sheet_name} {datetime.now(kst).strftime('%Y-%m-%d %H:%M')}"
640
- try:
641
- duplicate_requests = [{
642
- "duplicateSheet": {
643
- "sourceSheetId": contacts_ss,
644
- "newSheetName": sheet_name
645
- }
646
- }]
647
- duplicate_response = sheet_service.spreadsheets().batchUpdate(
648
- spreadsheetId=spreadsheet_id,
649
- body={
650
- 'requests': duplicate_requests
651
- }
652
- ).execute()
653
- sheet_id = duplicate_response['replies'][0]['duplicateSheet']['properties']['sheetId']
654
- logging.info(f"Created new sheet '{sheet_name}' with ID {sheet_id}")
655
- except HttpError as e:
656
- logging.error(f"Error duplicating sheet: {e}")
657
- raise HTTPException(status_code=500, detail=f"Failed to create new sheet: {str(e)}")
658
-
659
- requests = [
660
- {
661
- "updateSheetProperties": {
662
- "properties": {
663
- "sheetId": sheet_id,
664
- "hidden": False
665
- },
666
- "fields": "hidden"
667
- }
668
- },
669
- {
670
- "insertDimension": {
671
- "range": {
672
- "sheetId": sheet_id,
673
- "dimension": "ROWS",
674
- "startIndex": 1,
675
- "endIndex": no_of_rows + 1
676
- },
677
- "inheritFromBefore": False,
678
- }
679
- }
680
- ]
681
-
682
- try:
683
- sheet_service.spreadsheets().batchUpdate(
684
- spreadsheetId=spreadsheet_id,
685
- body={
686
- 'requests': requests
687
- }
688
- ).execute()
689
- logging.info(f"Inserted {no_of_rows} rows in sheet '{sheet_name}'")
690
- except HttpError as e:
691
- logging.error(f"Error updating sheet properties or inserting rows: {e}")
692
- raise HTTPException(status_code=500, detail=f"Failed to prepare sheet for data update: {str(e)}")
693
-
694
- update_body = {
695
- 'values': data
696
- }
697
- start_column = 'A'
698
- end_column = chr(ord(start_column) + no_of_cols - 1)
699
- range_to_update = f'{sheet_name}!{start_column}2:{end_column}{no_of_rows + 1}'
700
-
701
- try:
702
- result = sheet_service.spreadsheets().values().update(
703
- spreadsheetId=spreadsheet_id,
704
- range=range_to_update,
705
- valueInputOption='USER_ENTERED',
706
- body=update_body
707
- ).execute()
708
- logging.info(f"Updated {result.get('updatedCells')} cells in sheet '{sheet_name}'")
709
- except HttpError as e:
710
- logging.error(f"Error updating cell values: {e}")
711
- raise HTTPException(status_code=500, detail=f"Failed to update sheet data: {str(e)}")
712
-
713
- # Format the cells
714
- for row_index, row in enumerate(data):
715
- for col_index, cell_value in enumerate(row):
716
- if cell_value.startswith('=HYPERLINK'):
717
- color = {"red": 0, "green": 0, "blue": 1} # Blue for hyperlinks
718
- else:
719
- color = {"red": 0, "green": 0, "blue": 0} # Black for other text
720
-
721
- requests = [
722
- {
723
- "repeatCell": {
724
- "range": {
725
- "sheetId": sheet_id,
726
- "startRowIndex": row_index + 1,
727
- "endRowIndex": row_index + 2,
728
- "startColumnIndex": col_index,
729
- "endColumnIndex": col_index + 1
730
- },
731
- "cell": {
732
- "userEnteredFormat": {
733
- "textFormat": {
734
- "foregroundColor": color
735
- }
736
- }
737
- },
738
- "fields": "userEnteredFormat.textFormat.foregroundColor"
739
- }
740
- }
741
- ]
742
-
743
- try:
744
- sheet_service.spreadsheets().batchUpdate(
745
- spreadsheetId=spreadsheet_id,
746
- body={
747
- 'requests': requests
748
- }
749
- ).execute()
750
- except HttpError as e:
751
- logging.error(f"Error formatting cells: {e}")
752
- # We'll continue even if formatting fails, as it's not critical
753
- logging.warning("Cell formatting failed, but data was updated successfully")
754
-
755
- logging.info(f"Successfully updated and formatted sheet '{sheet_name}'")
756
- return True
757
-
758
- except ValueError as ve:
759
- logging.error(f"Value error in update_user_sheet: {ve}")
760
- raise HTTPException(status_code=400, detail=str(ve))
761
- except HttpError as he:
762
- logging.error(f"Google Sheets API error in update_user_sheet: {he}")
763
- raise HTTPException(status_code=500, detail=f"Google Sheets API error: {str(he)}")
764
  except Exception as e:
765
- logging.error(f"Unexpected error in update_user_sheet: {e}")
766
- raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {str(e)}")
767
-
768
- @app.post("/sync-data")
769
- async def sync_data(data_json: str = Form(...), user_id: str = Form(...)):
770
- try:
771
- # Validate input
772
- if not data_json or not user_id:
773
- raise HTTPException(status_code=400, detail="Both data_json and user_id are required")
774
 
775
- # Parse JSON data
776
- try:
777
- data: Dict[str, Any] = json.loads(data_json)
778
- except json.JSONDecodeError as e:
779
- logging.error(f"Invalid JSON data for user {user_id}: {str(e)}")
780
- raise HTTPException(status_code=400, detail="Invalid JSON data")
781
-
782
- # Get transaction data
783
- transaction_ref = transactions_collection_ref.document(user_id)
784
- try:
785
- transaction = transaction_ref.get()
786
- if not transaction.exists:
787
- transaction_ref.set({
788
- "no_sms": 0,
789
- "no_contacts": 0,
790
- "no_receipts": 0,
791
- "no_business_cards": 0,
792
- "purchased_credit": {
793
- "image_detection": 100,
794
- "sync_data": 500,
795
- }
796
- })
797
- transaction = transaction_ref.get()
798
- except GoogleAPICallError as e:
799
- logging.error(f"Firebase error for user {user_id}: {str(e)}")
800
- raise HTTPException(status_code=500, detail="Error accessing user transaction data")
801
-
802
- transaction_data = transaction.to_dict()
803
-
804
- # Check credit limit
805
- if transaction_data['no_contacts'] + transaction_data['no_sms'] >= transaction_data['purchased_credit']['sync_data']:
806
- raise HTTPException(status_code=403, detail="You have reached the limit of the number of items you can process. Please upgrade your plan.")
807
-
808
- # Get user data
809
- user_ref = users_collection_ref.document(user_id)
810
- try:
811
- user = user_ref.get()
812
- if not user.exists:
813
- raise HTTPException(status_code=404, detail="User not found")
814
- user_data = user.to_dict()
815
- except GoogleAPICallError as e:
816
- logging.error(f"Firebase error fetching user data for user {user_id}: {str(e)}")
817
- raise HTTPException(status_code=500, detail="Error accessing user data")
818
-
819
- # Process contacts
820
- if data.get('isContacts', False):
821
- try:
822
- await update_user_sheet(user_data['gDrive_metadata']['spreadsheet'], contacts_ss, "연락처", data['data'], True)
823
- new_contacts_count = len(data['data'])
824
- transaction_data['no_contacts'] += new_contacts_count
825
- transaction_ref.update({'no_contacts': transaction_data['no_contacts']})
826
- logging.info(f"Updated {new_contacts_count} contacts for user {user_id}")
827
- except HttpError as e:
828
- logging.error(f"Google Sheets API error updating contacts for user {user_id}: {str(e)}")
829
- raise HTTPException(status_code=500, detail="Error updating contact data")
830
- except KeyError as e:
831
- logging.error(f"Missing key in user data for user {user_id}: {str(e)}")
832
- raise HTTPException(status_code=500, detail="Error accessing user Google Drive data")
833
-
834
- # Process SMS
835
- else:
836
- if 'sms_ids' not in user_data:
837
- user_data['sms_ids'] = {}
838
-
839
- existing_phones = set(user_data.get('sms_ids', {}).keys())
840
- new_phones = [phone['address'] for phone in data['data'] if phone['address'] not in existing_phones]
841
-
842
- for phone_address in new_phones:
843
- try:
844
- new_sheet_id = await copy_sms_sheet(phone_address, user_data['gDrive_metadata']['spreadsheet'])
845
- user_data['sms_ids'][phone_address] = {'sheet_id': new_sheet_id, 'last_updated': "1970-01-01T00:00:00.000"}
846
- except HttpError as e:
847
- logging.error(f"Google Sheets API error creating new SMS sheet for user {user_id}, phone {phone_address}: {str(e)}")
848
- raise HTTPException(status_code=500, detail=f"Error creating new SMS sheet for phone {phone_address}")
849
-
850
- updated_sms_count = 0
851
- for phone in data['data']:
852
- try:
853
- last_updated = user_data['sms_ids'][phone['address']]['last_updated']
854
- last_updated_dt = datetime.fromisoformat(last_updated)
855
- update_message = [sms for sms in phone['sms'] if datetime.fromisoformat(sms[0]) > last_updated_dt]
856
-
857
- if update_message:
858
- sheet_id = user_data['sms_ids'][phone['address']]['sheet_id']
859
- await update_user_sheet(user_data['gDrive_metadata']['spreadsheet'], sheet_id, 'SMS ' + phone['address'], update_message, False)
860
- user_data['sms_ids'][phone['address']]['last_updated'] = phone['sms'][0][0]
861
- updated_sms_count += len(update_message)
862
- except KeyError as e:
863
- logging.error(f"Missing key in SMS data for user {user_id}, phone {phone['address']}: {str(e)}")
864
- continue
865
- except HttpError as e:
866
- logging.error(f"Google Sheets API error updating SMS for user {user_id}, phone {phone['address']}: {str(e)}")
867
- continue
868
-
869
- try:
870
- user_ref.update({'sms_ids': user_data['sms_ids']})
871
- transaction_data['no_sms'] += updated_sms_count
872
- transaction_ref.update({'no_sms': transaction_data['no_sms']})
873
- logging.info(f"Updated {updated_sms_count} SMS messages for user {user_id}")
874
- except GoogleAPICallError as e:
875
- logging.error(f"Firebase error updating user data for user {user_id}: {str(e)}")
876
- raise HTTPException(status_code=500, detail="Error updating user data")
877
-
878
- return {"success": True, "updated_count": new_contacts_count if data.get('isContacts', False) else updated_sms_count}
879
-
880
- except HTTPException as he:
881
- # Re-raise HTTP exceptions
882
- raise he
883
- except Exception as e:
884
- logging.error(f"Unexpected error in sync_data for user {user_id}: {str(e)}")
885
- raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {str(e)}")
 
1
+ from fastapi import FastAPI, HTTPException, Request, BackgroundTasks, Form
2
  from fastapi.middleware.cors import CORSMiddleware
3
  from pydantic import BaseModel
4
  from httpx import Timeout
5
  import httpx
6
  import logging
 
7
  import firebase_admin
8
+ from firebase_admin import credentials, firestore
 
 
 
 
 
 
9
  from google.oauth2 import service_account
10
  from googleapiclient.discovery import build
11
+ from googleapiclient.http import MediaIoBaseDownload
12
+ from io import BytesIO
13
+ import genai # Assume this is the Gemini AI client library
14
+
15
+ # Initialize Firebase
16
+ firebase_credentials_path = 'path/to/firebase_credentials.json'
17
+ cred = credentials.Certificate(firebase_credentials_path)
18
+ firebase_admin.initialize_app(cred)
19
+ db = firestore.client()
20
 
21
+ # Initialize FastAPI
22
  app = FastAPI()
23
 
24
+ # Allow all origins
25
  app.add_middleware(
26
  CORSMiddleware,
27
+ allow_origins=["*"],
28
+ allow_credentials=True,
29
+ allow_methods=["*"],
30
+ allow_headers=["*"],
31
  )
32
 
33
+ # Set up logging
34
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
35
  logging.info("Application starting up")
36
 
37
+ # Timeout configuration
38
  request_timeout = Timeout(1000.0, connect=600.0)
39
 
40
+ # Google Drive service
41
+ google_creds = service_account.Credentials.from_service_account_file(
42
+ 'path/to/google_service_account.json',
43
+ scopes=["https://www.googleapis.com/auth/drive.readonly"]
44
+ )
45
+ drive_service = build('drive', 'v3', credentials=google_creds)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
 
47
+ # Gemini model configuration
48
+ genai_client = genai.Client(api_key='your_gemini_api_key')
49
+ model = genai_client.GenerativeModel("gemini-1.5-flash")
 
 
 
 
 
 
 
 
 
 
50
 
51
+ @app.post("/upload")
52
+ async def upload_drive_document(file_id: str = Form(...)):
53
+ """
54
+ Endpoint to upload a document from Google Drive to Gemini
55
+ """
56
  try:
57
+ # Download file from Google Drive
58
+ file_content, mime_type = download_file_from_drive(file_id)
 
 
 
 
 
 
 
 
 
 
59
 
60
+ # Upload to Gemini
61
+ response = upload_to_gemini(file_content, mime_type)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
 
63
+ return {"status": "success", "gemini_response": response}
 
 
 
 
 
 
 
 
 
 
 
64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  except Exception as e:
66
+ logging.error(f"Error uploading document: {e}")
67
+ raise HTTPException(status_code=500, detail="Failed to upload document")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
 
69
+ def download_file_from_drive(file_id: str) -> (BytesIO, str):
70
+ """
71
+ Downloads a file from Google Drive using the file ID
72
+ """
73
  try:
74
+ request = drive_service.files().get_media(fileId=file_id)
75
+ file_io = BytesIO()
76
+ downloader = MediaIoBaseDownload(file_io, request)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
 
78
+ done = False
79
+ while not done:
80
+ status, done = downloader.next_chunk()
81
+ logging.info(f"Download progress: {int(status.progress() * 100)}%")
82
 
83
+ file_io.seek(0)
84
+ file_info = drive_service.files().get(fileId=file_id).execute()
85
+ mime_type = file_info.get('mimeType', 'application/octet-stream')
 
 
 
86
 
87
+ return file_io, mime_type
88
 
 
 
 
89
  except Exception as e:
90
+ logging.error(f"Error downloading file from Drive: {e}")
91
+ raise HTTPException(status_code=500, detail="Failed to download file from Drive")
92
 
93
+ def upload_to_gemini(file_content: BytesIO, mime_type: str) -> dict:
94
+ """
95
+ Uploads the file content to Gemini
96
+ """
97
  try:
98
+ response = genai_client.upload_file(file_content, mime_type=mime_type)
99
+ return response.json()
100
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
  except Exception as e:
102
+ logging.error(f"Error uploading to Gemini: {e}")
103
+ raise HTTPException(status_code=500, detail="Failed to upload to Gemini")
 
 
 
 
 
 
 
104