
                             d 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dlZddlZd Z	dZ
d	Zd
Zd Zd Zd Zd Zd Zedk(  r e        yy)zUtilities for running predictions locally.

This module will always be run within a subprocess, and therefore normal
conventions of Cloud SDK do not apply here.
    )absolute_import)division)print_function)unicode_literalsNc                  :    t        | dt        j                  i| y)zPrint to stderr.fileN)printsysstderr)argskwargss     9lib/googlecloudsdk/command_lib/ml_engine/local_predict.pyeprintr      s     )CJJ)&)    zsPlease verify the installed tensorflow version with: "python -c 'import tensorflow; print tensorflow.__version__'".zjPlease verify the installed sklearn version with: "python -c 'import sklearn; print sklearn.__version__'".zjPlease verify the installed xgboost version with: "python -c 'import xgboost; print xgboost.__version__'".c                 
   	 ddl mc m} 	 |j
                  | k  r t	        dj                  |       t               y	 y# t        $ r t	        d       Y yw xY w# t        t        f$ r}t	        d|dt               Y d}~yd}~ww xY w)	z@Check whether TensorFlow is installed at an appropriate version.r   NzNCannot import Tensorflow. Please verify "python -c 'import tensorflow'" works.Fz(Tensorflow version must be at least {} .z6Error while getting the installed TensorFlow version: 
T)
tensorflow.compat.v1compatv1ImportErrorr   __version__formatVERIFY_TENSORFLOW_VERSION	NameErrorAttributeError)versiontfes      r   _verify_tensorflowr   3   s    %%	~~7>>wG&(   
 
 
 6 7 ^	$ 
CQ*,s&   	= .A AAB&A==Bc                 >   	 ddl }	 ddl}	 |j                  | k  r t        dj                  |       t               y	 y	# t        $ r t        d       Y yw xY w# t        $ r t        d       Y yw xY w# t        t        f$ r}t        d|dt               Y d}~yd}~ww xY w)
zBCheck whether scikit-learn is installed at an appropriate version.r   NzfCannot import scipy, which is needed for scikit-learn. Please verify "python -c 'import scipy'" works.FzHCannot import sklearn. Please verify "python -c 'import sklearn'" works.z*Scikit-learn version must be at least {} .z8Error while getting the installed scikit-learn version: r   T)	scipyr   r   sklearnr   r   VERIFY_SCIKIT_LEARN_VERSIONr   r   )r   r!   r"   r   s       r   _verify_scikit_learnr$   K   s    W$9@@I(* % 
- 
 
 8 9 
 
 3 4 ^	$ 
Eq$&(s7   = A .A1 AAA.-A.1B BBc                     	 ddl }	 |j                  | k  r t        dj	                  |       t
               y	 y# t        $ r t        d       Y yw xY w# t        t        f$ r}t        d|dt
               Y d}~yd}~ww xY w)	z=Check whether xgboost is installed at an appropriate version.r   NzHCannot import xgboost. Please verify "python -c 'import xgboost'" works.Fz%Xgboost version must be at least {} .z3Error while getting the installed xgboost version: r   T)xgboostr   r   r   r   VERIFY_XGBOOST_VERSIONr   r   )r   r&   r   s      r   _verify_xgboostr(   k   s    W$4;;GD#% % 
 
 
 3 4 ^	$ 
@!T!#s&   8 .A AAA=!A88A=c                     | dk(  r!t        d      st        j                  d       y| dk(  r!t        d      st        j                  d       y| dk(  r"t	        d      st        j                  d       yyy)	z=Verifies the appropriate ML libs are installed per framework.
tensorflowz1.0.0scikit_learnz0.18.1r&   z0.6a2N)r   r
   exitr$   r(   )	frameworks    r   _verify_ml_libsr/      sZ    ,'9''BHHRLN"+?+IHHRLIog&>HHRL '?r   c                     t        j                         } | j                  ddd       | j                  ddd d       | j                  d	dd
       | j                         \  }}|j                  "ddlm} |j                  |j                        }n|j                  }|rt        |       ddlm
} g }t        j                  D ]7  }t        j                  |j                  d            }|j!                  |       9 |j#                  |j                  |||j$                        }	t'        t        j(                  |	             y )Nz--model-dirTzPath of the model.)requiredhelpz--frameworkFzThe ML framework used to train this version of the model. If not specified, the framework will be identified based on the model file name stored in the specified model-dir)r1   defaultr2   z--signature-namez0Tensorflow signature to select input/output map.r   )prediction_utils)prediction_libr   )	model_dir	instancesr.   signature_name)argparseArgumentParseradd_argumentparse_known_argsr.   cloud.ml.predictionr4   detect_frameworkr6   r/   r5   r
   stdinjsonloadsrstripappendlocal_predictr8   r	   dumps)
parserr   _r4   r.   r5   r7   lineinstancepredictionss
             r   mainrK      s-   ""$&md9MNE	  G 	(5M  O##%'$	^^4 11$..AIII
 1 )iidzz$++d+,HX  ,,((	 - *+
 

; r   __main__)__doc__
__future__r   r   r   r   r9   r@   r
   r   r   r#   r'   r   r$   r(   r/   rK   __name__ r   r   <module>rQ      sn    '  % '   
*@  ? : 
0@0'!T z& r   