Neurolingua commited on
Commit
05b09c6
1 Parent(s): 9f1c56d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -86
app.py CHANGED
@@ -11,11 +11,12 @@ from langchain.prompts import ChatPromptTemplate
11
  from langchain_community.llms.ollama import Ollama
12
  from get_embedding_function import get_embedding_function
13
  from langchain.document_loaders.pdf import PyPDFDirectoryLoader
14
- from langchain_text_splitters import RecursiveCharacterTextSplitter
15
  from langchain.schema.document import Document
16
 
17
  app = Flask(__name__)
18
  UPLOAD_FOLDER = '/code/uploads'
 
19
  if not os.path.exists(UPLOAD_FOLDER):
20
  os.makedirs(UPLOAD_FOLDER)
21
 
@@ -41,8 +42,6 @@ auth_token = os.environ.get('TWILIO_AUTH_TOKEN')
41
  client = Client(account_sid, auth_token)
42
  from_whatsapp_number = 'whatsapp:+14155238886'
43
 
44
- CHROMA_PATH = "chroma"
45
- DATA_PATH = "data"
46
  PROMPT_TEMPLATE = """
47
  Answer the question based only on the following context:
48
  {context}
@@ -201,11 +200,11 @@ def query_rag(query_text: str):
201
 
202
  def save_pdf_and_update_database(media_url):
203
  response = requests.get(media_url)
204
- pdf_filename = os.path.join(DATA_PATH, f"{uuid.uuid4()}.pdf")
205
  with open(pdf_filename, 'wb') as f:
206
  f.write(response.content)
207
 
208
- document_loader = PyPDFDirectoryLoader(DATA_PATH)
209
  documents = document_loader.load()
210
 
211
  text_splitter = RecursiveCharacterTextSplitter(
@@ -245,92 +244,79 @@ def calculate_chunk_ids(chunks):
245
  else:
246
  current_chunk_index = 0
247
 
248
- chunk_id = f"{current_page_id}:{current_chunk_index}"
249
  last_page_id = current_page_id
250
-
251
  chunk.metadata["id"] = chunk_id
252
 
253
  return chunks
254
 
255
- @app.route('/whatsapp', methods=['POST'])
256
- def whatsapp_webhook():
 
 
 
 
 
 
 
 
 
 
 
257
  incoming_msg = request.values.get('Body', '').lower()
258
- sender = request.values.get('From')
259
- num_media = int(request.values.get('NumMedia', 0))
260
-
261
- chat_history = conversation_memory.get_memory()
262
-
263
- if num_media > 0:
264
- media_url = request.values.get('MediaUrl0')
265
- response_text = media_url
266
- content_type = request.values.get('MediaContentType0')
267
- if content_type.startswith('image/'):
268
- filepath = convert_img(media_url, account_sid, auth_token)
269
- try:
270
- disease = predict_disease(filepath)
271
- except:
272
- disease = None
273
- try:
274
- pest = predict_pest(filepath)
275
- except:
276
- pest = None
277
-
278
- if disease:
279
- response_text = f"Detected disease: {disease}"
280
- disease_info = generate_response(f"Provide brief information about {disease} in plants", chat_history)
281
- response_text += f"\n\nAdditional information: {disease_info}"
282
- elif pest:
283
- response_text = f"Detected pest: {pest}"
284
- pest_info = generate_response(f"Provide brief information about {pest} in agriculture", chat_history)
285
- response_text += f"\n\nAdditional information: {pest_info}"
286
- else:
287
- response_text = "Please upload another image with good quality."
288
- elif content_type == "application/pdf":
289
  save_pdf_and_update_database(media_url)
290
- response_text = "Your PDF has been saved and processed."
291
  else:
292
- filepath = download_and_save_as_txt(media_url, account_sid, auth_token)
293
- response_text = query_rag(filepath)
294
- elif ('weather' in incoming_msg.lower()) or ('climate' in incoming_msg.lower()) or (
295
- 'temperature' in incoming_msg.lower()):
296
- response_text = get_weather(incoming_msg.lower())
297
- elif 'bookkeeping' in incoming_msg:
298
- response_text = "Please provide the details you'd like to record."
299
- elif ('rates' in incoming_msg.lower()) or ('price' in incoming_msg.lower()) or (
300
- 'market' in incoming_msg.lower()) or ('rate' in incoming_msg.lower()) or ('prices' in incoming_msg.lower()):
301
- rates = get_rates()
302
- response_text = generate_response(incoming_msg + ' data is ' + rates, chat_history)
303
- elif ('news' in incoming_msg.lower()) or ('information' in incoming_msg.lower()):
304
- news = get_news()
305
- response_text = generate_response(incoming_msg + ' data is ' + str(news), chat_history)
306
- else:
307
- response_text = generate_response(incoming_msg, chat_history)
308
-
309
- conversation_memory.add_to_memory({"user": incoming_msg, "assistant": response_text})
310
- send_message(sender, response_text)
311
- return '', 204
312
-
313
- def send_message(to, body):
314
- try:
315
- message = client.messages.create(
316
- from_=from_whatsapp_number,
317
- body=body,
318
- to=to
319
- )
320
- print(f"Message sent with SID: {message.sid}")
321
- except Exception as e:
322
- print(f"Error sending message: {e}")
323
-
324
- @app.route('/')
325
- def home():
326
- return "Welcome to the WhatsApp Chatbot!", 200
327
-
328
- def send_initial_message(to_number):
329
- send_message(
330
- f'whatsapp:{to_number}',
331
- 'Welcome to the Agri AI Chatbot! How can I assist you today?'
332
- )
333
-
334
- if __name__ == '__main__':
335
- send_initial_message('919080522395')
336
- app.run(host='0.0.0.0', port=7860)
 
 
11
  from langchain_community.llms.ollama import Ollama
12
  from get_embedding_function import get_embedding_function
13
  from langchain.document_loaders.pdf import PyPDFDirectoryLoader
14
+ from langchain.text_splitters import RecursiveCharacterTextSplitter
15
  from langchain.schema.document import Document
16
 
17
  app = Flask(__name__)
18
  UPLOAD_FOLDER = '/code/uploads'
19
+ CHROMA_PATH = UPLOAD_FOLDER # Use the same folder for Chroma
20
  if not os.path.exists(UPLOAD_FOLDER):
21
  os.makedirs(UPLOAD_FOLDER)
22
 
 
42
  client = Client(account_sid, auth_token)
43
  from_whatsapp_number = 'whatsapp:+14155238886'
44
 
 
 
45
  PROMPT_TEMPLATE = """
