File size: 4,535 Bytes
052e52f 3f861d9 cbda7a6 3f861d9 72b4474 d135d03 052e52f 77e49e3 cbda7a6 082246f edbd578 85cb515 052e52f f586e58 052e52f 8597bda cbda7a6 9672e77 cbda7a6 08961c8 cf6eaea cbda7a6 36ec663 30cfa3d 9672e77 30cfa3d 9672e77 30cfa3d 9672e77 30cfa3d 9672e77 36ec663 30cfa3d 9672e77 30cfa3d 9672e77 36ec663 9672e77 44d0ab9 cbda7a6 caeda3a 5a879b4 cbda7a6 052e52f 528f451 85cb515 d135d03 cf6eaea 85cb515 052e52f 9672e77 08961c8 052e52f 9672e77 052e52f e9086c4 052e52f c14c319 052e52f 08961c8 7f23710 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 117 118 119 |
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
from other_function import predict_pest,predict_disease,convert_img,generate_response,ConversationBufferMemory,get_weather,get_rates,get_news
app = Flask(__name__)
UPLOAD_FOLDER = '/code/uploads'
if not os.path.exists(UPLOAD_FOLDER):
os.makedirs(UPLOAD_FOLDER)
conversation_memory = ConversationBufferMemory(max_size=6)
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'
@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))
# Get the chat history
chat_history = conversation_memory.get_memory()
if num_media > 0:
media_url = request.values.get('MediaUrl0')
response_text=media_url
content_type = request.values.get('MediaContentType0')
if content_type.startswith('image/'):
filepath = convert_img(media_url, account_sid, auth_token)
try:
disease = predict_disease(filepath)
except:
disease = None
try:
pest = predict_pest(filepath)
except:
pest = None
if disease:
response_text = f"Detected disease: {disease}"
# Generate additional insights about the disease
disease_info = generate_response(f"Provide brief information about {disease} in plants", chat_history)
response_text += f"\n\nAdditional information: {disease_info}"
elif pest:
response_text = f"Detected pest: {pest}"
# Generate additional insights about the pest
pest_info = generate_response(f"Provide brief information about {pest} in agriculture", chat_history)
response_text += f"\n\nAdditional information: {pest_info}"
else:
response_text = "Please upload another image with good quality."
else:
filepath = download_and_save_as_txt(media_url, account_sid, auth_token)
#Rag code is as follows.Provide filepath inplace of pdf..
elif ('weather' in incoming_msg.lower()) or ('climate' in incoming_msg.lower()) or ('temperature' in incoming_msg.lower()):
response_text=get_weather(incoming_msg.lower())
elif 'bookkeeping' in incoming_msg:
response_text = "Please provide the details you'd like to record."
elif ('rates' in incoming_msg.lower()) or ('price' in incoming_msg.lower()) or ('market' in incoming_msg.lower()) or ('rate' in incoming_msg.lower()) or ('prices' in incoming_msg.lower()):
rates=get_rates()
response_text = generate_response(incoming_msg+'data is '+rates, chat_history)
elif ('news' in incoming_msg.lower()) or ('information' in incoming_msg.lower()):
news=get_news()
response_text = generate_response(incoming_msg+'data is '+str(news), chat_history)
else:
# Generate response considering the chat history
response_text = generate_response(incoming_msg, chat_history)
# Add the interaction to memory
conversation_memory.add_to_memory({"user": incoming_msg, "assistant": response_text})
send_message(sender, response_text)
return '', 204
def get_agricultural_insights(query):
return generate_response(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?'
)
if __name__ == '__main__':
send_initial_message('916382792828')
send_initial_message('919080522395')
app.run(host='0.0.0.0', port=7860) |