Safeguarding Personal Data in Sentiment Analysis: A Guide to PII Anonymization

Mar 21, 2024
Share this post
Sharing to FacebookSharing to LinkedInSharing to XSharing to Email

Let’s say you need to run sentiment analysis on a PII in dataset of reviews. You will use one of the pre-trained models offered by HuggingFace. There is only one problem: Your reviews contain names, addresses, and other Personal Identifiable Information (PII). What should you do? We can say what you shouldn’t do. Never send PII to a third-party API, even if they promise they won’t store the data. What can you do instead?

In our example, we will use a structured dataset with one column containing PII, but the same approach would also work if you were preparing data for an LLM.

Why shouldn’t you include PII in the dataset?

We don’t want to scare you, but here are some words that may bring you nightmares: GDPR, Law25, CCPA, LGPD, and, of course, the EU AI Act. Life would be too simple if those were all Data Protection Acts you must worry about. India, Australia, New Zealand, and Switzerland have their regulations too. The United Nations has 193 member states, so it’s almost certain there are some data protection regulations we never heard about. All of them are similar but also slightly different. Different just enough to cause legal problems when you are compliant with one but not the other. Suppose you remove PII before using the data to train a model or send the data to a third-party API. In that case, you will limit the areas where you must carefully check whether you comply with all relevant laws.

Also, most likely, the model will give you better results when you don’t include PII in dataset. Such improvement was observed by Sahil Verma, Michael Ernst, and Rene Just from the University of Washington in their research paper: “Removing biased data to improve fairness and accuracy” . In the paper, they noted the significance of removing the most biased data point (e.g., a loan application that was denied based on an indirect identifier like race). They removed the indirect identifier from the training data of the application that exhibited the most bias, ensuring that the model wouldn't be trained on it. However, they also compared their technique with the removal of sensitive identifiers that are considered "prohibited grounds" under human rights legislation. Their findings indicated that both techniques reduce bias to 0 and improve accuracy, although their method demonstrates a more significant improvement in accuracy compared to the removal of indirect discriminatory identifiers.

Should it matter for a sentiment analysis if the review’s author writes about Frieda or Emma? Perhaps having a model that sees names as relevant information and makes mistakes because of that isn’t a concern for you. And you may get accused of discrimination if your model always classifies reviews containing names popular in a minority group as negative.

In this text, we assume you are using a pre-trained third-party model. However, your users don’t care if you train the model yourself or use open-source models. The same rules often apply whether you send requests to HuggingFace API or remove PII from the training dataset while preparing your model.

What should you do?

If you remove  PII in dataset, you won’t have any PII-related problems. Of course, removing PII isn’t as simple as it sounds. You have several options.
The easiest and the worst way would be to remove all rows containing any PII. Of course, it means you lose data that could be useful for training. What if all rows contain PII? Would you give up training the model? What if you are using a pre-trained model in production? Dropping requests in production won’t make anybody happy.

A better way is to replace all PII with a placeholder. Replace people’s names with the words “NAME GIVEN,” addresses with the word “LOCATION,” or phone numbers with the words “PHONE NUMBER.” Of course, nothing stops you from doing the replacement manually.

However, even if you outsource the work to hundreds of people, you still risk mistakes or data leaks. You can implement your data anonymization script based on Named Entity Recognition, but capturing the diversity of language and meticulously addressing corner case after corner case is no small feat. Private AI offers a faster and already tested method of removing PII from your data.

Using Private AI to remove PII before sending requests to HuggingFace API

We will process Starbucks reviews and use the twitter-roberta-base-sentiment-latest model to determine the sentiment of the reviews. We assume you have already installed the transformers library and loaded the data into a Pandas data frame. Right now, the dataset looks like this:

Image description: Four rows of a Pandas Dataframe containing columns name, location, date, rating, review, and image_links. In the review column, one row contains employee names and another has a part of an address visible.

We already see two names of Starbucks employees and an address in the “Review” column.

We must decide which information to remove and what to keep. We don’t want to send people’s data to the HuggingFace API, but what about addresses? We have the “location” column, but a coffee shop address as a value isn’t precise enough on its own to uniquely re-identify an individual. It would be perfect to have the address of a location that gets lots of negative reviews if the review’s author included the address in their comment. Let’s remove people’s data but keep coffee shop addresses.

