themanas021 commited on
Commit
ac44904
·
1 Parent(s): 2d63e89

Update ingest.py

Browse files
Files changed (1) hide show
  1. ingest.py +24 -29
ingest.py CHANGED
@@ -1,37 +1,32 @@
1
- from langchain.document_loaders import PyPDFLoader
2
- from langchain.text_splitter import RecursiveCharacterTextSplitter
3
- from langchain.embeddings import SentenceTransformerEmbeddings
4
- from langchain.vectorstores import Chroma
5
- import base64
6
- import io
7
 
8
  persist_directory = "db"
9
 
10
- def process_pdf_content(pdf_content):
11
- # Process the PDF content here and generate a brief summary.
12
- # You can use libraries like PyPDF2, pdfminer, or other PDF processing tools.
13
-
14
- # For now, let's assume we have extracted the text from the PDF.
15
- pdf_text = "This is a brief summary of the PDF content."
16
-
17
- return pdf_text
18
-
19
- def create_embeddings(pdf_text):
20
- print("Loading Sentence Transformers model")
 
21
  embeddings = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")
22
- print("Creating embeddings. This may take some time...")
23
- db = Chroma.from_documents([pdf_text], embeddings, persist_directory=persist_directory)
 
24
  db.persist()
25
- print("Embeddings creation complete!")
26
 
27
- def main(uploaded_file):
28
- if uploaded_file is not None:
29
- with io.BytesIO(uploaded_file.read()) as pdf_buffer:
30
- pdf_content = pdf_buffer.read()
31
- pdf_summary = process_pdf_content(pdf_content)
32
- create_embeddings(pdf_summary)
33
 
34
  if __name__ == "__main__":
35
- # Replace None with the uploaded PDF file
36
- uploaded_pdf_file = None # Replace with the actual uploaded PDF file
37
- main(uploaded_pdf_file)
 
1
+ from langchain.document_loaders import PyPDFLoader, DirectoryLoader, PDFMinerLoader
2
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
3
+ from langchain.embeddings import SentenceTransformerEmbeddings
4
+ from langchain.vectorstores import Chroma
5
+ import os
6
+ from constants import CHROMA_SETTINGS
7
 
8
  persist_directory = "db"
9
 
10
+ def main():
11
+ for root, dirs, files in os.walk("docs"):
12
+ for file in files:
13
+ if file.endswith(".pdf"):
14
+ print(file)
15
+ loader = PyPDFLoader(os.path.join(root, file))
16
+ documents = loader.load()
17
+ print("splitting into chunks")
18
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=100)
19
+ texts = text_splitter.split_documents(documents)
20
+ #create embeddings here
21
+ print("Loading sentence transformers model")
22
  embeddings = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")
23
+ #create vector store here
24
+ print(f"Creating embeddings. May take some minutes...")
25
+ db = Chroma.from_documents(texts, embeddings, persist_directory=persist_directory, client_settings=CHROMA_SETTINGS)
26
  db.persist()
27
+ db=None
28
 
29
+ print(f"Ingestion complete! You can now run privateGPT.py to query your documents")
 
 
 
 
 
30
 
31
  if __name__ == "__main__":
32
+ main()