<s> """<STR_LIT>""" <EOL> import sys <EOL> from docopt import docopt <EOL> from flask import Flask <EOL> from flask import make_response <EOL> from flask import request <EOL> import numpy as np <EOL> import ujson <EOL> from werkzeug . exceptions import BadRequest <EOL> from . import __version__ <EOL> from . interfaces import PredictError <EOL> from . util import args_from_config <EOL> from . util import get_config <EOL> from . util import get_metadata <EOL> from . util import initialize_config <EOL> from . util import logger <EOL> from . util import memory_usage_psutil <EOL> from . util import PluggableDecorator <EOL> from . util import process_store <EOL> app = Flask ( __name__ ) <EOL> def make_ujson_response ( obj , status_code = <NUM_LIT:200> ) : <EOL> """<STR_LIT>""" <EOL> json_encoded = ujson . encode ( obj , ensure_ascii = False , double_precision = - <NUM_LIT:1> ) <EOL> resp = make_response ( json_encoded ) <EOL> resp . mimetype = '<STR_LIT:application/json>' <EOL> resp . content_type = '<STR_LIT>' <EOL> resp . status_code = status_code <EOL> return resp <EOL> class PredictService : <EOL> """<STR_LIT>""" <EOL> types = { <EOL> '<STR_LIT:float>' : float , <EOL> '<STR_LIT:int>' : int , <EOL> '<STR_LIT:str>' : str , <EOL> '<STR_LIT:bool>' : lambda x : x . lower ( ) == '<STR_LIT:true>' , <EOL> } <EOL> def __init__ ( self , mapping , params = ( ) , predict_proba = False , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . mapping = mapping <EOL> self . params = params <EOL> self . predict_proba = predict_proba <EOL> vars ( self ) . update ( kwargs ) <EOL> def __call__ ( self , model , request ) : <EOL> try : <EOL> return self . do ( model , request ) <EOL> except Exception as e : <EOL> return self . response_from_exception ( e ) <EOL> def do ( self , model , request ) : <EOL> if request . method == '<STR_LIT:GET>' : <EOL> single = True <EOL> samples = np . array ( [ self . sample_from_data ( model , request . args ) ] ) <EOL> else : <EOL> single = False <EOL> samples = [ ] <EOL> for data in request . json : <EOL> samples . append ( self . sample_from_data ( model , data ) ) <EOL> samples = np . array ( samples ) <EOL> params = self . params_from_data ( model , request . args ) <EOL> y_pred = self . predict ( model , samples , ** params ) <EOL> return self . response_from_prediction ( y_pred , single = single ) <EOL> def sample_from_data ( self , model , data ) : <EOL> """<STR_LIT>""" <EOL> values = [ ] <EOL> for key , type_name in self . mapping : <EOL> value_type = self . types [ type_name ] <EOL> values . append ( value_type ( data [ key ] ) ) <EOL> return np . array ( values , dtype = object ) <EOL> def params_from_data ( self , model , data ) : <EOL> """<STR_LIT>""" <EOL> params = { } <EOL> for key , type_name in self . params : <EOL> value_type = self . types [ type_name ] <EOL> if key in data : <EOL> params [ key ] = value_type ( data [ key ] ) <EOL> elif hasattr ( model , key ) : <EOL> params [ key ] = getattr ( model , key ) <EOL> return params <EOL> def predict ( self , model , sample , ** kwargs ) : <EOL> if self . predict_proba : <EOL> return model . predict_proba ( sample , ** kwargs ) <EOL> else : <EOL> return model . predict ( sample , ** kwargs ) <EOL> def response_from_prediction ( self , y_pred , single = True ) : <EOL> """<STR_LIT>""" <EOL> result = y_pred . tolist ( ) <EOL> if single : <EOL> result = result [ <NUM_LIT:0> ] <EOL> response = { <EOL> '<STR_LIT>' : get_metadata ( ) , <EOL> '<STR_LIT:result>' : result , <EOL> } <EOL> return make_ujson_response ( response , status_code = <NUM_LIT:200> ) <EOL> def response_from_exception ( self , exc ) : <EOL> if isinstance ( exc , PredictError ) : <EOL> return make_ujson_response ( { <EOL> '<STR_LIT>' : get_metadata ( <EOL> error_code = exc . error_code , <EOL> error_message = exc . error_message , <EOL> status = "<STR_LIT>" <EOL> ) <EOL> } , status_code = <NUM_LIT> ) <EOL> elif isinstance ( exc , BadRequest ) : <EOL> return make_ujson_response ( { <EOL> '<STR_LIT>' : get_metadata ( <EOL> error_code = - <NUM_LIT:1> , <EOL> error_message = "<STR_LIT>" . format ( exc . args ) , <EOL> status = "<STR_LIT>" <EOL> ) <EOL> } , status_code = <NUM_LIT> ) <EOL> else : <EOL> logger . exception ( "<STR_LIT>" ) <EOL> return make_ujson_response ( { <EOL> '<STR_LIT>' : get_metadata ( <EOL> error_code = - <NUM_LIT:1> , <EOL> error_message = "<STR_LIT>" . format ( <EOL> exc . __class__ . __name__ , str ( exc ) ) , <EOL> status = "<STR_LIT>" <EOL> ) <EOL> } , status_code = <NUM_LIT> ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> @ PluggableDecorator ( '<STR_LIT>' ) <EOL> @ args_from_config <EOL> def predict ( model_persister , predict_service ) : <EOL> try : <EOL> model = model_persister . read ( ) <EOL> response = predict_service ( model , request ) <EOL> except Exception as exc : <EOL> logger . exception ( "<STR_LIT>" ) <EOL> response = make_ujson_response ( { <EOL> "<STR_LIT:status>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : - <NUM_LIT:1> , <EOL> "<STR_LIT>" : "<STR_LIT>" . format ( exc . __class__ . __name__ , str ( exc ) ) , <EOL> } , status_code = <NUM_LIT> ) <EOL> return response <EOL> @ app . route ( '<STR_LIT>' ) <EOL> @ PluggableDecorator ( '<STR_LIT>' ) <EOL> @ args_from_config <EOL> def alive ( alive = None ) : <EOL> if alive is None : <EOL> alive = { } <EOL> mem , mem_vms = memory_usage_psutil ( ) <EOL> info = { <EOL> '<STR_LIT>' : mem , <EOL> '<STR_LIT>' : mem_vms , <EOL> '<STR_LIT>' : __version__ , <EOL> } <EOL> info [ '<STR_LIT>' ] = get_config ( ) . get ( '<STR_LIT>' , { } ) <EOL> status_code = <NUM_LIT:200> <EOL> for attr in alive . get ( '<STR_LIT>' , ( ) ) : <EOL> obj = process_store . get ( attr ) <EOL> if obj is not None : <EOL> obj_info = { } <EOL> obj_info [ '<STR_LIT>' ] = process_store . mtime [ '<STR_LIT>' ] . isoformat ( ) <EOL> if hasattr ( obj , '<STR_LIT>' ) : <EOL> obj_info [ '<STR_LIT>' ] = obj . __metadata__ <EOL> info [ attr ] = obj_info <EOL> else : <EOL> info [ attr ] = "<STR_LIT>" <EOL> status_code = <NUM_LIT> <EOL> return make_ujson_response ( info , status_code = status_code ) <EOL> def devserver_cmd ( argv = sys . argv [ <NUM_LIT:1> : ] ) : <EOL> """<STR_LIT>""" <EOL> arguments = docopt ( devserver_cmd . __doc__ , argv = argv ) <EOL> initialize_config ( ) <EOL> app . run ( <EOL> host = arguments [ '<STR_LIT>' ] , <EOL> port = int ( arguments [ '<STR_LIT>' ] ) , <EOL> debug = int ( arguments [ '<STR_LIT>' ] ) , <EOL> ) <EOL> class PredictStream : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . model = get_config ( ) [ '<STR_LIT>' ] . read ( ) <EOL> self . predict_service = get_config ( ) [ '<STR_LIT>' ] <EOL> def process_line ( self , line ) : <EOL> predict_service = self . predict_service <EOL> datas = ujson . loads ( line ) <EOL> samples = [ predict_service . sample_from_data ( self . model , data ) <EOL> for data in datas ] <EOL> samples = np . array ( samples ) <EOL> params = predict_service . params_from_data ( self . model , datas [ <NUM_LIT:0> ] ) <EOL> return predict_service . predict ( self . model , samples , ** params ) <EOL> def listen ( self , io_in , io_out , io_err ) : <EOL> """<STR_LIT>""" <EOL> for line in io_in : <EOL> if line . strip ( ) . lower ( ) == '<STR_LIT>' : <EOL> break <EOL> try : <EOL> y_pred = self . process_line ( line ) <EOL> except Exception as e : <EOL> io_out . write ( '<STR_LIT>' ) <EOL> io_err . write ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" . format ( line , type ( e ) , e ) ) <EOL> io_err . flush ( ) <EOL> else : <EOL> io_out . write ( ujson . dumps ( y_pred . tolist ( ) ) ) <EOL> io_out . write ( '<STR_LIT:\n>' ) <EOL> io_out . flush ( ) <EOL> def stream_cmd ( argv = sys . argv [ <NUM_LIT:1> : ] ) : <EOL> """<STR_LIT>""" <EOL> docopt ( stream_cmd . __doc__ , argv = argv ) <EOL> initialize_config ( ) <EOL> stream = PredictStream ( ) <EOL> stream . listen ( sys . stdin , sys . stdout , sys . stderr ) </s>
