Steps to build a PII Detection Bot using Microsoft Bot Framework SDK and Azure Cognitive Service for Language.
Personally Identifiable Information (PII) detection is one of the features offered by Azure Cognitive Service for Language which can identify, categorize, and even redact sensitive information in natural unstructured text. For example: phone numbers, email addresses, and forms of identification.
Here, we will be building this bot using:
- Azure Cognitive Service for Language which is a cloud-based service for Natural Language Processing(NLP) helping in understanding and analyzing text.
- Azure Bot Service which is a cloud platform to host bots and make them available to channels like Facebook, MS Teams etc.
- Microsoft Bot Framework SDK which is used to develop conversational applications in Python, Java, JavaScript and C#, that can be hosted on Azure Bot Service.
- Python 3.X version:
- Microsoft Azure Account
- Editor of choice:
- Terminal of choice with Azure CLI installed.
-
Sign-in to your Microsoft Azure account.
-
Create a Resource Group (in a region closest to your location) to logically contain your Azure Resources.
-
Create an Azure Cognitive Service for Language inside the Resource Group created above.
-
Open a Python project(like the one explained in echo-bot) in editor of choice(like VSCode).
-
Install the required dependencies as shown in the
requirements.txt
file using the command:pip install -r requirements.txt
This will install the dependencies like
botbuilder-core
,asyncio
,aiohttp
,cookiecutter
-
Login to Microsoft Azure Account and set your subscription:
az login az account set --subscription "<< subscription-id >>"
-
Create an App Registration in the Azure portal and make a note of its
Application (client) ID
andappSecret
. -
Update the
config.py
file in VSCode with the copiedappId
andappSecret
in theMicrosoftAppId
andMicrosoftAppPassword
respectively. -
Update
bot.py
with theKeys
andEndpoint
of theAzure Language Service
which are used to authenticate and connect to thetext-analytics-client
. Thetext-analytics-client
is used to recognize PII entities in the input user text. The bot contains mainly two functions:-
on_members_added_activity
: this function gets called whenever a user accesses the bot and greets the user with "Hello and welcome to the PII detection bot". -
on_message_activity
: this function gets called whenever a user interacts with the bot. This function takes the user input text and calls the text-analytics-client for PII recognition. This function prints the redacted text and the PII categories of the redacted text.
-
-
Create an App Service and App Service Plan with Azure Resource Manager(ARM) template:
-
Modify the
parameters-for-template-BotApp-with-rg.json
with values forappServiceName
,newAppServicePlanName
,newAppServicePlanLocation
,appId
,appSecret
-
Change directory to
deploymentTemplates\deployUseExistResourceGroup\
cd .\deploymentTemplates\deployUseExistResourceGroup\
-
Create a deployment for
App Service
andApp Service Plan
with ARM template-filetemplate-BotApp-with-rg.json
:az deployment group create --resource-group "<<Resource-Group-Name>>" --template-file "<<template-file-json>>" --parameters "@<<parameters-file-json>>"
-
-
Create an Azure Bot Service with Azure Resource Manager(ARM) template:
-
Modify the
parameters-for-template-AzureBot-with-rg.json
with values forazureBotId
,botEndpoint
,appId
. -
Create a deployment for
Azure Bot Service
with ARM template-filetemplate-AzureBot-with-rg.json
:az deployment group create --resource-group "<<Resource-Group-Name>>" --template-file "<<template-file-json>>" --parameters "@<<parameters-file-json>>"
-
-
The creation of
Azure Bot Service
,App Service
,App Service Plan
can be verified by navigating to the Azure portal and the respective Resource Group. -
Package the Microsoft Bot Framework SDK(python code) into a
.zip
file. -
Deploy the packaged SDK as a
web-app
-
Change the working directory to the root of the project.
-
Run the command:
az webapp deployment source config-zip --resource-group "<<resource-group-name>>" --name "<<app-servicename>>" --src "<<packaged-zip-code>>"
-
If the command runs successfully, it will deploy the Bot framework SDK as a web-application and the deployment endpoint responds with an HTTP status code 202.
-
-
The deployed
Azure Bot PII service
can be tested in the Azure portal with any example as shown below:-
Example 1:
My name is John Doe and my phone number is 1234567890. I can be contacted over john.doe@gmail.com. I live in Seattle.
-
Example 2:
Barack Obama is an American politician who served as the 44th President of the United States from 2009 to 2017. He is the first African American to have served as president, as well as the first born outside the contiguous United States.
-
Example 3:
You can even pre-order from their online menu at www.contososteakhouse.com, call 312-555-0176 or send email to order@contososteakhouse.com!
-