Update main.py
Browse files
main.py
CHANGED
|
@@ -353,6 +353,70 @@ def get_transaction_types():
|
|
| 353 |
}
|
| 354 |
return jsonify(transaction_types)
|
| 355 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 356 |
@app.route('/health', methods=['GET'])
|
| 357 |
def health_check():
|
| 358 |
"""Health check endpoint."""
|
|
|
|
| 353 |
}
|
| 354 |
return jsonify(transaction_types)
|
| 355 |
|
| 356 |
+
@app.route('/process-image', methods=['POST'])
|
| 357 |
+
def process_image():
|
| 358 |
+
"""Handle image upload, process it with Gemini Vision, and extract transactions."""
|
| 359 |
+
try:
|
| 360 |
+
if 'file' not in request.files:
|
| 361 |
+
return jsonify({'error': 'No file uploaded'}), 400
|
| 362 |
+
|
| 363 |
+
file = request.files['file']
|
| 364 |
+
if file.filename == '':
|
| 365 |
+
return jsonify({'error': 'No file selected'}), 400
|
| 366 |
+
|
| 367 |
+
# Check if file is an image
|
| 368 |
+
allowed_extensions = {'.jpg', '.jpeg', '.png', '.gif', '.bmp', '.webp'}
|
| 369 |
+
file_ext = os.path.splitext(file.filename)[1].lower()
|
| 370 |
+
if file_ext not in allowed_extensions:
|
| 371 |
+
return jsonify({'error': 'Invalid file type. Supported formats: JPG, JPEG, PNG, GIF, BMP, WEBP'}), 400
|
| 372 |
+
|
| 373 |
+
# Save to temp file
|
| 374 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=file_ext) as tmp:
|
| 375 |
+
file.save(tmp.name)
|
| 376 |
+
file_path = tmp.name
|
| 377 |
+
|
| 378 |
+
try:
|
| 379 |
+
model = configure_gemini(api_key)
|
| 380 |
+
|
| 381 |
+
# Upload image file to Gemini
|
| 382 |
+
logging.info(f"Uploading image file: {file.filename}")
|
| 383 |
+
uploaded_file = genai.upload_file(file_path)
|
| 384 |
+
|
| 385 |
+
# Wait for file to be processed
|
| 386 |
+
while uploaded_file.state.name == "PROCESSING":
|
| 387 |
+
time.sleep(1)
|
| 388 |
+
uploaded_file = genai.get_file(uploaded_file.name)
|
| 389 |
+
|
| 390 |
+
if uploaded_file.state.name == "FAILED":
|
| 391 |
+
raise ValueError("Image processing failed")
|
| 392 |
+
|
| 393 |
+
logging.info("Processing image for transaction extraction")
|
| 394 |
+
|
| 395 |
+
# Generate content with the image and prompt
|
| 396 |
+
response = model.generate_content([uploaded_file, PROMPT])
|
| 397 |
+
|
| 398 |
+
# Parse the response
|
| 399 |
+
result = extract_json_from_response(response.text)
|
| 400 |
+
transactions = result.get('transactions', [])
|
| 401 |
+
|
| 402 |
+
# Clean up the uploaded file from Gemini
|
| 403 |
+
genai.delete_file(uploaded_file.name)
|
| 404 |
+
|
| 405 |
+
return jsonify({'transactions': transactions})
|
| 406 |
+
|
| 407 |
+
finally:
|
| 408 |
+
# Clean up temp file
|
| 409 |
+
if os.path.exists(file_path):
|
| 410 |
+
os.remove(file_path)
|
| 411 |
+
|
| 412 |
+
except ValueError as ve:
|
| 413 |
+
logging.warning(f"Client error: {ve}")
|
| 414 |
+
return jsonify({'error': str(ve)}), 400
|
| 415 |
+
except Exception as e:
|
| 416 |
+
logging.error(f"Internal server error: {e}")
|
| 417 |
+
return jsonify({'error': 'Internal server error'}), 500
|
| 418 |
+
|
| 419 |
+
|
| 420 |
@app.route('/health', methods=['GET'])
|
| 421 |
def health_check():
|
| 422 |
"""Health check endpoint."""
|