Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,21 +1,37 @@
|
|
|
|
|
| 1 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
CHROMA_PATH = '/code/chroma_db'
|
| 4 |
if not os.path.exists(CHROMA_PATH):
|
| 5 |
os.makedirs(CHROMA_PATH)
|
| 6 |
-
from langchain.vectorstores.chroma import Chroma
|
| 7 |
-
from langchain.document_loaders import PyPDFLoader
|
| 8 |
-
from langchain.embeddings import HuggingFaceEmbeddings
|
| 9 |
-
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 10 |
|
| 11 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
def save_pdf_and_update_database(pdf_filepath):
|
| 13 |
try:
|
| 14 |
-
# Load the PDF
|
| 15 |
document_loader = PyPDFLoader(pdf_filepath)
|
| 16 |
documents = document_loader.load()
|
| 17 |
|
| 18 |
-
# Split the documents into manageable chunks
|
| 19 |
text_splitter = RecursiveCharacterTextSplitter(
|
| 20 |
chunk_size=800,
|
| 21 |
chunk_overlap=80,
|
|
@@ -24,19 +40,16 @@ def save_pdf_and_update_database(pdf_filepath):
|
|
| 24 |
)
|
| 25 |
chunks = text_splitter.split_documents(documents)
|
| 26 |
|
| 27 |
-
# Initialize Chroma with an embedding function
|
| 28 |
embedding_function = HuggingFaceEmbeddings()
|
| 29 |
db = Chroma(persist_directory=CHROMA_PATH, embedding_function=embedding_function)
|
| 30 |
|
| 31 |
-
# Add chunks to ChromaDB
|
| 32 |
db.add_documents(chunks)
|
| 33 |
db.persist()
|
| 34 |
print("PDF processed and data updated in Chroma.")
|
| 35 |
except Exception as e:
|
| 36 |
print(f"Error processing PDF: {e}")
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
def generate_response(query, chat_history):
|
| 41 |
response = ''
|
| 42 |
for chunk in AI71(AI71_API_KEY).chat.completions.create(
|
|
@@ -51,10 +64,10 @@ def generate_response(query, chat_history):
|
|
| 51 |
response += chunk.choices[0].delta.content
|
| 52 |
return response.replace("###", '').replace('\nUser:', '')
|
| 53 |
|
|
|
|
| 54 |
def query_rag(query_text: str, chat_history):
|
| 55 |
db = Chroma(persist_directory=CHROMA_PATH, embedding_function=HuggingFaceEmbeddings())
|
| 56 |
|
| 57 |
-
# Perform a similarity search in ChromaDB
|
| 58 |
results = db.similarity_search_with_score(query_text, k=5)
|
| 59 |
|
| 60 |
if not results:
|
|
@@ -62,41 +75,53 @@ def query_rag(query_text: str, chat_history):
|
|
| 62 |
|
| 63 |
context_text = "\n\n---\n\n".join([doc.page_content for doc, _score in results])
|
| 64 |
|
| 65 |
-
# Generate the response using the Falcon model
|
| 66 |
prompt = f"Context:\n{context_text}\n\nQuestion:\n{query_text}"
|
| 67 |
response = generate_response(prompt, chat_history)
|
| 68 |
|
| 69 |
return response
|
| 70 |
|
| 71 |
-
|
| 72 |
@app.route('/whatsapp', methods=['POST'])
|
| 73 |
def whatsapp_webhook():
|
| 74 |
incoming_msg = request.values.get('Body', '').lower()
|
| 75 |
sender = request.values.get('From')
|
| 76 |
num_media = int(request.values.get('NumMedia', 0))
|
| 77 |
|
| 78 |
-
chat_history =
|
| 79 |
|
| 80 |
if num_media > 0:
|
| 81 |
media_url = request.values.get('MediaUrl0')
|
| 82 |
content_type = request.values.get('MediaContentType0')
|
| 83 |
|
| 84 |
if content_type == 'application/pdf':
|
| 85 |
-
# Handle PDF processing
|
| 86 |
filepath = download_file(media_url, ".pdf")
|
| 87 |
save_pdf_and_update_database(filepath)
|
| 88 |
response_text = "PDF has been processed. You can now ask questions related to its content."
|
| 89 |
else:
|
| 90 |
response_text = "Unsupported file type. Please upload a PDF document."
|
| 91 |
else:
|
| 92 |
-
# Handle queries
|
| 93 |
response_text = query_rag(incoming_msg, chat_history)
|
| 94 |
|
| 95 |
-
|
| 96 |
send_message(sender, response_text)
|
| 97 |
return '', 204
|
| 98 |
-
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
if __name__ == "__main__":
|
| 101 |
send_initial_message('919080522395')
|
| 102 |
send_initial_message('916382792828')
|
|
|
|
| 1 |
+
from flask import Flask, request
|
| 2 |
import os
|
| 3 |
+
from langchain.vectorstores import Chroma
|
| 4 |
+
from langchain.document_loaders import PyPDFLoader
|
| 5 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
| 6 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 7 |
+
import requests
|
| 8 |
|
| 9 |
+
# Flask app
|
| 10 |
+
app = Flask(__name__)
|
| 11 |
+
|
| 12 |
+
# ChromaDB path
|
| 13 |
CHROMA_PATH = '/code/chroma_db'
|
| 14 |
if not os.path.exists(CHROMA_PATH):
|
| 15 |
os.makedirs(CHROMA_PATH)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
# Set AI71 API key
|
| 18 |
+
AI71_API_KEY = os.environ.get('AI71_API_KEY')
|
| 19 |
+
|
| 20 |
+
# Download file utility
|
| 21 |
+
def download_file(url, ext):
|
| 22 |
+
local_filename = f'/code/uploads/uploaded_file{ext}'
|
| 23 |
+
with requests.get(url, stream=True) as r:
|
| 24 |
+
with open(local_filename, 'wb') as f:
|
| 25 |
+
for chunk in r.iter_content(chunk_size=8192):
|
| 26 |
+
f.write(chunk)
|
| 27 |
+
return local_filename
|
| 28 |
+
|
| 29 |
+
# Process PDF and save to ChromaDB
|
| 30 |
def save_pdf_and_update_database(pdf_filepath):
|
| 31 |
try:
|
|
|
|
| 32 |
document_loader = PyPDFLoader(pdf_filepath)
|
| 33 |
documents = document_loader.load()
|
| 34 |
|
|
|
|
| 35 |
text_splitter = RecursiveCharacterTextSplitter(
|
| 36 |
chunk_size=800,
|
| 37 |
chunk_overlap=80,
|
|
|
|
| 40 |
)
|
| 41 |
chunks = text_splitter.split_documents(documents)
|
| 42 |
|
|
|
|
| 43 |
embedding_function = HuggingFaceEmbeddings()
|
| 44 |
db = Chroma(persist_directory=CHROMA_PATH, embedding_function=embedding_function)
|
| 45 |
|
|
|
|
| 46 |
db.add_documents(chunks)
|
| 47 |
db.persist()
|
| 48 |
print("PDF processed and data updated in Chroma.")
|
| 49 |
except Exception as e:
|
| 50 |
print(f"Error processing PDF: {e}")
|
| 51 |
|
| 52 |
+
# Generate response using Falcon model
|
|
|
|
| 53 |
def generate_response(query, chat_history):
|
| 54 |
response = ''
|
| 55 |
for chunk in AI71(AI71_API_KEY).chat.completions.create(
|
|
|
|
| 64 |
response += chunk.choices[0].delta.content
|
| 65 |
return response.replace("###", '').replace('\nUser:', '')
|
| 66 |
|
| 67 |
+
# Query the RAG system
|
| 68 |
def query_rag(query_text: str, chat_history):
|
| 69 |
db = Chroma(persist_directory=CHROMA_PATH, embedding_function=HuggingFaceEmbeddings())
|
| 70 |
|
|
|
|
| 71 |
results = db.similarity_search_with_score(query_text, k=5)
|
| 72 |
|
| 73 |
if not results:
|
|
|
|
| 75 |
|
| 76 |
context_text = "\n\n---\n\n".join([doc.page_content for doc, _score in results])
|
| 77 |
|
|
|
|
| 78 |
prompt = f"Context:\n{context_text}\n\nQuestion:\n{query_text}"
|
| 79 |
response = generate_response(prompt, chat_history)
|
| 80 |
|
| 81 |
return response
|
| 82 |
|
| 83 |
+
# Flask route to handle WhatsApp webhook
|
| 84 |
@app.route('/whatsapp', methods=['POST'])
|
| 85 |
def whatsapp_webhook():
|
| 86 |
incoming_msg = request.values.get('Body', '').lower()
|
| 87 |
sender = request.values.get('From')
|
| 88 |
num_media = int(request.values.get('NumMedia', 0))
|
| 89 |
|
| 90 |
+
chat_history = [] # You need to handle chat history appropriately
|
| 91 |
|
| 92 |
if num_media > 0:
|
| 93 |
media_url = request.values.get('MediaUrl0')
|
| 94 |
content_type = request.values.get('MediaContentType0')
|
| 95 |
|
| 96 |
if content_type == 'application/pdf':
|
|
|
|
| 97 |
filepath = download_file(media_url, ".pdf")
|
| 98 |
save_pdf_and_update_database(filepath)
|
| 99 |
response_text = "PDF has been processed. You can now ask questions related to its content."
|
| 100 |
else:
|
| 101 |
response_text = "Unsupported file type. Please upload a PDF document."
|
| 102 |
else:
|
|
|
|
| 103 |
response_text = query_rag(incoming_msg, chat_history)
|
| 104 |
|
| 105 |
+
# Assuming you have a function to send a message back to the user
|
| 106 |
send_message(sender, response_text)
|
| 107 |
return '', 204
|
| 108 |
+
|
| 109 |
+
def send_message(to, body):
|
| 110 |
+
try:
|
| 111 |
+
message = client.messages.create(
|
| 112 |
+
from_=from_whatsapp_number,
|
| 113 |
+
body=body,
|
| 114 |
+
to=to
|
| 115 |
+
)
|
| 116 |
+
print(f"Message sent with SID: {message.sid}")
|
| 117 |
+
except Exception as e:
|
| 118 |
+
print(f"Error sending message: {e}")
|
| 119 |
+
|
| 120 |
+
def send_initial_message(to_number):
|
| 121 |
+
send_message(
|
| 122 |
+
f'whatsapp:{to_number}',
|
| 123 |
+
'Welcome to the Agri AI Chatbot! How can I assist you today? You can send an image with "pest" or "disease" to classify it.'
|
| 124 |
+
)
|
| 125 |
if __name__ == "__main__":
|
| 126 |
send_initial_message('919080522395')
|
| 127 |
send_initial_message('916382792828')
|