Neurolingua commited on
Commit
783400a
1 Parent(s): 90c5e6b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -13
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
- with open(filepath, 'wb') as out_file:
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
- response_text = "Please specify if you want to detect a pest or a disease."
 
 
 
 
 
80
  except Exception as e:
81
  print(f"Error processing image: {e}")
82
- response_text = e
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: