
Hugging Face Blog
· 1 min read
Sentiment Analysis on Encrypted Data with Homomorphic Encryption
It is well-known that a sentiment analysis model determines whether a text is positive, negative, or neutral. However, this process typically requires access to unencrypted text, which can pose privacy concerns.
Homomorphic encryption is a type of encryption that allows for computation on encrypted data without needing to decrypt it first. This makes it well-suited for applications where user's personal and potentially sensitive data is at risk (e.g. sentiment analysis of private messages).
This blog post uses the Concrete-ML library, allowing data scientists to use machine learning models in fully homomorphic encryption (FHE) settings without any prior knowledge of cryptography. We provide a practical tutorial on how to use the library to build a sentiment analysis model on encrypted data.
The post covers:
- transformers
- how to use transformers with XGBoost to perform sentiment analysis
- how to do the training
- how to use Concrete-ML to turn predictions into predictions over encrypted data
- how to deploy to the cloud using a client/server protocol
Last but not least, we’ll finish with a complete demo over Hugging Face Spaces to show this functionality in action.
Setup the environment
First make sure your pip and setuptools are up to date by running:
pip install -U pip setuptools
Now we can install all the required libraries for the this blog with the following command.
pip install concrete-ml transformers datasets
Using a public dataset
The dataset we use in this notebook can be found here.
To represent the text for sentiment analysis, we chose to use a transformer hidden representation as it yields high accuracy for the final model in a very efficient way. For a comparison of this representation set against a more common procedure like the TF-IDF approach, please see this full notebook.
We can start by opening the dataset and visualize some statistics.
The output, then, looks like this:
Text representation using a transformer
Classifying with XGBoost
The output is as follows:
Original source
This story was published by Hugging Face Blog. SyncAI.news shows a preview; the complete article is on the publisher's site.
Read the full story on huggingface.co


