
    A                         S r SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJ	r	  SSK
Jr  SS	K
Jr  SS
KJr  SSKr " S S\R                   5      r " S S\R                   5      rSS jrSS jr " S S\5      rg)z+Utilities for dealing with ML versions API.    )absolute_import)division)unicode_literals)encoding)
list_pager)apis)
exceptions)yaml)textNc                       \ rS rSrSrSrg)InvalidVersionConfigFile   7Error indicating an invalid Version configuration file. N__name__
__module____qualname____firstlineno____doc____static_attributes__r       4lib/googlecloudsdk/api_lib/ml_engine/versions_api.pyr   r          ?r   r   c                       \ rS rSrSrSrg)NoFieldsSpecifiedError"   r   r   Nr   r   r   r   r   r   "   r   r   r   c                 0    [         R                  " SU 5      $ )Nml)r   GetMessagesModule)versions    r   r    r    &   s    			g	..r   c                 ,    [         R                  " SXS9$ )Nr   )no_http)r   GetClientInstance)r!   r#   s     r   r$   r$   *   s    			g	??r   c                       \ rS rSrSr\" / SQ5      r\" SS/5      rSS jr\	S 5       r
S	 rS
 rS r     SS jrS rS rS rS rS rS rS r                           SS jrSrg)VersionsClient.   z3Client for the versions service of Cloud ML Engine.)autoScalingdeploymentUridescription	frameworklabelsmachineTypemanualScalingpackageUrispredictionClasspythonVersionruntimeVersionserviceAccount	containerroutesNc                 |    U=(       d
    [        5       U l        U=(       d    U R                  R                  U l        g N)r$   clientMESSAGES_MODULEmessages)selfr8   r:   s      r   __init__VersionsClient.__init__E   s(    /-/DK; ; ;DMr   c                 .    U R                   R                  $ r7   )r:   GoogleCloudMlV1Version)r;   s    r   version_classVersionsClient.version_classI   s    ==///r   c                 6    U R                   R                  UUS9$ )N)parentgoogleCloudMlV1Version)r:   %MlProjectsModelsVersionsCreateRequest)r;   rC   r!   s      r   _MakeCreateRequest!VersionsClient._MakeCreateRequestM   s$    ==>>& ? ( (r   c                 j    U R                   R                  5       nU R                   R                  UUS9$ )N)name'googleCloudMlV1SetDefaultVersionRequest)r:   'GoogleCloudMlV1SetDefaultVersionRequest)MlProjectsModelsVersionsSetDefaultRequest)r;   rI   requests      r   _MakeSetDefaultRequest%VersionsClient._MakeSetDefaultRequestR   s7    mmCCEG==BB07 C 9 9r   c                     U R                   R                  R                  U R                  UR	                  5       US95      $ )z+Creates a new version in an existing model.)rC   r!   )r8   projects_models_versionsCreaterF   RelativeName)r;   	model_refr!   s      r   rR   VersionsClient.CreateX   sA    ;;//66))+ 	  	 r   c                    U R                   R                  5       n/ nUR                  (       a"  UR                  Ul        UR	                  S5        U(       a  X;l        UR	                  S5        Ub3  UR                  (       a"  UR	                  S5        UR                  Ul        Ub  UR	                  S5        X[l        Ub/  UR	                  S5        U R                   R                  US9Ul
        Ub/  UR	                  S5        U R                   R                  US9Ul        UbM  UR	                  S	5        UR                  b  XR                  l        OU R                   R                  US
