Spaces:
Runtime error
Runtime error
Download scripts/setup_vectorstore.py from MaryamKarimi080/SCR_Course_ChatBot: direct link, hf CLI and curl.
- Browser
- Download file 917 Bytes
-
https://huggingface.co/spaces/MaryamKarimi080/SCR_Course_ChatBot/resolve/eb6b51df30dae4cf83ab3d65442cb2f0bf8b47bb/scripts/setup_vectorstore.py
- Command line
-
hf download hf://spaces/MaryamKarimi080/SCR_Course_ChatBot@eb6b51df30dae4cf83ab3d65442cb2f0bf8b47bb/scripts/setup_vectorstore.py
-
curl -L -o setup_vectorstore.py https://huggingface.co/spaces/MaryamKarimi080/SCR_Course_ChatBot/resolve/eb6b51df30dae4cf83ab3d65442cb2f0bf8b47bb/scripts/setup_vectorstore.py
917 Bytes
| import pickle | |
| from pathlib import Path | |
| from langchain_chroma import Chroma | |
| from langchain_openai import OpenAIEmbeddings | |
| def main(): | |
| BASE_DIR = Path(__file__).resolve().parent.parent | |
| CHUNKS_PATH = BASE_DIR / "output" / "chunks.pkl" | |
| DB_DIR = BASE_DIR / "db" | |
| DB_DIR.mkdir(parents=True, exist_ok=True) # <-- create the db folder if not exists | |
| with open(CHUNKS_PATH, "rb") as f: | |
| chunks = pickle.load(f) | |
| embedding = OpenAIEmbeddings(model="text-embedding-3-small") | |
| vectorstore = Chroma(persist_directory=str(DB_DIR), embedding_function=embedding) | |
| BATCH_SIZE = 100 | |
| print(f"🧠 Embedding and adding {len(chunks)} chunks in batches...") | |
| for i in range(0, len(chunks), BATCH_SIZE): | |
| batch = chunks[i:i + BATCH_SIZE] | |
| vectorstore.add_documents(batch) | |
| print(f"✅ Added batch {i // BATCH_SIZE + 1}") | |
| if __name__ == "__main__": | |
| main() | |