Spaces:
Sleeping
Sleeping
Neurolingua
commited on
Commit
•
cbda7a6
1
Parent(s):
50c5b5a
Update app.py
Browse files
app.py
CHANGED
@@ -2,26 +2,57 @@ from flask import Flask, request
|
|
2 |
from twilio.twiml.messaging_response import MessagingResponse
|
3 |
from twilio.rest import Client
|
4 |
import os
|
|
|
|
|
5 |
|
6 |
-
# Initialize the Flask app
|
7 |
app = Flask(__name__)
|
|
|
|
|
|
|
8 |
|
|
|
9 |
account_sid = os.environ.get('TWILIO_ACCOUNT_SID')
|
10 |
auth_token = os.environ.get('TWILIO_AUTH_TOKEN')
|
11 |
client = Client(account_sid, auth_token)
|
12 |
|
13 |
-
# OpenAI API Key
|
14 |
-
|
15 |
# WhatsApp number to send messages from (your Twilio number)
|
16 |
from_whatsapp_number = 'whatsapp:+14155238886'
|
17 |
|
18 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
@app.route('/whatsapp', methods=['POST'])
|
20 |
def whatsapp_webhook():
|
21 |
incoming_msg = request.values.get('Body', '').lower()
|
22 |
sender = request.values.get('From')
|
23 |
|
24 |
-
if
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
response_text = "Please provide the details you'd like to record."
|
26 |
else:
|
27 |
response_text = get_agricultural_insights(incoming_msg)
|
@@ -30,8 +61,8 @@ def whatsapp_webhook():
|
|
30 |
return '', 204 # Return an empty response to Twilio
|
31 |
|
32 |
def get_agricultural_insights(query):
|
33 |
-
|
34 |
-
|
35 |
|
36 |
def send_message(to, body):
|
37 |
try:
|
@@ -48,12 +79,11 @@ def send_message(to, body):
|
|
48 |
def send_initial_message(to_number):
|
49 |
send_message(
|
50 |
f'whatsapp:{to_number}',
|
51 |
-
'Welcome to the Agri AI Chatbot! How can I assist you today?'
|
52 |
)
|
53 |
|
54 |
if __name__ == '__main__':
|
55 |
# Send an initial message when the script starts
|
56 |
send_initial_message('916382792828') # Replace with the recipient's number
|
57 |
-
|
58 |
# Start the Flask server
|
59 |
app.run(host='0.0.0.0', port=7860)
|
|
|
2 |
from twilio.twiml.messaging_response import MessagingResponse
|
3 |
from twilio.rest import Client
|
4 |
import os
|
5 |
+
from PIL import Image
|
6 |
+
import io
|
7 |
|
|
|
8 |
app = Flask(__name__)
|
9 |
+
UPLOAD_FOLDER = 'uploads'
|
10 |
+
if not os.path.exists(UPLOAD_FOLDER):
|
11 |
+
os.makedirs(UPLOAD_FOLDER)
|
12 |
|
13 |
+
# Initialize the Flask app
|
14 |
account_sid = os.environ.get('TWILIO_ACCOUNT_SID')
|
15 |
auth_token = os.environ.get('TWILIO_AUTH_TOKEN')
|
16 |
client = Client(account_sid, auth_token)
|
17 |
|
|
|
|
|
18 |
# WhatsApp number to send messages from (your Twilio number)
|
19 |
from_whatsapp_number = 'whatsapp:+14155238886'
|
20 |
|
21 |
+
# Placeholder functions for image classification
|
22 |
+
def classify_pest(image):
|
23 |
+
# Implement pest classification model here
|
24 |
+
return "Detected Pest: [Pest Name]"
|
25 |
+
|
26 |
+
def classify_disease(image):
|
27 |
+
# Implement disease classification model here
|
28 |
+
return "Detected Disease: [Disease Name]"
|
29 |
+
|
30 |
@app.route('/whatsapp', methods=['POST'])
|
31 |
def whatsapp_webhook():
|
32 |
incoming_msg = request.values.get('Body', '').lower()
|
33 |
sender = request.values.get('From')
|
34 |
|
35 |
+
# Check if an image is attached
|
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 |
+
# Download and process the image
|
44 |
+
image_data = client.request(media_url)
|
45 |
+
image = Image.open(io.BytesIO(image_data.content))
|
46 |
+
|
47 |
+
if 'pest' in incoming_msg:
|
48 |
+
response_text = classify_pest(image)
|
49 |
+
elif 'disease' in incoming_msg:
|
50 |
+
response_text = classify_disease(image)
|
51 |
+
else:
|
52 |
+
response_text = "Please specify if you want to detect a pest or a disease."
|
53 |
+
else:
|
54 |
+
response_text = "The attached file is not an image. Please send an image for classification."
|
55 |
+
elif 'bookkeeping' in incoming_msg:
|
56 |
response_text = "Please provide the details you'd like to record."
|
57 |
else:
|
58 |
response_text = get_agricultural_insights(incoming_msg)
|
|
|
61 |
return '', 204 # Return an empty response to Twilio
|
62 |
|
63 |
def get_agricultural_insights(query):
|
64 |
+
# Implement your agricultural insights logic here
|
65 |
+
return query
|
66 |
|
67 |
def send_message(to, body):
|
68 |
try:
|
|
|
79 |
def send_initial_message(to_number):
|
80 |
send_message(
|
81 |
f'whatsapp:{to_number}',
|
82 |
+
'Welcome to the Agri AI Chatbot! How can I assist you today? You can send an image with "pest" or "disease" to classify it.'
|
83 |
)
|
84 |
|
85 |
if __name__ == '__main__':
|
86 |
# Send an initial message when the script starts
|
87 |
send_initial_message('916382792828') # Replace with the recipient's number
|
|
|
88 |
# Start the Flask server
|
89 |
app.run(host='0.0.0.0', port=7860)
|