9Ul        U
b/  UR	                  S5        U R                   R                  U
S9Ul        U	bF  SU;  a0  UR	                  S5        U R                   R                  U	S9Ul        OXR                  l        U(       d  [#        S5      eU R$                  R&                  R)                  U R                   R+                  UR-                  5       USR/                  [1        U5      5      S95      $ )zUpdate a version.r,   r*   r0   r/   zmanualScaling.nodes)nodeszautoScaling.minNodes)minNodeszautoScaling.maxNodes)maxNodesrequestLoggingConfig)bigqueryTableName)samplingPercentagezNo updates requested.,)rI   rD   
updateMask)r:   r?   needs_updater,   appendr*   valuer0   r/   GoogleCloudMlV1ManualScalingr.   GoogleCloudMlV1AutoScalingr(   rY   #GoogleCloudMlV1RequestLoggingConfigrZ   r\   r   r8   rQ   Patch$MlProjectsModelsVersionsPatchRequestrS   joinsorted)r;   version_reflabels_updater*   prediction_class_updatepackage_urismanual_scaling_nodesauto_scaling_min_nodesauto_scaling_max_nodessampling_percentagebigquery_table_namer!   update_masks                r   re   VersionsClient.Patch_   s?    mm224GK!!$++gn"''*/F/S/S*+ 7 = =g'('./"mmHH$ I &g )/0 MMDD) E +g )/0				('=$"mmFF+ G - &/0%)]]%V%V/ &W &1g" &	{	212'+}}'X'X2 (Y (4$ ;N$$7"#:;;;;//55::))+#*xx{ 34 	; 	67 7r   c                     U R                   R                  R                  U R                  R	                  UR                  5       S95      $ )zDeletes a version from a model.rI   )r8   rQ   Deleter:   %MlProjectsModelsVersionsDeleteRequestrS   r;   ri   s     r   rv   VersionsClient.Delete   sB    ;;//66;;))+ 	< 	-. .r   c                     U R                   R                  R                  U R                  R	                  UR                  5       S95      $ )z-Gets details about an existing model version.ru   )r8   rQ   Getr:   "MlProjectsModelsVersionsGetRequestrS   rx   s     r   r{   VersionsClient.Get   sB    ;;//3388))+ 	9 	-. .r   c                     U R                   R                  UR                  5       S9n[        R                  " U R
                  R                  USSS9$ )zLists the versions for a model.)rC   versionspageSize)fieldbatch_size_attribute)r:   #MlProjectsModelsVersionsListRequestrS   r   YieldFromListr8   rQ   )r;   rT   list_requests      r   ListVersionsClient.List   sO    ==DD%%' E )L##,,lz; ;r   c                     U R                   R                  R                  U R                  UR	                  5       S95      $ )zSets a model's default version.ru   )r8   rQ   
