Neurolingua commited on
Commit
a8f0234
1 Parent(s): a812aa7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -28
app.py CHANGED
@@ -325,34 +325,16 @@ def whatsapp_webhook():
325
 
326
  if num_media > 0:
327
  media_url = request.values.get('MediaUrl0')
328
- response_text = media_url
329
  content_type = request.values.get('MediaContentType0')
 
330
  if content_type.startswith('image/'):
 
331
  filepath = convert_img(media_url, account_sid, auth_token)
332
- try:
333
- disease = predict_disease(filepath)
334
- except:
335
- disease = None
336
- try:
337
- pest = predict_pest(filepath)
338
- except:
339
- pest = None
340
-
341
- if disease:
342
- response_text = f"Detected disease: {disease}"
343
- disease_info = generate_response(f"Provide brief information about {disease} in plants", chat_history)
344
- response_text += f"\n\nAdditional information: {disease_info}"
345
- elif pest:
346
- response_text = f"Detected pest: {pest}"
347
- pest_info = generate_response(f"Provide brief information about {pest} in agriculture", chat_history)
348
- response_text += f"\n\nAdditional information: {pest_info}"
349
- else:
350
- response_text = "Please upload another image with good quality."
351
-
352
  else:
 
353
  filepath = download_and_save_as_txt(media_url, account_sid, auth_token)
354
- response_text = query_rag(filepath)
355
-
356
  elif ('weather' in incoming_msg.lower()) or ('climate' in incoming_msg.lower()) or (
357
  'temperature' in incoming_msg.lower()):
358
  response_text = get_weather(incoming_msg.lower())
@@ -366,16 +348,40 @@ def whatsapp_webhook():
366
  news = get_news()
367
  response_text = generate_response(incoming_msg + ' data is ' + str(news), chat_history)
368
  else:
369
- # Check if the query is related to a PDF document
370
- if 'from pdf' in incoming_msg or 'in pdf' in incoming_msg:
371
- response_text = query_rag(incoming_msg)
372
- else:
373
- response_text = generate_response(incoming_msg, chat_history)
374
 
375
  conversation_memory.add_to_memory({"user": incoming_msg, "assistant": response_text})
376
  send_message(sender, response_text)
377
  return '', 204
378
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
379
 
380
  def send_message(to, body):
381
  try:
 
325
 
326
  if num_media > 0:
327
  media_url = request.values.get('MediaUrl0')
 
328
  content_type = request.values.get('MediaContentType0')
329
+
330
  if content_type.startswith('image/'):
331
+ # Handle image processing (disease/pest detection)
332
  filepath = convert_img(media_url, account_sid, auth_token)
333
+ response_text = handle_image(filepath)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
334
  else:
335
+ # Handle PDF processing
336
  filepath = download_and_save_as_txt(media_url, account_sid, auth_token)
337
+ response_text = process_and_query_pdf(filepath)
 
338
  elif ('weather' in incoming_msg.lower()) or ('climate' in incoming_msg.lower()) or (
339
  'temperature' in incoming_msg.lower()):
340
  response_text = get_weather(incoming_msg.lower())
 
348
  news = get_news()
349
  response_text = generate_response(incoming_msg + ' data is ' + str(news), chat_history)
350
  else:
351
+ response_text = query_rag(incoming_msg)
 
 
 
 
352
 
353
  conversation_memory.add_to_memory({"user": incoming_msg, "assistant": response_text})
354
  send_message(sender, response_text)
355
  return '', 204
356
 
357
+ def handle_image(filepath):
358
+ try:
359
+ disease = predict_disease(filepath)
360
+ except:
361
+ disease = None
362
+ try:
363
+ pest = predict_pest(filepath)
364
+ except:
365
+ pest = None
366
+
367
+ if disease:
368
+ response_text = f"Detected disease: {disease}"
369
+ disease_info = generate_response(f"Provide brief information about {disease} in plants", chat_history)
370
+ response_text += f"\n\nAdditional information: {disease_info}"
371
+ elif pest:
372
+ response_text = f"Detected pest: {pest}"
373
+ pest_info = generate_response(f"Provide brief information about {pest} in agriculture", chat_history)
374
+ response_text += f"\n\nAdditional information: {pest_info}"
375
+ else:
376
+ response_text = "Please upload another image with good quality."
377
+
378
+ return response_text
379
+
380
+ def process_and_query_pdf(filepath):
381
+ # Assuming the PDF processing and embedding are handled here.
382
+ add_to_chroma(load_documents())
383
+ return query_rag("from pdf") # Replace with a more specific query if needed
384
+
385
 
386
  def send_message(to, body):
387
  try: