def add_user( email: str, password: str, first_name: str, last_name: str, account: int, bot: str, user: str, is_integration_user=False, role="trainer", ): Utility.is_exist( User, query={"email": email}, exp_message= "User already exists! try with different email address.", ) return (User( email=email, password=Utility.get_password_hash(password), first_name=first_name, last_name=last_name, account=account, bot=bot, user=user, is_integration_user=is_integration_user, role=role, ).save().to_mongo().to_dict())
def add_account(name: str, user: str): """ Adds a new account for the trainer app """ assert not Utility.check_empty_string(name), "Account Name cannot be empty or blank spaces" Utility.is_exist( Account, exp_message="Account name already exists!", name__iexact=name, status=True ) return Account(name=name.strip(), user=user).save().to_mongo().to_dict()
def add_user(): Utility.load_evironment() connect(Utility.environment["mongo_db"], host=Utility.environment["mongo_url"]) user = AccountProcessor.account_setup( { "email": "*****@*****.**", "first_name": "Demo", "last_name": "User", "password": "******", "account": "integration", "bot": "integration" }, user="******") processor = MongoProcessor() processor.save_from_path("tests/testing_data/all", user['bot'], "testAdmin") AccountProcessor.account_setup( { "email": "*****@*****.**", "first_name": "Demo", "last_name": "User", "password": "******", "account": "integration2", "bot": "integration2" }, user="******")
def add_user(): Utility.load_evironment() connect(Utility.environment["mongo_db"], host=Utility.environment["mongo_url"]) account = AccountProcessor.add_account("integration", "testAdmin") bot = AccountProcessor.add_bot("integration", account["_id"], "testAdmin") AccountProcessor.add_user( email="*****@*****.**", first_name="Demo", last_name="User", password="******", account=account["_id"], bot=bot["name"], user="******", ) account = AccountProcessor.add_account("integration2", "testAdmin") bot = AccountProcessor.add_bot("integration2", account["_id"], "testAdmin") AccountProcessor.add_user( email="*****@*****.**", first_name="Demo", last_name="User", password="******", account=account["_id"], bot=bot["name"], user="******", )
def train_model_for_bot(bot: str): """ Trains the rasa model, using the data that is loaded onto Mongo, through the bot files """ processor = MongoProcessor() nlu = processor.load_nlu(bot) if not nlu.training_examples: raise AppException("Training data does not exists!") domain = processor.load_domain(bot) stories = processor.load_stories(bot) config = processor.load_config(bot) directory = Utility.save_files( nlu.nlu_as_markdown().encode(), domain.as_yaml().encode(), stories.as_story_string().encode(), yaml.dump(config).encode(), ) output = os.path.join(DEFAULT_MODELS_PATH, bot) model = train(domain=os.path.join(directory,DEFAULT_DOMAIN_PATH), config=os.path.join(directory,DEFAULT_CONFIG_PATH), training_files=os.path.join(directory,DEFAULT_DATA_PATH), output=output) Utility.delete_directory(directory) return model
def validate(self, clean=True): Utility.validate_document_list(self.events) if Utility.check_empty_string(self.block_name): raise ValidationError( "Story path name cannot be empty or blank spaces") elif not self.events: raise ValidationError("Stories cannot be empty")
def add_user( email: str, password: str, first_name: str, last_name: str, account: int, bot: str, user: str, is_integration_user=False, role="trainer", ): """ Adds a new user to the app based on the details provided by the user """ assert not Utility.check_empty_string(email) and not Utility.check_empty_string(last_name) and not Utility.check_empty_string(first_name) and not Utility.check_empty_string(password),"Email, FirstName, LastName and password cannot be empty or blank spaces " Utility.is_exist( User, exp_message="User already exists! try with different email address.", email__iexact=email.strip(), status=True ) return ( User( email=email.strip(), password=Utility.get_password_hash(password.strip()), first_name=first_name.strip(), last_name=last_name.strip(), account=account, bot=bot.strip(), user=user.strip(), is_integration_user=is_integration_user, role=role.strip(), ) .save() .to_mongo() .to_dict() )
def validate(self, clean=True): if not self.title or not self.payload: raise ValidationError("title and payload must be present!") elif Utility.check_empty_string( self.title) or Utility.check_empty_string( self.payload.strip()): raise ValidationError( "Response title and payload cannot be empty or blank spaces")
def validate(self, clean=True): if Utility.check_empty_string(self.name): raise ValidationError( "Intent Name cannot be empty or blank spaces") else: if not Utility.special_match(self.name): raise ValidationError( "Intent Name must contains alphabets, number and underscore" )
def add_bot(name: str, account: int, user: str): """ Adds a bot to the specified user account """ assert not Utility.check_empty_string(name), "Bot Name cannot be empty or blank spaces" Utility.is_exist( Bot, exp_message="Bot already exists!", name__iexact=name, account=account, status=True ) return Bot(name=name, account=account, user=user).save().to_mongo().to_dict()
def validate(self, clean=True): if (Utility.check_empty_string(self.email) or Utility.check_empty_string(self.first_name) or Utility.check_empty_string(self.last_name) or Utility.check_empty_string(self.password)): raise ValidationError( "Email, FirstName, LastName and password cannot be empty or blank space" ) elif isinstance(email(self.email), ValidationFailure): raise ValidationError("Please enter valid email address")
def add_action(self, name: Text, bot: Text, user: Text): Utility.is_exist( Actions, query={ "name": name, "bot": bot }, exp_message="Entity already exists!", ) Actions(name=name, bot=bot, user=user).save()
def validate(self, clean=True): if Utility.check_empty_string(self.name) or Utility.check_empty_string( self.pattern): raise ValidationError( "Regex name and pattern cannot be empty or blank spaces") else: try: re.compile(self.pattern) except AppException as e: raise AppException("invalid regular expression " + self.pattern)
def add_bot(name: str, account: int, user: str): Utility.is_exist( Bot, query={ "name": name, "account": account }, exp_message="Bot already exists!", ) return Bot(name=name, account=account, user=user).save().to_mongo().to_dict()
def validate(self, clean=True): Utility.validate_document_list(self.events) if Utility.check_empty_string(self.block_name): raise ValidationError( "Story path name cannot be empty or blank spaces") elif not self.events: raise ValidationError("Stories cannot be empty") elif self.events[0].type != "user": raise ValidationError("Stories must start with intent") elif self.events[-1].type != "action": raise ValidationError("Stories must end with action")
def add_intent(self, text: Text, bot: Text, user: Text): Utility.is_exist( Intents, query={ "name": text, "bot": bot }, exp_message="Intent already exists!", ) saved = Intents(name=text, bot=bot, user=user).save().to_mongo().to_dict() return saved["_id"].__str__()
def validate(self, clean=True): if (Utility.check_empty_string(self.type) or Utility.check_empty_string(self.url) or Utility.check_empty_string(self.db)): raise ValidationError( "Type, Url and DB cannot be blank or empty spaces") else: if self.type == "mongo": try: parse_uri(self.url) except InvalidURI: raise AppException("Invalid tracker url!")
async def upload_and_save( self, nlu: bytes, domain: bytes, stories: bytes, config: bytes, bot: Text, user: Text, overwrite: bool = True, ): """Upload the training data to temporary path and then save into mongo.""" data_path = Utility.save_files(nlu, domain, stories, config) await self.save_from_path(data_path, bot, overwrite, user) Utility.delete_directory(data_path)
def add_training_example(self, examples: List[Text], intent: Text, bot: Text, user: Text): if not Utility.is_exist( Intents, query={ "name": intent, "bot": bot }, raise_error=False): self.add_intent(intent, bot, user) for example in examples: if Utility.is_exist(TrainingExamples, query={ "text": example, "bot": bot }, raise_error=False): yield { "text": example, "message": "Training Example already exists!", "_id": None, } else: training_example = TrainingExamples(intent=intent, text=example, bot=bot, user=user) if not Utility.check_empty_string(example): entities = MarkdownReader._find_entities_in_training_example( example) if entities: ext_entity = [ent["entity"] for ent in entities] self.__save_domain_entities(ext_entity, bot=bot, user=user) self.__add_slots_from_entities(ext_entity, bot, user) training_example.text = re.sub( ent_regex, lambda m: m.groupdict()["entity_text"], example) training_example.entities = list( self.__extract_entities(entities)) try: saved = training_example.save().to_mongo().to_dict() yield { "text": example, "_id": saved["_id"].__str__(), "message": "Training Example added successfully!", } except Exception as e: yield {"text": example, "_id": None, "message": str(e)}
def add_entity(self, name: Text, bot: Text, user: Text): Utility.is_exist( Entities, query={ "name": name, "bot": bot }, exp_message="Entity already exists!", ) Entities(name=name, bot=bot, user=user).save() if not Utility.is_exist( Slots, query={ "name": name, "bot": bot }, raise_error=False): Slots(name=name, type="text", bot=bot, user=user).save()
def test_augment_questions(): responses.add( responses.POST, "http://localhost:8000/questions", json={ "sucess": True, "data": { "questions": [ "where is digite centrally located?", "where is digite conveniently located?", "where is digite islocated?", "where is digite situated?", "where is digite strategically located?", ] }, "message": None, "error_code": 0, }, status=200, ) response = client.post( "/api/augment/questions", headers={ "Authorization": pytest.token_type + " " + pytest.access_token }, json={"data": ["where is digite located?'"]}, ) actual = response.json() assert actual["success"] assert actual["error_code"] == 0 assert actual["data"] assert Utility.check_empty_string(actual["message"])
def __check_response_existence(self, response: Dict, bot: Text, exp_message: Text = None, raise_error=True): saved_responses = list( Responses.objects(bot=bot, status=True).aggregate([{ "$group": { "_id": "$name", "texts": { "$push": "$text" }, "customs": { "$push": "$custom" }, } }])) saved_items = list( itertools.chain.from_iterable([ items["texts"] + items["customs"] for items in saved_responses ])) if response in saved_items: if raise_error: if Utility.check_empty_string(exp_message): raise AppException("Exception message cannot be empty") raise AppException(exp_message) else: return True else: if not raise_error: return False
def test_integration_token(): response = client.get("/api/auth/integration/token", headers={ "Authorization": pytest.token_type + " " + pytest.access_token }) actual = response.json() assert actual["success"] assert actual["error_code"] == 0 assert actual["data"]['access_token'] assert actual["data"]['token_type'] assert actual[ "message"] == '''It is your responsibility to keep the token secret. If leaked then other may have access to your system.''' response = client.get( "/api/bot/intents", headers={ "Authorization": actual["data"]['token_type'] + " " + actual["data"]['access_token'], "X-USER": "******" }, ) actual = response.json() assert "data" in actual assert len(actual["data"]) == 23 assert actual["success"] assert actual["error_code"] == 0 assert Utility.check_empty_string(actual["message"])
def test_load_from_path_all_sccenario(self): processor = MongoProcessor() processor.save_from_path("tests/testing_data/all", "all", "testUser") training_data = processor.load_nlu("all") assert isinstance(training_data, TrainingData) assert training_data.training_examples.__len__() == 283 assert training_data.entity_synonyms.__len__() == 3 assert training_data.regex_features.__len__() == 5 assert training_data.lookup_tables.__len__() == 1 story_graph = processor.load_stories("all") assert isinstance(story_graph, StoryGraph) == True assert story_graph.story_steps.__len__() == 13 domain = processor.load_domain("all") assert isinstance(domain, Domain) assert domain.slots.__len__() == 8 assert domain.templates.keys().__len__() == 21 assert domain.entities.__len__() == 7 assert domain.form_names.__len__() == 2 assert domain.user_actions.__len__() == 32 assert domain.intents.__len__() == 22 assert not Utility.check_empty_string( domain.templates["utter_cheer_up"][0]["image"] ) assert domain.templates["utter_did_that_help"][0]["buttons"].__len__() == 2 assert domain.templates["utter_offer_help"][0]["custom"] assert domain.slots[0].type_name == "unfeaturized"
def __check_event_existence(self, events: List[Dict], bot: Text, exp_message: Text = None, raise_error=True): saved_events = list( Stories.objects(bot=bot, status=True).aggregate([{ "$group": { "_id": "$name", "events": { "$push": "$events" } } }])) saved_items = list( itertools.chain.from_iterable( [items["events"] for items in saved_events])) if events in saved_items: if raise_error: if Utility.check_empty_string(exp_message): raise AppException("Exception message cannot be empty") raise AppException(exp_message) else: return True else: if not raise_error: return False
def __authenticate_user(self, username: str, password: str): user = AccountProcessor.get_user_details(username) if not user: return False if not Utility.verify_password(password, user["password"]): return False return user
async def get_current_user(self, request: Request, token: str = Depends(Utility.oauth2_scheme)): credentials_exception = HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="Could not validate credentials", headers={"WWW-Authenticate": "Bearer"}, ) try: payload = jwt.decode(token, self.SECRET_KEY, algorithms=[self.ALGORITHM]) username: str = payload.get("sub") if username is None: raise credentials_exception token_data = TokenData(username=username) except PyJWTError: raise credentials_exception user = AccountProcessor.get_user_details(token_data.username) if user is None: raise credentials_exception user_model = User(**user) if user['is_integration_user']: alias_user = request.headers.get("X-USER") if Utility.check_empty_string(alias_user): raise HTTPException( status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail="Alias user missing for integration", headers={"WWW-Authenticate": "Bearer"}, ) user_model.alias_user = alias_user return User(**user)
def validate(self, clean=True): if self.entities: for ent in self.entities: ent.validate() extracted_ent = self.text[ent.start:ent.end] if extracted_ent != ent.value: raise ValidationError( "Invalid entity: " + ent.entity + ", value: " + ent.value + " does not match with the position in the text " + extracted_ent) elif Utility.check_empty_string( self.text) or Utility.check_empty_string(self.intent): raise ValidationError( "Training Example name and text cannot be empty or blank spaces" )
def __authenticate_user(self, username: str, password: str): """ Checks if the user name and password match. It returns the user details if they match and returns False if not """ user = AccountProcessor.get_user_details(username) if not user: return False if not Utility.verify_password(password, user["password"]): return False return user
def get_training_examples(self, intent: Text, bot: Text): training_examples = list( TrainingExamples.objects(bot=bot, intent=intent, status=True)) for training_example in training_examples: example = training_example.to_mongo().to_dict() entities = example["entities"] if "entities" in example else None yield { "_id": example["_id"].__str__(), "text": Utility.prepare_nlu_text(example["text"], entities), }