U
    UºcŽ  ã                   @   sB   d Z ddlmZ ddlmZ G dd„ deƒZdd„ Zdd	d
„ZdS )z,Common utils for all inference plugin files.é    )Úclassification_pb2)Úregression_pb2c                   @   s$   e Zd ZdZdd„ Zedd„ ƒZdS )ÚInvalidUserInputErrorz¬An exception to throw if user input is detected to be invalid.

  Attributes:
    original_exception: The triggering `Exception` object to be wrapped, or
      a string.
  c                 C   s   || _ t | ¡ dS )zInits InvalidUserInputError.N)Úoriginal_exceptionÚ	ExceptionÚ__init__)Úselfr   © r	   úN/tmp/pip-unpacked-wheel-15h0ro02/tensorboard_plugin_wit/_utils/common_utils.pyr      s    zInvalidUserInputError.__init__c                 C   s   dt | jƒ S )NzInvalidUserInputError: )Ústrr   )r   r	   r	   r
   Úmessage"   s    zInvalidUserInputError.messageN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úpropertyr   r	   r	   r	   r
   r      s   r   c                 C   s~   | j |j }|j}|jdkrlg }t|jjd jƒD ]6}||jjd j }| ||||jjd j … ¡ q2n|}t	||| j
ƒS )aL  Converts a PredictResponse to ClassificationResponse or RegressionResponse.

  Args:
    pred: PredictResponse to convert.
    serving_bundle: A `ServingBundle` object that contains the information about
      the serving request that the response was generated by.

  Returns:
    A ClassificationResponse or RegressionResponse.
  Úclassificationr   é   )ÚoutputsZpredict_output_tensorZ	float_valÚ
model_typeÚrangeZtensor_shapeZdimÚsizeÚappendÚconvert_prediction_valuesÚ
model_spec)ÚpredÚserving_bundleÚoutputZ
raw_outputÚvaluesÚexample_indexÚstartr	   r	   r
   Úconvert_predict_response'   s    
$r!   Nc           	      C   s®   |j dkrjt ¡ }tt| ƒƒD ]H}|jj ¡ }tt| | ƒƒD ]&}|j ¡ }| | | |_	t
|ƒ|_q>qn0t ¡ }tt| ƒƒD ]}|jj ¡ }| | |_q~|rª|j |¡ |S )a“  Converts tensor values into ClassificationResponse or RegressionResponse.

  Args:
    values: For classification, a 2D list of numbers. The first dimension is for
      each example being predicted. The second dimension are the probabilities
      for each class ID in the prediction. For regression, a 1D list of numbers,
      with a regression score for each example being predicted.
    serving_bundle: A `ServingBundle` object that contains the information about
      the serving request that the response was generated by.
    model_spec: Optional model spec to put into the response.

  Returns:
    A ClassificationResponse or RegressionResponse.
  r   )r   r   ZClassificationResponser   ÚlenÚresultZclassificationsÚaddÚclassesZscorer   Úlabelr   ZRegressionResponseZregressionsÚvaluer   ZCopyFrom)	r   r   r   Úresponser   r   Zclass_indexZclass_scoreZ
regressionr	   r	   r
   r   =   s    

r   )N)r   Z6tensorboard_plugin_wit._vendor.tensorflow_serving.apisr   r   r   r   r!   r   r	   r	   r	   r
   Ú<module>   s
   