Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -107,19 +107,40 @@ def process_receipt_endpoint():
|
|
| 107 |
auth_header = request.headers.get('Authorization')
|
| 108 |
token = auth_header.split('Bearer ')[1] if auth_header else None
|
| 109 |
uid = verify_token(token) if token else None
|
| 110 |
-
|
| 111 |
if not uid:
|
| 112 |
return jsonify({'error': 'Invalid token'}), 401
|
| 113 |
|
| 114 |
user_ref = db.reference(f'users/{uid}')
|
| 115 |
user_data = user_ref.get()
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
if request.form.get('manual_entry') == 'true':
|
| 120 |
return handle_manual_entry(uid, user_ref, user_data)
|
| 121 |
|
| 122 |
-
# Handle image processing
|
| 123 |
file = request.files.get('receipt')
|
| 124 |
if not file:
|
| 125 |
return jsonify({'error': 'No file uploaded'}), 400
|
|
@@ -132,6 +153,13 @@ def process_receipt_endpoint():
|
|
| 132 |
if existing:
|
| 133 |
return jsonify({'error': 'Receipt already processed'}), 400
|
| 134 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
image = Image.open(io.BytesIO(image_bytes))
|
| 136 |
model = configure_gemini()
|
| 137 |
result_text = process_receipt(model, image)
|
|
@@ -145,14 +173,15 @@ def process_receipt_endpoint():
|
|
| 145 |
if not data.get('is_receipt', False):
|
| 146 |
return jsonify({'error': 'Not a valid receipt'}), 400
|
| 147 |
|
| 148 |
-
return
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
|
|
|
| 156 |
|
| 157 |
except Exception as e:
|
| 158 |
print(e)
|
|
|
|
| 107 |
auth_header = request.headers.get('Authorization')
|
| 108 |
token = auth_header.split('Bearer ')[1] if auth_header else None
|
| 109 |
uid = verify_token(token) if token else None
|
| 110 |
+
|
| 111 |
if not uid:
|
| 112 |
return jsonify({'error': 'Invalid token'}), 401
|
| 113 |
|
| 114 |
user_ref = db.reference(f'users/{uid}')
|
| 115 |
user_data = user_ref.get()
|
| 116 |
+
|
| 117 |
+
# If the confirmation flag is set, then use the form fields (and hidden file_hash/image_url)
|
| 118 |
+
# to create the transaction. (Note: image_bytes is None because the image was already uploaded.)
|
| 119 |
+
if request.form.get('confirmed') == 'true':
|
| 120 |
+
data = {
|
| 121 |
+
'total': float(request.form.get('total')),
|
| 122 |
+
'items': [item.strip() for item in request.form.get('items', '').split(',')],
|
| 123 |
+
'date': request.form.get('date'),
|
| 124 |
+
'receipt_number': request.form.get('receipt_number'),
|
| 125 |
+
'is_receipt': True,
|
| 126 |
+
'image_url': request.form.get('image_url') # provided by the first submission
|
| 127 |
+
}
|
| 128 |
+
file_hash = request.form.get('file_hash', '')
|
| 129 |
+
# In this confirmation branch, we pass image_bytes as None.
|
| 130 |
+
return validate_and_save_transaction(
|
| 131 |
+
uid=uid,
|
| 132 |
+
user_data=user_data,
|
| 133 |
+
data=data,
|
| 134 |
+
file_hash=file_hash,
|
| 135 |
+
image_bytes=None,
|
| 136 |
+
manual=False
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
+
# Handle manual entry (if the user explicitly selects manual entry)
|
| 140 |
if request.form.get('manual_entry') == 'true':
|
| 141 |
return handle_manual_entry(uid, user_ref, user_data)
|
| 142 |
|
| 143 |
+
# Handle image processing (first submission)
|
| 144 |
file = request.files.get('receipt')
|
| 145 |
if not file:
|
| 146 |
return jsonify({'error': 'No file uploaded'}), 400
|
|
|
|
| 153 |
if existing:
|
| 154 |
return jsonify({'error': 'Receipt already processed'}), 400
|
| 155 |
|
| 156 |
+
# Immediately upload the image so that it is stored regardless of review outcome.
|
| 157 |
+
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
|
| 158 |
+
blob = bucket.blob(f'receipts/{uid}/{timestamp}_{file_hash}.jpg')
|
| 159 |
+
blob.upload_from_string(image_bytes, content_type='image/jpeg')
|
| 160 |
+
image_url = blob.public_url
|
| 161 |
+
|
| 162 |
+
# Process the image with Gemini
|
| 163 |
image = Image.open(io.BytesIO(image_bytes))
|
| 164 |
model = configure_gemini()
|
| 165 |
result_text = process_receipt(model, image)
|
|
|
|
| 173 |
if not data.get('is_receipt', False):
|
| 174 |
return jsonify({'error': 'Not a valid receipt'}), 400
|
| 175 |
|
| 176 |
+
# Instead of saving immediately, return the extracted data along with the image info for review.
|
| 177 |
+
data['file_hash'] = file_hash
|
| 178 |
+
data['image_url'] = image_url
|
| 179 |
+
return jsonify({
|
| 180 |
+
'success': True,
|
| 181 |
+
'extracted': True,
|
| 182 |
+
'data': data,
|
| 183 |
+
'message': 'Review extracted information before confirming.'
|
| 184 |
+
})
|
| 185 |
|
| 186 |
except Exception as e:
|
| 187 |
print(e)
|