Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -138,95 +138,46 @@
|
|
138 |
|
139 |
|
140 |
|
141 |
-
import streamlit as st
|
142 |
-
from langchain.embeddings import HuggingFaceEmbeddings
|
143 |
-
from langchain.vectorstores import FAISS
|
144 |
-
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
145 |
-
from langchain.document_loaders import PyPDFLoader
|
146 |
-
from langchain.chains import RetrievalQA
|
147 |
-
from langchain.llms import HuggingFaceHub
|
148 |
-
import tempfile
|
149 |
-
import os
|
150 |
-
|
151 |
-
# Constants
|
152 |
-
EMBEDDING_MODEL_NAME = "BAAI/bge-base-en-v1.5"
|
153 |
-
LLM_MODEL_REPO = "mistralai/Mistral-7B-Instruct-v0.1"
|
154 |
-
CHUNK_SIZE = 500
|
155 |
-
CHUNK_OVERLAP = 300
|
156 |
-
|
157 |
-
# Load and split documents
|
158 |
-
def load_and_split_pdf(pdf_file):
|
159 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_file:
|
160 |
-
tmp_file.write(pdf_file.read())
|
161 |
-
tmp_file_path = tmp_file.name
|
162 |
-
|
163 |
-
loader = PyPDFLoader(tmp_file_path)
|
164 |
-
documents = loader.load()
|
165 |
-
splitter = RecursiveCharacterTextSplitter(chunk_size=CHUNK_SIZE, chunk_overlap=CHUNK_OVERLAP)
|
166 |
-
chunks = splitter.split_documents(documents)
|
167 |
-
return chunks
|
168 |
-
|
169 |
-
# Create FAISS vectorstore
|
170 |
-
def build_vectorstore(chunks):
|
171 |
-
embeddings = HuggingFaceEmbeddings(model_name=EMBEDDING_MODEL_NAME)
|
172 |
-
db = FAISS.from_documents(chunks, embedding=embeddings)
|
173 |
-
return db
|
174 |
-
|
175 |
-
# Initialize LLM from Hugging Face Hub
|
176 |
-
def get_llm():
|
177 |
-
return HuggingFaceHub(
|
178 |
-
repo_id=LLM_MODEL_REPO,
|
179 |
-
model_kwargs={"temperature": 0.3, "max_new_tokens": 512, "top_k": 10}
|
180 |
-
)
|
181 |
-
|
182 |
-
# Custom prompt for better accuracy
|
183 |
-
CUSTOM_PROMPT = """
|
184 |
-
You are a professional resume chatbot. Use the context below to accurately and concisely answer the user's question. If the information is not available in the context, say "Not found in the document.".
|
185 |
-
|
186 |
-
Context:
|
187 |
-
{context}
|
188 |
-
|
189 |
-
Question:
|
190 |
-
{question}
|
191 |
-
|
192 |
-
Answer:
|
193 |
-
"""
|
194 |
-
|
195 |
-
# Build QA chain
|
196 |
-
def build_qa_chain(vectorstore):
|
197 |
-
return RetrievalQA.from_chain_type(
|
198 |
-
llm=get_llm(),
|
199 |
-
retriever=vectorstore.as_retriever(),
|
200 |
-
chain_type="stuff",
|
201 |
-
chain_type_kwargs={
|
202 |
-
"prompt": CUSTOM_PROMPT
|
203 |
-
}
|
204 |
-
)
|
205 |
-
|
206 |
-
# Streamlit UI
|
207 |
-
def main():
|
208 |
-
st.set_page_config(page_title="Resume Q&A Bot", layout="wide")
|
209 |
-
st.title("Resume Chatbot - Ask Anything About the Uploaded PDF")
|
210 |
-
|
211 |
-
uploaded_file = st.file_uploader("Upload your resume (PDF)", type="pdf")
|
212 |
-
|
213 |
-
if uploaded_file is not None:
|
214 |
-
st.success("PDF uploaded successfully!")
|
215 |
-
with st.spinner("Processing document and creating knowledge base..."):
|
216 |
-
chunks = load_and_split_pdf(uploaded_file)
|
217 |
-
vectorstore = build_vectorstore(chunks)
|
218 |
-
qa_chain = build_qa_chain(vectorstore)
|
219 |
-
|
220 |
-
st.success("Knowledge base ready! Ask your question below:")
|
221 |
-
|
222 |
-
question = st.text_input("Your Question:")
|
223 |
-
|
224 |
-
if question:
|
225 |
-
with st.spinner("Generating answer..."):
|
226 |
-
response = qa_chain.run(question)
|
227 |
-
st.markdown(f"**Answer:** {response}")
|
228 |
-
|
229 |
-
if __name__ == '__main__':
|
230 |
-
main()
|
231 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
232 |
|
|
|
138 |
|
139 |
|
140 |
|
141 |
+
import streamlit as st from langchain_community.document_loaders import PyPDFLoader from langchain.text_splitter import RecursiveCharacterTextSplitter from langchain_community.vectorstores import FAISS from langchain.embeddings import HuggingFaceEmbeddings from langchain.chains import RetrievalQA from langchain.prompts import PromptTemplate from langchain.llms import HuggingFaceHub import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
142 |
|
143 |
+
Set Hugging Face API Token
|
144 |
+
|
145 |
+
os.environ["HUGGINGFACEHUB_API_TOKEN"] = "your_huggingfacehub_api_token_here"
|
146 |
+
|
147 |
+
Custom Prompt
|
148 |
+
|
149 |
+
custom_prompt = PromptTemplate( input_variables=["context", "question"], template=""" You are a helpful assistant. Use the context below to answer the question. If the answer is not in the context, say "I don't know."
|
150 |
+
|
151 |
+
Context: {context}
|
152 |
+
|
153 |
+
Question: {question}
|
154 |
+
|
155 |
+
Answer: """ )
|
156 |
+
|
157 |
+
Load PDF and split into chunks
|
158 |
+
|
159 |
+
def load_and_split_pdf(uploaded_file): loader = PyPDFLoader(uploaded_file.name) documents = loader.load() text_splitter = RecursiveCharacterTextSplitter(chunk_size=800, chunk_overlap=100) chunks = text_splitter.split_documents(documents) return chunks
|
160 |
+
|
161 |
+
Build vectorstore from document chunks
|
162 |
+
|
163 |
+
def build_vectorstore(chunks): embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2") db = FAISS.from_documents(chunks, embedding=embeddings) return db
|
164 |
+
|
165 |
+
Build QA chain
|
166 |
+
|
167 |
+
def build_qa_chain(vectorstore): llm = HuggingFaceHub(repo_id="mistralai/Mistral-7B-Instruct-v0.1", model_kwargs={"temperature": 0.2, "max_length": 512}) qa_chain = RetrievalQA.from_chain_type( llm=llm, retriever=vectorstore.as_retriever(), chain_type="stuff", chain_type_kwargs={"prompt": custom_prompt} ) return qa_chain
|
168 |
+
|
169 |
+
Streamlit App
|
170 |
+
|
171 |
+
st.set_page_config(page_title="Accurate PDF Chatbot", layout="centered") st.title("PDF QA Chatbot - RAG Powered")
|
172 |
+
|
173 |
+
uploaded_file = st.file_uploader("Upload a PDF", type="pdf")
|
174 |
+
|
175 |
+
if uploaded_file: with st.spinner("Reading and processing PDF..."): chunks = load_and_split_pdf(uploaded_file) vectorstore = build_vectorstore(chunks) qa_chain = build_qa_chain(vectorstore) st.success("PDF processed. Ask your question below.")
|
176 |
+
|
177 |
+
question = st.text_input("Ask a question from the PDF:")
|
178 |
+
|
179 |
+
if question:
|
180 |
+
with st.spinner("Searching answer..."):
|
181 |
+
answer = qa_chain.run(question)
|
182 |
+
st.markdown(f"**Answer:** {answer}")
|
183 |
|