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

Class CachedEvaluationModuleFactory

src/evaluate/loading.py:511–556  ·  view source on GitHub ↗

Get the module of a metric that has been loaded once already and cached. The script that is loaded from the cache is the most recent one with a matching name.

Source from the content-addressed store, hash-verified

509
510
511class CachedEvaluationModuleFactory(_EvaluationModuleFactory):
512 """
513 Get the module of a metric that has been loaded once already and cached.
514 The script that is loaded from the cache is the most recent one with a matching name.
515 """
516
517 def __init__(
518 self,
519 name: str,
520 module_type: str = "metrics",
521 dynamic_modules_path: Optional[str] = None,
522 ):
523 self.name = name
524 self.module_type = module_type
525 self.dynamic_modules_path = dynamic_modules_path
526 assert self.name.count("/") == 0
527
528 def get_module(self) -> ImportableModule:
529 dynamic_modules_path = self.dynamic_modules_path if self.dynamic_modules_path else init_dynamic_modules()
530 importable_directory_path = os.path.join(dynamic_modules_path, self.module_type, self.name)
531 hashes = (
532 [h for h in os.listdir(importable_directory_path) if len(h) == 64]
533 if os.path.isdir(importable_directory_path)
534 else None
535 )
536 if not hashes:
537 raise FileNotFoundError(f"Metric {self.name} is not cached in {dynamic_modules_path}")
538 # get most recent
539
540 def _get_modification_time(module_hash):
541 return (
542 (Path(importable_directory_path) / module_hash / (self.name.split("--")[-1] + ".py")).stat().st_mtime
543 )
544
545 hash = sorted(hashes, key=_get_modification_time)[-1]
546 logger.warning(
547 f"Using the latest cached version of the module from {os.path.join(importable_directory_path, hash)} "
548 f"(last modified on {time.ctime(_get_modification_time(hash))}) since it "
549 f"couldn't be found locally at {self.name}, or remotely on the Hugging Face Hub."
550 )
551 # make the new module to be noticed by the import system
552 module_path = ".".join(
553 [os.path.basename(dynamic_modules_path), self.module_type, self.name, hash, self.name.split("--")[-1]]
554 )
555 importlib.invalidate_caches()
556 return ImportableModule(module_path, hash)
557
558
559def evaluation_module_factory(

Callers 2

Calls

no outgoing calls

Tested by 1

Used in the wild real call sites across dependent graphs

searching dependent graphs…