
    {                         S r SSKJr  SSKJr  SSKJr  SSK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S
K
Jr  SSKJr  SSKJr  SSKr " S S\R(                  5      rS rSS jr SS jrS rS rSS jrSS jrg)z/Utilities for reading instances for prediction.    )absolute_import)division)unicode_literalsN)models)versions_api)
exceptions)
properties)	resources)
console_ioencodingc                       \ rS rSrSrSrg)InvalidInstancesFileError#   z6Indicates that the input file was invalid in some way. N)__name__
__module____qualname____firstlineno____doc____static_attributes__r       =lib/googlecloudsdk/command_lib/ml_engine/predict_utilities.pyr   r   #   s    >r   r   c                 Z   U R                  5       n[        U[        R                  5      (       a  [        R
                  " USS9n [        R                  " U5      nSU;  a  [        S5      eUS   n[        U[        5      (       d  [        S5      eU$ ! [         a    [        S5      ef = f)zReads a JSON request from the specified input file.

Args:
  input_file: An open file-like object for the input file.

Returns:
  A list of instances.

Raises:
  InvalidInstancesFileError: If the input file is invalid.
	utf-8-sigr   zZInput instances are not in JSON format. See "gcloud ml-engine predict --help" for details.	instancesz3Invalid JSON request: missing "instances" attributez0Invalid JSON request: "instances" must be a list)read
isinstancesixbinary_typer   Decodejsonloads
ValueErrorr   list)
input_filecontentsrequestr   s       r   ReadRequestr)   (   s     __(#//**x+>H>jj"G 
#=? ? k")	It	$	$
#:< < 
 
 >
#	=> >>s   B B*c                 &   / n[        U 5       H  u  pE[        U[        R                  5      (       a  [        R
                  " USS9nUR                  S5      nU(       d  [        S5      eU(       a*  XB:  a%  [        S[        R                  " U5      -   S-   5      eUS:X  a(   UR                  [        R                  " U5      5        M  US	:X  d  M  UR                  U5        M     U(       d  [        S
5      eU$ ! [         a    [        S5      ef = f)a  Reads the instances from input file.

Args:
  input_file: An open file-like object for the input file.
  data_format: str, data format of the input file, 'json' or 'text'.
  limit: int, the maximum number of instances allowed in the file

Returns:
  A list of instances.

Raises:
  InvalidInstancesFileError: If the input file is invalid (invalid format or
      contains too many/zero instances).
r   r   z
z0Empty line is not allowed in the instances file.z2The gcloud CLI can currently process no more than zJ instances per file. Please use the API directly if you need to send more.r"   z\Input instances are not in JSON format. See "gcloud ai-platform predict --help" for details.textz*No valid instance was found in input file.)	enumerater   r   r    r   r!   rstripr   	text_typeappendr"   r#   r$   )r&   data_formatlimitr   line_numlineline_contents          r   ReadInstancesr5   N   s    )!*-nh$((__TK8d;;v&L% '8 9 9"%
>
--
 
 fDL12
 
	|$+ .. 

#46 6 
  D'CD 	DDs   "%C::Dc                    XU/n[        U Vs1 s H  oU(       d  M  UiM     sn5      S:w  a  [        S5      eU (       a  SnU nU(       a  SnUnOU(       a  SnUn[        R                  " WSS9n[        R
                  " U5       n	WS:X  a  [        U	5      sSSS5        $ [        XUS	9sSSS5        $ s  snf ! , (       d  f       g= f)
a  Reads the instances from the given file path ('-' for stdin).

Exactly one of json_request, json_instances, text_instances must be given.

Args:
  json_request: str or None, a path to a file ('-' for stdin) containing
      the JSON body of a prediction request.
  json_instances: str or None, a path to a file ('-' for stdin) containing
      instances in JSON format.
  text_instances: str or None, a path to a file ('-' for stdin) containing
      instances in text format.
  limit: int, the maximum number of instances allowed in the file

Returns:
  A list of instances.

Raises:
  InvalidInstancesFileError: If the input file is invalid (invalid format or
      contains too many/zero instances), or an improper combination of input
      files was given.
   zWExactly one of --json-request, --json-instances and --text-instances must be specified.json_requestr"   r+   T)binaryN)r1   )lenr   r   ReadFromFileOrStdinioBytesIOr)   r5   )
r8   json_instancestext_instancesr1   
mutex_argsargr0   r&   datafs
             r   ReadInstancesFromArgsrD   }   s    2 n=*	+#s#	+,1
#	   KJKJKJ		'	'
4	@$	zz$1n$^  17	 ! 
,  s   
B3B3B8 	B88
Cc                 F   U(       aN  [         R                  R                  US[        R                  R
                  R                  R                  U S.S9$ [         R                  R                  U S[        R                  R
                  R                  R                  0SS9$ )Nzml.projects.models.versions)
projectsIdmodelsId)
collectionparamsrF   zml.projects.models)rI   rH   )r
   REGISTRYParser	   VALUEScoreproject	GetOrFail)model_id
version_ids     r   ParseModelOrVersionRefrR      s    ##0$++0088BB 
 $   ##j//44<<FFG' $ ) )r   c                     [        U [        5      (       d  gU (       d  g [        U S   [        5      (       a<  SR                  [	        U S   R                  5       5      5      nSR                  U5      $ g)Nr"   r   z, zl
          table(
              predictions:format="table(
                  {}
              )"
          )ztable[no-heading](predictions))r   r%   dictjoinsortedkeysformat)predictionsrW   s     r   GetDefaultFormatrZ      sg    	K	&	&  +a.$''99VKN//123D
 vd| ,r   c                     U(       a0  [        X5      n[        R                  " 5       R                  U5      nO.[        R
                  " 5       R                  U 5      R                  nUR                  UR                  4$ N)	rR   r   VersionsClientGetr   ModelsClientdefaultVersion	frameworkruntimeVersion)modelversionversion_refversion_datas       r   GetRuntimeVersionrg      s^    (8K..044[AL&&(,,U3BBL			!<!<	<<r   c                     [        X5      u  p#US:X  a>  [        [        UR                  S5      5      u  pAUS:H  =(       a    US:  =(       d    US:  $ g)z*Check if runtime-version is more than 1.8.
TENSORFLOW.r7      F)rg   mapintsplit)rc   rd   ra   runtime_versionreleases        r   CheckRuntimeVersionrq      sP    0@),3!6!6s!;=GqL)W\;w{;r   r\   )NN)r   
__future__r   r   r   r<   r"    googlecloudsdk.api_lib.ml_enginer   r   googlecloudsdk.corer   core_exceptionsr	   r
   googlecloudsdk.core.consoler   googlecloudsdk.core.utilr   r   Errorr   r)   r5   rD   rR   rZ   rg   rq   r   r   r   <module>ry      sn    6 &  ' 	  3 9 = * ) 2 - 
 5 5 
#L,d !%.8b) ,,=r   