Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -10,26 +10,21 @@ from langchain_community.document_loaders import PyPDFLoader
|
|
10 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
11 |
from dotenv import load_dotenv
|
12 |
|
13 |
-
load_dotenv()
|
14 |
-
|
15 |
-
# Load the GROQ API KEY
|
16 |
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
|
17 |
|
18 |
llm = ChatGroq(temperature=0, model_name='llama-3.1-8b-instant', groq_api_key=GROQ_API_KEY)
|
19 |
-
|
20 |
-
prompt = ChatPromptTemplate.from_template(
|
21 |
-
"""
|
22 |
-
Answer the questions based on the provided context only.
|
23 |
Please provide the most accurate response based on the question
|
24 |
-
<context>
|
25 |
-
{
|
26 |
-
</context>
|
27 |
-
Question: {input}
|
28 |
-
"""
|
29 |
-
)
|
30 |
|
31 |
embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
|
32 |
-
|
|
|
|
|
|
|
|
|
33 |
|
34 |
def process_pdf(file):
|
35 |
global vectors
|
@@ -38,57 +33,32 @@ def process_pdf(file):
|
|
38 |
docs = loader.load()
|
39 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
40 |
final_documents = text_splitter.split_documents(docs)
|
41 |
-
|
42 |
-
vectors = FAISS.from_documents(final_documents, embeddings)
|
43 |
-
else:
|
44 |
-
vectors.add_documents(final_documents)
|
45 |
return "PDF processed and added to the knowledge base."
|
46 |
return "No file uploaded."
|
47 |
|
48 |
def process_question(question):
|
|
|
49 |
if vectors is None:
|
50 |
return "Please upload a PDF first.", "", 0
|
51 |
-
|
52 |
document_chain = create_stuff_documents_chain(llm, prompt)
|
53 |
retriever = vectors.as_retriever()
|
54 |
retrieval_chain = create_retrieval_chain(retriever, document_chain)
|
55 |
response = retrieval_chain.invoke({'input': question})
|
56 |
-
|
57 |
context = "\n\n".join([doc.page_content for doc in response["context"]])
|
58 |
-
|
59 |
-
# Calculate a simple confidence score based on the relevance of retrieved documents
|
60 |
confidence_score = sum([doc.metadata.get('score', 0) for doc in response["context"]]) / len(response["context"])
|
61 |
-
|
62 |
return response['answer'], context, round(confidence_score, 2)
|
63 |
|
64 |
CSS = """
|
65 |
-
.duplicate-button {
|
66 |
-
|
67 |
-
|
68 |
-
background: black !important;
|
69 |
-
border-radius: 100vh !important;
|
70 |
-
}
|
71 |
-
h3, p, h1 {
|
72 |
-
text-align: center;
|
73 |
-
color: white;
|
74 |
-
}
|
75 |
-
footer {
|
76 |
-
text-align: center;
|
77 |
-
padding: 10px;
|
78 |
-
width: 100%;
|
79 |
-
background-color: rgba(240, 240, 240, 0.8);
|
80 |
-
z-index: 1000;
|
81 |
-
position: relative;
|
82 |
-
margin-top: 10px;
|
83 |
-
color: black;
|
84 |
-
}
|
85 |
"""
|
86 |
|
87 |
FOOTER_TEXT = """
|
88 |
<footer>
|
89 |
<p>If you enjoyed the functionality of the app, please leave a like!<br>
|
90 |
-
Check out more on <a href="https://www.linkedin.com/in/your-linkedin/" target="_blank">LinkedIn</a> |
|
91 |
-
<a href="https://your-portfolio-url.com/" target="_blank">Portfolio</a></p>
|
92 |
</footer>
|
93 |
"""
|
94 |
|
@@ -96,22 +66,22 @@ TITLE = "<h1>π RAG Document Q&A π</h1>"
|
|
96 |
|
97 |
with gr.Blocks(css=CSS, theme="Nymbo/Nymbo_Theme") as demo:
|
98 |
gr.HTML(TITLE)
|
99 |
-
|
100 |
with gr.Tab("PDF Uploader"):
|
101 |
pdf_file = gr.File(label="Upload PDF")
|
102 |
upload_button = gr.Button("Process PDF")
|
103 |
upload_output = gr.Textbox(label="Upload Status")
|
104 |
-
|
105 |
with gr.Tab("Q&A System"):
|
106 |
question_input = gr.Textbox(lines=2, placeholder="Enter your question here...")
|
107 |
submit_button = gr.Button("Ask Question")
|
108 |
answer_output = gr.Textbox(label="Answer")
|
109 |
context_output = gr.Textbox(label="Relevant Context", lines=10)
|
110 |
confidence_output = gr.Number(label="Confidence Score")
|
111 |
-
|
112 |
upload_button.click(process_pdf, inputs=[pdf_file], outputs=[upload_output])
|
113 |
submit_button.click(process_question, inputs=[question_input], outputs=[answer_output, context_output, confidence_output])
|
114 |
-
|
115 |
gr.HTML(FOOTER_TEXT)
|
116 |
|
117 |
if __name__ == "__main__":
|
|
|
10 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
11 |
from dotenv import load_dotenv
|
12 |
|
13 |
+
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 |
+
prompt = ChatPromptTemplate.from_template("""Answer the questions based on the provided context only.
|
|
|
|
|
|
|
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 |
+
def initialize_vectors():
|
25 |
+
return None
|
26 |
+
|
27 |
+
vectors = initialize_vectors()
|
28 |
|
29 |
def process_pdf(file):
|
30 |
global vectors
|
|
|
33 |
docs = loader.load()
|
34 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
35 |
final_documents = text_splitter.split_documents(docs)
|
36 |
+
vectors = FAISS.from_documents(final_documents, embeddings)
|
|
|
|
|
|
|
37 |
return "PDF processed and added to the knowledge base."
|
38 |
return "No file uploaded."
|
39 |
|
40 |
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 = """
|
53 |
+
.duplicate-button { margin: auto !important; color: white !important; background: black !important; border-radius: 100vh !important;}
|
54 |
+
h3, p, h1 { text-align: center; color: white;}
|
55 |
+
footer { text-align: center; padding: 10px; width: 100%; background-color: rgba(240, 240, 240, 0.8); z-index: 1000; position: relative; margin-top: 10px; color: black;}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
"""
|
57 |
|
58 |
FOOTER_TEXT = """
|
59 |
<footer>
|
60 |
<p>If you enjoyed the functionality of the app, please leave a like!<br>
|
61 |
+
Check out more on <a href="https://www.linkedin.com/in/your-linkedin/" target="_blank">LinkedIn</a> | <a href="https://your-portfolio-url.com/" target="_blank">Portfolio</a></p>
|
|
|
62 |
</footer>
|
63 |
"""
|
64 |
|
|
|
66 |
|
67 |
with gr.Blocks(css=CSS, theme="Nymbo/Nymbo_Theme") as demo:
|
68 |
gr.HTML(TITLE)
|
69 |
+
|
70 |
with gr.Tab("PDF Uploader"):
|
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...")
|
77 |
submit_button = gr.Button("Ask Question")
|
78 |
answer_output = gr.Textbox(label="Answer")
|
79 |
context_output = gr.Textbox(label="Relevant Context", lines=10)
|
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)
|
86 |
|
87 |
if __name__ == "__main__":
|