Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -460,25 +460,29 @@ def get_response_from_pdf(query, model, selected_docs, num_calls=3, temperature=
|
|
460 |
yield "No documents available. Please upload PDF documents to answer questions."
|
461 |
return
|
462 |
|
463 |
-
|
464 |
-
|
465 |
-
|
466 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
467 |
|
468 |
-
|
469 |
-
filtered_docs = [doc for doc in relevant_docs if doc.metadata["source"] in selected_docs]
|
470 |
-
logging.info(f"Number of filtered documents: {len(filtered_docs)}")
|
471 |
-
|
472 |
-
if not filtered_docs:
|
473 |
logging.warning(f"No relevant information found in the selected documents: {selected_docs}")
|
474 |
yield "No relevant information found in the selected documents. Please try selecting different documents or rephrasing your query."
|
475 |
return
|
476 |
|
477 |
-
for doc in
|
478 |
logging.info(f"Document source: {doc.metadata['source']}")
|
479 |
logging.info(f"Document content preview: {doc.page_content[:100]}...") # Log first 100 characters of each document
|
480 |
|
481 |
-
context_str = "\n".join([doc.page_content for doc in
|
482 |
logging.info(f"Total context length: {len(context_str)}")
|
483 |
|
484 |
if model == "@cf/meta/llama-3.1-8b-instruct":
|
|
|
460 |
yield "No documents available. Please upload PDF documents to answer questions."
|
461 |
return
|
462 |
|
463 |
+
all_filtered_docs = []
|
464 |
+
k_per_doc = max(5, 20 // len(selected_docs)) # Adjust this value as needed
|
465 |
+
|
466 |
+
for doc_name in selected_docs:
|
467 |
+
logging.info(f"Retrieving documents for: {doc_name}")
|
468 |
+
doc_filter = lambda doc: doc.metadata["source"] == doc_name
|
469 |
+
doc_retriever = database.as_retriever(search_kwargs={"k": k_per_doc, "filter": doc_filter})
|
470 |
+
relevant_docs = doc_retriever.get_relevant_documents(query)
|
471 |
+
all_filtered_docs.extend(relevant_docs)
|
472 |
+
logging.info(f"Retrieved {len(relevant_docs)} documents for {doc_name}")
|
473 |
+
|
474 |
+
logging.info(f"Total number of filtered documents: {len(all_filtered_docs)}")
|
475 |
|
476 |
+
if not all_filtered_docs:
|
|
|
|
|
|
|
|
|
477 |
logging.warning(f"No relevant information found in the selected documents: {selected_docs}")
|
478 |
yield "No relevant information found in the selected documents. Please try selecting different documents or rephrasing your query."
|
479 |
return
|
480 |
|
481 |
+
for doc in all_filtered_docs:
|
482 |
logging.info(f"Document source: {doc.metadata['source']}")
|
483 |
logging.info(f"Document content preview: {doc.page_content[:100]}...") # Log first 100 characters of each document
|
484 |
|
485 |
+
context_str = "\n".join([doc.page_content for doc in all_filtered_docs])
|
486 |
logging.info(f"Total context length: {len(context_str)}")
|
487 |
|
488 |
if model == "@cf/meta/llama-3.1-8b-instruct":
|