MCPcopy
hub / github.com/MaartenGr/BERTopic / load_files_from_hf

Function load_files_from_hf

bertopic/_save_utils.py:224–268  ·  view source on GitHub ↗

Load files from HuggingFace.

(path)

Source from the content-addressed store, hash-verified

222
223
224def load_files_from_hf(path):
225 """Load files from HuggingFace."""
226 path = str(path)
227
228 # Configs
229 topics = load_cfg_from_json(hf_hub_download(path, TOPICS_NAME, revision=None))
230 params = load_cfg_from_json(hf_hub_download(path, CONFIG_NAME, revision=None))
231
232 # Topic Embeddings
233 try:
234 tensors = hf_hub_download(path, HF_SAFE_WEIGHTS_NAME, revision=None)
235 tensors = load_safetensors(tensors)
236 except: # noqa: E722
237 tensors = hf_hub_download(path, HF_WEIGHTS_NAME, revision=None)
238 tensors = torch.load(tensors, map_location="cpu")
239
240 # c-TF-IDF
241 try:
242 ctfidf_config = load_cfg_from_json(hf_hub_download(path, CTFIDF_CFG_NAME, revision=None))
243 try:
244 ctfidf_tensors = hf_hub_download(path, CTFIDF_SAFE_WEIGHTS_NAME, revision=None)
245 ctfidf_tensors = load_safetensors(ctfidf_tensors)
246 except: # noqa: E722
247 ctfidf_tensors = hf_hub_download(path, CTFIDF_WEIGHTS_NAME, revision=None)
248 ctfidf_tensors = torch.load(ctfidf_tensors, map_location="cpu")
249 except: # noqa: E722
250 ctfidf_config, ctfidf_tensors = None, None
251
252 # Load images if they exist
253 images = None
254 if _has_vision:
255 try:
256 hf_hub_download(path, "images/0.jpg", revision=None)
257 _has_images = True
258 except: # noqa: E722
259 _has_images = False
260
261 if _has_images:
262 topic_list = list(topics["topic_representations"].keys())
263 images = {}
264 for topic in topic_list:
265 image = Image.open(hf_hub_download(path, f"images/{topic}.jpg", revision=None))
266 images[int(topic)] = image
267
268 return topics, params, tensors, ctfidf_tensors, ctfidf_config, images
269
270
271def generate_readme(model, repo_id: str):

Callers

nothing calls this directly

Calls 3

load_cfg_from_jsonFunction · 0.85
load_safetensorsFunction · 0.85
loadMethod · 0.80

Tested by

no test coverage detected