These examples demonstrate how to mask PII from text to safeguard personal identification information of employees stored in a TeradataML DataFrame.
obj_aws.mask_pii_(column="employee_data", data=df_employeeData, persist=True)
Output:
| employee_data | PII_Entities | Masked_Phrase |
|---|---|---|
| Linda Taylor a German cleared all their loans by 2020 12 31 They can be reached at 555 555 5559 Thei | ('Name'='Linda Taylor', 'start_position'=0, 'length'=12), ('nationality'='German', 'start_position'=15, 'length'=6), ('date/time'='2020 12 31', 'start_position'=49, 'length'=10), ('contact numbers'='555 555 5559', 'start_position'=83, 'length'=12) | ************ a ****** cleared all their loans by ********** They can be reached at ************ Thei |
| Parker Doe originally from Brazil has successfully cleared all their loans by 2020 04 25 Reach them | ('Name'='Parker Doe', 'start_position'=0, 'length'=10), ('date/time'='2020 04 25', 'start_position'=78, 'length'=10) | ********** originally from Brazil has successfully cleared all their loans by ********** Reach them |
| Emily Johnson from the UK cleared all their loans by 2021 01 15 They can be contacted at 555 555 555 | ('Name'='Emily Johnson', 'start_position'=0, 'length'=13), ('address'='the UK', 'start_position'=19, 'length'=6), ('date/time'='2021 01 15', 'start_position'=53, 'length'=10), ('contact numbers'='555 555 555', 'start_position'=89, 'length'=11) | ************* from ****** cleared all their loans by ********** They can be contacted at *********** |
| Michael Brown an Australian has a loan due on 2023 07 20 Contact them at 555 555 5558 Their SSN is 6 | ('Name'='Michael Brown', 'start_position'=0, 'length'=13), ('nationality'='Australian', 'start_position'=17, 'length'=10), ('date/time'='2023 07 20', 'start_position'=46, 'length'=10), ('contact numbers'='555 555 5558', 'start_position'=73, 'length'=12) | ************* an ********** has a loan due on ********** Contact them at ************ Their SSN is 6 |
| Alex Smith a Canadian has an outstanding loan due on 2022 05 30 Their contact number is 555 555 5556 | ('Name'='Alex Smith', 'start_position'=0, 'length'=10), ('Nationality'='Canadian', 'start_position'=13, 'length'=8), ('date/time'='2022 05 30', 'start_position'=53, 'length'=10), ('contact numbers'='555 555 5556', 'start_position'=88, 'length'=12) | ********** a ******** has an outstanding loan due on ********** Their contact number is ************ |
obj_azure.mask_pii(column="employee_data",data=data,accumulate='employee_data',volatile=True)
Output:
| employee_data | PII_Entities | Masked_Phrase |
|---|---|---|
| Linda Taylor a German cleared all their loans by 2020 12 31 They can be reached at 555 555 5559 Thei | ('Name'='Linda Taylor', 'start_position'=0, 'length'=12), ('nationality'='German', 'start_position'=15, 'length'=6), ('date/time'='2020 12 31', 'start_position'=49, 'length'=10), ('contact numbers'='555 555 5559', 'start_position'=83, 'length'=12) | ************ a ****** cleared all their loans by ********** They can be reached at ************ Thei |
| Parker Doe originally from Brazil has successfully cleared all their loans by 2020 04 25 Reach them | ('Name'='Parker Doe', 'start_position'=0, 'length'=10), ('date/time'='2020 04 25', 'start_position'=78, 'length'=10) | ********** originally from Brazil has successfully cleared all their loans by ********** Reach them |
| Emily Johnson from the UK cleared all their loans by 2021 01 15 They can be contacted at 555 555 555 | ('Name'='Emily Johnson', 'start_position'=0, 'length'=13), ('address'='the UK', 'start_position'=19, 'length'=6), ('date/time'='2021 01 15', 'start_position'=53, 'length'=10), ('contact numbers'='555 555 555', 'start_position'=89, 'length'=11) | ************* from ****** cleared all their loans by ********** They can be contacted at *********** |
| Michael Brown an Australian has a loan due on 2023 07 20 Contact them at 555 555 5558 Their SSN is 6 | ('Name'='Michael Brown', 'start_position'=0, 'length'=13), ('nationality'='Australian', 'start_position'=17, 'length'=10), ('date/time'='2023 07 20', 'start_position'=46, 'length'=10), ('contact numbers'='555 555 5558', 'start_position'=73, 'length'=12) | ************* an ********** has a loan due on ********** Contact them at ************ Their SSN is 6 |
| Alex Smith a Canadian has an outstanding loan due on 2022 05 30 Their contact number is 555 555 5556 | ('Name'='Alex Smith', 'start_position'=0, 'length'=10), ('Nationality'='Canadian', 'start_position'=13, 'length'=8), ('date/time'='2022 05 30', 'start_position'=53, 'length'=10), ('contact numbers'='555 555 5556', 'start_position'=88, 'length'=12) | ********** a ******** has an outstanding loan due on ********** Their contact number is ************ |
obj_gcp.mask_pii(column="employee_data",data=data,accumulate='employee_data',volatile=True)
Output:
| employee_data | PII_Entities | Masked_Phrase |
|---|---|---|
| Linda Taylor a German cleared all their loans by 2020 12 31 They can be reached at 555 555 5559 Thei | ('Name'=', ', 'start_position'=-1, 'length'=2), ('date/time'=', ', 'start_position'=-1, 'length'=2), ('contact numbers'=', ', 'start_position'=-1, 'length'=2) | Linda Taylor a German cleared all their loans by 2020 12 31 They can be reached at 555 555 5559 Thei |
| Parker Doe originally from Brazil has successfully cleared all their loans by 2020 04 25 Reach them | ('Name'='Parker Doe', 'start_position'=0, 'length'=10), ('date/time'='2020 04 25', 'start_position'=78, 'length'=10) | ********** originally from Brazil has successfully cleared all their loans by ********** Reach them |
| Emily Johnson from the UK cleared all their loans by 2021 01 15 They can be contacted at 555 555 555 | ('Name'=', ', 'start_position'=-1, 'length'=2), ('date/time'=', ', 'start_position'=-1, 'length'=2), ('contact numbers'=', ', 'start_position'=-1, 'length'=2) | Emily Johnson from the UK cleared all their loans by 2021 01 15 They can be contacted at 555 555 555 |
| Michael Brown an Australian has a loan due on 2023 07 20 Contact them at 555 555 5558 Their SSN is 6 | ('Name'='Michael Brown', 'start_position'=0, 'length'=13), ('nationality'='Australian', 'start_position'=17, 'length'=10), ('date/time'='2023 07 20', 'start_position'=46, 'length'=10), ('contact numbers'='555 555 5558', 'start_position'=73, 'length'=12) | ************* an ********** has a loan due on ********** Contact them at ************ Their SSN is 6 |
| Alex Smith a Canadian has an outstanding loan due on 2022 05 30 Their contact number is 555 555 5556 | ('Name'=', ', 'start_position'=-1, 'length'=2), ('date/time'=', ', 'start_position'=-1, 'length'=2), ('contact numbers'=', ', 'start_position'=-1, 'length'=2) | Alex Smith a Canadian has an outstanding loan due on 2022 05 30 Their contact number is 555 555 5556 |
Example setup: Import the modules and create a teradataml DataFrame
>>> import os
>>> import teradatagenai
>>> from teradatagenai import TeradataAI, TextAnalyticsAI, load_data
>>> from teradataml import DataFrame
>>> load_data('employee', 'employee_data')
>>> data = DataFrame('employee_data')
>>> df_employeeData = data.select(["employee_id", "employee_name", "employee_data"])
Complete the example setup requirements.
Example 1: Recognize PII entities in the 'employee_data' column of a teradataml DataFrame using hugging face model 'lakshyakh93/deberta_finetuned_pii'
The text containing potential PII like names, addresses, credit card numbers, etc., is passed as a column name along with the teradataml DataFrame. Setting the 'internal_mask' as True indicates masking to be done by the inbuilt function.
Create an LLM endpoint.
>>> model_name = 'lakshyakh93/deberta_finetuned_pii'
>>> model_args = {'transformer_class': 'AutoModelForTokenClassification',
'task' : 'token-classification'}
>>> llm = TeradataAI(api_type = "hugging_face",
model_name = model_name,
model_args = model_args)
Create a TextAnalyticsAI object.
>>> obj = TextAnalyticsAI(llm = llm)
>>> obj.mask_pii(column="employee_data",
data=df_employeeData,
delimiter="#",
internal_mask=True)
Output:
| text | Masked_Phrase |
|---|---|
| Parker Doe originally from Brazil has successfully cleared all their loans by 2020 04 25 Reach them | Parker Doe originally from Brazil has successfully cleared all their loans by 2020 04 25 Reach them |
| Emily Johnson from the UK cleared all their loans by 2021 01 15 They can be contacted at 555 555 555 | Emily Johnson from the UK cleared all their loans by 2021 01 15 They can be contacted at 555 555 555 |
| Alex Smith a Canadian has an outstanding loan due on 2022 05 30 Their contact number is 555 555 5556 | Alex Smith a Canadian has an outstanding loan due on 2022 05 30 Their contact number is 555 555 5556 |
| Linda Taylor a German cleared all their loans by 2020 12 31 They can be reached at 555 555 5559 Thei | Linda Taylor a German cleared all their loans by 2020 12 31 They can be reached at 555 555 5559 Thei |
| Michael Brown an Australian has a loan due on 2023 07 20 Contact them at 555 555 5558 Their SSN is 6 | Michael Brown an Australian has *** loan due on 2023 07 20 Contact them at 555 555 5558 Their SSN is *** |
Example 2: Extend Example 1 to use user defined script for masking
>>> base_dir = os.path.dirname(teradatagenai.__file__)
>>> mask_pii_script = os.path.join(base_dir, 'example-data',
'mask_pii.py')
>>> obj.mask_pii(column = "employee_data",
data = df_employeeData,
script = mask_pii_script,
delimiter = "#")
Output:
| text | Masked_Phrase |
|---|---|
| Michael Brown an Australian has a loan due on 2023 07 20 Contact them at 555 555 5558 Their SSN is 6 | Michael Brown an Australian has a loan due on*** Contact them at*** Their SSN is*** |
| Emily Johnson from the UK cleared all their loans by 2021 01 15 They can be contacted at 555 555 555 | Emily Johnson from the UK cleared all their loans by*** They can be contacted at*** |
| Parker Doe originally from Brazil has successfully cleared all their loans by 2020 04 25 Reach them | Parker Doe from*** has successfully cleared all their loans by*** Reach them |
| Alex Smith a Canadian has an outstanding loan due on 2022 05 30 Their contact number is 555 555 5556 | Alex*** a Canadian has an outstanding loan due on*** Their contact number is*** |
| Linda Taylor a German cleared all their loans by 2020 12 31 They can be reached at 555 555 5559 Thei | Linda Taylor a*** cleared all their loans by*** They can be reached at*** Thei |
obj_nim.mask_pii(column="employee_data",data=data,accumulate='employee_data',volatile=True)
Output:
| employee_data | PII_Entities | Masked_Phrase |
|---|---|---|
| Linda Taylor a German cleared all their loans by 2020 12 31 They can be reached at 555 555 5559 Thei | ('Name'='Linda Taylor', 'start_position'=0, 'length'=12), ('Nationality'='German', 'start_position'=15, 'length'=6), ('date/time'='2020 12 31', 'start_position'=49, 'length'=10), ('Contact numbers'='555 555 5559', 'start_position'=83, 'length'=12), ('Serial numbers'='Thei', 'start_position'=96, 'length'=4) | ************ a ****** cleared all their loans by ********** They can be reached at ************ **** |
| Parker Doe originally from Brazil has successfully cleared all their loans by 2020 04 25 Reach them | ('Name'='Parker Doe', 'start_position'=0, 'length'=10), ('Country_of_Origin'='Brazil', 'start_position'=27, 'length'=6), ('date/time'='2020 04 25', 'start_position'=78, 'length'=10) | ********** originally from Brazil has successfully cleared all their loans by ********** Reach them |
| Emily Johnson from the UK cleared all their loans by 2021 01 15 They can be contacted at 555 555 555 | ('Name'='Emily Johnson', 'start_position'=0, 'length'=13), ('Country'='UK', 'start_position'=23, 'length'=2), ('date/time'='2021 01 15', 'start_position'=53, 'length'=10), ('contact numbers'='555 555 555', 'start_position'=89, 'length'=11) | ************* from the ** cleared all their loans by ********** They can be contacted at *********** |
| Michael Brown an Australian has a loan due on 2023 07 20 Contact them at 555 555 5558 Their SSN is 6 | ('Name'='Michael Brown', 'start_position'=0, 'length'=13), ('Nationality'='Australian', 'start_position'=17, 'length'=10), ('date/time'='2023 07 20', 'start_position'=46, 'length'=10), ('Contact Number'='555 555 5558', 'start_position'=73, 'length'=12), ('SSN'='6', 'start_position'=99, 'length'=1) | ************* an ********** has a loan due on ********** Contact them at ************ Their SSN is 6 |
| Alex Smith a Canadian has an outstanding loan due on 2022 05 30 Their contact number is 555 555 5556 | ('Name'='Alex Smith', 'start_position'=0, 'length'=10), ('Nationality'='Canadian', 'start_position'=13, 'length'=8), ('date/time'='2022 05 30', 'start_position'=53, 'length'=10), ('Contact numbers'='555 555 5556', 'start_position'=88, 'length'=12) | ********** a ******** has an outstanding loan due on ********** Their contact number is ************ |