Spaces:
Sleeping
Sleeping
Neurolingua
commited on
Commit
•
dd1112c
1
Parent(s):
d27eb97
Update app.py
Browse files
app.py
CHANGED
@@ -28,32 +28,31 @@ def classify_pest(image_path):
|
|
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'])
|
33 |
def whatsapp_webhook():
|
34 |
incoming_msg = request.values.get('Body', '').lower()
|
35 |
sender = request.values.get('From')
|
36 |
|
37 |
-
# Check if an image is attached
|
38 |
num_media = int(request.values.get('NumMedia', 0))
|
39 |
|
40 |
if num_media > 0:
|
41 |
-
|
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(
|
56 |
|
|
|
57 |
if 'pest' in incoming_msg:
|
58 |
response_text = classify_pest(filepath)
|
59 |
elif 'disease' in incoming_msg:
|
|
|
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 |
@app.route('/whatsapp', methods=['POST'])
|
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:
|