Spaces:
Runtime error
Runtime error
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,196 +0,0 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
from PyPDF2 import PdfReader
|
| 3 |
-
|
| 4 |
-
# Import Document schema to hold text + metadata (page number)
|
| 5 |
-
from langchain.schema import Document
|
| 6 |
-
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 7 |
-
from langchain_community.vectorstores import FAISS
|
| 8 |
-
from langchain_community.chat_models import ChatOllama
|
| 9 |
-
from langchain_community.embeddings import OllamaEmbeddings
|
| 10 |
-
|
| 11 |
-
from langchain.chains.question_answering import load_qa_chain
|
| 12 |
-
from langchain_core.prompts import PromptTemplate
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
@st.cache_data
|
| 16 |
-
def get_pdf_text(pdf_docs):
|
| 17 |
-
"""
|
| 18 |
-
Extracts text from PDFs and converts them into Document objects
|
| 19 |
-
containing the page number and filename in metadata.
|
| 20 |
-
"""
|
| 21 |
-
documents = []
|
| 22 |
-
for pdf in pdf_docs:
|
| 23 |
-
pdf_reader = PdfReader(pdf)
|
| 24 |
-
pdf_filename = pdf.name
|
| 25 |
-
|
| 26 |
-
for i, page in enumerate(pdf_reader.pages):
|
| 27 |
-
text = page.extract_text()
|
| 28 |
-
if text:
|
| 29 |
-
# Create a Document object for every page
|
| 30 |
-
# 'i + 1' ensures pages start at 1, not 0
|
| 31 |
-
doc = Document(
|
| 32 |
-
page_content=text,
|
| 33 |
-
metadata={"page": i + 1, "source": pdf_filename}
|
| 34 |
-
)
|
| 35 |
-
documents.append(doc)
|
| 36 |
-
return documents
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
@st.cache_data
|
| 40 |
-
def get_text_chunks(documents):
|
| 41 |
-
"""
|
| 42 |
-
Splits Document objects into manageable chunks while preserving metadata.
|
| 43 |
-
"""
|
| 44 |
-
text_splitter = RecursiveCharacterTextSplitter(
|
| 45 |
-
chunk_size=2000, # Reduced size for better page granularity
|
| 46 |
-
chunk_overlap=200
|
| 47 |
-
)
|
| 48 |
-
# split_documents (instead of split_text) keeps the metadata attached to chunks
|
| 49 |
-
chunks = text_splitter.split_documents(documents)
|
| 50 |
-
return chunks
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
def get_vector_store(text_chunks):
|
| 54 |
-
"""
|
| 55 |
-
Creates and saves a local FAISS vector store from Document chunks.
|
| 56 |
-
"""
|
| 57 |
-
if not text_chunks:
|
| 58 |
-
st.error("No text chunks found. Cannot create vector store.")
|
| 59 |
-
return
|
| 60 |
-
|
| 61 |
-
# Use the local Ollama embedding model
|
| 62 |
-
embeddings = OllamaEmbeddings(model="nomic-embed-text")
|
| 63 |
-
|
| 64 |
-
try:
|
| 65 |
-
# from_documents handles the Document objects including metadata
|
| 66 |
-
vector_store = FAISS.from_documents(documents=text_chunks, embedding=embeddings)
|
| 67 |
-
vector_store.save_local("faiss_index")
|
| 68 |
-
st.success("Vector store created and saved locally.")
|
| 69 |
-
except Exception as e:
|
| 70 |
-
st.error(f"Error creating vector store: {e}")
|
| 71 |
-
print(f"Error creating vector store: {e}")
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
def get_conversational_chain():
|
| 75 |
-
"""
|
| 76 |
-
Creates a question-answering chain with a local Ollama model.
|
| 77 |
-
"""
|
| 78 |
-
prompt_template = """
|
| 79 |
-
You are a helpful and informative AI assistant. Your primary goal is to answer questions based on the provided context, which consists of user-uploaded PDF documents.
|
| 80 |
-
Instructions:
|
| 81 |
-
1. First, thoroughly analyze the content of the user-uploaded PDFs to extract relevant information for answering the question.
|
| 82 |
-
2. If the answer can be fully derived from the PDF content, provide it directly and ensure accuracy and completeness.
|
| 83 |
-
3. If you are uncertain about the answer or if the PDFs and your knowledge do not provide enough information to form a conclusive answer, state clearly that the information is not sufficient to provide a definitive answer.
|
| 84 |
-
4. Avoid fabricating information or making assumptions that cannot be supported by the PDF content or reliable external knowledge.
|
| 85 |
-
|
| 86 |
-
**Important:** If the user prompt is not related to the PDF content,Just say "I don't know this answer".
|
| 87 |
-
**Important:** If the user prompt is not related to the PDF content,Never say the answer if you know then still dont say.
|
| 88 |
-
|
| 89 |
-
Context:\n{context}
|
| 90 |
-
Question: \n{question}
|
| 91 |
-
Answer:
|
| 92 |
-
"""
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
model = ChatOllama(model="gemma3:1b", temperature=0.3)
|
| 96 |
-
|
| 97 |
-
prompt = PromptTemplate(
|
| 98 |
-
template=prompt_template,
|
| 99 |
-
input_variables=["context", "question"]
|
| 100 |
-
)
|
| 101 |
-
chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
|
| 102 |
-
return chain
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
def load_faiss_index():
|
| 106 |
-
"""Loads the local FAISS index."""
|
| 107 |
-
try:
|
| 108 |
-
embeddings = OllamaEmbeddings(model="nomic-embed-text")
|
| 109 |
-
db = FAISS.load_local(
|
| 110 |
-
"faiss_index",
|
| 111 |
-
embeddings,
|
| 112 |
-
allow_dangerous_deserialization=True
|
| 113 |
-
)
|
| 114 |
-
return db
|
| 115 |
-
except Exception as e:
|
| 116 |
-
st.error(f"Failed to load FAISS index: {e}")
|
| 117 |
-
return None
|
| 118 |
-
|
| 119 |
-
def user_input(user_question, db):
|
| 120 |
-
"""
|
| 121 |
-
Handles user question, performs similarity search, gets response,
|
| 122 |
-
and extracts source page numbers programmatically.
|
| 123 |
-
"""
|
| 124 |
-
if db is None:
|
| 125 |
-
st.warning("Vector store is not loaded. Please process PDFs first.")
|
| 126 |
-
return
|
| 127 |
-
|
| 128 |
-
try:
|
| 129 |
-
# 1. Retrieve relevant chunks (Docs)
|
| 130 |
-
docs = db.similarity_search(user_question)
|
| 131 |
-
|
| 132 |
-
# 2. Generate Answer using LLM
|
| 133 |
-
chain = get_conversational_chain()
|
| 134 |
-
response = chain(
|
| 135 |
-
{"input_documents": docs, "question": user_question},
|
| 136 |
-
return_only_outputs=True
|
| 137 |
-
)
|
| 138 |
-
|
| 139 |
-
# 3. Display Answer
|
| 140 |
-
st.write("Reply: ", response["output_text"])
|
| 141 |
-
|
| 142 |
-
# 4. Display Sources (Page Numbers)
|
| 143 |
-
# We use a set to avoid duplicate page numbers if multiple chunks come from the same page
|
| 144 |
-
sources = set()
|
| 145 |
-
for doc in docs:
|
| 146 |
-
page_num = doc.metadata.get("page", "Unknown")
|
| 147 |
-
file_name = doc.metadata.get("source", "Unknown")
|
| 148 |
-
sources.add(f"{file_name} (Page {page_num})")
|
| 149 |
-
|
| 150 |
-
# Display the sources in an expandable box
|
| 151 |
-
with st.expander("📚 View Source Pages"):
|
| 152 |
-
for source in sources:
|
| 153 |
-
st.markdown(f"- {source}")
|
| 154 |
-
|
| 155 |
-
except Exception as e:
|
| 156 |
-
st.error(f"Error processing your question: {e}")
|
| 157 |
-
print(f"Error processing your question: {e}")
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
def main():
|
| 161 |
-
st.set_page_config(page_title="Chat PDF (Local)")
|
| 162 |
-
st.header("Chat with PDF using Gemma3:1b 🤖")
|
| 163 |
-
|
| 164 |
-
if "faiss_db" not in st.session_state:
|
| 165 |
-
st.session_state.faiss_db = None
|
| 166 |
-
|
| 167 |
-
with st.sidebar:
|
| 168 |
-
st.title("Menu:")
|
| 169 |
-
pdf_docs = st.file_uploader(
|
| 170 |
-
"Upload your PDF Files and Click on the Process Button",
|
| 171 |
-
accept_multiple_files=True
|
| 172 |
-
)
|
| 173 |
-
|
| 174 |
-
if st.button("Process PDFs"):
|
| 175 |
-
if pdf_docs:
|
| 176 |
-
with st.spinner("Processing PDF files..."):
|
| 177 |
-
# Pass document objects now, not raw text
|
| 178 |
-
raw_documents = get_pdf_text(pdf_docs)
|
| 179 |
-
text_chunks = get_text_chunks(raw_documents)
|
| 180 |
-
get_vector_store(text_chunks)
|
| 181 |
-
|
| 182 |
-
st.session_state.faiss_db = load_faiss_index()
|
| 183 |
-
else:
|
| 184 |
-
st.warning("Please upload at least one PDF file.")
|
| 185 |
-
|
| 186 |
-
user_question = st.text_input("Ask a Question from the PDF Files")
|
| 187 |
-
|
| 188 |
-
if user_question:
|
| 189 |
-
if st.session_state.faiss_db is not None:
|
| 190 |
-
user_input(user_question, st.session_state.faiss_db)
|
| 191 |
-
else:
|
| 192 |
-
st.warning("Please upload and process at least one PDF first.")
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
if __name__ == "__main__":
|
| 196 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|