
    h>                        d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
 ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ  ej8                  ej:                  j<                         G d dej>                               Z  ej8                  ej:                  jB                  ej:                  jD                         G d de              Z#y)z'Command to upload a model in Vertex AI.    )absolute_import)division)unicode_literals)extra_types)
operations)client)apis)messages)base)
exceptions)	constants)endpoint_util)flags)models_util)operations_util)region_util)yamlc                   J     e Zd ZdZ fdZed        Zd Zd Zd Z	d Z
 xZS )UploadV1a<  Upload a new model.

  ## EXAMPLES

  To upload a model under project ``example'' in region
  ``us-central1'', run:

    $ {command} --container-image-uri="gcr.io/example/my-image"
    --description=example-model --display-name=my-model
    --artifact-uri='gs://bucket/path' --project=example --region=us-central1
  c                    t        t        | 
  |i | t        j                  t
        j                  t
        j                  t
        j                           }t        j                  ||j                        j                  | _        y )Nr   r
   )superr   __init__r	   GetClientInstancer   AI_PLATFORM_API_NAMEAI_PLATFORM_API_VERSION
GA_VERSIONr   ModelsClientMESSAGES_MODULEr
   )selfargskwargsclient_instance	__class__s       lib/surface/ai/models/upload.pyr   zUploadV1.__init__3   sm    	(D"D3F3,,&&)))*>*>?AO '' 0022:( 	M    c                 L    t        j                  | t        j                         y N)r   AddUploadModelFlagsr   PromptForOpRegionparsers    r%   ArgszUploadV1.Args<   s    	fk&C&CDr&   c                    |j                   j                  j                         }|j                         d   }t	        j
                  t        j                  |      5  t        j                  t        j                  t        j                  t        j                           }t        j                  ||j                        j                  ||j                   |j"                  |j$                  |j&                  |j(                  |j*                  |j,                  |j.                  |j0                  |j2                  |j4                  |j6                  |j8                  |j:                  |j<                  |j>                  |j@                  |jB                  |jD                  |jF                  | jI                  |      |jJ                  |jL                  |jN                  |jP                        }tS        jT                  tW        jX                  ||j                        |t[        j\                  |j^                              cd d d        S # 1 sw Y   y xY w)NlocationsIdversionregionr   )explanation_specparent_modelmodel_idversion_aliaseslabelsoperations_clientopop_ref)0CONCEPTSr2   ParseAsDictr   AiplatformEndpointOverridesr   r   r	   r   r   r   r   r   r   r   display_namedescriptionversion_descriptionartifact_uri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_BuildExplanationSpecr4   r5   r6   r7   r   WaitForOpMayber   OperationsClientr   ParseModelOperationname)r    r!   
region_refr2   r#   	operations         r%   RunzUploadV1.Run@   s   %%++-J /F		2	2$$V
5..

(
(

+
+I,@,@
ACo %% "2244<H&&&&$$!!%%""''**))7722//99::..8899#99$?,,}}"22[[5 5= 5" : ++&77$/N/NP00@	BE
5 
5 
5s   G IIc                    d}|j                   }|sy|j                         dk(  rQ| j                  j                  | j                  j	                  |j
                  | j                  |                  }n|j                         dk(  rQ| j                  j                  | j                  j                  |j
                  | j                  |                  }nj|j                         dk(  rA| j                  j                  | j                  j                  |j                        	      }nt        j                  d
d      | j                  j                  | j                  |j                        |      S )a{  Generate explanation configs if anything related to XAI is specified.

    Args:
      args: argparse.Namespace. All the arguments that were provided to this
        command invocation.

    Returns:
      An object of GoogleCloudAiplatformV1ExplanationSpec.

    Raises:
      BadArgumentException: An error if the explanation method provided can not
        be recognized.
    Nintegrated-gradients	stepCountsmoothGradConfigintegratedGradientsAttributionxraixraiAttributionsampled-shapley	pathCountsampledShapleyAttribution--explanation-methodzWExplanation method must be one of `integrated-gradients`, `xrai` and `sampled-shapley`.metadata
parameters)explanation_methodlowerr
   ,GoogleCloudAiplatformV1ExplanationParameters5GoogleCloudAiplatformV1IntegratedGradientsAttributionexplanation_step_count_BuildSmoothGradConfig&GoogleCloudAiplatformV1XraiAttribution0GoogleCloudAiplatformV1SampledShapleyAttributionexplanation_path_countgcloud_exceptionsBadArgumentException&GoogleCloudAiplatformV1ExplanationSpec_ReadExplanationMetadataexplanation_metadata_filer    r!   rn   methods       r%   rT   zUploadV1._BuildExplanationSpeck   sv    J$$F||~//
--
D
D-1]]DD 77#'#>#>t#D E F E G  
6	!
--
D
D"mm55 77#'#>#>t#D 6 F E G  
,	,
--
D
D(,?? 77 @ 9 E :  22
 *+ + ==??..t/M/MN @  r&   c                    |j                   |j                  |j                  y|j                   "|j                  t        j                  dd      | j
                  j                  |j                   |j                        }|j                  }|rV| j
                  j                  |D cg c]+  }| j
                  j                  |t        ||               - c}      |_
        |S c c}w )a  Generate smooth grad configs from the arguments specified.

    Args:
      args: argparse.Namespace. All the arguments that were provided to this
        command invocation.

    Returns:
      An object of GoogleCloudAiplatformV1SmoothGradConfig.

    Raises:
      BadArgumentException: An error if both smooth-grad-noise-sigma and
        smooth-grad-noise-sigma-by-feature are set.
    N--smooth-grad-noise-sigmaVOnly one of smooth-grad-noise-sigma and smooth-grad-noise-sigma-by-feature can be set.
