MCPcopy Create free account
hub / github.com/RightNow-AI/autokernel / _load_model_from_file

Function _load_model_from_file

profile.py:219–246  ·  view source on GitHub ↗

Load a model class from a Python file and instantiate it.

(model_path: str, class_name: str)

Source from the content-addressed store, hash-verified

217
218
219def _load_model_from_file(model_path: str, class_name: str) -> nn.Module:
220 """Load a model class from a Python file and instantiate it."""
221 model_path = os.path.abspath(model_path)
222 if not os.path.isfile(model_path):
223 raise FileNotFoundError(f"Model file not found: {model_path}")
224
225 module_name = Path(model_path).stem
226 spec = importlib.util.spec_from_file_location(module_name, model_path)
227 if spec is None or spec.loader is None:
228 raise ImportError(f"Cannot create module spec from {model_path}")
229 module = importlib.util.module_from_spec(spec)
230 spec.loader.exec_module(module) # type: ignore[union-attr]
231
232 if not hasattr(module, class_name):
233 available = [n for n in dir(module) if not n.startswith("_")]
234 raise AttributeError(
235 f"Class '{class_name}' not found in {model_path}. Available: {available}"
236 )
237
238 cls = getattr(module, class_name)
239 try:
240 model = cls()
241 except TypeError as e:
242 raise RuntimeError(
243 f"Could not instantiate {class_name}() with no arguments: {e}. "
244 "If the model requires config, provide a factory function or use --module."
245 ) from e
246 return model
247
248
249def _load_model_from_module(

Callers 1

load_modelFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected