Spaces:
Runtime error
Runtime error
Update generate_embeddings.py
Browse files- generate_embeddings.py +6 -17
generate_embeddings.py
CHANGED
|
@@ -1,18 +1,12 @@
|
|
| 1 |
import os
|
| 2 |
import time
|
| 3 |
from langchain_community.vectorstores import Chroma
|
| 4 |
-
from
|
| 5 |
-
from
|
| 6 |
from langchain_community.document_loaders import PyPDFLoader
|
| 7 |
|
| 8 |
-
# ==============================
|
| 9 |
-
# Folder path (IMPORTANT)
|
| 10 |
-
# ==============================
|
| 11 |
folder_path = "documents"
|
| 12 |
|
| 13 |
-
# ==============================
|
| 14 |
-
# Load PDFs
|
| 15 |
-
# ==============================
|
| 16 |
def load_pdfs(folder_path):
|
| 17 |
docs = []
|
| 18 |
for filename in os.listdir(folder_path):
|
|
@@ -29,9 +23,7 @@ def load_pdfs(folder_path):
|
|
| 29 |
print(f"Total documents loaded: {len(docs)}")
|
| 30 |
return docs
|
| 31 |
|
| 32 |
-
|
| 33 |
-
# Split documents
|
| 34 |
-
# ==============================
|
| 35 |
def split_documents(docs):
|
| 36 |
text_splitter = RecursiveCharacterTextSplitter(
|
| 37 |
chunk_size=500,
|
|
@@ -41,9 +33,7 @@ def split_documents(docs):
|
|
| 41 |
print(f"Total chunks: {len(chunks)}")
|
| 42 |
return chunks
|
| 43 |
|
| 44 |
-
|
| 45 |
-
# Create vector DB
|
| 46 |
-
# ==============================
|
| 47 |
def create_vectorstore(docs):
|
| 48 |
embedding = HuggingFaceEmbeddings(
|
| 49 |
model_name="sentence-transformers/all-MiniLM-L6-v2"
|
|
@@ -63,9 +53,7 @@ def create_vectorstore(docs):
|
|
| 63 |
print(f"Done in {time.time() - start} sec")
|
| 64 |
return vector_db
|
| 65 |
|
| 66 |
-
|
| 67 |
-
# Main
|
| 68 |
-
# ==============================
|
| 69 |
def main():
|
| 70 |
docs = load_pdfs(folder_path)
|
| 71 |
chunks = split_documents(docs)
|
|
@@ -73,5 +61,6 @@ def main():
|
|
| 73 |
|
| 74 |
print(f"Stored documents: {vector_db._collection.count()}")
|
| 75 |
|
|
|
|
| 76 |
if __name__ == "__main__":
|
| 77 |
main()
|
|
|
|
| 1 |
import os
|
| 2 |
import time
|
| 3 |
from langchain_community.vectorstores import Chroma
|
| 4 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 5 |
+
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 6 |
from langchain_community.document_loaders import PyPDFLoader
|
| 7 |
|
|
|
|
|
|
|
|
|
|
| 8 |
folder_path = "documents"
|
| 9 |
|
|
|
|
|
|
|
|
|
|
| 10 |
def load_pdfs(folder_path):
|
| 11 |
docs = []
|
| 12 |
for filename in os.listdir(folder_path):
|
|
|
|
| 23 |
print(f"Total documents loaded: {len(docs)}")
|
| 24 |
return docs
|
| 25 |
|
| 26 |
+
|
|
|
|
|
|
|
| 27 |
def split_documents(docs):
|
| 28 |
text_splitter = RecursiveCharacterTextSplitter(
|
| 29 |
chunk_size=500,
|
|
|
|
| 33 |
print(f"Total chunks: {len(chunks)}")
|
| 34 |
return chunks
|
| 35 |
|
| 36 |
+
|
|
|
|
|
|
|
| 37 |
def create_vectorstore(docs):
|
| 38 |
embedding = HuggingFaceEmbeddings(
|
| 39 |
model_name="sentence-transformers/all-MiniLM-L6-v2"
|
|
|
|
| 53 |
print(f"Done in {time.time() - start} sec")
|
| 54 |
return vector_db
|
| 55 |
|
| 56 |
+
|
|
|
|
|
|
|
| 57 |
def main():
|
| 58 |
docs = load_pdfs(folder_path)
|
| 59 |
chunks = split_documents(docs)
|
|
|
|
| 61 |
|
| 62 |
print(f"Stored documents: {vector_db._collection.count()}")
|
| 63 |
|
| 64 |
+
|
| 65 |
if __name__ == "__main__":
|
| 66 |
main()
|