
    y                        d Z ddlmZ ddlmZ ddlmZ ddlZddl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Zd Zd Ze
j0                   e
j2                  e
j4                  j6                         G d de
j8                                      Ze
j0                   e
j2                  e
j4                  j<                  e
j4                  j>                         G d de                    Z y)z/Vertex AI endpoints stream-raw-predict command.    )absolute_import)division)unicode_literalsN)client)base)	constants)endpoint_util)flags)region_util)
exceptions)
console_ioc                     t        j                  | dt        j                         t        j                         j                  |        t        j                         j                  |        y )Nz"to do online stream raw prediction)prompt_func)r
   AddEndpointResourceArgr   PromptForOpRegionGetRawPredictHeadersArgAddToParserGetRawPredictRequestArgparsers    .lib/surface/ai/endpoints/stream_raw_predict.py_AddArgsr   #   sO    *// !--f5!--f5    c           	   #   L  K   | j                   j                  j                         }|j                         d   | _        t        j                  || j                        5  | j                  j                  d      r%t        j                  | j                  dd d      }n| j                  j                  d      }t        j                  |	      }|j                  || j                  |      D ]d  }| j!                  d
      s*t"        j$                  j&                  j)                  |       >	 t+        j,                  |j/                  d             f 	 ddd       y# t0        $ r, t3        j4                  dt7        j8                  |      z         w xY w# 1 sw Y   yxY ww)z Run Vertex AI online prediction.locationsId)region@   NT)binaryzutf-8)versionformatz=No JSON object could be decoded from the HTTP response body:
)CONCEPTSendpointParseAsDictr   r	   AiplatformEndpointOverridesrequest
startswithr   ReadFromFileOrStdinencoder   EndpointsClientStreamRawPredicthttp_headersIsSpecifiedsysstdoutbufferwritejsonloadsdecode
ValueErrorcore_exceptionsErrorsix	text_type)argsr    endpoint_refr'   endpoints_clientresponses         r   _Runr?   .   sO    ''--/,##%m4$+00M||s#..t||AB/?Mg##G,g--g>$55d'' h'

)
jj122 NM(  
##LmmH%&
 	

) NMs7   AF$ CF.&E F	F$ 5FFF!F$c                   &    e Zd ZdZed        Zd Zy)r,     Run Vertex AI online stream raw prediction.

  `{command}` sends a stream raw prediction request to a Vertex AI endpoint. The
  request can be given on the command line or read from a file or stdin.

  ## EXAMPLES

  To predict against an endpoint ``123'' under project ``example'' in region
  ``us-central1'', reading the request from the command line, run:

    $ {command} 123 --project=example --region=us-central1 --request='{
        "instances": [
          { "values": [1, 2, 3, 4], "key": 1 },
          { "values": [5, 6, 7, 8], "key": 2 }
        ]
      }'

  If the request body was in the file ``input.json'', run:

    $ {command} 123 --project=example --region=us-central1 --request=@input.json

  To send the image file ``image.jpeg'' and set the *content type*, run:

    $ {command} 123 --project=example --region=us-central1
    --http-headers=Content-Type=image/jpeg --request=@image.jpeg
  c                     t        |        y N)r   r   s    r   ArgszStreamRawPredict.Argsl   s
    Vr   c                 6    t        |t        j                        S rC   )r?   r   
GA_VERSIONselfr;   s     r   RunzStreamRawPredict.Runp   s    i**++r   N)__name__
__module____qualname____doc__staticmethodrD   rI    r   r   r,   r,   N   s     6  ,r   r,   c                       e Zd ZdZd Zy)StreamRawPredictBetarA   c                 6    t        |t        j                        S rC   )r?   r   BETA_VERSIONrG   s     r   rI   zStreamRawPredictBeta.Run   s    i,,--r   N)rJ   rK   rL   rM   rI   rO   r   r   rQ   rQ   t   s    6.r   rQ   )!rM   
__future__r   r   r   r3   r/   #googlecloudsdk.api_lib.ai.endpointsr   googlecloudsdk.callioper   googlecloudsdk.command_lib.air   r	   r
   r   googlecloudsdk.corer   r7   googlecloudsdk.core.consoler   r9   r   r?   DefaultUniverseOnlyReleaseTracksReleaseTrackGACommandr,   BETAALPHArQ   rO   r   r   <module>ra      s    6 &  '  
 6 ( 3 7 / 5 = 2 
6
@ D%%(()!,t|| !, * !,H D%%**D,=,=,C,CD.+ . E .r   