Neurolingua commited on
Commit
a4f745d
1 Parent(s): e60f1a2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -14
app.py CHANGED
@@ -15,15 +15,7 @@ if not os.path.exists(UPLOAD_FOLDER):
15
  from inference_sdk import InferenceHTTPClient
16
  import base64
17
 
18
- def encode_image_to_base64(filepath):
19
- try:
20
- with open(filepath, "rb") as image_file:
21
- encoded_string = base64.b64encode(image_file.read()).decode('utf-8')
22
- print(f"Encoded image length: {len(encoded_string)}") # Debug: Check length
23
- return encoded_string
24
- except Exception as e:
25
- print(f"Error encoding image: {e}")
26
- return None
27
 
28
  def predict_pest(filepath):
29
  CLIENT = InferenceHTTPClient(
@@ -32,7 +24,7 @@ def predict_pest(filepath):
32
  )
33
 
34
  try:
35
- encoded_image = filepath
36
  result = CLIENT.infer(encoded_image, model_id="pest-detection-ueoco/1")
37
  return result['predicted_classes'][0]
38
  except Exception as e:
@@ -46,7 +38,7 @@ def predict_disease(filepath):
46
  )
47
 
48
  try:
49
- encoded_image = filepath
50
  result = CLIENT.infer(encoded_image, model_id="plant-disease-detection-iefbi/1")
51
  return result['predicted_classes'][0]
52
  except Exception as e:
@@ -60,7 +52,6 @@ client = Client(account_sid, auth_token)
60
  # WhatsApp number to send messages from (your Twilio number)
61
  from_whatsapp_number = 'whatsapp:+14155238886'
62
 
63
- # Placeholder functions for image classification
64
  def classify_pest(image_path):
65
  # Implement pest classification model here
66
  return f"Detected Pest: [Pest Name] for image at {image_path}"
@@ -83,8 +74,9 @@ def whatsapp_webhook():
83
  if content_type.startswith('image/'):
84
  r = requests.get(media_url)
85
  r.raise_for_status()
 
86
 
87
- # Generate a unique filename
88
  filename = f"{uuid.uuid4()}.jpg"
89
  filepath = os.path.join(UPLOAD_FOLDER, filename)
90
 
@@ -101,7 +93,7 @@ def whatsapp_webhook():
101
  elif 'disease' in incoming_msg:
102
  response_text = predict_disease(filepath)
103
  else:
104
- response_text = "Please specify if you want to detect a pest or a disease."
105
 
106
  else:
107
  response_text = "The attached file is not an image. Please send an image for classification."
 
15
  from inference_sdk import InferenceHTTPClient
16
  import base64
17
 
18
+
 
 
 
 
 
 
 
 
19
 
20
  def predict_pest(filepath):
21
  CLIENT = InferenceHTTPClient(
 
24
  )
25
 
26
  try:
27
+ encoded_image = encode_image_to_base64(filepath)
28
  result = CLIENT.infer(encoded_image, model_id="pest-detection-ueoco/1")
29
  return result['predicted_classes'][0]
30
  except Exception as e:
 
38
  )
39
 
40
  try:
41
+ encoded_image = encode_image_to_base64(filepath)
42
  result = CLIENT.infer(encoded_image, model_id="plant-disease-detection-iefbi/1")
43
  return result['predicted_classes'][0]
44
  except Exception as e:
 
52
  # WhatsApp number to send messages from (your Twilio number)
53
  from_whatsapp_number = 'whatsapp:+14155238886'
54
 
 
55
  def classify_pest(image_path):
56
  # Implement pest classification model here
57
  return f"Detected Pest: [Pest Name] for image at {image_path}"
 
74
  if content_type.startswith('image/'):
75
  r = requests.get(media_url)
76
  r.raise_for_status()
77
+ response_text=media_url
78
 
79
+ '''# Generate a unique filename
80
  filename = f"{uuid.uuid4()}.jpg"
81
  filepath = os.path.join(UPLOAD_FOLDER, filename)
82
 
 
93
  elif 'disease' in incoming_msg:
94
  response_text = predict_disease(filepath)
95
  else:
96
+ response_text = "Please specify if you want to detect a pest or a disease."'''
97
 
98
  else:
99
  response_text = "The attached file is not an image. Please send an image for classification."