async def train( background_tasks: BackgroundTasks, current_user: User = Depends(auth.get_current_user), ): """ Trains the chatbot """ ModelProcessor.is_training_inprogress(current_user.get_bot()) ModelProcessor.is_daily_training_limit_exceeded(current_user.get_bot()) background_tasks.add_task(start_training, current_user.get_bot(), current_user.get_user()) return {"message": "Model training started."}
async def get_model_training_history( current_user: User = Depends(auth.get_current_user), ): """ Fetches model training history, when and who trained the bot """ training_history = list( ModelProcessor.get_training_history(current_user.get_bot())) return {"data": {"training_history": training_history}}
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
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
async def train( background_tasks: BackgroundTasks, current_user: User = Depends(auth.get_current_user), ): """ Trains the chatbot """ ModelProcessor.is_training_inprogress(current_user.get_bot()) ModelProcessor.is_daily_training_limit_exceeded(current_user.get_bot()) ModelProcessor.set_training_status( bot=current_user.get_bot(), user=current_user.get_user(), status=MODEL_TRAINING_STATUS.INPROGRESS.value, ) token = auth.create_access_token(data={"sub": current_user.email}) background_tasks.add_task( start_training, current_user.get_bot(), current_user.get_user(), token.decode('utf8') ) return {"message": "Model training started."}