noiseSigmanoisySampleCountrX   sigmar   )smooth_grad_noise_sigmasmooth_grad_noisy_sample_count"smooth_grad_noise_sigma_by_featurerx   ry   r
   'GoogleCloudAiplatformV1SmoothGradConfig(GoogleCloudAiplatformV1FeatureNoiseSigma<GoogleCloudAiplatformV1FeatureNoiseSigmaNoiseSigmaForFeaturefloatfeatureNoiseSigmar    r!   smooth_grad_configsigmasks        r%   rt   zUploadV1._BuildSmoothGradConfig   s    	$$,++3//7$$0//;22
% (?@ @ 	==33!@@ 	> 	B  44F
--
@
@ <BM <Ba mmKKfQi 0 L 22;AM
@  * M   ,0C+c                     d}|s|S t        j                  |      }|r*t        j                  || j                  j
                        }|S )a0  Read local explanation metadata file provided.

    Args:
      explanation_metadata_file: str. A local file for explanation metadata.

    Returns:
      An object of GoogleCloudAiplatformV1ExplanationMetadata.

    Raises:
      BadArgumentException: An error if explanation_metadata_file is None.
    N)r   	load_pathmessages_utilDictToMessageWithErrorCheckr
   *GoogleCloudAiplatformV1ExplanationMetadatar    r|   explanation_metadatadatas       r%   r{   z!UploadV1._ReadExplanationMetadata   sL      $!!>>34D*FF
HHJr&   )__name__
__module____qualname____doc__r   staticmethodr-   r[   rT   rt   r{   __classcell__r$   s   @r%   r   r   %   s;    
; E E)BV-^#J r&   r   c                   P     e Zd ZdZ fdZed        Zd Zd Zd Z	d Z
d Z xZS )	UploadV1Beta1a8  Upload a new model.

  ## EXAMPLES

  To upload a model under project `example` in region
  `us-central1`, run:

    $ {command} --container-image-uri="gcr.io/example/my-image"
    --description=example-model --display-name=my-model
    --artifact-uri='gs://bucket/path' --project=example --region=us-central1
  c                 r    t        t        | 
  |i | t        j                         j
                  | _        y r(   )r   r   r   r   r   r
   )r    r!   r"   r$   s      r%   r   zUploadV1Beta1.__init__   s-    	-'88'')22DMr&   c                 v    t        j                  | t        j                         t        j                  |        y r(   )r   r)   r   r*    AddUploadModelFlagsForSimilarityr+   s    r%   r-   zUploadV1Beta1.Args   s&    	fk&C&CD	**62r&   c                    |j                   j                  j                         }|j                         d   }t	        j
                  t        j                  |      5  t        j                         j                  ||j                  |j                  |j                  |j                  |j                  |j                   |j"                  |j$                  |j&                  |j(                  |j*                  |j,                  |j.                  |j0                  |j2                  |j4                  |j6                  |j8                  |j:                  |j<                  | j?                  |      |j@                  |jB                  |jD                  |jF                        }tI        jJ                  tM        jN                         |tQ        jR                  |jT                              cd d d        S # 1 sw Y   y xY w)Nr/   r0   )r4   r5   r6   r7   r8   )+r<   r2   r=   r>   r   r?   r   BETA_VERSIONr   r   r   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rS   rT   r4   r5   r6   r7   r   rU   r   rV   r   rW   rX   )r    r!   rY   r2   rZ   s        r%   r[   zUploadV1Beta1.Run   s   %%++-J /F		2	2&&v
7%%'55








"
"




"
"

 
 




!
!




#
#

&
&

%
%

3
3

.
.

+
+

5
5

6
6

*
*

4
4

5
5

