
                             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
\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.    )absolute_import)division)unicode_literalsN)
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/   )r:   r(   r!   r   r;   r"   r<   r=   s           r   Explainr@   W   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   
__future__r   r   r   r1   googlecloudsdk.corer   core_exceptionsgooglecloudsdk.core.credentialsr   	six.movesr   r6   Errorr
   r   r$   r>   r@   r   r   r   <module>rG      sU    1 &  '  = 4 ,?00 
?00 
-&GR#Gr   