Beispiel #1
0
def start_training(bot: str, user: str, token: str = None, reload=True):
    """
    prevents training of the bot,
    if the training session is in progress otherwise start training

    :param reload: whether to reload model in the cache
    :param bot: bot id
    :param token: JWT token for remote model reload
    :param user: user id
    :return: model path
    """
    exception = None
    model_file = None
    training_status = None
    if Utility.environment.get('model') and Utility.environment['model'][
            'train'].get('event_url'):
        Utility.train_model_event(bot, user, token)
    else:
        try:
            model_file = train_model_for_bot(bot)
            training_status = MODEL_TRAINING_STATUS.DONE.value
            agent_url = Utility.environment['model']['train'].get('agent_url')
            if agent_url:
                Utility.http_request(
                    'get', urljoin(agent_url, "/api/bot/model/reload"), token,
                    user)
            else:
                if reload:
                    AgentProcessor.reload(bot)
        except Exception as e:
            logging.exception(e)
            training_status = MODEL_TRAINING_STATUS.FAIL.value
            exception = str(e)
        finally:
            ModelProcessor.set_training_status(
                bot=bot,
                user=user,
                status=training_status,
                model_path=model_file,
                exception=exception,
            )
    return model_file
Beispiel #2
0
def start_training(bot: str, user: str, reload=True):
    """
    prevents training of the bot,
    if the training session is in progress otherwise start training

    :param reload: whether to reload model in the cache
    :param bot: bot id
    :param user: user id
    :return: model path
    """
    exception = None
    model_file = None
    training_status = None

    ModelProcessor.set_training_status(
        bot=bot, user=user, status=MODEL_TRAINING_STATUS.INPROGRESS.value,
    )
    try:
        model_file = train_model_for_bot(bot)
        training_status = MODEL_TRAINING_STATUS.DONE.value
    except Exception as e:
        logging.exception(e)
        training_status = MODEL_TRAINING_STATUS.FAIL.value
        exception = str(e)
        raise AppException(exception)
    finally:
        ModelProcessor.set_training_status(
            bot=bot,
            user=user,
            status=training_status,
            model_path=model_file,
            exception=exception,
        )
    if reload:
        AgentProcessor.reload(bot)
    return model_file