Spaces:
Sleeping
Sleeping
Download create_db.py from Shivam-99x/Trying: direct link, hf CLI and curl.
- Browser
- Download file 1.39 kB
-
https://huggingface.co/spaces/Shivam-99x/Trying/resolve/main/create_db.py
- Command line
-
hf download hf://spaces/Shivam-99x/Trying/create_db.py
-
curl -L -o create_db.py https://huggingface.co/spaces/Shivam-99x/Trying/resolve/main/create_db.py
1.39 kB
| import os | |
| from langchain_huggingface import HuggingFaceEmbeddings | |
| from langchain_chroma import Chroma | |
| from langchain_community.document_loaders import TextLoader | |
| from langchain_text_splitters import CharacterTextSplitter | |
| from dotenv import load_dotenv | |
| # 1. Load Environment Variables | |
| load_dotenv() | |
| # 2. Setup Embeddings (The tool that turns text into numbers) | |
| # We use a free, high-quality model from HuggingFace | |
| print("Loading embedding model...") | |
| embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-mpnet-base-v2") | |
| # 3. Load Your Data | |
| print("Loading text file...") | |
| # Ensure you have created 'club_info.txt' in the same folder | |
| loader = TextLoader("club_info.txt", encoding="utf-8") | |
| documents = loader.load() | |
| # 4. Chunk the Data | |
| # We split the text into smaller pieces (500 characters) so the AI can digest it easily | |
| text_splitter = CharacterTextSplitter(chunk_size=500, chunk_overlap=50) | |
| chunks = text_splitter.split_documents(documents) | |
| print(f"Split data into {len(chunks)} chunks.") | |
| # 5. Create & Save the Database | |
| # This creates a folder named 'chroma_db' which acts as your permanent memory | |
| print("Creating database... (This might take a moment)") | |
| db = Chroma.from_documents( | |
| documents=chunks, | |
| embedding=embeddings, | |
| persist_directory="chroma_db" | |
| ) | |
| print("✅ Success! Database created in 'chroma_db' folder.") |