Neurolingua commited on
Commit
3f861d9
1 Parent(s): e615e06

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -12
app.py CHANGED
@@ -1,10 +1,11 @@
1
- import requests
2
  from flask import Flask, request
3
  from twilio.twiml.messaging_response import MessagingResponse
4
  from twilio.rest import Client
5
  import os
 
6
  from PIL import Image
7
  import io
 
8
 
9
  app = Flask(__name__)
10
  UPLOAD_FOLDER = 'uploads'
@@ -20,15 +21,15 @@ client = Client(account_sid, auth_token)
20
  from_whatsapp_number = 'whatsapp:+14155238886'
21
 
22
  # Placeholder functions for image classification
23
- def classify_pest(image):
24
  # Implement pest classification model here
25
- return "Detected Pest: [Pest Name]"
 
26
 
27
- def classify_disease(image):
28
  # Implement disease classification model here
29
- return "Detected Disease: [Disease Name]"
30
-
31
-
32
 
33
  @app.route('/whatsapp', methods=['POST'])
34
  def whatsapp_webhook():
@@ -43,15 +44,21 @@ def whatsapp_webhook():
43
  content_type = request.values.get('MediaContentType0')
44
 
45
  if content_type.startswith('image/'):
46
- # Download and process the image
47
  response = requests.get(media_url)
48
  if response.status_code == 200:
49
- image = Image.open(io.BytesIO(response.content))
 
 
 
 
 
 
50
 
51
  if 'pest' in incoming_msg:
52
- response_text = classify_pest(image)
53
  elif 'disease' in incoming_msg:
54
- response_text = classify_disease(image)
55
  else:
56
  response_text = "Please specify if you want to detect a pest or a disease."
57
  else:
@@ -64,7 +71,7 @@ def whatsapp_webhook():
64
  response_text = get_agricultural_insights(incoming_msg)
65
 
66
  send_message(sender, response_text)
67
- return '', 204
68
 
69
  def get_agricultural_insights(query):
70
  # Implement your agricultural insights logic here
 
 
1
  from flask import Flask, request
2
  from twilio.twiml.messaging_response import MessagingResponse
3
  from twilio.rest import Client
4
  import os
5
+ import requests
6
  from PIL import Image
7
  import io
8
+ import uuid
9
 
10
  app = Flask(__name__)
11
  UPLOAD_FOLDER = 'uploads'
 
21
  from_whatsapp_number = 'whatsapp:+14155238886'
22
 
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'])
35
  def whatsapp_webhook():
 
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)
60
  elif 'disease' in incoming_msg:
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:
 
71
  response_text = get_agricultural_insights(incoming_msg)
72
 
73
  send_message(sender, response_text)
74
+ return '', 204 # Return an empty response to Twilio
75
 
76
  def get_agricultural_insights(query):
77
  # Implement your agricultural insights logic here