Spaces:
Runtime error
Runtime error
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,131 +0,0 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
from PyPDF2 import PdfReader
|
| 3 |
-
|
| 4 |
-
# β
Updated LangChain imports
|
| 5 |
-
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 6 |
-
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 7 |
-
from langchain_community.vectorstores import FAISS
|
| 8 |
-
|
| 9 |
-
from transformers import pipeline
|
| 10 |
-
|
| 11 |
-
# -------------------------------
|
| 12 |
-
# PAGE CONFIG
|
| 13 |
-
# -------------------------------
|
| 14 |
-
st.set_page_config(
|
| 15 |
-
page_title="Multi-PDF Chat Agent",
|
| 16 |
-
page_icon="π",
|
| 17 |
-
layout="wide"
|
| 18 |
-
)
|
| 19 |
-
|
| 20 |
-
st.title("π Multi-PDF Chat Agent")
|
| 21 |
-
st.caption("Upload PDFs and ask questions from them")
|
| 22 |
-
|
| 23 |
-
# -------------------------------
|
| 24 |
-
# SESSION STATE
|
| 25 |
-
# -------------------------------
|
| 26 |
-
if "vector_store" not in st.session_state:
|
| 27 |
-
st.session_state.vector_store = None
|
| 28 |
-
|
| 29 |
-
# -------------------------------
|
| 30 |
-
# LOAD QA MODEL (FIXED)
|
| 31 |
-
# -------------------------------
|
| 32 |
-
@st.cache_resource
|
| 33 |
-
def load_qa_pipeline():
|
| 34 |
-
return pipeline(
|
| 35 |
-
"question-answering",
|
| 36 |
-
model="distilbert-base-cased-distilled-squad"
|
| 37 |
-
)
|
| 38 |
-
|
| 39 |
-
qa_pipeline = load_qa_pipeline()
|
| 40 |
-
|
| 41 |
-
# -------------------------------
|
| 42 |
-
# SIDEBAR
|
| 43 |
-
# -------------------------------
|
| 44 |
-
with st.sidebar:
|
| 45 |
-
st.header("π Upload PDFs")
|
| 46 |
-
|
| 47 |
-
uploaded_files = st.file_uploader(
|
| 48 |
-
"Upload one or more PDF files",
|
| 49 |
-
type="pdf",
|
| 50 |
-
accept_multiple_files=True
|
| 51 |
-
)
|
| 52 |
-
|
| 53 |
-
if st.button("β‘ Process PDFs"):
|
| 54 |
-
if not uploaded_files:
|
| 55 |
-
st.warning("Please upload at least one PDF.")
|
| 56 |
-
else:
|
| 57 |
-
with st.spinner("Processing documents..."):
|
| 58 |
-
|
| 59 |
-
text = ""
|
| 60 |
-
|
| 61 |
-
# β
Extract text safely
|
| 62 |
-
for file in uploaded_files:
|
| 63 |
-
pdf_reader = PdfReader(file)
|
| 64 |
-
for page in pdf_reader.pages:
|
| 65 |
-
page_text = page.extract_text()
|
| 66 |
-
if page_text:
|
| 67 |
-
text += page_text
|
| 68 |
-
|
| 69 |
-
if not text.strip():
|
| 70 |
-
st.error("No readable text found in PDFs.")
|
| 71 |
-
else:
|
| 72 |
-
# β
Split text
|
| 73 |
-
splitter = RecursiveCharacterTextSplitter(
|
| 74 |
-
chunk_size=800,
|
| 75 |
-
chunk_overlap=100
|
| 76 |
-
)
|
| 77 |
-
chunks = splitter.split_text(text)
|
| 78 |
-
|
| 79 |
-
# β
Embeddings
|
| 80 |
-
embeddings = HuggingFaceEmbeddings(
|
| 81 |
-
model_name="sentence-transformers/all-MiniLM-L6-v2"
|
| 82 |
-
)
|
| 83 |
-
|
| 84 |
-
# β
Vector store
|
| 85 |
-
vector_store = FAISS.from_texts(
|
| 86 |
-
texts=chunks,
|
| 87 |
-
embedding=embeddings
|
| 88 |
-
)
|
| 89 |
-
|
| 90 |
-
st.session_state.vector_store = vector_store
|
| 91 |
-
st.success("β
PDFs processed successfully!")
|
| 92 |
-
|
| 93 |
-
# -------------------------------
|
| 94 |
-
# MAIN CHAT
|
| 95 |
-
# -------------------------------
|
| 96 |
-
if st.session_state.vector_store:
|
| 97 |
-
|
| 98 |
-
st.subheader("π¬ Ask Questions")
|
| 99 |
-
|
| 100 |
-
question = st.text_input(
|
| 101 |
-
"Enter your question:",
|
| 102 |
-
placeholder="What is the main topic?"
|
| 103 |
-
)
|
| 104 |
-
|
| 105 |
-
if question:
|
| 106 |
-
with st.spinner("Thinking..."):
|
| 107 |
-
|
| 108 |
-
# β
Retrieve relevant chunks
|
| 109 |
-
docs = st.session_state.vector_store.similarity_search(
|
| 110 |
-
question,
|
| 111 |
-
k=2 # reduced for HF memory safety
|
| 112 |
-
)
|
| 113 |
-
|
| 114 |
-
context = "\n".join([doc.page_content for doc in docs])
|
| 115 |
-
|
| 116 |
-
# β
Get answer
|
| 117 |
-
result = qa_pipeline(
|
| 118 |
-
question=question,
|
| 119 |
-
context=context
|
| 120 |
-
)
|
| 121 |
-
|
| 122 |
-
st.markdown("### β
Answer")
|
| 123 |
-
st.write(result["answer"])
|
| 124 |
-
|
| 125 |
-
st.markdown("### π Source Excerpts")
|
| 126 |
-
for i, doc in enumerate(docs):
|
| 127 |
-
st.markdown(f"**Excerpt {i+1}:**")
|
| 128 |
-
st.write(doc.page_content)
|
| 129 |
-
|
| 130 |
-
else:
|
| 131 |
-
st.info("π Upload and process PDFs to begin.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|