46
  Answer the question based only on the following context:
47
  {context}
 
200
 
201
  def save_pdf_and_update_database(media_url):
202
  response = requests.get(media_url)
203
+ pdf_filename = os.path.join(UPLOAD_FOLDER, f"{uuid.uuid4()}.pdf")
204
  with open(pdf_filename, 'wb') as f:
205
  f.write(response.content)
206
 
207
+ document_loader = PyPDFDirectoryLoader(UPLOAD_FOLDER)
208
  documents = document_loader.load()
209
 
210
  text_splitter = RecursiveCharacterTextSplitter(
 
244
  else:
245
  current_chunk_index = 0
246
 
 
247
  last_page_id = current_page_id
248
+ chunk_id = f"{current_page_id}:{current_chunk_index}"
249
  chunk.metadata["id"] = chunk_id
250
 
251
  return chunks
252
 
253
+ @app.route("/pdf", methods=["POST"])
254
+ def receive_pdf():
255
+ media_url = request.values.get("MediaUrl", None)
256
+ if media_url:
257
+ save_pdf_and_update_database(media_url)
258
+ return "PDF processed and saved successfully."
259
+ return "No media URL found."
260
+
261
+ @app.route("/whatsapp", methods=["POST"])
262
+ def incoming_whatsapp():
263
+ media_url = request.values.get("MediaUrl", None)
264
+ from_number = request.values.get("From", "").strip()
265
+ from_number = from_number[2:] if from_number.startswith("91") else from_number
266
  incoming_msg = request.values.get('Body', '').lower()
267
+ response = MessagingResponse()
268
+ message = response.message()
269
+
270
+ if media_url:
271
+ extension = os.path.splitext(media_url)[1]
272
+ if extension.lower() == ".pdf":
273
+ media_filepath = download_and_save_as_txt(media_url, account_sid, auth_token)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
274
  save_pdf_and_update_database(media_url)
275
+ message.body("The PDF was processed successfully.")
276
  else:
277
+ message.body("Please send a PDF file.")
278
+ return str(response)
279
+
280
+ if 'get weather for' in incoming_msg:
281
+ city = incoming_msg.replace("get weather for", "")
282
+ temperature = get_weather(city)
283
+ message.body(f'The temperature in {city} is {temperature}.')
284
+ return str(response)
285
+
286
+ if 'get rates' in incoming_msg:
287
+ message.body(get_rates())
288
+ return str(response)
289
+
290
+ if 'get news' in incoming_msg:
291
+ message.body(get_news())
292
+ return str(response)
293
+
294
+ if 'pest' in incoming_msg:
295
+ text = predict_pest(media_filepath)
296
+ message.body(text)
297
+ return str(response)
298
+
299
+ if 'disease' in incoming_msg:
300
+ text = predict_disease(media_filepath)
301
+ message.body(text)
302
+ return str(response)
303
+
304
+ if 'question:' in incoming_msg:
305
+ conversation_memory.add_to_memory(f"User: {incoming_msg}")
306
+ chat_history = "\n".join(conversation_memory.get_memory())
307
+ response_text = generate_response(incoming_msg.replace("question:", ""), chat_history)
308
+ conversation_memory.add_to_memory(f"Assistant: {response_text}")
309
+ message.body(response_text)
310
+ return str(response)
311
+
312
+ if 'query:' in incoming_msg:
313
+ query = incoming_msg.replace("query:", "").strip()
314
+ response_text = query_rag(query)
315
+ message.body(response_text)
316
+ return str(response)
317
+
318
+ message.body("I'm sorry, I don't understand that command.")
319
+ return str(response)
320
+
321
+ if __name__ == "__main__":
322
+ app.run(debug=True)