
                             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	KJr  S
 rS r " S S\5      rg)zUtilities for Edge ML API.    )absolute_import)division)unicode_literalsN)encoding_helper)
operations)util)
propertiesc                 0    [         R                  " SSU SS9$ )z?Converts model[.tflite] style filename to model_edgetpu.tflite.z(\.tflite)?$z_edgetpu.tflite   countresubsources    +lib/googlecloudsdk/api_lib/edgeml/edgeml.py_CompileDestinationr      s    	!2F!	DD    c                 0    [         R                  " SSU SS9$ )zGConverts model[/saved_model.(pb|pbtxt)] style filename to model.tflite.z(/saved_model\.(pb|pbtxt))?$z.tfliter   r   r   r   s    r   _ConvertDestinationr   "   s    	/F!	LLr   c                   <    \ rS rSrSrS	S jrS rS
S jrS
S jrSr	g)EdgeMlClient'   z}Client for Edge ML service.

Attributes:
  client: Generated Edge ML API client.
  messages: Generated Edge ML API messages.
Nc                     U=(       d    [         R                  " 5       U l        U=(       d    [         R                  " U5      U l        U R                  R
                  U l        [        R                  " X5      U l	        g N)
r   GetClientInstanceclientGetMessagesModulemessagesprojects_models_servicer   OperationsClient_operations_client)selfr   r    s      r   __init__EdgeMlClient.__init__/   sR    4D224DK> 6 6v >DMKK//DM(99&KDr   c                    S[         R                  R                  R                  R	                  5       -   nU R
                  R                  U R
                  R                  U R
                  R                  U/S9S9US9nU R                  R                  U5      nU R                  R                  U5      n[        R                  " U R
                  R                  [        R                   " U5      5      nU$ )a  Analyzes a machine learning model.

The AnalyzeResponse will contain model's framework, and for TF models
it will also contain Edge TPU compiliability and input/output tensor
information.

Args:
  source: str, GCS object URI to the model file or directory to analyze.

Returns:
  AnalyzedResponse on the finish of analyze operation.

Raises:
  LongrunningError: when long running operation fails.
	projects/	inputUris	gcsSource)analyzeModelRequestproject)r	   VALUEScorer/   	GetOrFailr    "EdgemlProjectsModelsAnalyzeRequestAnalyzeModelRequest	GcsSourcer"   Analyzer$   WaitForOperationr   JsonToMessageAnalyzeModelResponseMessageToJson)r%   r   r/   analyze_req	operationresultresponses          r   r6   EdgeMlClient.Analyze5   s      J--22::DDFFG--BB MM==mm---A > C C K %%k2I$$55i@F,,**%%f-/H Or   c                    S[         R                  R                  R                  R	                  5       -   nU(       d  [        U5      nU R                  R                  nUR                  R                  nU R                  R                  U R                  R                  U/S9S9nU R                  R                  U R                  R                  US9S9nU R                  R                  U" UUUS9US9nU R                  R!                  U5      n	U R"                  R%                  U	5      n
[&        R(                  " U R                  R*                  [&        R,                  " U
5      5      nX4$ )a  Optimizes a TFLite model for EdgeTPU.

Args:
  source: str, GCS object URI to the model file to compile. Must be a
    .tflite file.
  destination: str, GCS URI to an output tflite object. If not provided,
    for source filename `model[.tflite]` this will be set to
    `model_edgetpu.tflite`.

Returns:
  (CompileModelResponse, output object URI) on the finish of compilation.

Raises:
  LongrunningError: when long running operation fails.
r)   r*   r,   	outputUrigcsDestination)chipTypeinputConfigoutputConfig)compileModelRequestr/   )r	   r0   r1   r/   r2   r   r    CompileModelRequestChipTypeValueValuesEnumEDGE_TPU_V1InputConfigr5   OutputConfigGcsDestination"EdgemlProjectsModelsCompileRequestr"   Compiler$   r7   r   r8   CompileModelResponser:   )r%   r   destinationr/   compile_req_type	chip_typeinput_configoutput_configcompile_reqr<   r=   r>   s               r   rP   EdgeMlClient.CompileR   sJ     J--22::DDFFG'/k}}88 88DDI==,,--))VH)= - ?LMM..}}33k3J / LM --BB,$&(  C K %%k2I$$55i@F,,**%%f-/H   r   c                    S[         R                  R                  R                  R	                  5       -   nU(       d  [        U5      nU R                  R                  U R                  R                  U/S9S9nU R                  R                  U R                  R                  US9S9nU R                  R                  U R                  R                  XES9US9nU R                  R                  U5      nU R                  R!                  U5      n["        R$                  " U R                  R&                  ["        R(                  " U5      5      n	X4$ )a  Converts Tensorflow SavedModel to TFLite model.

Args:
  source: str, GCS URI to an input SavedModel archive
  destination: str, GCS URI to an output tflite object. If not provided,
    for source filename `model[/saved_model.(pb|pbtxt)]` this will be
    set to `model.tflite`.

Returns:
  (ConvertModelResponse, output object URI) on the finish of
  convert operation.

Raises:
  LongrunningError: when long running operation fails.
r)   r*   r,   rA   rC   )rF   rG   )convertModelRequestr/   )r	   r0   r1   r/   r2   r   r    rL   r5   rM   rN   "EdgemlProjectsModelsConvertRequestConvertModelRequestr"   Convertr$   r7   r   r8   ConvertModelResponser:   )
r%   r   rR   r/   rU   rV   convert_reqr<   r=   r>   s
             r   r]   EdgeMlClient.Convert|   s1     J--22::DDFFG'/k==,,--))VH)= - ?LMM..}}33k3J / LM --BB MM==$ > B C K
 %%k2I$$55i@F,,**%%f-/H   r   )r$   r"   r   r    )NNr   )
__name__
__module____qualname____firstlineno____doc__r&   r6   rP   r]   __static_attributes__ r   r   r   r   '   s    L:(!T$!r   r   )re   
__future__r   r   r   r   apitools.base.pyr   googlecloudsdk.api_lib.edgemlr   r   googlecloudsdk.corer	   r   r   objectr   rg   r   r   <module>rm      s<    ! &  ' 	 , 4 . *E
M
y!6 y!r   