Neurolingua commited on
Commit
9672e77
1 Parent(s): 30cfa3d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -21
app.py CHANGED
@@ -7,12 +7,13 @@ from PIL import Image
7
  import io
8
  import uuid
9
  import shutil
10
- from other_function import predict_pest,predict_disease,convert_img,generate_response
11
  app = Flask(__name__)
12
  UPLOAD_FOLDER = '/code/uploads'
13
  if not os.path.exists(UPLOAD_FOLDER):
14
  os.makedirs(UPLOAD_FOLDER)
15
 
 
16
 
17
  # Initialize the Flask app
18
  account_sid = os.environ.get('TWILIO_ACCOUNT_SID')
@@ -21,53 +22,57 @@ client = Client(account_sid, auth_token)
21
  # WhatsApp number to send messages from (your Twilio number)
22
  from_whatsapp_number = 'whatsapp:+14155238886'
23
 
 
24
  @app.route('/whatsapp', methods=['POST'])
25
  def whatsapp_webhook():
26
  incoming_msg = request.values.get('Body', '').lower()
27
  sender = request.values.get('From')
28
-
29
  # Check if an image is attached
30
  num_media = int(request.values.get('NumMedia', 0))
31
-
 
 
 
32
  if num_media > 0:
33
  media_url = request.values.get('MediaUrl0')
34
  content_type = request.values.get('MediaContentType0')
35
-
36
  if content_type.startswith('image/'):
37
-
38
-
39
-
40
-
41
  filepath = convert_img(media_url, account_sid, auth_token)
42
-
43
  try:
44
- disease =predict_disease(filepath)
45
  except:
46
- disease=None
47
-
48
  try:
49
- pest=predict_pest(filepath)
50
  except:
51
- pest=None
52
 
53
  if disease:
54
-
55
- response_text = disease
 
 
56
  elif pest:
57
- response_text=pest
 
 
 
58
  else:
59
- response_text = "Please upload other image with good quality."
60
 
61
  else:
62
  response_text = "The attached file is not an image. Please send an image for classification."
63
  elif 'bookkeeping' in incoming_msg:
64
  response_text = "Please provide the details you'd like to record."
65
  else:
66
- response_text = get_agricultural_insights(incoming_msg)
 
 
 
 
67
 
68
  send_message(sender, response_text)
69
- return '', 204 # Return an empty response to Twilio
70
-
71
  def get_agricultural_insights(query):
72
  return generate_response(query)
73
 
 
7
  import io
8
  import uuid
9
  import shutil
10
+ from other_function import predict_pest,predict_disease,convert_img,generate_response,ConversationBufferMemory
11
  app = Flask(__name__)
12
  UPLOAD_FOLDER = '/code/uploads'
13
  if not os.path.exists(UPLOAD_FOLDER):
14
  os.makedirs(UPLOAD_FOLDER)
15
 
16
+ conversation_memory = ConversationBufferMemory()
17
 
18
  # Initialize the Flask app
19
  account_sid = os.environ.get('TWILIO_ACCOUNT_SID')
 
22
  # WhatsApp number to send messages from (your Twilio number)
23
  from_whatsapp_number = 'whatsapp:+14155238886'
24
 
25
+ @app.route('/whatsapp', methods=['POST'])
26
  @app.route('/whatsapp', methods=['POST'])
27
  def whatsapp_webhook():
28
  incoming_msg = request.values.get('Body', '').lower()
29
  sender = request.values.get('From')
 
30
  # Check if an image is attached
31
  num_media = int(request.values.get('NumMedia', 0))
32
+
33
+ # Get the chat history
34
+ chat_history = conversation_memory.get_memory()
35
+
36
  if num_media > 0:
37
  media_url = request.values.get('MediaUrl0')
38
  content_type = request.values.get('MediaContentType0')
 
39
  if content_type.startswith('image/'):
 
 
 
 
40
  filepath = convert_img(media_url, account_sid, auth_token)
 
41
  try:
42
+ disease = predict_disease(filepath)
43
  except:
44
+ disease = None
 
45
  try:
46
+ pest = predict_pest(filepath)
47
  except:
48
+ pest = None
49
 
50
  if disease:
51
+ response_text = f"Detected disease: {disease}"
52
+ # Generate additional insights about the disease
53
+ disease_info = generate_response(f"Provide brief information about {disease} in plants", chat_history)
54
+ response_text += f"\n\nAdditional information: {disease_info}"
55
  elif pest:
56
+ response_text = f"Detected pest: {pest}"
57
+ # Generate additional insights about the pest
58
+ pest_info = generate_response(f"Provide brief information about {pest} in agriculture", chat_history)
59
+ response_text += f"\n\nAdditional information: {pest_info}"
60
  else:
61
+ response_text = "Please upload another image with good quality."
62
 
63
  else:
64
  response_text = "The attached file is not an image. Please send an image for classification."
65
  elif 'bookkeeping' in incoming_msg:
66
  response_text = "Please provide the details you'd like to record."
67
  else:
68
+ # Generate response considering the chat history
69
+ response_text = generate_response(incoming_msg, chat_history)
70
+
71
+ # Add the interaction to memory
72
+ conversation_memory.add_to_memory({"user": incoming_msg, "assistant": response_text})
73
 
74
  send_message(sender, response_text)
75
+ return '', 204
 
76
  def get_agricultural_insights(query):
77
  return generate_response(query)
78