Пример #1
0
def create_app(config: Type[Config] = Config) -> Tuple[Flask, Api]:
    app = Flask(__name__)
    api = Api(app)

    app.config.from_object(config)
    """----------------- Logging -----------------"""
    app_logger = AppLogger(app_name=config.APP_NAME, env=config.ENV)
    """----------------- Middleware ---------------------"""
    # Before Request
    # Perform these functions in the declared order before every request.
    app.before_request_funcs = {
        None: [record_time_of_request, record_request_id]
    }

    # After Request
    # Perform this function after every request.
    app.after_request(record_access_log)
    """----------------- Bind Resources -----------------"""
    api.add_resource(Health, '/healthcheck')
    api.add_resource(Liveness, '/liveness')
    api.add_resource(Readiness,
                     '/readiness',
                     resource_class_kwargs={
                         'config': app.config,
                         'app_logger': app_logger
                     })

    # Initialize redis repository
    redis_repository = RedisRepository(config, app_logger)

    loader = Filesystem(Repository(config.MODEL_BASE_DIR))

    model_info = ModelInfo(config.USE_CASE_ID, config.MODEL_ID,
                           config.MODEL_VERSION_ID, config.TRAINING_ID)

    app_logger.info(msg="Loading model from file..")
    model = loader.load_model(model_info)

    api.add_resource(Predict,
                     '/predict',
                     resource_class_kwargs={
                         'predict_service':
                         PredictService(
                             model=model,
                             app_logger=app_logger,
                             customer_bgc_scores_repository=redis_repository),
                         'app_logger':
                         app_logger
                     })
    api.add_resource(Home,
                     '/',
                     resource_class_kwargs={
                         'config': app.config,
                         'model_info': model.to_model_info(),
                         'app_logger': app_logger
                     })

    add_error_handler(app)

    return app, api
Пример #2
0
from functools import wraps
from ssense_logger.app_logger import AppLogger
from app.config import Config

app_logger = AppLogger(app_name=Config.APP_NAME, env=Config.ENV)


def exception_decorator(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        try:
            result = func(*args, **kwargs)
            return result
        except BaseException as e:
            app_logger.error(msg=f'{func.__name__} failed: {e.__str__()}')

    return wrapper
Пример #3
0
class CollabTrain(object):
    def __init__(self, hits_data: DataFrame):
        self.hits_data = hits_data
        self.item_colname = 'b_g_c'
        self.model_params = {'B': 0.75, 'K': 250, 'K1': 100}
        self.model = None
        self.app_logger = AppLogger(app_name=Config.APP_NAME, env=Config.ENV)

    def coo_transform(self,
                      dataset,
                      userCol='memberID',
                      countCol='total_hits'):

        return coo_matrix((dataset[countCol].astype(np.float64),
                           (dataset[self.item_colname].cat.codes.copy(),
                            dataset[userCol].cat.codes.copy())),
                          dtype=np.float64)

    def item_mapping(self, dataset, userCol='memberID'):

        # Map each item and user to a unique numeric value
        dataset = dataset.copy()
        dataset[userCol] = dataset[userCol].astype('category')
        dataset[self.item_colname] = dataset[self.item_colname].astype(
            'category')

        # item dictionary
        item_dict = dict(enumerate(dataset[self.item_colname].cat.categories))

        hits_matrix = self.coo_transform(dataset)

        return hits_matrix, item_dict

    def transform_data(self):

        # Read in triples of user/item/hits from the input dataset

        self.app_logger.info(msg='Transforming user data')

        return self.item_mapping(dataset=self.hits_data)

    def get_model(self):

        # Get a model based off the input params

        self.app_logger.info(msg='Initializing the nearest neighbors model')

        return BM25Recommender(**self.model_params)

    def fit(self):

        # Transform data into sparse matrix
        hits_matrix, item_dict = self.transform_data()

        # Create a models from the input data
        self.model = self.get_model()

        self.app_logger.info(msg='Training the nearest neighbors model')

        sw = Stopwatch(start=True)

        self.model.fit(hits_matrix, show_progress=True)

        self.app_logger.info(msg='Elapsed time of model training: {}'.format(
            sw.elapsed.human_str()))

        return self.model.similarity, item_dict
Пример #4
0
 def __init__(self, hits_data: DataFrame):
     self.hits_data = hits_data
     self.item_colname = 'b_g_c'
     self.model_params = {'B': 0.75, 'K': 250, 'K1': 100}
     self.model = None
     self.app_logger = AppLogger(app_name=Config.APP_NAME, env=Config.ENV)
Пример #5
0
 def __init__(self, hits_data: DataFrame):
     self.hits_data = hits_data
     self.item_colname = "b_g_c"
     self.model_params = {"B": 0.75, "K": 250, "K1": 100}
     self.model = None
     self.app_logger = AppLogger(app_name=Config.APP_NAME, env=Config.ENV)
Пример #6
0
 def create(self) -> AppLogger:
     return AppLogger(app_name=self.app,
                      env=self.env,
                      get_correlation_ids=get_correlation_ids)