Neurolingua commited on
Commit
08961c8
1 Parent(s): 8597bda

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -24
app.py CHANGED
@@ -16,7 +16,6 @@ if not os.path.exists(UPLOAD_FOLDER):
16
  account_sid = os.environ.get('TWILIO_ACCOUNT_SID')
17
  auth_token = os.environ.get('TWILIO_AUTH_TOKEN')
18
  client = Client(account_sid, auth_token)
19
-
20
  # WhatsApp number to send messages from (your Twilio number)
21
  from_whatsapp_number = 'whatsapp:+14155238886'
22
 
@@ -37,31 +36,22 @@ def whatsapp_webhook():
37
  num_media = int(request.values.get('NumMedia', 0))
38
 
39
  if num_media > 0:
40
- media_sid = request.values.get('MediaSid0')
41
  content_type = request.values.get('MediaContentType0')
42
 
43
  if content_type.startswith('image/'):
44
  try:
45
- # Retrieve the media using Twilio client
46
- media = client.messages.media(request.values.get('MessageSid')).list()[0]
47
-
48
  # Generate a unique filename
49
  filename = f"{uuid.uuid4()}.jpg"
50
  filepath = os.path.join(UPLOAD_FOLDER, filename)
51
-
52
- # Download and save the image
53
  with open(filepath, 'wb') as f:
54
- f.write(media.fetch())
55
-
56
- # Check downloaded image format and size (optional)
57
- print(f"Downloaded image: {filepath}, size: {os.path.getsize(filepath)} bytes")
58
- try:
59
- Image.open(filepath) # Basic check if image can be opened
60
- except Exception as e:
61
- print(f"Error opening downloaded image: {e}")
62
- response_text = "Failed to process the image. Please try again."
63
- raise # Re-raise the exception for further handling
64
-
65
  if 'pest' in incoming_msg:
66
  response_text = classify_pest(filepath)
67
  elif 'disease' in incoming_msg:
@@ -77,13 +67,13 @@ def whatsapp_webhook():
77
  response_text = "Please provide the details you'd like to record."
78
  else:
79
  response_text = get_agricultural_insights(incoming_msg)
80
-
81
  send_message(sender, response_text)
82
- return '', 204
83
 
84
  def get_agricultural_insights(query):
85
  # Implement your agricultural insights logic here
86
- return query
87
 
88
  def send_message(to, body):
89
  try:
@@ -104,7 +94,5 @@ def send_initial_message(to_number):
104
  )
105
 
106
  if __name__ == '__main__':
107
- # Send an initial message when the script starts
108
- send_initial_message('916382792828') # Replace with the recipient's number
109
- # Start the Flask server
110
  app.run(host='0.0.0.0', port=7860)
 
16
  account_sid = os.environ.get('TWILIO_ACCOUNT_SID')
17
  auth_token = os.environ.get('TWILIO_AUTH_TOKEN')
18
  client = Client(account_sid, auth_token)
 
19
  # WhatsApp number to send messages from (your Twilio number)
20
  from_whatsapp_number = 'whatsapp:+14155238886'
21
 
 
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
  try:
44
+ # Download the image
45
+ r = requests.get(media_url)
46
+
47
  # Generate a unique filename
48
  filename = f"{uuid.uuid4()}.jpg"
49
  filepath = os.path.join(UPLOAD_FOLDER, filename)
50
+
51
+ # Save the image
52
  with open(filepath, 'wb') as f:
53
+ f.write(r.content)
54
+
 
 
 
 
 
 
 
 
 
55
  if 'pest' in incoming_msg:
56
  response_text = classify_pest(filepath)
57
  elif 'disease' in incoming_msg:
 
67
  response_text = "Please provide the details you'd like to record."
68
  else:
69
  response_text = get_agricultural_insights(incoming_msg)
70
+
71
  send_message(sender, response_text)
72
+ return '', 204 # Return an empty response to Twilio
73
 
74
  def get_agricultural_insights(query):
75
  # Implement your agricultural insights logic here
76
+ return f"Insights related to: {query}"
77
 
78
  def send_message(to, body):
79
  try:
 
94
  )
95
 
96
  if __name__ == '__main__':
97
+ send_initial_message('916382792828')
 
 
98
  app.run(host='0.0.0.0', port=7860)