[docs]@classmethoddefload_config(cls,config_path:Optional[str]=None,**overrides)->"Config":""" Load YAML configuration file and apply overrides. Args: config_path (Optional[str]): Path to the configuration YAML file. **overrides: Dictionary of command-line overrides. Returns: Config: An instance of the Config class with loaded values. """config=cls()ifconfig_pathandPath(config_path).resolve().exists():withopen(config_path,"r",encoding="utf8")asfile:yaml_data=yaml.safe_load(file)or{}config=cls(**yaml_data)config.dataset.model_name=config.training.model_nameconfig._apply_overrides(overrides)config._create_directories()returnconfig
def_apply_overrides(self,overrides):""" Applies CLI override values to the configuration. Args: overrides (dict): Dictionary of values to override. """forkey,valueinoverrides.items():ifvalueisNone:continueforsection_nameinself.__annotations__:# Iterate over attributessection=getattr(self,section_name)ifhasattr(section,key):# Check if the override key exists in the sectionsetattr(section,key,value)breakdef_create_directories(self):""" Creates required directories if they do not exist. Prints the directories that were created. """created_dirs=[dir_pathfordir_pathinvars(self.directories).values()ifnotPath(dir_path).exists()andPath(dir_path).mkdir(parents=True,exist_ok=True)isNone]ifcreated_dirs:print("Created directories:\n"+"\n".join(created_dirs))