SetDefaultrN   rS   rx   s     r   r   VersionsClient.SetDefault   s;    ;;//::##)A)A)C#DF Fr   c                     [         R                  " U5      nU(       a   [        R                  " X0R                  5      n[        WR                  5        Vs/ s H+  n[        XVR                  5      (       d  M  UR                  PM-     sn5      nXr-
  [        UR                  5       5      -  nU(       ai  [        SR	                  [         R"                  " [%        U5      S5      SR'                  [)        U5      5      USR'                  [)        U5      5      S95      eU$ ! [         R                   a<  n[        SR	                  U[
        R                  " UR                  5      S95      eSnAff = fs  snf )a  Read a config file and return Version object with the values.

The object is based on a YAML configuration file. The file may only
have the fields given in `allowed_fields`.

Args:
  path: str, the path to the YAML file.
  allowed_fields: Collection, the fields allowed in the YAML.

Returns:
  A Version object (for the corresponding API version).

Raises:
  InvalidVersionConfigFile: If the file contains unexpected fields.
z:Could not read Version configuration file [{path}]:

{err})patherrNzVInvalid {noun} [{fields}] in configuration file [{path}]. Allowed fields: [{allowed}].r   , )nounfieldsr   allowed)r
   	load_pathErrorr   formatsix	text_typeinner_errorr   DictToMessager@   set
all_fieldsgetattrrI   all_unrecognized_fieldsr   	Pluralizelenrg   rh   )	r;   r   allowed_fieldsdatar   r!   fspecified_fieldsinvalid_fieldss	            r   
ReadConfigVersionsClient.ReadConfig   s=    I^^D!d
 &&t-?-?@gG,>,>,@ 6,@q#GVV4 #AFF,@ 6 7&7'99;<=N$))/>>#n"5w?YYvn56ii~ 67	 *0 *9: : N' JJ I$&dcoo(F&GI II6s#   D E28E2E/37E**E/c           	         [        UR                  5       5      (       d  gUS   (       do  SR                  [        UR	                  5       5       VVs/ s H  u  p4U(       d  M  SR                  U5      PM!     snn5      n[        SR                  U5      5      eU R                  R                  US   S9Ul	        US   (       a  US   UR                  l
        US   (       a  US   UR                  l        US	   (       aN  US	   R	                  5        VVs/ s H  u  pgU R                  R                  XgS
9PM      snnUR                  l        US   (       a<  US    Vs/ s H  nU R                  R                  US9PM     snUR                  l        US   (       d
  US   (       a&  U R                  R!                  US   US   S9Ul        ggs  snnf s  snnf s  snf )z0Adds `container` and `routes` fields to version.Nimager   z--{}z--image was not provided, but other container related flags were specified. Please specify --image or remove the following flags: {})r   commandargsenv_vars)rI   ra   ports)containerPortpredict_routehealth_route)predicthealth)anyvaluesrg   rh   itemsr   
ValueErrorr:   GoogleCloudMlV1ContainerSpecr4   r   r   GoogleCloudMlV1EnvVarenvGoogleCloudMlV1ContainerPortr   GoogleCloudMlV1RouteMapr5   )	r;   r!   kwargskv	set_flagsrI   ra   ps	            r   _ConfigureContainer"VersionsClient._ConfigureContainer   s   v}}'?))(.v||~(>
D(>!6==(>
DFivi " "
 BBWo C Gi"("3gf~%f~gj $J/55797kd --
-
-4
-
E79g g '?!"a --
4
41
4
E"!g
 o&"8}}<<)' = gn #9- E9!s   G
"G
%G#Gc                 0   [        UR                  5       5      (       d  gU R                  R                  5       Ul        US   (       a  US   UR                  l        US   (       a  US   UR                  l        US   (       Ga  / UR                  l        SUS   ;   av  [        US   S   5      nUR                  R                  R                  U R                  R                  U R                  R                  R                  R                  US95        SUS   ;   aw  [        US   S   5      nUR                  R                  R                  U R                  R                  U R                  R                  R                  R                  US95        ggg)z&Adds `auto_scaling` fields to version.N	min_nodes	max_nodesmetricsz	cpu-usage)rI   targetzgpu-duty-cycle)r   r   r:   rc   r(   rX   rY   r   intr`   GoogleCloudMlV1MetricSpecNameValueValuesEnum	CPU_USAGEGPU_DUTY_CYCLE)r;   r!   r   ts       r   _ConfigureAutoScaling$VersionsClient._ConfigureAutoScaling  sa   v}}--BBDGk%+K%8g"k%+K%8g"i$&g!	y)	)y!+./##**MM33]]<<PP 4 	
 
VI.	.y!"234##**MM33]]<<PP 4 	 
/ r   c                 H   U(       a5  U R                   nU(       d  UU R                  -  nU R                  UU5      nOU R                  5       nUUUUUUUU	U
UUUS.nSn US:X  aC  U R                  R                  5       n U R                  R                  5       n!UU!l        U!U l        OUS:X  aC  U R                  R                  5       n U R                  R                  5       n"UU"l
        U"U l        OHUS:X  aB  U R                  R                  5       n U R                  R                  5       n#UU#l        U#U l        U b  U US'   U(       d  U R                  UUUUUUUUS9  U(       d  U R                  UUUUS9  UR!                  5        H  u  n$n%U%c  M  [#        UU$U%5        M     U$ )	a!
  Create a Version object.

The object is based on an optional YAML configuration file and the
parameters to this method; any provided method parameters override any
provided in-file configuration.

The file may only have the fields given in
VersionsClientBase._ALLOWED_YAML_FIELDS specified; the only parameters
allowed are those that can be specified on the command line.

Args:
  name: str, the name of the version object to create.
  path: str, the path to the YAML file.
  deployment_uri: str, the deploymentUri to set for the Version
  runtime_version: str, the runtimeVersion to set for the Version
  labels: Version.LabelsValue, the labels to set for the version
  machine_type: str, the machine type to serve the model version on.
  description: str, the version description.
  framework: FrameworkValueValuesEnum, the ML framework used to train this
    version of the model.
  python_version: str, The version of Python used to train the model.
  prediction_class: str, the FQN of a Python class implementing the Model
    interface for custom prediction.
  package_uris: list of str, Cloud Storage URIs containing user-supplied
    Python code to use.
  accelerator_config: an accelerator config message object.
  service_account: Specifies the service account for resource access
    control.
  explanation_method: Enables explanations and selects the explanation
    method. Valid options are 'integrated-gradients' and 'sampled-shapley'.
  num_integral_steps: Number of integral steps for Integrated Gradients and
    XRAI.
  num_paths: Number of paths for Sampled Shapley.
  image: The container image to deploy.
  command: Entrypoint for the container image.
  container_args: The command-line args to pass the container.
  env_vars: The environment variables to set on the container.
  ports: The ports to which traffic will be sent in the container.
  predict_route: The HTTP path within the container that predict requests
    are sent to.
  health_route: The HTTP path within the container that health checks are
    sent to.
  min_nodes: The minimum number of nodes to scale this model under load.
  max_nodes: The maximum number of nodes to scale this model under load.
  metrics: List of key-value pairs to set as metrics' target for
    autoscaling.
  containers_hidden: Whether or not container-related fields are hidden on
    this track.
  autoscaling_hidden: Whether or not autoscaling fields are hidden on this
    track.

Returns:
  A Version object (for the corresponding API version).

Raises:
  InvalidVersionConfigFile: If the file contains unexpected fields.
)rI   r)   r2   r,   r-   r*   r+   r1   r0   r/   acceleratorConfigr3   Nzintegrated-gradientszsampled-shapleyxraiexplanationConfig)r   r   r   r   r   r   r   )r   r   r   )_ALLOWED_YAML_FIELDS_CONTAINER_FIELDSr   r@   r:    GoogleCloudMlV1ExplanationConfig-GoogleCloudMlV1IntegratedGradientsAttributionnumIntegralStepsintegratedGradientsAttribution(GoogleCloudMlV1SampledShapleyAttributionnumPathssampledShapleyAttributionGoogleCloudMlV1XraiAttributionxraiAttributionr   r   r   setattr)&r;   rI   r   deployment_uriruntime_versionr,   machine_typer*   r+   python_versionprediction_classrl   accelerator_configservice_accountexplanation_methodnum_integral_steps	num_pathsr   r   container_argsr   r   r   r   r   r   r   containers_hiddenautoscaling_hiddenr   r!   additional_fieldsexplanation_config	ig_configshap_configxrai_config
field_namera   s&                                         r   BuildVersionVersionsClient.BuildVersion  s   l 00n$000n5g""$g ')#"'+#/) 33==IIK--MMOi#5i :C7	0	0==IIKMMJJLk&k5@2	v	%==IIKMM@@Bk%7k"+6(%/A+,

%#  % 
  
Y)W ! N /446
E		U+ 7 Nr   )r8   r:   )NN)NNNNNNNN)NNNNNNNNNNNNNNNNNNNNNNNNNTT)r   r   r   r   r   	frozensetr   r   r<   propertyr@   rF   rN   rR   re   rv   r{   r   r   r   r   r   r   r   r   r   r   r&   r&   .   s    ;" $   ! 
< 0 0(
9 ;?7;>B=A $	>7@..;F
%N"H< "&#' $#!"&$( $&*#'&*&*!"& !% $!!%)&*9Tr   r&   )v1)r   F)r   
__future__r   r   r   apitools.base.pyr   r   googlecloudsdk.api_lib.utilr   googlecloudsdk.corer	   r
   googlecloudsdk.core.utilr   r   r   r   r   r    r$   objectr&   r   r   r   <module>r      si    2 &  ' % ' , * $ ) 
@z// @@Z-- @/@EV Er   