
    t                     b    S 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.    N)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   r3   EdgeMlClient.Analyze2   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   r-   r.   r,   r/   r   r   CompileModelRequestChipTypeValueValuesEnumEDGE_TPU_V1InputConfigr2   OutputConfigGcsDestination"EdgemlProjectsModelsCompileRequestr   Compiler!   r4   r   r5   CompileModelResponser7   )r"   r   destinationr,   compile_req_type	chip_typeinput_configoutput_configcompile_reqr9   r:   r;   s               r   rM   EdgeMlClient.CompileO   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)   r>   r@   )rC   rD   )convertModelRequestr,   )r   r-   r.   r,   r/   r   r   rI   r2   rJ   rK   "EdgemlProjectsModelsConvertRequestConvertModelRequestr   Convertr!   r4   r   r5   ConvertModelResponser7   )
r"   r   rO   r,   rR   rS   convert_reqr9   r:   r;   s
             r   rZ   EdgeMlClient.Converty   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#   r3   rM   rZ   __static_attributes__ r   r   r   r   $   s    L:(!T$!r   r   )rb   r   apitools.base.pyr   googlecloudsdk.api_lib.edgemlr   r   googlecloudsdk.corer   r   r   objectr   rd   r   r   <module>ri      s5    ! 
 , 4 . *E
M
y!6 y!r   