Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
from langchain_groq import ChatGroq
|
4 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
5 |
+
from langchain.chains.combine_documents import create_stuff_documents_chain
|
6 |
+
from langchain_core.prompts import ChatPromptTemplate
|
7 |
+
from langchain.chains import create_retrieval_chain
|
8 |
+
from langchain_community.vectorstores import FAISS
|
9 |
+
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 |
+
os.environ['GROQ_API_KEY'] = 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 |
+
{context}
|
26 |
+
</context>
|
27 |
+
Question: {input}
|
28 |
+
"""
|
29 |
+
)
|
30 |
+
|
31 |
+
embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
|
32 |
+
vectors = None
|
33 |
+
|
34 |
+
def process_pdf(file):
|
35 |
+
global vectors
|
36 |
+
if file is not None:
|
37 |
+
loader = PyPDFLoader(file.name)
|
38 |
+
docs = loader.load()
|
39 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
40 |
+
final_documents = text_splitter.split_documents(docs)
|
41 |
+
if vectors is None:
|
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 |
+
margin: auto !important;
|
67 |
+
color: white !important;
|
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 |
+
|
95 |
+
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__":
|
118 |
+
demo.launch()
|