m8chaa commited on
Commit
46fabd3
·
verified ·
1 Parent(s): 21ff58d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +127 -132
app.py CHANGED
@@ -15,8 +15,9 @@ import firebase_admin
15
  from firebase_admin import credentials
16
  from firebase_admin import firestore
17
 
 
18
  app = FastAPI()
19
-
20
  app.add_middleware(
21
  CORSMiddleware,
22
  allow_origins=["*"], # Allows all origins
@@ -24,12 +25,11 @@ app.add_middleware(
24
  allow_methods=["*"], # Allows all methods
25
  allow_headers=["*"], # Allows all headers
26
  )
27
-
28
- timeout = Timeout(1000.0, connect=600.0)
29
-
30
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
31
  logging.info("Application starting up")
32
 
 
33
  def get_secret(secret_name):
34
  secret_path = f"/app/secrets/{secret_name}.txt"
35
  try:
@@ -38,10 +38,12 @@ def get_secret(secret_name):
38
  except FileNotFoundError:
39
  raise Exception(f"Secret file for {secret_name} not found")
40
 
41
- # Use the function to get your DEZGO_API_KEY
42
  dezgo_api_key = get_secret('dezgo_api_key')
43
  appsScriptsURL = get_secret('appsscript_url')
 
44
 
 
45
  firebase_config = {
46
  "type": "service_account",
47
  "project_id": "resheet-k6x6vh",
@@ -55,16 +57,15 @@ firebase_config = {
55
  "client_x509_cert_url": get_secret('client_x509_cert_url'),
56
  "universe_domain": "googleapis.com"
57
  }
58
-
59
- print(firebase_config)
60
  cred = credentials.Certificate(firebase_config)
61
  firebase_admin.initialize_app(cred, {
62
  'projectId': firebase_config['project_id'],
63
  })
64
-
65
  db = firestore.client()
 
66
  users_ref = db.collection('users')
67
 
 
68
  class ImageData(BaseModel):
69
  sheetID: str
70
  base64Image: str
@@ -75,25 +76,8 @@ class UnprocessedImage(BaseModel):
75
  sheetID: str
76
  s3Url: str
77
 
78
- @app.post("/redirect")
79
- async def redirect_to_google_apps_script(request: Request):
80
- # Get the body of the incoming request
81
- body = await request.body()
82
-
83
- async with httpx.AsyncClient(timeout=300.0, follow_redirects=True) as client: # Follow redirects
84
- response = await client.post(
85
- 'https://script.google.com/macros/s/AKfycbw9_31pMwQVhJPezJ9qlMRCAamQKRREnun-tfT_TnS5TOwOVcfdgU1Y9xTpAv6bZuiKOA/exec',
86
- data=body
87
- )
88
- if response.status_code != 200:
89
- logging.error(f"Unexpected response from Google Apps Script API: {response.status_code} - {response.text}")
90
- raise HTTPException(status_code=500, detail="Error from external API.")
91
- return response.json()
92
-
93
- dezgo_headers = {
94
- 'X-Dezgo-Key': dezgo_api_key,
95
- }
96
 
 
97
  # update the google sheet with failure
98
  async def record_failure(s3_original_url, failed_task, sheetID):
99
  body = {
@@ -107,7 +91,8 @@ async def record_failure(s3_original_url, failed_task, sheetID):
107
  if response.status_code != 200:
108
  logging.error(f"Unexpected response from Google Apps Script API: {response.status_code} - {response.text}")
109
  raise HTTPException(status_code=500, detail="Error from external API.")
110
-
 
111
  async def post_to_google_apps_script(appsScriptsURL, body, s3_orginal_url):
112
  async with httpx.AsyncClient(follow_redirects=True) as client:
113
  response = await client.post(appsScriptsURL, json=body)
@@ -115,7 +100,8 @@ async def post_to_google_apps_script(appsScriptsURL, body, s3_orginal_url):
115
  logging.error(f"Unexpected response from Google Apps Script API: {response.status_code} - {response.text}")
116
  await record_failure(s3_orginal_url, 'Failed to queue processed image.', body.sheetID)
117
 
118
- async def background_worker(sheeturl, image_contents, filename, content_type, appsScriptsURL, user_ref):
 
119
  sheetID = sheeturl.split('/')[-2]
120
  user_doc = users_ref.document(user_ref)
121
  # get the 'total_uploaded' field from the user's document
@@ -126,7 +112,13 @@ async def background_worker(sheeturl, image_contents, filename, content_type, ap
126
  logging.info('user not found')
127
  await record_failure('', 'User not found.', sheetID)
128
  return {"message": "User not found."}
129
- total_uploaded = user_data.get('total_uploaded', 0)
 
 
 
 
 
 
130
 
131
 
132
  image_data = {
@@ -152,7 +144,7 @@ async def background_worker(sheeturl, image_contents, filename, content_type, ap
152
  # get the number of total cut photos
153
  receipt_count = cut_images.__len__()
154
  # set the user's total_uploaded field to the current total_uploaded + receipt_count
155
- user_doc.update({'total_uploaded': total_uploaded + receipt_count})
156
 
157
  for i, base64_str in enumerate(cut_images, start=1):
158
  image_bytes = base64.b64decode(base64_str)
@@ -175,6 +167,7 @@ async def background_worker(sheeturl, image_contents, filename, content_type, ap
175
  "queued": queued,
176
  "mode": "mobile",
177
  "sheetID": sheetID,
 
178
  }
179
 
180
  print(body)
@@ -182,89 +175,6 @@ async def background_worker(sheeturl, image_contents, filename, content_type, ap
182
  await post_to_google_apps_script(appsScriptsURL, body, s3_orginal_url)
183
  return {"message": "Image processing completed."}
184
 
185
- # receive a sheetID and an image as multipart form data
186
- @app.post('/mobile-process-receipts')
187
- async def mobile_process_receipts(background_tasks: BackgroundTasks, sheeturl: str = Form(...), user_ref: str = Form(...), image: UploadFile = File(...)):
188
- image_contents = await image.read()
189
- uuid = str(uuid4())
190
- timestamp = str(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
191
- sheetID = sheeturl.split('/')[-2]
192
-
193
- message = "영수기 AI가 데이터 분석 중입니다. 잠시만 기다려주세요."
194
- data = [timestamp, uuid, message]
195
- body = {
196
- "sheetID": sheetID,
197
- "data": data,
198
- "mode": "alert-user"
199
- }
200
- async with httpx.AsyncClient(follow_redirects=True) as client:
201
- response = await client.post(appsScriptsURL, json=body)
202
- if response.status_code != 200:
203
- logging.error(f"Unexpected response from Google Apps Script API: {response.status_code} - {response.text}")
204
- raise HTTPException(status_code=500, detail="Error from external API.")
205
-
206
- background_tasks.add_task(background_worker, sheeturl, image_contents, uuid, image.content_type, appsScriptsURL, user_ref)
207
-
208
- return {"message": "Image processing completed."}
209
-
210
-
211
- @app.post('/process-receipts')
212
- async def mobile_process_receipts(background_tasks: BackgroundTasks, request: Request):
213
- data = await request.json()
214
- base64_str = data['base64Image']
215
- prefix, base64_str = base64_str.split(',', 1) # Split on the first comma
216
-
217
- content_type = data['mimeType']
218
- sheeturl = data['sheetID']
219
-
220
- checkPhoto = {
221
- 'sheetID': sheeturl.split('/')[-2],
222
- 'mode': "check",
223
- 'photo': base64_str,
224
- 'mimeType': content_type,
225
- 'fileName': 'test.png'
226
- }
227
- async with httpx.AsyncClient(timeout=300, follow_redirects=True) as client:
228
- response = await client.post(appsScriptsURL, json=checkPhoto)
229
- if response.status_code != 200:
230
- logging.error(f"Unexpected response from Google Apps Script API: {response.status_code} - {response.text}")
231
- raise HTTPException(status_code=500, detail="Error from external API.")
232
- if response.json()['status'] == 'invalid':
233
- return {"message": "Invalid photo."}
234
-
235
- image_contents = base64.b64decode(base64_str)
236
- uuid = str(uuid4())
237
- timestamp = str(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
238
- message = "영수기 AI가 데이터 분석 중입니다. 잠시만 기다려주세요."
239
- data = [timestamp, uuid, message]
240
- body = {
241
- "sheetID": sheeturl.split('/')[-2],
242
- "data": data,
243
- "mode": "alert-user"
244
- }
245
- async with httpx.AsyncClient(follow_redirects=True) as client:
246
- response = await client.post(appsScriptsURL, json=body)
247
- if response.status_code != 200:
248
- logging.error(f"Unexpected response from Google Apps Script API: {response.status_code} - {response.text}")
249
- raise HTTPException(status_code=500, detail="Error from external API.")
250
-
251
- # before continuing on, search db for user via sheetID
252
- sheetID = sheeturl.split('/')[-2]
253
- # Query the database for documents where 'sheetID' field is equal to sheetID
254
- query_ref = users_ref.where('sheetID', '==', sheetID)
255
- # Get the documents that match the query
256
- user_docs = query_ref.stream()
257
-
258
- # user_docs is an iterator of DocumentSnapshot
259
- # You can convert it to a list if you want to work with it directly
260
- user_docs = list(user_docs)
261
-
262
- user_ref = user_docs[0].id
263
-
264
- background_tasks.add_task(background_worker, sheeturl, image_contents, uuid, content_type, appsScriptsURL,user_ref)
265
-
266
- return {"message": "Image processing completed."}
267
-
268
  # send the image to dezgo
269
  async def detect_receipts(image_contents, s3_orginal_url, sheetID):
270
  files = {
@@ -272,6 +182,9 @@ async def detect_receipts(image_contents, s3_orginal_url, sheetID):
272
  'mode': 'mask',
273
  }
274
  logging.info("Sending image to Dezgo API")
 
 
 
275
  async with httpx.AsyncClient(timeout=timeout) as client:
276
  response = await client.post('https://api.dezgo.com/remove-background', headers=dezgo_headers, files=files)
277
  # if successful, pass the masked image to cutting_image function
@@ -352,24 +265,106 @@ async def cutting_image(masked_image, image_contents, cut_images, s3_orginal_url
352
  _, img_encoded = cv2.imencode('.jpg', cropped_image, [int(cv2.IMWRITE_JPEG_QUALITY), 90])
353
  base64_str = base64.b64encode(img_encoded).decode('utf-8')
354
  cut_images.append(base64_str)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
355
 
356
 
357
- # @app.post('/test')
358
- # async def mobile_process_receipts(background_tasks: BackgroundTasks, sheeturl: str = Form(...), user_ref: str = Form(...), image: UploadFile = File(...)):
359
- # image_contents = await image.read()
360
- # uuid = str(uuid4())
361
- # timestamp = str(datetime.now().strftime("%Y-%m-%d-%H-%M-%S"))
362
- # message = "영수기가 데이터 분석 중입니다. 잠시만 기다려주세요."
363
- # data = [timestamp, uuid, message]
364
- # body = {
365
- # "sheetID": sheeturl.split('/')[-2],
366
- # "data": data,
367
- # "mode": "alert-user"
368
- # }
369
- # async with httpx.AsyncClient(follow_redirects=True) as client:
370
- # response = await client.post(appsScriptsURL, json=body)
371
- # if response.status_code != 200:
372
- # logging.error(f"Unexpected response from Google Apps Script API: {response.status_code} - {response.text}")
373
- # raise HTTPException(status_code=500, detail="Error from external API.")
374
- # background_tasks.add_task(background_worker, sheeturl, image_contents, image.filename, image.content_type, appsScriptsURL)
375
- # return {"message": "Image processing completed."}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  from firebase_admin import credentials
16
  from firebase_admin import firestore
17
 
18
+ # start application
19
  app = FastAPI()
20
+ # allow all origins
21
  app.add_middleware(
22
  CORSMiddleware,
23
  allow_origins=["*"], # Allows all origins
 
25
  allow_methods=["*"], # Allows all methods
26
  allow_headers=["*"], # Allows all headers
27
  )
28
+ # set up logging
 
 
29
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
30
  logging.info("Application starting up")
31
 
32
+ # get secret from file
33
  def get_secret(secret_name):
34
  secret_path = f"/app/secrets/{secret_name}.txt"
35
  try:
 
38
  except FileNotFoundError:
39
  raise Exception(f"Secret file for {secret_name} not found")
40
 
41
+ # get appropriate API keys and URLs
42
  dezgo_api_key = get_secret('dezgo_api_key')
43
  appsScriptsURL = get_secret('appsscript_url')
44
+ timeout = Timeout(1000.0, connect=600.0)
45
 
46
+ # set up firebase
47
  firebase_config = {
48
  "type": "service_account",
49
  "project_id": "resheet-k6x6vh",
 
57
  "client_x509_cert_url": get_secret('client_x509_cert_url'),
58
  "universe_domain": "googleapis.com"
59
  }
 
 
60
  cred = credentials.Certificate(firebase_config)
61
  firebase_admin.initialize_app(cred, {
62
  'projectId': firebase_config['project_id'],
63
  })
 
64
  db = firestore.client()
65
+ # get db
66
  users_ref = db.collection('users')
67
 
68
+
69
  class ImageData(BaseModel):
70
  sheetID: str
71
  base64Image: str
 
76
  sheetID: str
77
  s3Url: str
78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
 
80
+ # functions
81
  # update the google sheet with failure
82
  async def record_failure(s3_original_url, failed_task, sheetID):
83
  body = {
 
91
  if response.status_code != 200:
92
  logging.error(f"Unexpected response from Google Apps Script API: {response.status_code} - {response.text}")
93
  raise HTTPException(status_code=500, detail="Error from external API.")
94
+
95
+ # post the processed image to google apps script
96
  async def post_to_google_apps_script(appsScriptsURL, body, s3_orginal_url):
97
  async with httpx.AsyncClient(follow_redirects=True) as client:
98
  response = await client.post(appsScriptsURL, json=body)
 
100
  logging.error(f"Unexpected response from Google Apps Script API: {response.status_code} - {response.text}")
101
  await record_failure(s3_orginal_url, 'Failed to queue processed image.', body.sheetID)
102
 
103
+ # background worker
104
+ async def background_worker(sheeturl, image_contents, filename, content_type, appsScriptsURL, user_ref, doc_type):
105
  sheetID = sheeturl.split('/')[-2]
106
  user_doc = users_ref.document(user_ref)
107
  # get the 'total_uploaded' field from the user's document
 
112
  logging.info('user not found')
113
  await record_failure('', 'User not found.', sheetID)
114
  return {"message": "User not found."}
115
+
116
+ if doc_type == 'receipt':
117
+ total_uploaded = user_data.get('receipts_uploaded', 0)
118
+ current_upload = 'receipts_uploaded'
119
+ elif doc_type == 'business_card':
120
+ total_uploaded = user_data.get('business_cards_uploaded', 0)
121
+ current_upload = 'business_cards_uploaded'
122
 
123
 
124
  image_data = {
 
144
  # get the number of total cut photos
145
  receipt_count = cut_images.__len__()
146
  # set the user's total_uploaded field to the current total_uploaded + receipt_count
147
+ user_doc.update({current_upload: total_uploaded + receipt_count})
148
 
149
  for i, base64_str in enumerate(cut_images, start=1):
150
  image_bytes = base64.b64decode(base64_str)
 
167
  "queued": queued,
168
  "mode": "mobile",
169
  "sheetID": sheetID,
170
+ "docType": doc_type,
171
  }
172
 
173
  print(body)
 
175
  await post_to_google_apps_script(appsScriptsURL, body, s3_orginal_url)
176
  return {"message": "Image processing completed."}
177
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
178
  # send the image to dezgo
179
  async def detect_receipts(image_contents, s3_orginal_url, sheetID):
180
  files = {
 
182
  'mode': 'mask',
183
  }
184
  logging.info("Sending image to Dezgo API")
185
+ dezgo_headers = {
186
+ 'X-Dezgo-Key': dezgo_api_key,
187
+ }
188
  async with httpx.AsyncClient(timeout=timeout) as client:
189
  response = await client.post('https://api.dezgo.com/remove-background', headers=dezgo_headers, files=files)
190
  # if successful, pass the masked image to cutting_image function
 
265
  _, img_encoded = cv2.imencode('.jpg', cropped_image, [int(cv2.IMWRITE_JPEG_QUALITY), 90])
266
  base64_str = base64.b64encode(img_encoded).decode('utf-8')
267
  cut_images.append(base64_str)
268
+
269
+ # redirect to google apps script
270
+ @app.post("/redirect")
271
+ async def redirect_to_google_apps_script(request: Request):
272
+ # Get the body of the incoming request
273
+ body = await request.body()
274
+
275
+ async with httpx.AsyncClient(timeout=300.0, follow_redirects=True) as client: # Follow redirects
276
+ response = await client.post(
277
+ 'https://script.google.com/macros/s/AKfycbw9_31pMwQVhJPezJ9qlMRCAamQKRREnun-tfT_TnS5TOwOVcfdgU1Y9xTpAv6bZuiKOA/exec',
278
+ data=body
279
+ )
280
+ if response.status_code != 200:
281
+ logging.error(f"Unexpected response from Google Apps Script API: {response.status_code} - {response.text}")
282
+ raise HTTPException(status_code=500, detail="Error from external API.")
283
+ return response.json()
284
+
285
+ # receive a sheetID and an image as multipart form data
286
+ @app.post('/mobile-process')
287
+ async def mobile_process_receipts(background_tasks: BackgroundTasks, sheeturl: str = Form(...), user_ref: str = Form(...), doc_type: str = Form(...), image: UploadFile = File(...)):
288
+ image_contents = await image.read()
289
+ uuid = str(uuid4())
290
+ timestamp = str(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
291
+ sheetID = sheeturl.split('/')[-2]
292
+
293
+ message = "영수기 AI가 데이터 분석 중입니다. 잠시만 기다려주세요."
294
+ data = [timestamp, uuid, message]
295
+ body = {
296
+ "sheetID": sheetID,
297
+ "data": data,
298
+ "mode": "alert-user",
299
+ "docType": doc_type,
300
+ }
301
+ async with httpx.AsyncClient(follow_redirects=True) as client:
302
+ response = await client.post(appsScriptsURL, json=body)
303
+ if response.status_code != 200:
304
+ logging.error(f"Unexpected response from Google Apps Script API: {response.status_code} - {response.text}")
305
+ raise HTTPException(status_code=500, detail="Error from external API.")
306
+
307
+ background_tasks.add_task(background_worker, sheeturl, image_contents, uuid, image.content_type, appsScriptsURL, user_ref, doc_type)
308
+
309
+ return {"message": "Image processing completed."}
310
 
311
 
312
+ @app.post('/process')
313
+ async def mobile_process_receipts(background_tasks: BackgroundTasks, request: Request):
314
+ data = await request.json()
315
+ base64_str = data['base64Image']
316
+ prefix, base64_str = base64_str.split(',', 1) # Split on the first comma
317
+
318
+ content_type = data['mimeType']
319
+ sheeturl = data['sheetID']
320
+ doc_type = data['docType']
321
+
322
+ checkPhoto = {
323
+ 'sheetID': sheeturl.split('/')[-2],
324
+ 'mode': "check",
325
+ 'photo': base64_str,
326
+ 'mimeType': content_type,
327
+ 'fileName': 'test.png',
328
+ 'docType': doc_type,
329
+ }
330
+ async with httpx.AsyncClient(timeout=300, follow_redirects=True) as client:
331
+ response = await client.post(appsScriptsURL, json=checkPhoto)
332
+ if response.status_code != 200:
333
+ logging.error(f"Unexpected response from Google Apps Script API: {response.status_code} - {response.text}")
334
+ raise HTTPException(status_code=500, detail="Error from external API.")
335
+ if response.json()['status'] == 'invalid':
336
+ return {"message": "Invalid photo."}
337
+
338
+ image_contents = base64.b64decode(base64_str)
339
+ uuid = str(uuid4())
340
+ timestamp = str(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
341
+ message = "영수기 AI가 데이터 분석 중입니다. 잠시만 기다려주세요."
342
+ data = [timestamp, uuid, message]
343
+ body = {
344
+ "sheetID": sheeturl.split('/')[-2],
345
+ "data": data,
346
+ "mode": "alert-user"
347
+ }
348
+ async with httpx.AsyncClient(follow_redirects=True) as client:
349
+ response = await client.post(appsScriptsURL, json=body)
350
+ if response.status_code != 200:
351
+ logging.error(f"Unexpected response from Google Apps Script API: {response.status_code} - {response.text}")
352
+ raise HTTPException(status_code=500, detail="Error from external API.")
353
+
354
+ # before continuing on, search db for user via sheetID
355
+ sheetID = sheeturl.split('/')[-2]
356
+ # Query the database for documents where 'sheetID' field is equal to sheetID
357
+ query_ref = users_ref.where('sheetID', '==', sheetID)
358
+ # Get the documents that match the query
359
+ user_docs = query_ref.stream()
360
+
361
+ # user_docs is an iterator of DocumentSnapshot
362
+ # You can convert it to a list if you want to work with it directly
363
+ user_docs = list(user_docs)
364
+
365
+ user_ref = user_docs[0].id
366
+
367
+ background_tasks.add_task(background_worker, sheeturl, image_contents, uuid, content_type, appsScriptsURL, user_ref, doc_type)
368
+
369
+ return {"message": "Image processing completed."}
370
+