Load local BERTopic files.
(path)
| 172 | |
| 173 | |
| 174 | def load_local_files(path): |
| 175 | """Load local BERTopic files.""" |
| 176 | # Load json configs |
| 177 | topics = load_cfg_from_json(path / TOPICS_NAME) |
| 178 | params = load_cfg_from_json(path / CONFIG_NAME) |
| 179 | |
| 180 | # Load Topic Embeddings |
| 181 | safetensor_path = path / HF_SAFE_WEIGHTS_NAME |
| 182 | if safetensor_path.is_file(): |
| 183 | tensors = load_safetensors(safetensor_path) |
| 184 | else: |
| 185 | torch_path = path / HF_WEIGHTS_NAME |
| 186 | if torch_path.is_file(): |
| 187 | tensors = torch.load(torch_path, map_location="cpu") |
| 188 | tensors = {k: v.numpy() for k, v in tensors.items()} |
| 189 | |
| 190 | # c-TF-IDF |
| 191 | try: |
| 192 | ctfidf_tensors = None |
| 193 | safetensor_path = path / CTFIDF_SAFE_WEIGHTS_NAME |
| 194 | if safetensor_path.is_file(): |
| 195 | ctfidf_tensors = load_safetensors(safetensor_path) |
| 196 | else: |
| 197 | torch_path = path / CTFIDF_WEIGHTS_NAME |
| 198 | if torch_path.is_file(): |
| 199 | ctfidf_tensors = torch.load(torch_path, map_location="cpu") |
| 200 | ctfidf_tensors = {k: v.numpy() for k, v in ctfidf_tensors.items()} |
| 201 | ctfidf_config = load_cfg_from_json(path / CTFIDF_CFG_NAME) |
| 202 | except: # noqa: E722 |
| 203 | ctfidf_config, ctfidf_tensors = None, None |
| 204 | |
| 205 | # Load images |
| 206 | images = None |
| 207 | if _has_vision: |
| 208 | try: |
| 209 | Image.open(path / "images/0.jpg") |
| 210 | _has_images = True |
| 211 | except: # noqa: E722 |
| 212 | _has_images = False |
| 213 | |
| 214 | if _has_images: |
| 215 | topic_list = list(topics["topic_representations"].keys()) |
| 216 | images = {} |
| 217 | for topic in topic_list: |
| 218 | image = Image.open(path / f"images/{topic}.jpg") |
| 219 | images[int(topic)] = image |
| 220 | |
| 221 | return topics, params, tensors, ctfidf_tensors, ctfidf_config, images |
| 222 | |
| 223 | |
| 224 | def load_files_from_hf(path): |
nothing calls this directly
no test coverage detected