Spaces:
Build error
Build error
SHAMIL SHAHBAZ AWAN commited on
Update app.py
Browse files12345613 unmodified lines202122232425262728296 unmodified lines3637383940414243444546474849505152535455565758596061626364656667686970import osimport streamlit as stfrom PyPDF2 import PdfReaderfrom 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 documentsdef chunk_text(text, chunk_size=500, overlap=100):chunks = []for i in range(0, len(text), chunk_size - overlap):chunks.append(text[i:i + chunk_size])return chunksif 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))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!")# User interfacest.title("Atomic Habits RAG Application")12345613 unmodified lines2021222324256 unmodified lines32333435363738394041424344454647484950515253545556575859606162import osimport streamlit as stimport pdfplumberfrom sentence_transformers import SentenceTransformerfrom transformers import pipelineimport faiss13 unmodified lines# Pathsfile_path = "RagBaseApp/Atomic habits ( PDFDrive ).pdf"VECTORSTORE_FOLDER = "vectorstore"# Initialize FAISS vector store6 unmodified lineselse:index = faiss.IndexFlatL2(embedder.get_sentence_embedding_dimension())# 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 textdef 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 chunksif 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("PDF processed and vectorstore updated!")# User interfacest.title("Atomic Habits RAG Application")
app.py+15-23