Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,10 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 3 |
-
from
|
| 4 |
-
from
|
| 5 |
-
from
|
| 6 |
from langchain.chains import RetrievalQA
|
| 7 |
-
from langchain.
|
| 8 |
from transformers import pipeline
|
| 9 |
|
| 10 |
# ----------------------------
|
|
@@ -15,7 +15,7 @@ st.title("📘 PDF Question Answering App")
|
|
| 15 |
st.markdown("Upload a PDF and ask questions about its content.")
|
| 16 |
|
| 17 |
# ----------------------------
|
| 18 |
-
# GLOBAL
|
| 19 |
# ----------------------------
|
| 20 |
qa_chain = None
|
| 21 |
|
|
@@ -23,6 +23,7 @@ qa_chain = None
|
|
| 23 |
# FUNCTIONS
|
| 24 |
# ----------------------------
|
| 25 |
def load_pdf(pdf_file):
|
|
|
|
| 26 |
loader = PyPDFLoader(pdf_file.name)
|
| 27 |
documents = loader.load()
|
| 28 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=800, chunk_overlap=100)
|
|
@@ -30,11 +31,13 @@ def load_pdf(pdf_file):
|
|
| 30 |
return docs
|
| 31 |
|
| 32 |
def build_vectorstore(docs):
|
|
|
|
| 33 |
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
| 34 |
vectorstore = FAISS.from_documents(docs, embeddings)
|
| 35 |
return vectorstore
|
| 36 |
|
| 37 |
def build_qa_chain(vectorstore):
|
|
|
|
| 38 |
llm = HuggingFacePipeline(
|
| 39 |
pipeline=pipeline(
|
| 40 |
"text2text-generation",
|
|
@@ -45,7 +48,7 @@ def build_qa_chain(vectorstore):
|
|
| 45 |
)
|
| 46 |
qa_chain = RetrievalQA.from_chain_type(
|
| 47 |
llm=llm,
|
| 48 |
-
retriever=vectorstore.as_retriever(search_kwargs={"k":3}),
|
| 49 |
chain_type="stuff"
|
| 50 |
)
|
| 51 |
return qa_chain
|
|
@@ -67,5 +70,5 @@ if qa_chain:
|
|
| 67 |
if query:
|
| 68 |
with st.spinner("Searching..."):
|
| 69 |
answer = qa_chain.run(query)
|
| 70 |
-
st.
|
| 71 |
st.write(answer)
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 3 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 4 |
+
from langchain_community.vectorstores import FAISS
|
| 5 |
+
from langchain_community.document_loaders import PyPDFLoader
|
| 6 |
from langchain.chains import RetrievalQA
|
| 7 |
+
from langchain.llms import HuggingFacePipeline
|
| 8 |
from transformers import pipeline
|
| 9 |
|
| 10 |
# ----------------------------
|
|
|
|
| 15 |
st.markdown("Upload a PDF and ask questions about its content.")
|
| 16 |
|
| 17 |
# ----------------------------
|
| 18 |
+
# GLOBAL VARIABLE
|
| 19 |
# ----------------------------
|
| 20 |
qa_chain = None
|
| 21 |
|
|
|
|
| 23 |
# FUNCTIONS
|
| 24 |
# ----------------------------
|
| 25 |
def load_pdf(pdf_file):
|
| 26 |
+
"""Load PDF and split into chunks"""
|
| 27 |
loader = PyPDFLoader(pdf_file.name)
|
| 28 |
documents = loader.load()
|
| 29 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=800, chunk_overlap=100)
|
|
|
|
| 31 |
return docs
|
| 32 |
|
| 33 |
def build_vectorstore(docs):
|
| 34 |
+
"""Create FAISS vector store from documents"""
|
| 35 |
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
| 36 |
vectorstore = FAISS.from_documents(docs, embeddings)
|
| 37 |
return vectorstore
|
| 38 |
|
| 39 |
def build_qa_chain(vectorstore):
|
| 40 |
+
"""Build QA chain using FLAN-T5"""
|
| 41 |
llm = HuggingFacePipeline(
|
| 42 |
pipeline=pipeline(
|
| 43 |
"text2text-generation",
|
|
|
|
| 48 |
)
|
| 49 |
qa_chain = RetrievalQA.from_chain_type(
|
| 50 |
llm=llm,
|
| 51 |
+
retriever=vectorstore.as_retriever(search_kwargs={"k": 3}),
|
| 52 |
chain_type="stuff"
|
| 53 |
)
|
| 54 |
return qa_chain
|
|
|
|
| 70 |
if query:
|
| 71 |
with st.spinner("Searching..."):
|
| 72 |
answer = qa_chain.run(query)
|
| 73 |
+
st.subheader("📌 Answer:")
|
| 74 |
st.write(answer)
|