Spaces:
Sleeping
Sleeping
Neurolingua
commited on
Commit
•
05b09c6
1
Parent(s):
9f1c56d
Update app.py
Browse files
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
|
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(
|
205 |
with open(pdf_filename, 'wb') as f:
|
206 |
f.write(response.content)
|
207 |
|
208 |
-
document_loader = PyPDFDirectoryLoader(
|
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(
|
256 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
257 |
incoming_msg = request.values.get('Body', '').lower()
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
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 |
-
|
291 |
else:
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
-
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
|
310 |
-
|
311 |
-
|
312 |
-
|
313 |
-
|
314 |
-
|
315 |
-
|
316 |
-
|
317 |
-
|
318 |
-
|
319 |
-
|
320 |
-
|
321 |
-
|
322 |
-
|
323 |
-
|
324 |
-
|
325 |
-
|
326 |
-
|
327 |
-
|
328 |
-
|
329 |
-
|
330 |
-
|
331 |
-
|
332 |
-
|
333 |
-
|
334 |
-
|
335 |
-
|
336 |
-
|
|
|
|
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)
|