|
from flask import Flask, request |
|
from twilio.twiml.messaging_response import MessagingResponse |
|
from twilio.rest import Client |
|
import os |
|
import requests |
|
from PIL import Image |
|
import io |
|
import uuid |
|
import shutil |
|
app = Flask(__name__) |
|
UPLOAD_FOLDER = '/code/uploads' |
|
if not os.path.exists(UPLOAD_FOLDER): |
|
os.makedirs(UPLOAD_FOLDER) |
|
|
|
from inference_sdk import InferenceHTTPClient |
|
import base64 |
|
|
|
def encode_image_to_base64(filepath): |
|
try: |
|
with open(filepath, "rb") as image_file: |
|
encoded_string = base64.b64encode(image_file.read()).decode('utf-8') |
|
print(f"Encoded image length: {len(encoded_string)}") |
|
return encoded_string |
|
except Exception as e: |
|
print(f"Error encoding image: {e}") |
|
return None |
|
|
|
def predict_pest(filepath): |
|
CLIENT = InferenceHTTPClient( |
|
api_url="https://detect.roboflow.com", |
|
api_key="oF1aC4b1FBCDtK8CoKx7" |
|
) |
|
|
|
try: |
|
encoded_image = filepath |
|
result = CLIENT.infer(encoded_image, model_id="pest-detection-ueoco/1") |
|
return result['predicted_classes'][0] |
|
except Exception as e: |
|
print(f"API call error: {e}") |
|
return e |
|
|
|
def predict_disease(filepath): |
|
CLIENT = InferenceHTTPClient( |
|
api_url="https://classify.roboflow.com", |
|
api_key="oF1aC4b1FBCDtK8CoKx7" |
|
) |
|
|
|
try: |
|
encoded_image = filepath |
|
result = CLIENT.infer(encoded_image, model_id="plant-disease-detection-iefbi/1") |
|
return result['predicted_classes'][0] |
|
except Exception as e: |
|
print(f"API call error: {e}") |
|
return e |
|
|
|
|
|
account_sid = os.environ.get('TWILIO_ACCOUNT_SID') |
|
auth_token = os.environ.get('TWILIO_AUTH_TOKEN') |
|
client = Client(account_sid, auth_token) |
|
|
|
from_whatsapp_number = 'whatsapp:+14155238886' |
|
|
|
|
|
def classify_pest(image_path): |
|
|
|
return f"Detected Pest: [Pest Name] for image at {image_path}" |
|
|
|
def classify_disease(image_path): |
|
|
|
return f"Detected Disease: [Disease Name] for image at {image_path}" |
|
@app.route('/whatsapp', methods=['POST']) |
|
def whatsapp_webhook(): |
|
incoming_msg = request.values.get('Body', '').lower() |
|
sender = request.values.get('From') |
|
|
|
|
|
num_media = int(request.values.get('NumMedia', 0)) |
|
|
|
if num_media > 0: |
|
media_url = request.values.get('MediaUrl0') |
|
content_type = request.values.get('MediaContentType0') |
|
|
|
if content_type.startswith('image/'): |
|
r = requests.get(media_url) |
|
r.raise_for_status() |
|
|
|
|
|
filename = f"{uuid.uuid4()}.jpg" |
|
filepath = os.path.join(UPLOAD_FOLDER, filename) |
|
|
|
if 1==1: |
|
with open(filepath, 'wb') as out_file: |
|
out_file.write(r.content) |
|
|
|
|
|
if os.path.getsize(filepath) == 0: |
|
response_text = "The image file is empty. Please send a valid image." |
|
else: |
|
if 'pest' in incoming_msg: |
|
response_text = predict_pest(filepath) |
|
elif 'disease' in incoming_msg: |
|
response_text = predict_disease(filepath) |
|
else: |
|
response_text = "Please specify if you want to detect a pest or a disease." |
|
|
|
else: |
|
response_text = "The attached file is not an image. Please send an image for classification." |
|
elif 'bookkeeping' in incoming_msg: |
|
response_text = "Please provide the details you'd like to record." |
|
else: |
|
response_text = get_agricultural_insights(incoming_msg) |
|
|
|
send_message(sender, response_text) |
|
return '', 204 |
|
|
|
def get_agricultural_insights(query): |
|
|
|
return f"Insights related to: {query}" |
|
|
|
def send_message(to, body): |
|
try: |
|
message = client.messages.create( |
|
from_=from_whatsapp_number, |
|
body=body, |
|
to=to |
|
) |
|
print(f"Message sent with SID: {message.sid}") |
|
except Exception as e: |
|
print(f"Error sending message: {e}") |
|
|
|
|
|
def send_initial_message(to_number): |
|
send_message( |
|
f'whatsapp:{to_number}', |
|
'Welcome to the Agri AI Chatbot! How can I assist you today? You can send an image with "pest" or "disease" to classify it.' |
|
) |
|
|
|
if __name__ == '__main__': |
|
send_initial_message('916382792828') |
|
app.run(host='0.0.0.0', port=7860) |