in src/huggingface_inference_toolkit/utils.py [0:0]
def check_and_register_custom_pipeline_from_directory(model_dir):
"""
Checks if a custom pipeline is available and registers it if so.
"""
# path to custom handler
custom_module = Path(model_dir).joinpath(HF_DEFAULT_PIPELINE_NAME)
legacy_module = Path(model_dir).joinpath("pipeline.py")
if custom_module.is_file():
logger.info(f"Found custom pipeline at {custom_module}")
spec = importlib.util.spec_from_file_location(HF_MODULE_NAME, custom_module)
if spec:
# add the whole directory to path for submodlues
sys.path.insert(0, model_dir)
# import custom handler
handler = importlib.util.module_from_spec(spec)
sys.modules[HF_MODULE_NAME] = handler
spec.loader.exec_module(handler)
# init custom handler with model_dir
custom_pipeline = handler.EndpointHandler(model_dir)
elif legacy_module.is_file():
logger.warning(
"""You are using a legacy custom pipeline.
Please update to the new format.
See documentation for more information."""
)
spec = importlib.util.spec_from_file_location("pipeline.PreTrainedPipeline", legacy_module)
if spec:
# add the whole directory to path for submodlues
sys.path.insert(0, model_dir)
# import custom handler
pipeline = importlib.util.module_from_spec(spec)
sys.modules["pipeline.PreTrainedPipeline"] = pipeline
spec.loader.exec_module(pipeline)
# init custom handler with model_dir
custom_pipeline = pipeline.PreTrainedPipeline(model_dir)
else:
logger.info(f"No custom pipeline found at {custom_module}")
custom_pipeline = None
return custom_pipeline