Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -10,7 +10,7 @@ from langchain_community.document_loaders import PyPDFLoader
|
|
10 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
11 |
from dotenv import load_dotenv
|
12 |
|
13 |
-
# Load
|
14 |
load_dotenv()
|
15 |
|
16 |
# Load the GROQ API key
|
@@ -46,22 +46,35 @@ def process_pdf(file):
|
|
46 |
return "PDF processed and added to the knowledge base."
|
47 |
return "No file uploaded."
|
48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
# Function to process questions
|
50 |
def process_question(question):
|
|
|
51 |
if vectors is None:
|
52 |
return "Please upload a PDF first.", "", 0
|
53 |
|
54 |
-
|
55 |
-
retriever = vectors.as_retriever()
|
56 |
-
|
57 |
-
|
58 |
-
response = retrieval_chain.invoke({'input': question})
|
59 |
-
context = "\n\n".join([doc.page_content for doc in response["context"]])
|
60 |
|
61 |
-
|
62 |
-
|
63 |
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
|
66 |
# CSS styling
|
67 |
CSS = """
|
@@ -89,6 +102,7 @@ with gr.Blocks(css=CSS, theme="Nymbo/Nymbo_Theme") as demo:
|
|
89 |
with gr.Tab("PDF Uploader"):
|
90 |
pdf_file = gr.File(label="Upload PDF")
|
91 |
upload_button = gr.Button("Process PDF")
|
|
|
92 |
upload_output = gr.Textbox(label="Upload Status")
|
93 |
|
94 |
with gr.Tab("Q&A System"):
|
@@ -101,6 +115,9 @@ with gr.Blocks(css=CSS, theme="Nymbo/Nymbo_Theme") as demo:
|
|
101 |
# Button actions
|
102 |
upload_button.click(process_pdf, inputs=[pdf_file], outputs=[upload_output])
|
103 |
submit_button.click(process_question, inputs=[question_input], outputs=[answer_output, context_output, confidence_output])
|
|
|
|
|
|
|
104 |
|
105 |
gr.HTML(FOOTER_TEXT)
|
106 |
|
|
|
10 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
11 |
from dotenv import load_dotenv
|
12 |
|
13 |
+
# Load environment variables
|
14 |
load_dotenv()
|
15 |
|
16 |
# Load the GROQ API key
|
|
|
46 |
return "PDF processed and added to the knowledge base."
|
47 |
return "No file uploaded."
|
48 |
|
49 |
+
# Function to clear the knowledge base
|
50 |
+
def clear_knowledge_base():
|
51 |
+
global vectors
|
52 |
+
vectors = None # Reset the vector store
|
53 |
+
return "Knowledge base cleared."
|
54 |
+
|
55 |
# Function to process questions
|
56 |
def process_question(question):
|
57 |
+
global vectors
|
58 |
if vectors is None:
|
59 |
return "Please upload a PDF first.", "", 0
|
60 |
|
61 |
+
# Create document retrieval chain
|
62 |
+
retriever = vectors.as_retriever(search_type="similarity", search_kwargs={"k": 5})
|
63 |
+
documents = retriever.get_relevant_documents(question)
|
|
|
|
|
|
|
64 |
|
65 |
+
if not documents:
|
66 |
+
return "No relevant context found.", "", 0
|
67 |
|
68 |
+
# Create context from retrieved documents
|
69 |
+
context = "\n\n".join([doc.page_content for doc in documents])
|
70 |
+
|
71 |
+
# Use the LLM to answer the question
|
72 |
+
response = llm({"context": context, "input": question})
|
73 |
+
|
74 |
+
# Confidence score as average relevance
|
75 |
+
confidence_score = sum([doc.metadata.get('score', 0) for doc in documents]) / len(documents)
|
76 |
+
|
77 |
+
return response, context, round(confidence_score, 2)
|
78 |
|
79 |
# CSS styling
|
80 |
CSS = """
|
|
|
102 |
with gr.Tab("PDF Uploader"):
|
103 |
pdf_file = gr.File(label="Upload PDF")
|
104 |
upload_button = gr.Button("Process PDF")
|
105 |
+
clear_button = gr.Button("Clear Knowledge Base") # New button to clear the knowledge base
|
106 |
upload_output = gr.Textbox(label="Upload Status")
|
107 |
|
108 |
with gr.Tab("Q&A System"):
|
|
|
115 |
# Button actions
|
116 |
upload_button.click(process_pdf, inputs=[pdf_file], outputs=[upload_output])
|
117 |
submit_button.click(process_question, inputs=[question_input], outputs=[answer_output, context_output, confidence_output])
|
118 |
+
|
119 |
+
# Action to clear the knowledge base
|
120 |
+
clear_button.click(clear_knowledge_base, outputs=[upload_output])
|
121 |
|
122 |
gr.HTML(FOOTER_TEXT)
|
123 |
|