def run_similar_user_generator(gen_model, params, data_loader, model_handle): logger.info("Entering run_similar_user_generator function") try: data = data_loader.load_data() if model_handle == "dnn_collab_filtering_user_course_level": model = gen_model(data, **params) else: model = gen_model(**params) model.fit(data) count = 0 for user_handle in data.user_handle.unique(): count += 1 similar_users = model.predict_similar_users( user_handle=user_handle).similar_users.tolist() user = UserListModel( user_handle=int(user_handle), model_handle=model_handle, similar_users=similar_users, created_at=datetime.utcnow(), ) user.save() if count % 1000 == 0: logger.info(f"Saved {count} records in the Database") logger.info("Existing run_similar_user_generator function") except: log_exception()
def post(self): data = SimilarUserList.parser.parse_args() try: user_handle = data["user_handle"] if data["model_handle"]: model_handle = data["model_handle"] response = UserListModel.query( user_handle, UserListModel.model_handle == model_handle.lower() ).__next__() else: response = UserListModel.query(user_handle).__next__() return ( { "model_handle": response.model_handle, "similar_users": response.similar_users, }, ) except StopIteration: return ( {"message": f'User with user_handle: {data["user_handle"]} not found'}, 404, )
def query_table(user_handle, model_handle="knn_user_interest"): return UserListModel.query( user_handle, UserListModel.model_handle == model_handle ).__next__()
def delete_table(): UserListModel.delete_table()
def check_if_table_exists(): return UserListModel.exists()
def create_table(): if not UserListModel.exists(): UserListModel.create_table( read_capacity_units=1, write_capacity_units=1, wait=True )
def create_mock_user_entry_in_db(get_mock_user_sim_users_pair): user, sim_users = get_mock_user_sim_users_pair user = UserListModel(user_handle=user, similar_users=sim_users) print() user.save()