Spaces:
Running
Running
Download PDF_Reader.py from Mr-TD/RAG-PDF-QnA-ChatBot: direct link, hf CLI and curl.
- Browser
- Download file 943 Bytes
-
https://huggingface.co/spaces/Mr-TD/RAG-PDF-QnA-ChatBot/resolve/main/PDF_Reader.py
- Command line
-
hf download hf://spaces/Mr-TD/RAG-PDF-QnA-ChatBot/PDF_Reader.py
-
curl -L -o PDF_Reader.py https://huggingface.co/spaces/Mr-TD/RAG-PDF-QnA-ChatBot/resolve/main/PDF_Reader.py
943 Bytes
| import PyPDF2 | |
| from langchain.text_splitter import RecursiveCharacterTextSplitter | |
| from langchain.embeddings import HuggingFaceBgeEmbeddings | |
| from langchain.vectorstores import FAISS | |
| def read_pdf(uploaded_file): | |
| pdf_reader = PyPDF2.PdfReader(uploaded_file) | |
| text = "" | |
| for page in pdf_reader.pages: | |
| text += page.extract_text() | |
| return text | |
| def Chunks(docs): | |
| text_splitter = RecursiveCharacterTextSplitter( | |
| # Set a really small chunk size, just to show. | |
| chunk_size = 1000, | |
| chunk_overlap = 100, | |
| ) | |
| doc = text_splitter.split_text(docs) | |
| return doc | |
| def PDF_4_QA(file): | |
| content = read_pdf(file) | |
| pdf_chunks = Chunks(docs=content) | |
| embeddings = HuggingFaceBgeEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2', | |
| model_kwargs={'device': 'cpu'}) | |
| vectorstore_openai = FAISS.from_texts(pdf_chunks, embeddings) | |
| return vectorstore_openai |