Update main.py
Browse files
main.py
CHANGED
|
@@ -378,30 +378,19 @@ def process_image():
|
|
| 378 |
try:
|
| 379 |
model = configure_gemini(api_key)
|
| 380 |
|
| 381 |
-
|
| 382 |
-
logging.info(f"Uploading image file: {file.filename}")
|
| 383 |
-
uploaded_file = genai.upload_file(file_path)
|
| 384 |
|
| 385 |
-
#
|
| 386 |
-
|
| 387 |
-
|
| 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([
|
| 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:
|
|
|
|
| 378 |
try:
|
| 379 |
model = configure_gemini(api_key)
|
| 380 |
|
| 381 |
+
logging.info(f"Processing image file: {file.filename}")
|
|
|
|
|
|
|
| 382 |
|
| 383 |
+
# Read image file as bytes
|
| 384 |
+
import PIL.Image
|
| 385 |
+
img = PIL.Image.open(file_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 386 |
|
| 387 |
# Generate content with the image and prompt
|
| 388 |
+
response = model.generate_content([PROMPT, img])
|
| 389 |
|
| 390 |
# Parse the response
|
| 391 |
result = extract_json_from_response(response.text)
|
| 392 |
transactions = result.get('transactions', [])
|
| 393 |
|
|
|
|
|
|
|
|
|
|
| 394 |
return jsonify({'transactions': transactions})
|
| 395 |
|
| 396 |
finally:
|