Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -7,7 +7,6 @@ from langchain.text_splitter import RecursiveCharacterTextSplitter
|
|
7 |
import requests
|
8 |
from twilio.rest import Client
|
9 |
|
10 |
-
|
11 |
# Flask app
|
12 |
app = Flask(__name__)
|
13 |
|
@@ -15,10 +14,11 @@ app = Flask(__name__)
|
|
15 |
CHROMA_PATH = '/code/chroma_db'
|
16 |
if not os.path.exists(CHROMA_PATH):
|
17 |
os.makedirs(CHROMA_PATH)
|
|
|
|
|
18 |
def initialize_chroma():
|
19 |
try:
|
20 |
-
|
21 |
-
embedding_function = HuggingFaceEmbeddings() # Use your desired embedding function
|
22 |
db = Chroma(persist_directory=CHROMA_PATH, embedding_function=embedding_function)
|
23 |
|
24 |
# Perform an initial operation to ensure the database is correctly initialized
|
@@ -29,7 +29,6 @@ def initialize_chroma():
|
|
29 |
|
30 |
initialize_chroma()
|
31 |
|
32 |
-
|
33 |
# Set AI71 API key
|
34 |
AI71_API_KEY = os.environ.get('AI71_API_KEY')
|
35 |
account_sid = os.environ.get('TWILIO_ACCOUNT_SID')
|
@@ -46,68 +45,16 @@ def download_file(url, ext):
|
|
46 |
f.write(chunk)
|
47 |
return local_filename
|
48 |
|
49 |
-
# Process PDF and
|
50 |
-
def
|
51 |
try:
|
52 |
document_loader = PyPDFLoader(pdf_filepath)
|
53 |
documents = document_loader.load()
|
54 |
-
|
55 |
-
|
56 |
-
chunk_size=800,
|
57 |
-
chunk_overlap=80,
|
58 |
-
length_function=len,
|
59 |
-
is_separator_regex=False,
|
60 |
-
)
|
61 |
-
chunks = text_splitter.split_documents(documents)
|
62 |
-
|
63 |
-
embedding_function = HuggingFaceEmbeddings()
|
64 |
-
db = Chroma(persist_directory=CHROMA_PATH, embedding_function=embedding_function)
|
65 |
-
|
66 |
-
db.add_documents(chunks)
|
67 |
-
db.persist()
|
68 |
-
print("PDF processed and data updated in Chroma.")
|
69 |
except Exception as e:
|
70 |
print(f"Error processing PDF: {e}")
|
71 |
-
|
72 |
-
# Generate response using Falcon model
|
73 |
-
def generate_response(query, chat_history):
|
74 |
-
response = ''
|
75 |
-
for chunk in AI71(AI71_API_KEY).chat.completions.create(
|
76 |
-
model="tiiuae/falcon-180b-chat",
|
77 |
-
messages=[
|
78 |
-
{"role": "system", "content": "You are the best agricultural assistant. Remember to give a response in not more than 2 sentences."},
|
79 |
-
{"role": "user", "content": f'''Answer the query based on history {chat_history}: {query}'''},
|
80 |
-
],
|
81 |
-
stream=True,
|
82 |
-
):
|
83 |
-
if chunk.choices[0].delta.content:
|
84 |
-
response += chunk.choices[0].delta.content
|
85 |
-
return response.replace("###", '').replace('\nUser:', '')
|
86 |
-
|
87 |
-
# Query the RAG system
|
88 |
-
def query_rag(query_text: str, chat_history):
|
89 |
-
try:
|
90 |
-
# Ensure the database is initialized
|
91 |
-
initialize_chroma()
|
92 |
-
|
93 |
-
embedding_function = HuggingFaceEmbeddings()
|
94 |
-
db = Chroma(persist_directory=CHROMA_PATH, embedding_function=embedding_function)
|
95 |
-
|
96 |
-
results = db.similarity_search_with_score(query_text, k=5)
|
97 |
-
|
98 |
-
if not results:
|
99 |
-
return "Sorry, I couldn't find any relevant information."
|
100 |
-
|
101 |
-
context_text = "\n\n---\n\n".join([doc.page_content for doc, _score in results])
|
102 |
-
|
103 |
-
prompt = f"Context:\n{context_text}\n\nQuestion:\n{query_text}"
|
104 |
-
response = generate_response(prompt, chat_history)
|
105 |
-
|
106 |
-
return response
|
107 |
-
except Exception as e:
|
108 |
-
print(f"Error querying RAG system: {e}")
|
109 |
-
return "An error occurred while querying the RAG system."
|
110 |
-
|
111 |
|
112 |
# Flask route to handle WhatsApp webhook
|
113 |
@app.route('/whatsapp', methods=['POST'])
|
@@ -116,25 +63,23 @@ def whatsapp_webhook():
|
|
116 |
sender = request.values.get('From')
|
117 |
num_media = int(request.values.get('NumMedia', 0))
|
118 |
|
119 |
-
chat_history = [] # You need to handle chat history appropriately
|
120 |
-
|
121 |
if num_media > 0:
|
122 |
media_url = request.values.get('MediaUrl0')
|
123 |
content_type = request.values.get('MediaContentType0')
|
124 |
|
125 |
if content_type == 'application/pdf':
|
126 |
filepath = download_file(media_url, ".pdf")
|
127 |
-
|
128 |
-
response_text = "
|
129 |
else:
|
130 |
response_text = "Unsupported file type. Please upload a PDF document."
|
131 |
else:
|
132 |
-
response_text =
|
133 |
|
134 |
-
# Assuming you have a function to send a message back to the user
|
135 |
send_message(sender, response_text)
|
136 |
return '', 204
|
137 |
-
|
|
|
138 |
def send_message(to, body):
|
139 |
try:
|
140 |
message = client.messages.create(
|
@@ -145,12 +90,7 @@ def send_message(to, body):
|
|
145 |
print(f"Message sent with SID: {message.sid}")
|
146 |
except Exception as e:
|
147 |
print(f"Error sending message: {e}")
|
148 |
-
|
149 |
-
def send_initial_message(to_number):
|
150 |
-
send_message(
|
151 |
-
f'whatsapp:{to_number}',
|
152 |
-
'Welcome to the Agri AI Chatbot! How can I assist you today? You can send an image with "pest" or "disease" to classify it.'
|
153 |
-
)
|
154 |
if __name__ == "__main__":
|
155 |
send_initial_message('919080522395')
|
156 |
send_initial_message('916382792828')
|
|
|
7 |
import requests
|
8 |
from twilio.rest import Client
|
9 |
|
|
|
10 |
# Flask app
|
11 |
app = Flask(__name__)
|
12 |
|
|
|
14 |
CHROMA_PATH = '/code/chroma_db'
|
15 |
if not os.path.exists(CHROMA_PATH):
|
16 |
os.makedirs(CHROMA_PATH)
|
17 |
+
|
18 |
+
# Initialize ChromaDB
|
19 |
def initialize_chroma():
|
20 |
try:
|
21 |
+
embedding_function = HuggingFaceEmbeddings()
|
|
|
22 |
db = Chroma(persist_directory=CHROMA_PATH, embedding_function=embedding_function)
|
23 |
|
24 |
# Perform an initial operation to ensure the database is correctly initialized
|
|
|
29 |
|
30 |
initialize_chroma()
|
31 |
|
|
|
32 |
# Set AI71 API key
|
33 |
AI71_API_KEY = os.environ.get('AI71_API_KEY')
|
34 |
account_sid = os.environ.get('TWILIO_ACCOUNT_SID')
|
|
|
45 |
f.write(chunk)
|
46 |
return local_filename
|
47 |
|
48 |
+
# Process PDF and return text
|
49 |
+
def extract_text_from_pdf(pdf_filepath):
|
50 |
try:
|
51 |
document_loader = PyPDFLoader(pdf_filepath)
|
52 |
documents = document_loader.load()
|
53 |
+
text = "\n\n".join([doc.page_content for doc in documents])
|
54 |
+
return text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
except Exception as e:
|
56 |
print(f"Error processing PDF: {e}")
|
57 |
+
return "Error extracting text from PDF."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
# Flask route to handle WhatsApp webhook
|
60 |
@app.route('/whatsapp', methods=['POST'])
|
|
|
63 |
sender = request.values.get('From')
|
64 |
num_media = int(request.values.get('NumMedia', 0))
|
65 |
|
|
|
|
|
66 |
if num_media > 0:
|
67 |
media_url = request.values.get('MediaUrl0')
|
68 |
content_type = request.values.get('MediaContentType0')
|
69 |
|
70 |
if content_type == 'application/pdf':
|
71 |
filepath = download_file(media_url, ".pdf")
|
72 |
+
extracted_text = extract_text_from_pdf(filepath)
|
73 |
+
response_text = f"Here is the content of the PDF:\n\n{extracted_text}"
|
74 |
else:
|
75 |
response_text = "Unsupported file type. Please upload a PDF document."
|
76 |
else:
|
77 |
+
response_text = "Please upload a PDF document."
|
78 |
|
|
|
79 |
send_message(sender, response_text)
|
80 |
return '', 204
|
81 |
+
|
82 |
+
# Function to send message
|
83 |
def send_message(to, body):
|
84 |
try:
|
85 |
message = client.messages.create(
|
|
|
90 |
print(f"Message sent with SID: {message.sid}")
|
91 |
except Exception as e:
|
92 |
print(f"Error sending message: {e}")
|
93 |
+
|
|
|
|
|
|
|
|
|
|
|
94 |
if __name__ == "__main__":
|
95 |
send_initial_message('919080522395')
|
96 |
send_initial_message('916382792828')
|