Spaces:
Build error
Build error
SHAMIL SHAHBAZ AWAN commited on
Update app.py
Browse files123345613 unmodified lines202122232425262324256 unmodified lines32333439404142434445464748493536373839404142434445464748495057585960616251525354555657586759606162import osimport streamlit as stfrom PyPDF2 import PdfReaderimport pdfplumberfrom sentence_transformers import SentenceTransformerfrom transformers import pipelineimport faiss13 unmodified lines
# Pathsfile_path = "RagBaseApp/Atomic habits ( PDFDrive ).pdf"
with pdfplumber.open(file_path) as pdf: for page in pdf.pages: print(page.extract_text()) VECTORSTORE_FOLDER = "vectorstore"
# Initialize FAISS vector store6 unmodified lineselse: index = faiss.IndexFlatL2(embedder.get_sentence_embedding_dimension())
# Load and process documentsdef load_documents(folder): documents = [] for filename in os.listdir(folder): if filename.endswith(".pdf"): pdf_reader = PdfReader(os.path.join(folder, filename)) text = "" for page in pdf_reader.pages: text += page.extract_text() documents.append(text) return documents# Load and process the PDF filedef load_pdf_text(file_path): """Extract text from a PDF file.""" text = "" with pdfplumber.open(file_path) as pdf: for page in pdf.pages: text += page.extract_text() return text
def chunk_text(text, chunk_size=500, overlap=100): """Split text into overlapping chunks.""" chunks = [] for i in range(0, len(text), chunk_size - overlap): chunks.append(text[i:i + chunk_size]) return chunks
if st.button("Process Documents"): st.info("Processing documents...") all_text = load_documents(DOCUMENTS_FOLDER) chunks = [] for text in all_text: chunks.extend(chunk_text(text))if st.button("Process PDF"): st.info("Processing PDF document...") text = load_pdf_text(file_path) chunks = chunk_text(text) embeddings = embedder.encode(chunks, show_progress_bar=True) index.add(np.array(embeddings)) faiss.write_index(index, vectorstore_path) st.success("Documents processed and vectorstore updated!") st.success("PDF processed and vectorstore updated!")
# User interfacest.title("Atomic Habits RAG Application")app.py+15-23