Before we start, we must install and initialize the Private AI client. For the sake of simplicity, we assume you have already loaded the Private AI API Key into the “api_key” variable and your HuggingFace token into the “hf_token” variable



!pip install privateai_client<br>from privateai_client import PAIClient<br>from privateai_client import request_objects<br>client = PAIClient(url="https://api.private-ai.com/deid/", api_key=api_key)

After initializing the client, we have to configure a request object and send it to Private AI. We will implement a function encapsulating those operations to use later in the Pandas apply function.



def redact_text(text):<br>proc_obj = request_objects.processed_text_obj(<br>type="MARKER",<br>pattern="BEST_ENTITY_TYPE"<br>)<br>entity_type_selector = request_objects.entity_type_selector_obj(<br>type="DISABLE",<br>value=[<br>'ORGANIZATION','LOCATION','LOCATION_ADDRESS',<br>'LOCATION_STATE','LOCATION_CITY','DATE'<br>]<br>)<br>entity_detection = request_objects.entity_detection_obj(<br>entity_types=[entity_type_selector]<br>)<br>text_req = request_objects.process_text_obj(<br>text=[text],<br>processed_text=proc_obj,<br>entity_detection=entity_detection<br>)<br>return client.process_text(text_req).processed_text[0]

The “processed_text_obj” function configures whether we care about individual entities in the text or a general category of personal data.

For example, setting the “pattern” parameter to “BEST_ENTITY_TYPE” gives us generic entity names such as “NAME_GIVEN”. If we set the parameter to UNIQUE_NUMBERED_ENTITY_TYPE, the first row of our example dataset would contain “NAME_GIVEN_1” and “NAME_GIVEN_2” after anonymization because the text includes the names of two people. The other available options are “ALL_ENTITY_TYPES” and “UNIQUE_HASHED_ENTITY_TYPE.” “ALL_ENTITY_TYPES” produces a hierarchy of entity types such as: “[NAME, NAME_GIVEN]”. The “UNIQUE_HASHED_ENTITY_TYPE” uses hashes to distinguish between entities instead of numbers, so the output could look like this: “NAME_GIVEN_q13R2.”

The number of people mentioned in a sentence seems irrelevant for sentiment analysis purposes, so we use the “BEST_ENTITY_TYPE” value.

Earlier, we decided to keep the location data in the reviews. Therefore, we have to configure an entity_type_selector and disable entity types related to locations. Similarly, the date in the review may be relevant information that we may need, so we disable the anonymization of dates. Note that, with external information like security camera footage of the date and location, it could be possible to narrow down the review to a small set of individuals. For this task, we assume the LLM provider does not have access to such external information.

We put the entity selector into an array and pass the selector to the entity_detection_obj to configure the redaction. We can pass multiple configurations at once! Afterward, we build a process_text_obj containing the configuration and the text we want to process.

In the last line, we send the request to the Private AI API and retrieve the redacted text.

When we use the function we have just defined, we get a dataset containing the original review text and the redacted version:



data_frame["redacted_review"] = data_frame["Review"].apply(redact_text)

Image description: The same Pandas Dataframe as before, but with an additional “redacted_review” column. The row containing employee’s names has the names replaced with the “NAME_GIVEN” placeholder.

To use the review with anonymized PII, we implement the “get_sentiment” function. The function accepts the review text as a parameter, sends a request to the HuggingFace Inference API, and returns an array of scores for all supported sentiment labels.



import requests<br>def get_sentiment(text):<br>API_URL = "https://api-inference.huggingface.co/models/cardiffnlp/twitter-roberta-base-sentiment-latest"<br>headers = {"Authorization": f'Bearer {hf_token}'}<br>def query(payload):<br>response = requests.post(API_URL, headers=headers, json=payload)<br>return response.json()<br>output = query({<br>"inputs": text,<br>})<br>return output<br>

Now, we send two requests to the API. The first one contains the PII, and the second one uses the redacted version.



print(get_sentiment(data_frame.iloc[0][4]))<br>print(get_sentiment(data_frame.iloc[0][6]))

