
    _                         S r SSKJr  SSKJr  SSKJr  SSKrSSKJr  SSKJ	r	  SSK
Jr  SS	K
Jr  S
rSrSrS\S\4S jrS\S\4S jrS r " S S\5      rg)z*Utilities for Vertex AI Model Garden APIs.    )absolute_import)division)unicode_literalsN)
list_pager)apis)	constants)flagszis_hf_wildcard(true)zis_hf_wildcard(false)z=labels.VERIFIED_DEPLOYMENT_CONFIG=VERIFIED_DEPLOYMENT_SUCCEED
model_namereturnc                 B    [        [        R                  " SU 5      5      $ )z2Returns whether the model is a Hugging Face model.z^[^/]+/[^/@]+$boolrematch)r
   s    4lib/googlecloudsdk/api_lib/ai/model_garden/client.pyIsHuggingFaceModelr   #   s    	bhh(*5	66    modelc                 B    [        [        R                  " SU 5      5      $ )z4Returns whether the model is a custom weights model.z^gs://r   )r   s    r   IsCustomWeightsModelr   (   s    	bhhy%(	))r   c           
         U R                  5       nU R                  US9Ul        U(       a1  U R                  U R	                  U R                  UUUS9SS9S9Ul        U R                  SU SU 3US9n	UR                  U	5      $ )	zDeploys a custom weights model.)gcsUri)machineTypeacceleratorTypeacceleratorCount   )machineSpecminReplicaCount)dedicatedResources	projects//locations/destination)googleCloudAiplatformV1beta1DeployRequest)	)GoogleCloudAiplatformV1beta1DeployRequest4GoogleCloudAiplatformV1beta1DeployRequestCustomModelcustomModel5GoogleCloudAiplatformV1beta1DeployRequestDeployConfig.GoogleCloudAiplatformV1beta1DedicatedResources'GoogleCloudAiplatformV1beta1MachineSpecdeployConfig(AiplatformProjectsLocationsDeployRequestDeploy)
messagesprojects_locations_servicer   machine_typeaccelerator_typeaccelerator_countprojectlocationdeploy_requestrequests
             r   DeployCustomWeightsModelr7   -   s     EEG.CC D   "*"`"`#RR HH( 0!2 I 
  S 
 #a 	#N ==gYk(<0> > ' 
$	*	*7	33r   c                       \ rS rSrSr\R                  4S jr   S	S jr                S
S jr	    SS jr
Srg)ModelGardenClientR   z3Client used for interacting with Model Garden APIs.c                     [         R                  " [        R                  [        R                  U   5      nUR
                  U l        UR                  U l        UR                  U l
        g )N)r   GetClientInstancer   AI_PLATFORM_API_NAMEAI_PLATFORM_API_VERSIONMESSAGES_MODULE	_messagespublishers_models_publishers_models_serviceprojects_locations_projects_locations_service)selfversionclients      r   __init__ModelGardenClient.__init__U   sT    ##&&))'2F ++DN&,&>&>D#'-'@'@D$r   Nc                 p    U R                   R                  UUUUS9nU R                  R                  U5      $ )a  Get a publisher model.

Args:
  model_name: The name of the model to get. The format should be
    publishers/{publisher}/models/{model}
  is_hugging_face_model: Whether the model is a hugging face model.
  include_equivalent_model_garden_model_deployment_configs: Whether to
    include equivalent Model Garden model deployment configs for Hugging
    Face models.
  hugging_face_token: The Hugging Face access token to access the model
    artifacts for gated models unverified by Model Garden.

