@@ -494,6 +494,11 @@ def __init__(
494494 # Complete the init if the bundle path is known, otherwise delay till the compute function is called
495495 # and try to get the model/bundle path from the execution context.
496496 try :
497+ # Original design is to rely on the Model Factory to find and load the model bundle file path, hence,
498+ # Complete the init if the bundle path is known, otherwise delay till the compute function is called
499+ # and try to get the model/bundle path from the execution context.
500+ # But supporting directory-based bundle with the app generator CLI may require the bundle path to be set
501+
497502 self ._bundle_path = Path (bundle_path ) if bundle_path and len (str (bundle_path ).strip ()) > 0 else None
498503
499504 if self ._bundle_path and self ._bundle_path .is_file ():
@@ -537,9 +542,10 @@ def bundle_path(self) -> Union[Path, None]:
537542
538543 @bundle_path .setter
539544 def bundle_path (self , bundle_path : Union [str , Path ]):
540- if not bundle_path or not Path (bundle_path ).expanduser ().is_file ():
545+ if bundle_path and (Path (bundle_path ).expanduser ().is_file () or Path (bundle_path ).expanduser ().is_dir ()):
546+ self ._bundle_path = Path (bundle_path ).expanduser ().resolve ()
547+ else :
541548 raise ValueError (f"Value, { bundle_path } , is not a valid file path." )
542- self ._bundle_path = Path (bundle_path ).expanduser ().resolve ()
543549
544550 @property
545551 def parser (self ) -> Union [ConfigParser , None ]:
@@ -561,12 +567,10 @@ def _init_config(self, config_names):
561567 """
562568
563569 # Ensure bundle root is on sys.path for directory-based bundles
564- if self ._bundle_path :
565- bundle_path_obj = Path (self ._bundle_path )
566- if bundle_path_obj .is_dir ():
567- _ensure_bundle_in_sys_path (bundle_path_obj )
570+ if self .bundle_path and self .bundle_path .is_dir ():
571+ _ensure_bundle_in_sys_path (self .bundle_path )
568572
569- parser = get_bundle_config (str (self ._bundle_path ), config_names )
573+ parser = get_bundle_config (str (self .bundle_path ), config_names )
570574 self ._parser = parser
571575
572576 meta = self .parser ["_meta_" ]
@@ -699,34 +703,34 @@ def compute(self, op_input, op_output, context):
699703 if not self ._init_completed :
700704 with self ._lock :
701705 if not self ._init_completed :
702- self ._bundle_path = Path (self ._model_network .path )
703- logging .info (f"Parsing from bundle_path: { self ._bundle_path } " )
706+ # Use property setter to set the bundle path for consistency
707+ self .bundle_path = Path (self ._model_network .path )
708+ logging .info (f"Parsing from bundle_path: { self .bundle_path } " )
704709 self ._init_config (self ._bundle_config_names .config_names )
705710 self ._init_completed = True
706- elif self ._bundle_path :
711+ elif self .bundle_path :
707712 # For the case of local dev/testing when the bundle path is not passed in as an exec cmd arg.
708713 # When run as a MAP docker, the bundle file is expected to be in the context, even if the model
709714 # network is loaded on a remote inference server (when the feature is introduced).
710- logging .debug (f"Model network not loaded. Trying to load from model path: { self ._bundle_path } " )
715+ logging .debug (f"Model network not loaded. Trying to load from model path: { self .bundle_path } " )
711716
712717 # Check if bundle_path is a directory
713- bundle_path_obj = Path (self ._bundle_path )
714- if bundle_path_obj .is_dir ():
718+ if self .bundle_path .is_dir ():
715719 # Ensure device is set
716720 if not hasattr (self , "_device" ):
717721 self ._device = torch .device ("cuda" if torch .cuda .is_available () else "cpu" )
718722
719723 # Initialize config for directory bundles if not already done
720724 if not self ._init_completed :
721- logging .info (f"Initializing config from directory bundle: { self ._bundle_path } " )
725+ logging .info (f"Initializing config from directory bundle: { self .bundle_path } " )
722726 self ._init_config (self ._bundle_config_names .config_names )
723727 self ._init_completed = True
724728
725729 # Load model using helper function
726- self ._model_network = _load_model_from_directory_bundle (bundle_path_obj , self ._device , self ._parser )
730+ self ._model_network = _load_model_from_directory_bundle (self . bundle_path , self ._device , self ._parser )
727731 else :
728732 # Original ZIP bundle handling
729- self ._model_network = torch .jit .load (bundle_path_obj , map_location = self ._device ).eval ()
733+ self ._model_network = torch .jit .load (self . bundle_path , map_location = self ._device ).eval ()
730734 else :
731735 raise IOError ("Model network is not load and model file not found." )
732736
0 commit comments