@@ -4656,6 +4656,125 @@ def upload_tensorflow_saved_model(
46564656 upload_request_timeout = upload_request_timeout ,
46574657 )
46584658
4659+ # TODO(b/273499620): Add async support.
4660+ def copy (
4661+ self ,
4662+ destination_location : str ,
4663+ destination_model_id : Optional [str ] = None ,
4664+ destination_parent_model : Optional [str ] = None ,
4665+ encryption_spec_key_name : Optional [str ] = None ,
4666+ copy_request_timeout : Optional [float ] = None ,
4667+ ) -> "Model" :
4668+ """Copys a model and returns a Model representing the copied Model
4669+ resource. This method is a blocking call.
4670+
4671+ Example usage:
4672+ copied_model = my_model.copy(
4673+ destination_location="us-central1"
4674+ )
4675+
4676+ Args:
4677+ destination_location (str):
4678+ The destination location to copy the model to.
4679+ destination_model_id (str):
4680+ Optional. The ID to use for the copied Model, which will
4681+ become the final component of the model resource name.
4682+ This value may be up to 63 characters, and valid characters
4683+ are `[a-z0-9_-]`. The first character cannot be a number or hyphen.
4684+
4685+ Only set this field when copying as a new model. If this field is not set,
4686+ a numeric model id will be generated.
4687+ destination_parent_model (str):
4688+ Optional. The resource name or model ID of an existing model that the
4689+ newly-copied model will be a version of.
4690+
4691+ Only set this field when copying as a new version of an existing model.
4692+ encryption_spec_key_name (Optional[str]):
4693+ Optional. The Cloud KMS resource identifier of the customer
4694+ managed encryption key used to protect the model. Has the
4695+ form:
4696+ ``projects/my-project/locations/my-region/keyRings/my-kr/cryptoKeys/my-key``.
4697+ The key needs to be in the same region as where the compute
4698+ resource is created.
4699+
4700+ If set, this Model and all sub-resources of this Model will be secured by this key.
4701+
4702+ Overrides encryption_spec_key_name set in aiplatform.init.
4703+ copy_request_timeout (float):
4704+ Optional. The timeout for the copy request in seconds.
4705+
4706+ Returns:
4707+ model (aiplatform.Model):
4708+ Instantiated representation of the copied model resource.
4709+
4710+ Raises:
4711+ ValueError: If both `destination_model_id` and `destination_parent_model` are set.
4712+ """
4713+ if destination_model_id is not None and destination_parent_model is not None :
4714+ raise ValueError (
4715+ "`destination_model_id` and `destination_parent_model` can not be set together."
4716+ )
4717+
4718+ parent = initializer .global_config .common_location_path (
4719+ initializer .global_config .project , destination_location
4720+ )
4721+
4722+ source_model = self .versioned_resource_name
4723+
4724+ destination_parent_model = ModelRegistry ._get_true_version_parent (
4725+ parent_model = destination_parent_model ,
4726+ project = initializer .global_config .project ,
4727+ location = destination_location ,
4728+ )
4729+
4730+ encryption_spec = initializer .global_config .get_encryption_spec (
4731+ encryption_spec_key_name = encryption_spec_key_name ,
4732+ )
4733+
4734+ if destination_model_id is not None :
4735+ request = gca_model_service_compat .CopyModelRequest (
4736+ parent = parent ,
4737+ source_model = source_model ,
4738+ model_id = destination_model_id ,
4739+ encryption_spec = encryption_spec ,
4740+ )
4741+ else :
4742+ request = gca_model_service_compat .CopyModelRequest (
4743+ parent = parent ,
4744+ source_model = source_model ,
4745+ parent_model = destination_parent_model ,
4746+ encryption_spec = encryption_spec ,
4747+ )
4748+
4749+ api_client = initializer .global_config .create_client (
4750+ client_class = utils .ModelClientWithOverride ,
4751+ location_override = destination_location ,
4752+ credentials = initializer .global_config .credentials ,
4753+ )
4754+
4755+ _LOGGER .log_action_start_against_resource ("Copying" , "" , self )
4756+
4757+ lro = api_client .copy_model (
4758+ request = request ,
4759+ timeout = copy_request_timeout ,
4760+ )
4761+
4762+ _LOGGER .log_action_started_against_resource_with_lro (
4763+ "Copy" , "" , self .__class__ , lro
4764+ )
4765+
4766+ model_copy_response = lro .result (timeout = None )
4767+
4768+ this_model = models .Model (
4769+ model_copy_response .model ,
4770+ version = model_copy_response .model_version_id ,
4771+ location = destination_location ,
4772+ )
4773+
4774+ _LOGGER .log_action_completed_against_resource ("" , "copied" , this_model )
4775+
4776+ return this_model
4777+
46594778 def list_model_evaluations (
46604779 self ,
46614780 ) -> List ["model_evaluation.ModelEvaluation" ]:
0 commit comments