Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -14,17 +14,23 @@ load_dotenv() # Load the GROQ API KEY
|
|
14 |
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
|
15 |
|
16 |
llm = ChatGroq(temperature=0, model_name='llama-3.1-8b-instant', groq_api_key=GROQ_API_KEY)
|
17 |
-
|
|
|
|
|
18 |
Please provide the most accurate response based on the question
|
19 |
<context>{context}</context>
|
20 |
-
Question: {input}
|
|
|
21 |
|
22 |
embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
|
23 |
|
24 |
-
|
25 |
-
|
26 |
|
27 |
-
|
|
|
|
|
|
|
28 |
|
29 |
def process_pdf(file):
|
30 |
global vectors
|
@@ -41,12 +47,15 @@ def process_question(question):
|
|
41 |
global vectors
|
42 |
if vectors is None:
|
43 |
return "Please upload a PDF first.", "", 0
|
|
|
44 |
document_chain = create_stuff_documents_chain(llm, prompt)
|
45 |
retriever = vectors.as_retriever()
|
46 |
retrieval_chain = create_retrieval_chain(retriever, document_chain)
|
47 |
response = retrieval_chain.invoke({'input': question})
|
|
|
48 |
context = "\n\n".join([doc.page_content for doc in response["context"]])
|
49 |
confidence_score = sum([doc.metadata.get('score', 0) for doc in response["context"]]) / len(response["context"])
|
|
|
50 |
return response['answer'], context, round(confidence_score, 2)
|
51 |
|
52 |
CSS = """
|
@@ -71,6 +80,8 @@ with gr.Blocks(css=CSS, theme="Nymbo/Nymbo_Theme") as demo:
|
|
71 |
pdf_file = gr.File(label="Upload PDF")
|
72 |
upload_button = gr.Button("Process PDF")
|
73 |
upload_output = gr.Textbox(label="Upload Status")
|
|
|
|
|
74 |
|
75 |
with gr.Tab("Q&A System"):
|
76 |
question_input = gr.Textbox(lines=2, placeholder="Enter your question here...")
|
@@ -80,6 +91,7 @@ with gr.Blocks(css=CSS, theme="Nymbo/Nymbo_Theme") as demo:
|
|
80 |
confidence_output = gr.Number(label="Confidence Score")
|
81 |
|
82 |
upload_button.click(process_pdf, inputs=[pdf_file], outputs=[upload_output])
|
|
|
83 |
submit_button.click(process_question, inputs=[question_input], outputs=[answer_output, context_output, confidence_output])
|
84 |
|
85 |
gr.HTML(FOOTER_TEXT)
|
|
|
14 |
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
|
15 |
|
16 |
llm = ChatGroq(temperature=0, model_name='llama-3.1-8b-instant', groq_api_key=GROQ_API_KEY)
|
17 |
+
|
18 |
+
prompt = ChatPromptTemplate.from_template("""
|
19 |
+
Answer the questions based on the provided context only.
|
20 |
Please provide the most accurate response based on the question
|
21 |
<context>{context}</context>
|
22 |
+
Question: {input}
|
23 |
+
""")
|
24 |
|
25 |
embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
|
26 |
|
27 |
+
# Global variable to store the vector store
|
28 |
+
vectors = None
|
29 |
|
30 |
+
def clear_knowledge_base():
|
31 |
+
global vectors
|
32 |
+
vectors = None
|
33 |
+
return "Knowledge base cleared."
|
34 |
|
35 |
def process_pdf(file):
|
36 |
global vectors
|
|
|
47 |
global vectors
|
48 |
if vectors is None:
|
49 |
return "Please upload a PDF first.", "", 0
|
50 |
+
|
51 |
document_chain = create_stuff_documents_chain(llm, prompt)
|
52 |
retriever = vectors.as_retriever()
|
53 |
retrieval_chain = create_retrieval_chain(retriever, document_chain)
|
54 |
response = retrieval_chain.invoke({'input': question})
|
55 |
+
|
56 |
context = "\n\n".join([doc.page_content for doc in response["context"]])
|
57 |
confidence_score = sum([doc.metadata.get('score', 0) for doc in response["context"]]) / len(response["context"])
|
58 |
+
|
59 |
return response['answer'], context, round(confidence_score, 2)
|
60 |
|
61 |
CSS = """
|
|
|
80 |
pdf_file = gr.File(label="Upload PDF")
|
81 |
upload_button = gr.Button("Process PDF")
|
82 |
upload_output = gr.Textbox(label="Upload Status")
|
83 |
+
clear_button = gr.Button("Clear Knowledge Base")
|
84 |
+
clear_output = gr.Textbox(label="Clear Status")
|
85 |
|
86 |
with gr.Tab("Q&A System"):
|
87 |
question_input = gr.Textbox(lines=2, placeholder="Enter your question here...")
|
|
|
91 |
confidence_output = gr.Number(label="Confidence Score")
|
92 |
|
93 |
upload_button.click(process_pdf, inputs=[pdf_file], outputs=[upload_output])
|
94 |
+
clear_button.click(clear_knowledge_base, outputs=[clear_output])
|
95 |
submit_button.click(process_question, inputs=[question_input], outputs=[answer_output, context_output, confidence_output])
|
96 |
|
97 |
gr.HTML(FOOTER_TEXT)
|