As expected, we get similar scores in both cases:



[[{'label': 'positive', 'score': 0.9743512868881226}, {'label': 'neutral', 'score': 0.02006211131811142}, {'label': 'negative', 'score': 0.005586681421846151}]]<br>[[{'label': 'positive', 'score': 0.9751478433609009}, {'label': 'neutral', 'score': 0.019699547439813614}, {'label': 'negative', 'score': 0.005152557976543903}]]

Of course, in your production implementation, remember to use only the redacted column.

If you want to see the example code in a single Jupyter Notebook file, take a look at our Github repository.

By Bartosz Mikulski, Business Process Automation and AI Consultant

Sign up for our Community API

The “get to know us” plan. Our full product, but limited to 75 API calls per day and hosted by us.

Get Started Today

Data Left Behind: AI Scribes’ Promises in Healthcare

Data Left Behind: Healthcare’s Untapped Goldmine

The Future of Health Data: How New Tech is Changing the Game

Why is linguistics essential when dealing with healthcare data?

Why Health Data Strategies Fail Before They Start

Private AI to Redefine Enterprise Data Privacy and Compliance with NVIDIA

EDPB’s Pseudonymization Guideline and the Challenge of Unstructured Data

HHS’ proposed HIPAA Amendment to Strengthen Cybersecurity in Healthcare and how Private AI can Support Compliance

Japan's Health Data Anonymization Act: Enabling Large-Scale Health Research

What the International AI Safety Report 2025 has to say about Privacy Risks from General Purpose AI

Private AI 4.0: Your Data’s Potential, Protected and Unlocked

How Private AI Facilitates GDPR Compliance for AI Models: Insights from the EDPB's Latest Opinion

Navigating the New Frontier of Data Privacy: Protecting Confidential Company Information in the Age of AI

Belgium’s Data Protection Authority on the Interplay of the EU AI Act and the GDPR

Enhancing Compliance with US Privacy Regulations for the Insurance Industry Using Private AI

Navigating Compliance with Quebec’s Act Respecting Health and Social Services Information Through Private AI’s De-identification Technology

Unlocking New Levels of Accuracy in Privacy-Preserving AI with Co-Reference Resolution

Strengthened Data Protection Enforcement on the Horizon in Japan

How Private AI Can Help to Comply with Thailand's PDPA

How Private AI Can Help Financial Institutions Comply with OSFI Guidelines

The American Privacy Rights Act – The Next Generation of Privacy Laws

How Private AI Can Help with Compliance under China’s Personal Information Protection Law (PIPL)

PII Redaction for Reviews Data: Ensuring Privacy Compliance when Using Review APIs

Independent Review Certifies Private AI’s PII Identification Model as Secure and Reliable

To Use or Not to Use AI: A Delicate Balance Between Productivity and Privacy

To Use or Not to Use AI: A Delicate Balance Between Productivity and Privacy

News from NIST: Dioptra, AI Risk Management Framework (AI RMF) Generative AI Profile, and How PII Identification and Redaction can Support Suggested Best Practices

Handling Personal Information by Financial Institutions in Japan – The Strict Requirements of the FSA Guidelines

日本における金融機関の個人情報の取り扱い - 金融庁ガイドラインの要件

Leveraging Private AI to Meet the EDPB’s AI Audit Checklist for GDPR-Compliant AI Systems

Who is Responsible for Protecting PII?

How Private AI can help the Public Sector to Comply with the Strengthening Cyber Security and Building Trust in the Public Sector Act, 2024

A Comparison of the Approaches to Generative AI in Japan and China

Updated OECD AI Principles to keep up with novel and increased risks from general purpose and generative AI

Is Consent Required for Processing Personal Data via LLMs?

The evolving landscape of data privacy legislation in healthcare in Germany

The CIO’s and CISO’s Guide for Proactive Reporting and DLP with Private AI and Elastic

The Evolving Landscape of Health Data Protection Laws in the United States

Comparing Privacy and Safety Concerns Around Llama 2, GPT4, and Gemini

How to Safely Redact PII from Segment Events using Destination Insert Functions and Private AI API

