Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -58,28 +58,29 @@ def whatsapp_webhook():
|
|
| 58 |
content_type = request.values.get('MediaContentType0')
|
| 59 |
|
| 60 |
if content_type.startswith('image/'):
|
| 61 |
-
r = requests.get(media_url)
|
| 62 |
|
| 63 |
# Generate a unique filename
|
| 64 |
filename = f"{uuid.uuid4()}.jpg"
|
| 65 |
filepath = os.path.join(UPLOAD_FOLDER, filename)
|
| 66 |
-
|
| 67 |
-
shutil.copyfileobj(r.raw, out_file)
|
| 68 |
try:
|
|
|
|
|
|
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
if ('pest' in incoming_msg) or ('Pest' in incoming_msg):
|
| 75 |
-
response_text = predict_pest(filepath)
|
| 76 |
-
elif ('disease' in incoming_msg) or ('Disease' in incoming_msg):
|
| 77 |
-
response_text = predict_disease(filepath)
|
| 78 |
else:
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
except Exception as e:
|
| 81 |
print(f"Error processing image: {e}")
|
| 82 |
-
response_text =
|
| 83 |
else:
|
| 84 |
response_text = "The attached file is not an image. Please send an image for classification."
|
| 85 |
elif 'bookkeeping' in incoming_msg:
|
|
|
|
| 58 |
content_type = request.values.get('MediaContentType0')
|
| 59 |
|
| 60 |
if content_type.startswith('image/'):
|
| 61 |
+
r = requests.get(media_url, stream=True)
|
| 62 |
|
| 63 |
# Generate a unique filename
|
| 64 |
filename = f"{uuid.uuid4()}.jpg"
|
| 65 |
filepath = os.path.join(UPLOAD_FOLDER, filename)
|
| 66 |
+
|
|
|
|
| 67 |
try:
|
| 68 |
+
with open(filepath, 'wb') as out_file:
|
| 69 |
+
shutil.copyfileobj(r.raw, out_file)
|
| 70 |
|
| 71 |
+
# Check file size and existence
|
| 72 |
+
if os.path.getsize(filepath) == 0:
|
| 73 |
+
response_text = "The image file is empty. Please send a valid image."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
else:
|
| 75 |
+
if 'pest' in incoming_msg:
|
| 76 |
+
response_text = predict_pest(filepath)
|
| 77 |
+
elif 'disease' in incoming_msg:
|
| 78 |
+
response_text = predict_disease(filepath)
|
| 79 |
+
else:
|
| 80 |
+
response_text = "Please specify if you want to detect a pest or a disease."
|
| 81 |
except Exception as e:
|
| 82 |
print(f"Error processing image: {e}")
|
| 83 |
+
response_text = 'Error processing image.'
|
| 84 |
else:
|
| 85 |
response_text = "The attached file is not an image. Please send an image for classification."
|
| 86 |
elif 'bookkeeping' in incoming_msg:
|