Skip to main content

Index your local folders

This guide will help you upload files from a local folder and its subfolders to your Nuclia knowledgebox.

Prerequisites

  1. API Key: Obtain a contributor or writer API key as detailed here.

  2. Python 3: Ensure Python 3 is installed on your system:

    python --version
  3. Nuclia SDK: Install the Nuclia package in your environment:

    pip install nuclia

Run the script

Use the following Python script to recursively upload files from a specified folder to your Nuclia knowledgebox:

import sys
from pathlib import Path

from nuclia import sdk

# File extensions to ignore during the upload process, feel free to edit it
IGNORE_EXTENSIONS = (
".pptx",
".py",
)

# Nuclia knowledge box URL and API key
KNOWLEDGE_BOX_URL = "https://<zone>.nuclia.cloud/api/v1/kb/<your-knowledge-box-id>"
API_KEY = "<your-api-key>"

# Authenticate with the Nuclia SDK
sdk.NucliaAuth().kb(url=KNOWLEDGE_BOX_URL, token=API_KEY)


def is_hidden(path: Path) -> bool:
"""
Check if the given path is hidden.
"""
return any(part.startswith(".") for part in path.parts)


def upload_folder(folder_path: str):
"""
Uploads all files from the specified folder and subfolders to the knowledge box,
excluding files with extensions listed in IGNORE_EXTENSIONS and hidden paths.

Args:
folder_path (str): The path to the folder containing files to upload.
"""
# Recursively get all file paths in the specified folder
paths = Path(folder_path).rglob("*")

# Filter out directories, hidden files, and files with ignored extensions
filtered_files = [
str(path)
for path in paths
if path.is_file() and not is_hidden(path) and path.suffix not in IGNORE_EXTENSIONS
]

# Upload each filtered file to the Nuclia knowledge box
for file_path in filtered_files:
sdk.NucliaUpload().file(path=file_path)


if __name__ == "__main__":
# Ensure a folder path argument is provided
if len(sys.argv) != 2:
print("Usage: python script.py <folder_path>")
sys.exit(1)

# Get the root folder path from command line arguments
folder_path = sys.argv[1]

# Upload files from the specified folder
upload_folder(folder_path)

To execute the script and start uploading files, run the following command:

python3 script.py /path/to/upload

Replace /path/to/upload with the path to the folder you want to upload.

For more information on the Nuclia Python SDK, see the Nuclia Python SDK documentation.

tip

Consider using the Nuclia Sync Agent for continuous synchronization. The Sync Agent not only uploads your existing files but also automatically uploads any new files added to your local folder, ensuring your knowledgebox stays up-to-date.