Spaces:
Sleeping
Sleeping
Download build_db.py from amethyst39/appcreatingspace: direct link, hf CLI and curl.
- Browser
- Download file 676 Bytes
-
https://huggingface.co/spaces/amethyst39/appcreatingspace/resolve/main/build_db.py
- Command line
-
hf download hf://spaces/amethyst39/appcreatingspace/build_db.py
-
curl -L -o build_db.py https://huggingface.co/spaces/amethyst39/appcreatingspace/resolve/main/build_db.py
676 Bytes
| from langchain.document_loaders import PyPDFLoader, TextLoader | |
| from langchain.text_splitter import RecursiveCharacterTextSplitter | |
| from langchain.embeddings import OpenAIEmbeddings | |
| from langchain.vectorstores import FAISS | |
| import os | |
| docs = [] | |
| for file in os.listdir("data"): | |
| path = f"data/{file}" | |
| if file.endswith(".pdf"): | |
| docs.extend(PyPDFLoader(path).load()) | |
| elif file.endswith(".txt"): | |
| docs.extend(TextLoader(path).load()) | |
| splitter = RecursiveCharacterTextSplitter( | |
| chunk_size=600, | |
| chunk_overlap=100 | |
| ) | |
| chunks = splitter.split_documents(docs) | |
| db = FAISS.from_documents( | |
| chunks, | |
| OpenAIEmbeddings() | |
| ) | |
| db.save_local("vector_db") | |