MCPcopy Create free account
hub / github.com/huggingface/evaluate / HubEvaluationModuleFactory

Class HubEvaluationModuleFactory

src/evaluate/loading.py:443–508  ·  view source on GitHub ↗

Get the module of a metric from a metric repository on the Hub.

Source from the content-addressed store, hash-verified

441
442
443class HubEvaluationModuleFactory(_EvaluationModuleFactory):
444 """Get the module of a metric from a metric repository on the Hub."""
445
446 def __init__(
447 self,
448 name: str,
449 module_type: str = "metrics",
450 revision: Optional[Union[str, Version]] = None,
451 download_config: Optional[DownloadConfig] = None,
452 download_mode: Optional[DownloadMode] = None,
453 dynamic_modules_path: Optional[str] = None,
454 ):
455 self.name = name
456 self.module_type = module_type
457 self.revision = revision
458 self.download_config = download_config or DownloadConfig()
459 self.download_mode = download_mode
460 self.dynamic_modules_path = dynamic_modules_path
461 assert self.name.count("/") == 1
462 increase_load_count(name, resource_type="metric")
463
464 def download_loading_script(self, revision) -> str:
465 file_path = hf_hub_url(path=self.name, name=self.name.split("/")[1] + ".py", revision=revision)
466 download_config = self.download_config.copy()
467 if download_config.download_desc is None:
468 download_config.download_desc = "Downloading builder script"
469 return cached_path(file_path, download_config=download_config)
470
471 def get_module(self) -> ImportableModule:
472 revision = self.revision or os.getenv("HF_SCRIPTS_VERSION", SCRIPTS_VERSION)
473
474 if re.match(r"\d*\.\d*\.\d*", revision): # revision is version number (three digits separated by full stops)
475 revision = "v" + revision # tagging convention on evaluate repository starts with v
476
477 # get script and other files
478 try:
479 local_path = self.download_loading_script(revision)
480 except FileNotFoundError as err:
481 # if there is no file found with current revision tag try to load main
482 if self.revision is None and os.getenv("HF_SCRIPTS_VERSION", SCRIPTS_VERSION) != "main":
483 revision = "main"
484 local_path = self.download_loading_script(revision)
485 else:
486 raise err
487
488 imports = get_imports(local_path)
489 local_imports = _download_additional_modules(
490 name=self.name,
491 base_path=hf_hub_url(path=self.name, name="", revision=revision),
492 imports=imports,
493 download_config=self.download_config,
494 )
495 # copy the script and the files in an importable directory
496 dynamic_modules_path = self.dynamic_modules_path if self.dynamic_modules_path else init_dynamic_modules()
497 module_path, hash = _create_importable_file(
498 local_path=local_path,
499 local_imports=local_imports,
500 additional_files=[],

Calls

no outgoing calls

Used in the wild real call sites across dependent graphs

searching dependent graphs…