Neurolingua commited on
Commit
8597bda
1 Parent(s): dd1112c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -14
app.py CHANGED
@@ -32,27 +32,36 @@ def classify_disease(image_path):
32
  def whatsapp_webhook():
33
  incoming_msg = request.values.get('Body', '').lower()
34
  sender = request.values.get('From')
35
-
 
36
  num_media = int(request.values.get('NumMedia', 0))
37
-
38
  if num_media > 0:
39
- media_url = request.values.get('MediaUrl0')
40
  content_type = request.values.get('MediaContentType0')
41
-
42
  if content_type.startswith('image/'):
43
  try:
 
 
 
44
  # Generate a unique filename
45
  filename = f"{uuid.uuid4()}.jpg"
46
  filepath = os.path.join(UPLOAD_FOLDER, filename)
47
-
48
- # Download and save the image using the media URL
49
- response = requests.get(media_url)
50
- image_data = response.content
51
-
52
  with open(filepath, 'wb') as f:
53
- f.write(image_data)
54
-
55
- # Call the appropriate classification function
 
 
 
 
 
 
 
 
56
  if 'pest' in incoming_msg:
57
  response_text = classify_pest(filepath)
58
  elif 'disease' in incoming_msg:
@@ -68,9 +77,9 @@ def whatsapp_webhook():
68
  response_text = "Please provide the details you'd like to record."
69
  else:
70
  response_text = get_agricultural_insights(incoming_msg)
71
-
72
  send_message(sender, response_text)
73
- return '', 204 # Return an empty response to Twilio
74
 
75
  def get_agricultural_insights(query):
76
  # Implement your agricultural insights logic here
 
32
  def whatsapp_webhook():
33
  incoming_msg = request.values.get('Body', '').lower()
34
  sender = request.values.get('From')
35
+
36
+ # Check if an image is attached
37
  num_media = int(request.values.get('NumMedia', 0))
38
+
39
  if num_media > 0:
40
+ media_sid = request.values.get('MediaSid0')
41
  content_type = request.values.get('MediaContentType0')
42
+
43
  if content_type.startswith('image/'):
44
  try:
45
+ # Retrieve the media using Twilio client
46
+ media = client.messages.media(request.values.get('MessageSid')).list()[0]
47
+
48
  # Generate a unique filename
49
  filename = f"{uuid.uuid4()}.jpg"
50
  filepath = os.path.join(UPLOAD_FOLDER, filename)
51
+
52
+ # Download and save the image
 
 
 
53
  with open(filepath, 'wb') as f:
54
+ f.write(media.fetch())
55
+
56
+ # Check downloaded image format and size (optional)
57
+ print(f"Downloaded image: {filepath}, size: {os.path.getsize(filepath)} bytes")
58
+ try:
59
+ Image.open(filepath) # Basic check if image can be opened
60
+ except Exception as e:
61
+ print(f"Error opening downloaded image: {e}")
62
+ response_text = "Failed to process the image. Please try again."
63
+ raise # Re-raise the exception for further handling
64
+
65
  if 'pest' in incoming_msg:
66
  response_text = classify_pest(filepath)
67
  elif 'disease' in incoming_msg:
 
77
  response_text = "Please provide the details you'd like to record."
78
  else:
79
  response_text = get_agricultural_insights(incoming_msg)
80
+
81
  send_message(sender, response_text)
82
+ return '', 204
83
 
84
  def get_agricultural_insights(query):
85
  # Implement your agricultural insights logic here