WHO’s AI Ethics and Governance Guidance for Large Multi-Modal Models operating in the Health Sector – Data Protection Considerations

How to Protect Confidential Corporate Information in the ChatGPT Era

Unlocking the Power of Retrieval Augmented Generation with Added Privacy: A Comprehensive Guide

Leveraging ChatGPT and other AI Tools for Legal Services

Leveraging ChatGPT and other AI tools for HR

Leveraging ChatGPT in the Banking Industry

Law 25 and Data Transfers Outside of Quebec

The Colorado and Connecticut Data Privacy Acts

Unlocking Compliance with the Japanese Data Privacy Act (APPI) using Private AI

Tokenization and Its Benefits for Data Protection

Private AI Launches Cloud API to Streamline Data Privacy

Processing of Special Categories of Data in Germany

End-to-end Privacy Management

Privacy Breach Reporting Requirements under Law25

Migrating Your Privacy Workflows from Amazon Comprehend to Private AI

A Comparison of the Approaches to Generative AI in the US and EU

Benefits of AI in Healthcare and Data Sources (Part 1)

Privacy Attacks against Data and AI Models (Part 3)

Risks of Noncompliance and Challenges around Privacy-Preserving Techniques (Part 2)

Enhancing Data Lake Security: A Guide to PII Scanning in S3 buckets

The Costs of a Data Breach in the Healthcare Sector and its Privacy Compliance Implications

Navigating GDPR Compliance in the Life Cycle of LLM-Based Solutions

What’s New in Version 3.8

How to Protect Your Business from Data Leaks: Lessons from Toyota and the Department of Home Affairs

New York's Acceptable Use of AI Policy: A Focus on Privacy Obligations

Safeguarding Personal Data in Sentiment Analysis: A Guide to PII Anonymization

Changes to South Korea’s Personal Information Protection Act to Take Effect on March 15, 2024

Australia’s Plan to Regulate High-Risk AI

How Private AI can help comply with the EU AI Act

Comment la Loi 25 Impacte l'Utilisation de ChatGPT et de l'IA en Général

Endgültiger Entwurf des Gesetzes über Künstliche Intelligenz – Datenschutzpflichten der KI-Modelle mit Allgemeinem Verwendungszweck

How Law25 Impacts the Use of ChatGPT and AI in General

Is Salesforce Law25 Compliant?

Creating De-Identified Embeddings

Exciting Updates in 3.7

EU AI Act Final Draft – Obligations of General-Purpose AI Systems relating to Data Privacy

FTC Privacy Enforcement Actions Against AI Companies

The CCPA, CPRA, and California's Evolving Data Protection Landscape

HIPAA Compliance – Expert Determination Aided by Private AI

Private AI Software As a Service Agreement

EU's Review of Canada's Data Protection Adequacy: Implications for Ongoing Privacy Reform

Acceptable Use Policy

ISO/IEC 42001: A New Standard for Ethical and Responsible AI Management

Reviewing OpenAI's 31st Jan 2024 Privacy and Business Terms Updates

Comparing OpenAI vs. Azure OpenAI Services

Quebec’s Draft Regulation Respecting the Anonymization of Personal Information

Version 3.6 Release: Enhanced Streaming, Auto Model Selection, and More in Our Data Privacy Platform

Brazil's LGPD: Anonymization, Pseudonymization, and Access Requests

LGPD do Brasil: Anonimização, Pseudonimização e Solicitações de Acesso à Informação

Canada’s Principles for Responsible, Trustworthy and Privacy-Protective Generative AI Technologies and How to Comply Using Private AI

Private AI Named One of The Most Innovative RegTech Companies by RegTech100

Data Integrity, Data Security, and the New NIST Cybersecurity Framework

Safeguarding Privacy with Commercial LLMs

Cybersecurity in the Public Sector: Protecting Vital Services

Privacy Impact Assessment (PIA) Requirements under Law25

Elevate Your Experience with Version 3.5

Fine-Tuning LLMs with a Focus on Privacy

GDPR in Germany: Challenges of German Data Privacy (Part 2)

Comply with US Executive Order on Safe, Secure, and Trustworthy Artificial Intelligence using Private AI

How to Comply with EU AI Act using PrivateGPT