Spaces:
Sleeping
Sleeping
revert
Browse files
app.py
CHANGED
|
@@ -12,10 +12,6 @@ import logging
|
|
| 12 |
import boto3
|
| 13 |
import base64
|
| 14 |
|
| 15 |
-
import firebase_admin
|
| 16 |
-
from firebase_admin import credentials
|
| 17 |
-
from firebase_admin import firestore
|
| 18 |
-
|
| 19 |
app = FastAPI()
|
| 20 |
|
| 21 |
app.add_middleware(
|
|
@@ -31,32 +27,6 @@ timeout = Timeout(100.0, connect=600.0)
|
|
| 31 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 32 |
logging.info("Application starting up")
|
| 33 |
|
| 34 |
-
appsScriptsURL = 'https://script.google.com/macros/s/AKfycbw9_31pMwQVhJPezJ9qlMRCAamQKRREnun-tfT_TnS5TOwOVcfdgU1Y9xTpAv6bZuiKOA/exec'
|
| 35 |
-
dezgo_api_key = 'DEZGO-AA84DEB300562DA089E56CEA6081AE7DB601EF2D3EE4C693910E1C25E59A4FF8023D4932'
|
| 36 |
-
|
| 37 |
-
dezgo_headers = {
|
| 38 |
-
'X-Dezgo-Key': dezgo_api_key,
|
| 39 |
-
}
|
| 40 |
-
|
| 41 |
-
firebase_config = {
|
| 42 |
-
"apiKey": "AIzaSyA5wGzaM1OB1qnn2q6wSPsgqvHfGZP-baA",
|
| 43 |
-
"authDomain": "resheet-k6x6vh.firebaseapp.com",
|
| 44 |
-
"projectId": "resheet-k6x6vh",
|
| 45 |
-
"storageBucket": "resheet-k6x6vh.appspot.com",
|
| 46 |
-
"messagingSenderId": "532354534433",
|
| 47 |
-
"appId": "1:532354534433:web:35eb1e4e5c1deaa916a760",
|
| 48 |
-
"measurementId": "G-38XY8HEDF4"
|
| 49 |
-
}
|
| 50 |
-
|
| 51 |
-
cred = credentials.Certificate('./resheet-k6x6vh-firebase-adminsdk-iivci-cb667b16a7.json')
|
| 52 |
-
firebase_admin.initialize_app(cred, {
|
| 53 |
-
'projectId': firebase_config['projectId'],
|
| 54 |
-
})
|
| 55 |
-
|
| 56 |
-
db = firestore.client()
|
| 57 |
-
users_ref = db.collection('users')
|
| 58 |
-
|
| 59 |
-
|
| 60 |
class ImageData(BaseModel):
|
| 61 |
sheetID: str
|
| 62 |
base64Image: str
|
|
@@ -82,7 +52,12 @@ async def redirect_to_google_apps_script(request: Request):
|
|
| 82 |
raise HTTPException(status_code=500, detail="Error from external API.")
|
| 83 |
return response.json()
|
| 84 |
|
|
|
|
|
|
|
| 85 |
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
# update the google sheet with failure
|
| 88 |
async def record_failure(s3_original_url, failed_task, sheetID):
|
|
@@ -106,16 +81,7 @@ async def post_to_google_apps_script(appsScriptsURL, body, s3_orginal_url):
|
|
| 106 |
await record_failure(s3_orginal_url, 'Failed to queue processed image.', body.sheetID)
|
| 107 |
|
| 108 |
async def background_worker(sheeturl, image_contents, filename, content_type, appsScriptsURL):
|
| 109 |
-
|
| 110 |
sheetID = sheeturl.split('/')[-2]
|
| 111 |
-
user_doc = users_ref.document(sheetID)
|
| 112 |
-
# get the 'total_uploaded' field from the user's document
|
| 113 |
-
user_data = user_doc.get().to_dict()
|
| 114 |
-
if user_data is None:
|
| 115 |
-
await record_failure(s3_orginal_url, 'User not found.', sheetID)
|
| 116 |
-
return {"message": "User not found."}
|
| 117 |
-
total_uploaded = user_data.get('total_uploaded', 0)
|
| 118 |
-
|
| 119 |
image_data = {
|
| 120 |
'sheetID': sheetID,
|
| 121 |
'fileName': filename,
|
|
@@ -136,11 +102,6 @@ async def background_worker(sheeturl, image_contents, filename, content_type, ap
|
|
| 136 |
await record_failure(s3_orginal_url, 'No valid receipts found.', sheetID)
|
| 137 |
return {"message": "No valid receipts found."}
|
| 138 |
else:
|
| 139 |
-
# get the number of total cut photos
|
| 140 |
-
receipt_count = cut_images.__len__()
|
| 141 |
-
# set the user's total_uploaded field to the current total_uploaded + receipt_count
|
| 142 |
-
user_doc.update({'total_uploaded': total_uploaded + receipt_count})
|
| 143 |
-
# save the processed images to s3
|
| 144 |
for i, base64_str in enumerate(cut_images, start=1):
|
| 145 |
image_bytes = base64.b64decode(base64_str)
|
| 146 |
object_name = f"{object_name}_processed_{i}.png"
|
|
@@ -176,22 +137,6 @@ async def mobile_process_receipts(background_tasks: BackgroundTasks, sheeturl: s
|
|
| 176 |
uuid = str(uuid4())
|
| 177 |
timestamp = str(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
|
| 178 |
sheetID = sheeturl.split('/')[-2]
|
| 179 |
-
base64_str = base64.b64encode(image_contents).decode('utf-8')
|
| 180 |
-
|
| 181 |
-
checkPhoto = {
|
| 182 |
-
'sheetID': sheetID,
|
| 183 |
-
'mode': "check",
|
| 184 |
-
'photo': base64_str,
|
| 185 |
-
'mimeType': image.content_type,
|
| 186 |
-
'fileName': 'test.png'
|
| 187 |
-
}
|
| 188 |
-
async with httpx.AsyncClient(timeout=300, follow_redirects=True) as client:
|
| 189 |
-
response = await client.post(appsScriptsURL, json=checkPhoto)
|
| 190 |
-
if response.status_code != 200:
|
| 191 |
-
logging.error(f"Unexpected response from Google Apps Script API: {response.status_code} - {response.text}")
|
| 192 |
-
raise HTTPException(status_code=500, detail="Error from external API.")
|
| 193 |
-
if response.json()['status'] == 'invalid':
|
| 194 |
-
return {"message": "Invalid photo."}
|
| 195 |
|
| 196 |
message = "영수기 AI가 데이터 분석 중입니다. 잠시만 기다려주세요."
|
| 197 |
data = [timestamp, uuid, message]
|
|
|
|
| 12 |
import boto3
|
| 13 |
import base64
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
app = FastAPI()
|
| 16 |
|
| 17 |
app.add_middleware(
|
|
|
|
| 27 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 28 |
logging.info("Application starting up")
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
class ImageData(BaseModel):
|
| 31 |
sheetID: str
|
| 32 |
base64Image: str
|
|
|
|
| 52 |
raise HTTPException(status_code=500, detail="Error from external API.")
|
| 53 |
return response.json()
|
| 54 |
|
| 55 |
+
appsScriptsURL = 'https://script.google.com/macros/s/AKfycbw9_31pMwQVhJPezJ9qlMRCAamQKRREnun-tfT_TnS5TOwOVcfdgU1Y9xTpAv6bZuiKOA/exec'
|
| 56 |
+
dezgo_api_key = 'DEZGO-AA84DEB300562DA089E56CEA6081AE7DB601EF2D3EE4C693910E1C25E59A4FF8023D4932'
|
| 57 |
|
| 58 |
+
dezgo_headers = {
|
| 59 |
+
'X-Dezgo-Key': dezgo_api_key,
|
| 60 |
+
}
|
| 61 |
|
| 62 |
# update the google sheet with failure
|
| 63 |
async def record_failure(s3_original_url, failed_task, sheetID):
|
|
|
|
| 81 |
await record_failure(s3_orginal_url, 'Failed to queue processed image.', body.sheetID)
|
| 82 |
|
| 83 |
async def background_worker(sheeturl, image_contents, filename, content_type, appsScriptsURL):
|
|
|
|
| 84 |
sheetID = sheeturl.split('/')[-2]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
image_data = {
|
| 86 |
'sheetID': sheetID,
|
| 87 |
'fileName': filename,
|
|
|
|
| 102 |
await record_failure(s3_orginal_url, 'No valid receipts found.', sheetID)
|
| 103 |
return {"message": "No valid receipts found."}
|
| 104 |
else:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
for i, base64_str in enumerate(cut_images, start=1):
|
| 106 |
image_bytes = base64.b64decode(base64_str)
|
| 107 |
object_name = f"{object_name}_processed_{i}.png"
|
|
|
|
| 137 |
uuid = str(uuid4())
|
| 138 |
timestamp = str(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
|
| 139 |
sheetID = sheeturl.split('/')[-2]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
message = "영수기 AI가 데이터 분석 중입니다. 잠시만 기다려주세요."
|
| 142 |
data = [timestamp, uuid, message]
|