Returns:
  A publisher model.
)nameisHuggingFaceModel2includeEquivalentModelGardenModelDeploymentConfigshuggingFaceToken)r@   $AiplatformPublishersModelsGetRequestrB   Get)rE   r
   is_hugging_face_model8include_equivalent_model_garden_model_deployment_configshugging_face_tokenr6   s         r   GetPublisherModel#ModelGardenClient.GetPublisherModel^   sC    , nnAA0;s+	 B G **..w77r   c                 j   SnU(       Ga  U R                   R                  UUUS9nU(       a  UUl        U(       a  UUl        U(       a3  U Vs/ s H   nU R                   R	                  UUU   S9PM"     snUl        U(       a/  U V s/ s H  n U R                   R                  U S9PM     sn Ul        U(       a/  U V s/ s H  n U R                   R                  U S9PM     sn Ul        U(       a  [        U5      S-   Ul
        U(       a  UUl        U(       d  U(       d  U(       aB  Sn!U(       a  U R                   R                  US9n!U R                   R                  U!UUS9Ul        U(       d  U(       d  U(       aB  Sn"U(       a  U R                   R                  US9n"U R                   R                  U"UUS9Ul        [!        U5      (       a&  [#        U R                   U R$                  UUUUUU5      $ ['        U5      (       a  U R                   R)                  US9n#OU R                   R)                  US	9n#U R                   R+                  U	UUS
9U#l        U R                   R/                  UUS9U#l        U R                   R3                  U R                   R5                  U R                   R7                  UUU[8        R:                  " U[<        R>                  5      S9SU
S9US9U#l         U R                   RC                  SU SU 3U#S9n$U R$                  RE                  U$5      $ s  snf s  sn f s  sn f )a+0  Deploy an open weight model.

Args:
  project: The project to deploy the model to.
  location: The location to deploy the model to.
  model: The name of the model to deploy or its gcs uri for custom weights.
  accept_eula: Whether to accept the end-user license agreement.
  accelerator_type: The type of accelerator to use.
  accelerator_count: The number of accelerators to use.
  machine_type: The type of machine to use.
  endpoint_display_name: The display name of the endpoint.
  hugging_face_access_token: The Hugging Face access token.
  spot: Whether to deploy the model on Spot VMs.
  reservation_affinity: The reservation affinity to use.
  use_dedicated_endpoint: Whether to use a dedicated endpoint.
  enable_fast_tryout: Whether to enable fast tryout.
  container_image_uri: Immutable. URI of the Docker image to be used as the
    custom container for serving predictions. This URI must identify an
    image in Artifact Registry or Container Registry. Learn more about the
    [container publishing requirements](https://cloud.google.com/vertex-
    ai/docs/predictions/custom-container-requirements#publishing), including
    permissions requirements for the Vertex AI Service Agent. The container
    image is ingested upon ModelService.UploadModel, stored internally, and
    this original path is afterwards not used. To learn about the
    requirements for the Docker image itself, see [Custom container
    requirements](https://cloud.google.com/vertex-
    ai/docs/predictions/custom-container-requirements#). You can use the URI
    to one of Vertex AI's [pre-built container images for
    prediction](https://cloud.google.com/vertex-ai/docs/predictions/pre-
    built-containers) in this field.
  container_command: Specifies the command that runs when the container
    starts. This overrides the container's [ENTRYPOINT](https://docs.docker.
    com/engine/reference/builder/#entrypoint). Specify this field as an
    array of executable and arguments, similar to a Docker `ENTRYPOINT`'s
    "exec" form, not its "shell" form. If you do not specify this field,
    then the container's `ENTRYPOINT` runs, in conjunction with the args
    field or the container's
    [`CMD`](https://docs.docker.com/engine/reference/builder/#cmd), if
    either exists. If this field is not specified and the container does not
    have an `ENTRYPOINT`, then refer to the Docker documentation about [how
    `CMD` and `ENTRYPOINT`
    interact](https://docs.docker.com/engine/reference/builder/#understand-
    how-cmd-and-entrypoint-interact). If you specify this field, then you
    can also specify the `args` field to provide additional arguments for
    this command. However, if you specify this field, then the container's
    `CMD` is ignored. See the [Kubernetes documentation about how the
    `command` and `args` fields interact with a container's `ENTRYPOINT` and
    `CMD`](https://kubernetes.io/docs/tasks/inject-data-application/define-
    command-argument-container/#notes). In this field, you can reference
    [environment variables set by Vertex
    AI](https://cloud.google.com/vertex-ai/docs/predictions/custom-
    container-requirements#aip-variables) and environment variables set in
    the env field. You cannot reference environment variables set in the
    Docker image. In order for environment variables to be expanded,
    reference them by using the following syntax: $( VARIABLE_NAME) Note
    that this differs from Bash variable expansion, which does not use
    parentheses. If a variable cannot be resolved, the reference in the
    input string is used unchanged. To avoid variable expansion, you can
    escape this syntax with `$$`; for example: $$(VARIABLE_NAME) This field
    corresponds to the `command` field of the Kubernetes Containers [v1 core
    API](https://kubernetes.io/docs/reference/generated/kubernetes-
    api/v1.23/#container-v1-core).
  container_args: Specifies arguments for the command that runs when the
    container starts. This overrides the container's
    [`CMD`](https://docs.docker.com/engine/reference/builder/#cmd). Specify
    this field as an array of executable and arguments, similar to a Docker
    `CMD`'s "default parameters" form. If you don't specify this field but
    do specify the command field, then the command from the `command` field
    runs without any additional arguments. See the [Kubernetes documentation
    about how the `command` and `args` fields interact with a container's
    `ENTRYPOINT` and `CMD`](https://kubernetes.io/docs/tasks/inject-data-
    application/define-command-argument-container/#notes). If you don't
    specify this field and don't specify the `command` field, then the
    container's
    [`ENTRYPOINT`](https://docs.docker.com/engine/reference/builder/#cmd)
    and `CMD` determine what runs based on their default behavior. See the
    Docker documentation about [how `CMD` and `ENTRYPOINT`
    interact](https://docs.docker.com/engine/reference/builder/#understand-
    how-cmd-and-entrypoint-interact). In this field, you can reference
    [environment variables set by Vertex
    AI](https://cloud.google.com/vertex-ai/docs/predictions/custom-
    container-requirements#aip-variables) and environment variables set in
    the env field. You cannot reference environment variables set in the
    Docker image. In order for environment variables to be expanded,
    reference them by using the following syntax: $( VARIABLE_NAME) Note
    that this differs from Bash variable expansion, which does not use
    parentheses. If a variable cannot be resolved, the reference in the
    input string is used unchanged. To avoid variable expansion, you can
    escape this syntax with `$$`; for example: $$(VARIABLE_NAME) This field
    corresponds to the `args` field of the Kubernetes Containers [v1 core
    API](https://kubernetes.io/docs/reference/generated/kubernetes-
    api/v1.23/#container-v1-core)..
  container_env_vars: List of environment variables to set in the container.
    After the container starts running, code running in the container can
    read these environment variables. Additionally, the command and args
    fields can reference these variables. Later entries in this list can
    also reference earlier entries. For example, the following example sets
    the variable `VAR_2` to have the value `foo bar`: ```json [ { "name":
    "VAR_1", "value": "foo" }, { "name": "VAR_2", "value": "$(VAR_1) bar" }
    ] ``` If you switch the order of the variables in the example, then the
    expansion does not occur. This field corresponds to the `env` field of
    the Kubernetes Containers [v1 core
    API](https://kubernetes.io/docs/reference/generated/kubernetes-
    api/v1.23/#container-v1-core).
  container_ports: List of ports to expose from the container. Vertex AI
    sends any http prediction requests that it receives to the first port on
    this list. Vertex AI also sends [liveness and health
    checks](https://cloud.google.com/vertex-ai/docs/predictions/custom-
    container-requirements#liveness) to this port. If you do not specify
    this field, it defaults to following value: ```json [ { "containerPort":
    8080 } ] ``` Vertex AI does not use ports other than the first one
    listed. This field corresponds to the `ports` field of the Kubernetes
    Containers [v1 core
    API](https://kubernetes.io/docs/reference/generated/kubernetes-
    api/v1.23/#container-v1-core).
  container_grpc_ports: List of ports to expose from the container. Vertex
    AI sends any grpc prediction requests that it receives to the first port
    on this list. Vertex AI also sends [liveness and health
    checks](https://cloud.google.com/vertex-ai/docs/predictions/custom-
    container-requirements#liveness) to this port. If you do not specify
    this field, gRPC requests to the container will be disabled. Vertex AI
    does not use ports other than the first one listed. This field
    corresponds to the `ports` field of the Kubernetes Containers [v1 core
    API](https://kubernetes.io/docs/reference/generated/kubernetes-
    api/v1.23/#container-v1-core).
  container_predict_route: HTTP path on the container to send prediction
    requests to. Vertex AI forwards requests sent using
    projects.locations.endpoints.predict to this path on the container's IP
    address and port. Vertex AI then returns the container's response in the
    API response. For example, if you set this field to `/foo`, then when
    Vertex AI receives a prediction request, it forwards the request body in
    a POST request to the `/foo` path on the port of your container
    specified by the first value of this `ModelContainerSpec`'s ports field.
    If you don't specify this field, it defaults to the following value when
    you deploy this Model to an Endpoint:
    /v1/endpoints/ENDPOINT/deployedModels/DEPLOYED_MODEL:predict The
    placeholders in this value are replaced as follows: * ENDPOINT: The last
    segment (following `endpoints/`)of the Endpoint.name][] field of the
    Endpoint where this Model has been deployed. (Vertex AI makes this value
    available to your container code as the [`AIP_ENDPOINT_ID` environment
    variable](https://cloud.google.com/vertex-ai/docs/predictions/custom-
    container-requirements#aip-variables).) * DEPLOYED_MODEL:
    DeployedModel.id of the `DeployedModel`. (Vertex AI makes this value
    available to your container code as the [`AIP_DEPLOYED_MODEL_ID`
    environment variable](https://cloud.google.com/vertex-
    ai/docs/predictions/custom-container-requirements#aip-variables).)
  container_health_route: HTTP path on the container to send health checks
    to. Vertex AI intermittently sends GET requests to this path on the
    container's IP address and port to check that the container is healthy.
    Read more about [health checks](https://cloud.google.com/vertex-
    ai/docs/predictions/custom-container-requirements#health). For example,
    if you set this field to `/bar`, then Vertex AI intermittently sends a
    GET request to the `/bar` path on the port of your container specified
    by the first value of this `ModelContainerSpec`'s ports field. If you
    don't specify this field, it defaults to the following value when you
    deploy this Model to an Endpoint: /v1/endpoints/ENDPOINT/deployedModels/
    DEPLOYED_MODEL:predict The placeholders in this value are replaced as
      follows * ENDPOINT: The last segment (following `endpoints/`)of the
      Endpoint.name][] field of the Endpoint where this Model has been
      deployed. (Vertex AI makes this value available to your container code
      as the [`AIP_ENDPOINT_ID` environment
      variable](https://cloud.google.com/vertex-ai/docs/predictions/custom-
      container-requirements#aip-variables).) * DEPLOYED_MODEL:
      DeployedModel.id of the `DeployedModel`. (Vertex AI makes this value
      available to your container code as the [`AIP_DEPLOYED_MODEL_ID`
      environment variable](https://cloud.google.com/vertex-
      ai/docs/predictions/custom-container-requirements#aip-variables).)
  container_deployment_timeout_seconds (int): Deployment timeout in seconds.
  container_shared_memory_size_mb (int): The amount of the VM memory to
    reserve as the shared memory for the model in megabytes.
  container_startup_probe_exec (Sequence[str]): Exec specifies the action to
    take. Used by startup probe. An example of this argument would be
    ["cat", "/tmp/healthy"]
  container_startup_probe_period_seconds (int): How often (in seconds) to
    perform the startup probe. Default to 10 seconds. Minimum value is 1.
  container_startup_probe_timeout_seconds (int): Number of seconds after
    which the startup probe times out. Defaults to 1 second. Minimum value
    is 1.
  container_health_probe_exec (Sequence[str]): Exec specifies the action to
    take. Used by health probe. An example of this argument would be ["cat",
    "/tmp/healthy"]
  container_health_probe_period_seconds (int): How often (in seconds) to
    perform the health probe. Default to 10 seconds. Minimum value is 1.
  container_health_probe_timeout_seconds (int): Number of seconds after
    which the health probe times out. Defaults to 1 second. Minimum value is
    1.

Returns:
  The deploy long-running operation.
N)healthRouteimageUripredictRoute)rK   value)containerPorts)command)exec_periodSecondstimeoutSeconds)huggingFaceModelId)publisherModelName)huggingFaceAccessToken
acceptEulacontainerSpec)endpointDisplayNamededicatedEndpointEnabled)r   r   r   reservationAffinityr   )r   r   spot)r   fastTryoutEnabledr    r!   r"   )#r@   .GoogleCloudAiplatformV1beta1ModelContainerSpecr]   args"GoogleCloudAiplatformV1beta1EnvVarenv GoogleCloudAiplatformV1beta1Portports	grpcPortsstrdeploymentTimeoutsharedMemorySizeMb+GoogleCloudAiplatformV1beta1ProbeExecAction!GoogleCloudAiplatformV1beta1ProbestartupProbehealthProber   r7   rD   r   r%   4GoogleCloudAiplatformV1beta1DeployRequestModelConfigmodelConfig7GoogleCloudAiplatformV1beta1DeployRequestEndpointConfigendpointConfigr(   r)   r*   r	   ParseReservationAffinityFlagr   BETA_VERSIONr+   r,   r-   )%rE   r3   r4   r   accept_eular1   r2   r0   endpoint_display_namehugging_face_access_tokenri   reservation_affinityuse_dedicated_endpointenable_fast_tryoutcontainer_image_uricontainer_commandcontainer_argscontainer_env_varscontainer_portscontainer_grpc_portscontainer_predict_routecontainer_health_route$container_deployment_timeout_secondscontainer_shared_memory_size_mbcontainer_startup_probe_exec&container_startup_probe_period_seconds'container_startup_probe_timeout_secondscontainer_health_probe_exec%container_health_probe_period_seconds&container_health_probe_timeout_secondscontainer_speckportstartup_probe_exechealth_probe_execr5   r6   s%                                        r   r-   ModelGardenClient.Deploy|   s   | N
..
G
G0*2 H   
!2	,	
 (	
 ( NN==03 >  (	
 
 ( 
' NN;;$;O' 
 
 -$
, NN;;$;O,$
  
.45; 	( 
),K)
&34!'nnHH6 I   NN<<(DF =  	# &23 &nnHH5 I   NN<<'CE =  	" E""%
..

*
*





	 	 
E	"	"~~OO" P n ~~OO" P n
 	KK#<"( 	L 	
  	NN 5%; 	O 	
 ! #'.."f"f>>XXNN( 0!2$)$F$F()*@*@%	 O   Y 
 - #g #N nnEEyH:>2@ F G ++227;;c
 

$
s   'L&#L+=#L0c           	          [         nU(       a  SR                  [        [        /5      nU(       a  U SU SU S3n[        R
                  " U R                  U R                  R                  SSUS9SS	UUS
9$ )a~  List publisher models in Model Garden.

Args:
  limit: The maximum number of items to list. None if all available records
    should be yielded.
  batch_size: The number of items to list per page.
  list_hf_models: Whether to only list Hugging Face models.
  model_filter: The filter on model name to apply on server-side.

Returns:
  The list of publisher models in Model Garden..
z AND z AND (model_user_id=~"(?i).*z.*" OR display_name=~"(?i).*z.*")zpublishers/*T)parentlistAllVersionsfilterpublisherModelspageSize)fieldbatch_size_attribute
batch_sizelimit)	_NATIVE_MODEL_FILTERjoin_HF_WILDCARD_FILTER_VERIFIED_DEPLOYMENT_FILTERr   YieldFromListrB   r@   %AiplatformPublishersModelsListRequest)rE   r   r   list_hf_modelsmodel_filter
filter_strs         r   ListPublisherModels%ModelGardenClient.ListPublisherModels  s    & &J<< ;
<j L4\N C##/.6  ##''<<!  	= 	

  ' r   )r@   rD   rB   )FTN)NNNNNNNNNNNNNNNN)Nd   FN)__name__
__module____qualname____firstlineno____doc__r   r~   rH   rT   r-   r   __static_attributes__ r   r   r9   r9   R   sq    ;&33 A "?C8Z "!+/&*#'-1.2"&,0-1=]<B (r   r9   )r   
__future__r   r   r   r   apitools.base.pyr   googlecloudsdk.api_lib.utilr   googlecloudsdk.command_lib.air   r	   r   r   r   rr   r   r   r   r7   objectr9   r   r   r   <module>r      sp    1 &  ' 	 ' , 3 /, . C 
73 74 7
* * *
"4Jq qr   