
    ]                         S r SSKrSSKJr  SSKJr  SSKJr	   " S S\R                  5      r " S S	\R                  5      rS
 rSS jrS rg)z*Utilities for dealing with ML predict API.    N)
exceptions)requests)http_clientc                       \ rS rSrSrSrg)InstancesEncodeError   zFIndicates that error occurs while decoding the instances in http body. N__name__
__module____qualname____firstlineno____doc____static_attributes__r	       /lib/googlecloudsdk/api_lib/ml_engine/predict.pyr   r      s    Nr   r   c                       \ rS rSrSrSrg)HttpRequestFailError   z2Indicates that the http request fails in some way.r	   Nr
   r	   r   r   r   r      s    :r   r   c                 z    [         R                  " 5       R                  SXUS9nUR                  UR                  4$ )z,Make http request to get prediction results.POST)dataheaders)r   
GetSessionrequeststatus_codetext)urlbodyr   responses       r   _GetPredictionr!   $   s<      "**cg + /(			x}}	,,r   c                    U R                  5       S-   nSS0nSU0nU(       a  X%S'    [        R                  " USS9n[        X6U5      u  px[        U5      [        R                  :w  a  [        S	U-   5      e [        R                  " U5      $ ! [        [        4 a    [        S5      ef = f! [         a    [        S
U-   5      ef = f)a  Performs online prediction on the input data file.

Args:
    model_or_version_ref: a Resource representing either a model or a version.
    instances: a list of JSON or UTF-8 encoded instances to perform
        prediction on.
    signature_name: name of input/output signature in the TF meta graph.

Returns:
    A json object that contains predictions.

Raises:
    HttpRequestFailError: if error happens with http request, or parsing
        the http response.
z:predictContent-Typeapplication/json	instancessignature_nameT	sort_keysRInstances cannot be JSON encoded, probably because the input is not utf-8 encoded.HTTP request failed. Response: =No JSON object could be decoded from the HTTP response body: SelfLinkjsondumpsUnicodeDecodeError	TypeErrorr   r!   inthttplibOKr   loads
ValueError)	model_or_version_refr%   r&   r   r   contentr   response_statusresponse_bodys	            r   Predictr;   +   s      	%%'*4#/0')$' .J::g.D $2#W#E /WZZ'
@, - . .G::m$$ i	( J
  I J JJ 
 G
  68E F G GGs   B 7B, B),Cc                 z   U R                  5       S-   nSS0nSU0n [        R                  " USS9n[        X%U5      u  pg[        U5      [        R                  :w  a  [        SU-   5      e [        R                  " U5      $ ! [        [        4 a    [        S5      ef = f! [         a    [        S	U-   5      ef = f)
a  Performs online explanation on the input data file.

Args:
    model_or_version_ref: a Resource representing either a model or a version.
    instances: a list of JSON or UTF-8 encoded instances to perform
        prediction on.

Returns:
    A json object that contains explanations.

Raises:
    HttpRequestFailError: if error happens with http request, or parsing
        the http response.
z:explainr#   r$   r%   Tr'   r)   r*   r+   r,   )r7   r%   r   r   r8   r   r9   r:   s           r   Explainr=   T   s     	%%'*4#/0')$'J::g.D $2#W#E /WZZ'
@, - . .G::m$$ i	( J
  I J JJ 
 G
  68E F G GGs   B ,B! B!B:)N)r   r.   googlecloudsdk.corer   core_exceptionsgooglecloudsdk.core.credentialsr   	six.movesr   r3   Errorr   r   r!   r;   r=   r	   r   r   <module>rC      sN    1  = 4 ,?00 
?00 
-&GR#Gr   