$
$T
*((==..5 6 i6 ++&77900@B;
7 
7 
7s   FG**G3c                    d }|j                   }|sy |j                         dk(  rR| j                  j                  | j                  j	                  |j
                  | j                  |                  }nL|j                         dk(  rR| j                  j                  | j                  j                  |j
                  | j                  |                  }n|j                         dk(  rB| j                  j                  | j                  j                  |j                              }n|j                         d	k(  rh|j                  r| j                  j                  | j                  j                  | j                  j                  |j                  
      |j                  | j                  |j                                    }n| j                  j                  | j                  j                  | j                  j                  |j                  
      |j                  | j                  j!                  | j                  j                   j#                  |j$                        | j                  j                   j'                  |j(                                          }nt+        j,                  dd      | j                  j/                  | j1                  |j2                        |      S )Nr]   r^   ra   rc   rd   rf   rg   ri   examples)uris)	gcsSourceneighborCountnearestNeighborSearchConfig)r   )modalityquery)r   r   presetsrk   zcExplanation method must be one of `integrated-gradients`, `xrai`, `sampled-shapley` and `examples`.rl   )ro   rp   r
   1GoogleCloudAiplatformV1beta1ExplanationParameters:GoogleCloudAiplatformV1beta1IntegratedGradientsAttributionrs   rt   +GoogleCloudAiplatformV1beta1XraiAttribution5GoogleCloudAiplatformV1beta1SampledShapleyAttributionrw   /explanation_nearest_neighbor_search_config_file$GoogleCloudAiplatformV1beta1Examples%GoogleCloudAiplatformV1beta1GcsSourcer   explanation_neighbor_count_ReadIndexMetadata#GoogleCloudAiplatformV1beta1PresetsModalityValueValuesEnumexplanation_modalityQueryValueValuesEnumexplanation_queryrx   ry   +GoogleCloudAiplatformV1beta1ExplanationSpecr{   r|   r}   s       r%   rT   z#UploadV1Beta1._BuildExplanationSpec  s   J$$F||~//
--
I
I-1]]II 77#'#>#>t#D J F J G  
6	!
--
I
I"mm:: 77#'#>#>t#D ; F J G  
,	,
--
I
I(,DD 77 E 9 J : 
 
:	%		=	=MMKKKK"mm::		:J"&"A"A040G0GLL1N	 L O L P 	 MMKKKK"mm::		:J"&"A"A MMMM!%<<001J1JK"mmOO--d.D.DE N G	 L 	H L 
I 	 22
 67 7 ==DD..t/M/MN E  r&   c                    |j                   |j                  |j                  y |j                   "|j                  t        j                  dd      | j
                  j                  |j                   |j                        }|j                  }|rV| j
                  j                  |D cg c]+  }| j
                  j                  |t        ||               - c}      |_
        |S c c}w )Nr   r   r   r   r   )r   r   r   rx   ry   r
   ,GoogleCloudAiplatformV1beta1SmoothGradConfig-GoogleCloudAiplatformV1beta1FeatureNoiseSigmaAGoogleCloudAiplatformV1beta1FeatureNoiseSigmaNoiseSigmaForFeaturer   r   r   s        r%   rt   z$UploadV1Beta1._BuildSmoothGradConfigK  s   $$,++3//7$$0//;22
% (?@ @ 	BB33!@@ 	C 	B  44F
--88 <BE <Ba mmOOfQi 0 P 22;AE8  * Er   c                     d }|s|S t        j                  |      }|r*t        j                  || j                  j
                        }|S r(   )r   r   r   r   r
   /GoogleCloudAiplatformV1beta1ExplanationMetadatar   s       r%   r{   z&UploadV1Beta1._ReadExplanationMetadatad  sJ    $!!>>34D*FF
MMOr&   c                     d}t        j                  |      }|r$t        j                  |t        j
                        }|S )zParse json metadata file.N)r   r   r   r   r   	JsonValue)r    index_metadata_fileindex_metadatar   s       r%   r   z UploadV1Beta1._ReadIndexMetadatao  s:    N>>-.D$@@
%%'nr&   )r   r   r   r   r   r   r-   r[   rT   rt   r{   r   r   r   s   @r%   r   r      s=    
3 3 3#BJ7r2	 r&   r   N)$r   
__future__r   r   r   apitools.base.pyr   googlecloudsdk.api_lib.air    googlecloudsdk.api_lib.ai.modelsr   googlecloudsdk.api_lib.utilr	   r
   r   googlecloudsdk.callioper   r   rx   googlecloudsdk.command_lib.air   r   r   r   r   r   googlecloudsdk.corer   ReleaseTracksReleaseTrackGACreateCommandr   ALPHABETAr    r&   r%   <module>r      s    . &  ' ( 0 3 , A ( C 3 7 / 5 9 5 $ D%%(()m t!! m  *m ` D%%++T->->-C-CD`H ` E`r&   