Load a Vicinity instance from the Hugging Face Hub. :param repo_id: The repository ID on the Hugging Face Hub. :param token: Optional authentication token for private repositories. :param **kwargs: Additional arguments passed to load_dataset. :return: A Vicinity instance loaded
(
repo_id: str, token: str | None = None, **kwargs: Any
)
| 99 | |
| 100 | |
| 101 | def load_from_hub( |
| 102 | repo_id: str, token: str | None = None, **kwargs: Any |
| 103 | ) -> tuple[list[dict[str, Any]], BasicVectorStore | None, AbstractBackend, dict[str, Any]]: |
| 104 | """ |
| 105 | Load a Vicinity instance from the Hugging Face Hub. |
| 106 | |
| 107 | :param repo_id: The repository ID on the Hugging Face Hub. |
| 108 | :param token: Optional authentication token for private repositories. |
| 109 | :param **kwargs: Additional arguments passed to load_dataset. |
| 110 | :return: A Vicinity instance loaded from the Hub. |
| 111 | """ |
| 112 | # Load dataset and extract items and vectors |
| 113 | dataset = load_dataset(repo_id, token=token, split="train", **kwargs) |
| 114 | has_vectors = "vectors" in dataset.column_names |
| 115 | if has_vectors: |
| 116 | vectors = dataset["vectors"] |
| 117 | if _VICINITY_ITEM_COLUMN in dataset.column_names: |
| 118 | items = dataset[_VICINITY_ITEM_COLUMN] |
| 119 | else: |
| 120 | # Remove the vectors column (if it exists) and then convert to a list. |
| 121 | if has_vectors: |
| 122 | dataset = dataset.remove_columns("vectors") |
| 123 | items = dataset.to_list() |
| 124 | vector_store = BasicVectorStore(vectors=vectors) if has_vectors else None |
| 125 | |
| 126 | # Download and load config and backend |
| 127 | repo_path = Path(snapshot_download(repo_id=repo_id, token=token, repo_type="dataset")) |
| 128 | with open(repo_path / "config.json") as f: |
| 129 | config = json.load(f) |
| 130 | backend_type = Backend(config["backend_type"]) |
| 131 | backend = get_backend_class(backend_type).load(repo_path / "backend") |
| 132 | return items, vector_store, backend, config |
no test coverage detected
searching dependent graphs…