Neurolingua commited on
Commit
d27eb97
1 Parent(s): 403fe3d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -10
app.py CHANGED
@@ -23,12 +23,10 @@ from_whatsapp_number = 'whatsapp:+14155238886'
23
  # Placeholder functions for image classification
24
  def classify_pest(image_path):
25
  # Implement pest classification model here
26
- # This function should now accept a file path instead of an image object
27
  return f"Detected Pest: [Pest Name] for image at {image_path}"
28
 
29
  def classify_disease(image_path):
30
  # Implement disease classification model here
31
- # This function should now accept a file path instead of an image object
32
  return f"Detected Disease: [Disease Name] for image at {image_path}"
33
 
34
  @app.route('/whatsapp', methods=['POST'])
@@ -40,20 +38,21 @@ def whatsapp_webhook():
40
  num_media = int(request.values.get('NumMedia', 0))
41
 
42
  if num_media > 0:
43
- media_url = request.values.get('MediaUrl0')
44
  content_type = request.values.get('MediaContentType0')
45
 
46
  if content_type.startswith('image/'):
47
- # Download and save the image
48
- response = requests.get(media_url)
49
- if response.status_code == 200:
 
50
  # Generate a unique filename
51
  filename = f"{uuid.uuid4()}.jpg"
52
  filepath = os.path.join(UPLOAD_FOLDER, filename)
53
 
54
- # Save the image
55
  with open(filepath, 'wb') as f:
56
- f.write(response.content)
57
 
58
  if 'pest' in incoming_msg:
59
  response_text = classify_pest(filepath)
@@ -61,8 +60,9 @@ def whatsapp_webhook():
61
  response_text = classify_disease(filepath)
62
  else:
63
  response_text = "Please specify if you want to detect a pest or a disease."
64
- else:
65
- response_text = "Failed to download the image. Please try again."
 
66
  else:
67
  response_text = "The attached file is not an image. Please send an image for classification."
68
  elif 'bookkeeping' in incoming_msg:
 
23
  # Placeholder functions for image classification
24
  def classify_pest(image_path):
25
  # Implement pest classification model here
 
26
  return f"Detected Pest: [Pest Name] for image at {image_path}"
27
 
28
  def classify_disease(image_path):
29
  # Implement disease classification model here
 
30
  return f"Detected Disease: [Disease Name] for image at {image_path}"
31
 
32
  @app.route('/whatsapp', methods=['POST'])
 
38
  num_media = int(request.values.get('NumMedia', 0))
39
 
40
  if num_media > 0:
41
+ media_sid = request.values.get('MediaSid0')
42
  content_type = request.values.get('MediaContentType0')
43
 
44
  if content_type.startswith('image/'):
45
+ try:
46
+ # Retrieve the media using Twilio client
47
+ media = client.messages.media(request.values.get('MessageSid')).list()[0]
48
+
49
  # Generate a unique filename
50
  filename = f"{uuid.uuid4()}.jpg"
51
  filepath = os.path.join(UPLOAD_FOLDER, filename)
52
 
53
+ # Download and save the image
54
  with open(filepath, 'wb') as f:
55
+ f.write(media.fetch())
56
 
57
  if 'pest' in incoming_msg:
58
  response_text = classify_pest(filepath)
 
60
  response_text = classify_disease(filepath)
61
  else:
62
  response_text = "Please specify if you want to detect a pest or a disease."
63
+ except Exception as e:
64
+ print(f"Error processing image: {e}")
65
+ response_text = "Failed to process the image. Please try again."
66
  else:
67
  response_text = "The attached file is not an image. Please send an image for classification."
68
  elif 'bookkeeping' in incoming_msg: