Spaces:
Sleeping
Sleeping
File size: 4,148 Bytes
052e52f 3f861d9 cbda7a6 3f861d9 72b4474 052e52f cbda7a6 082246f 052e52f cbda7a6 052e52f cbda7a6 3f861d9 cbda7a6 3f861d9 cbda7a6 3f861d9 cbda7a6 3f861d9 052e52f f586e58 052e52f 8597bda cbda7a6 8597bda cbda7a6 08961c8 cbda7a6 8597bda cbda7a6 c67a732 196f53b 72b4474 d27eb97 08961c8 196f53b 08961c8 7241f1f 08961c8 082246f e615e06 d27eb97 082246f cbda7a6 052e52f 08961c8 052e52f 08961c8 052e52f cbda7a6 08961c8 052e52f cbda7a6 052e52f 08961c8 90742c7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
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 = 'uploads'
if not os.path.exists(UPLOAD_FOLDER):
os.makedirs(UPLOAD_FOLDER)
from inference_sdk import InferenceHTTPClient
def predict_disease(filepath):
CLIENT = InferenceHTTPClient(
api_url="https://classify.roboflow.com",
api_key="oF1aC4b1FBCDtK8CoKx7"
)
result = CLIENT.infer(filepath, model_id="plant-disease-detection-iefbi/1")
return result['predicted_classes'][0]
def predict_pest(filepath):
CLIENT = InferenceHTTPClient(
api_url="https://detect.roboflow.com",
api_key="oF1aC4b1FBCDtK8CoKx7")
result = CLIENT.infer(filepath, model_id="pest-detection-ueoco/1")
return result['predicted_classes'][0]
# Initialize the Flask app
account_sid = os.environ.get('TWILIO_ACCOUNT_SID')
auth_token = os.environ.get('TWILIO_AUTH_TOKEN')
client = Client(account_sid, auth_token)
# WhatsApp number to send messages from (your Twilio number)
from_whatsapp_number = 'whatsapp:+14155238886'
# Placeholder functions for image classification
def classify_pest(image_path):
# Implement pest classification model here
return f"Detected Pest: [Pest Name] for image at {image_path}"
def classify_disease(image_path):
# Implement disease classification model here
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')
# Check if an image is attached
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)
# Generate a unique filename
filename = f"{uuid.uuid4()}.jpg"
filepath = os.path.join(UPLOAD_FOLDER, filename)
with open(filepath, 'wb') as out_file:
shutil.copyfileobj(r.raw, out_file)
try:
if ('pest' in incoming_msg) or ('Pest' in incoming_msg):
response_text = predict_pest(filepath)
elif ('disease' in incoming_msg) or ('Disease' in incoming_msg):
response_text = predict_disease(filepath)
else:
response_text = "Please specify if you want to detect a pest or a disease."
except Exception as e:
print(f"Error processing image: {e}")
response_text = 'Invalid image'
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 # Return an empty response to Twilio
def get_agricultural_insights(query):
# Implement your agricultural insights logic here
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}")
# Function to send an initial message
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) |