---
title: "Sentiment Analysis on Encrypted Data with Homomorphic Encryption"
description: "Sentiment classification can run on encrypted text by computing a transformer (RoBERTa) hidden representation on the client, then running a quantized XGBoost classifier under fully homomorphic encryption with Concrete-ML."
url: "https://swe.quest/articles/sentiment-analysis-on-encrypted-data-with-homomorphic-encryption-62bd5104a9f4964a"
canonical: "https://huggingface.co/blog/sentiment-analysis-fhe"
source: "Hugging Face"
published: "2022-11-17"
difficulty: "medium"
kind: "How-to"
reading_minutes: 8
tags: ["privacy","cryptography","machine-learning","inference","python"]
---

# Sentiment Analysis on Encrypted Data with Homomorphic Encryption

A post on [Hugging Face](https://swe.quest/sources/hugging-face.md), published 2022-11-17, 8 min read, How-to. Original: [https://huggingface.co/blog/sentiment-analysis-fhe](https://huggingface.co/blog/sentiment-analysis-fhe)

## Summary

Sentiment classification can run on encrypted text by computing a transformer (RoBERTa) hidden representation on the client, then running a quantized XGBoost classifier under fully homomorphic encryption with Concrete-ML. On the Twitter airline sentiment dataset the transformer-embedding + XGBoost model reaches 85% accuracy (above the transformer alone at 80%), and FHE predictions match clear-text probabilities exactly. Compilation takes about 9 seconds and FHE inference about 4.4 seconds per example on a 16-core CPU. Deployment uses Concrete-ML's FHEModelDev API to export client/server artifacts, demonstrated in a Hugging Face Space with a Gradio client and a FastAPI server.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Privacy](https://swe.quest/tags/privacy.md) (Security)
- [Cryptography](https://swe.quest/tags/cryptography.md) (Security)
- [Machine learning](https://swe.quest/tags/machine-learning.md) (AI & ML)
- [Inference & serving](https://swe.quest/tags/inference.md) (AI & ML)
- [Python](https://swe.quest/tags/python.md) (Languages)

## Prerequisites

- Python ML basics ([easy posts on Python](https://swe.quest/tags/python.md?difficulty=easy))
- scikit-learn
- transformer embeddings ([easy posts on RAG & retrieval](https://swe.quest/tags/rag.md?difficulty=easy))

## Keywords

Concrete-ML, fully homomorphic encryption, XGBoost, RoBERTa, Hugging Face Transformers, Hugging Face Spaces, Zama

## Related posts

- [Running Privacy-Preserving Inferences on Hugging Face Endpoints](https://swe.quest/articles/running-privacy-preserving-inferences-on-hugging-face-endpoints-de0383d315f32496.md): Hugging Face · 2024-04-16 · Medium · 8 min read · Privacy, Inference & serving, Cryptography, Machine learning, Python
  Zama's Concrete ML models, compiled for Fully Homomorphic Encryption (FHE), can be deployed as Hugging Face Inference Endpoints using custom inference handlers.
- [Towards Encrypted Large Language Models with FHE](https://swe.quest/articles/towards-encrypted-large-language-models-with-fhe-6e22ac5193234b25.md): Hugging Face · 2023-08-02 · Hard · 7 min read · Cryptography, Privacy, Inference & serving, LLMs
  A single attention head of GPT2's first multi-head attention layer can run on encrypted data using TFHE via Zama's Concrete-Python and Concrete-ML, with the rest of the model executing in plaintext on the client.
- [Data-Centric Machine Learning: Building Shopify Inbox’s Message Classification Model](https://swe.quest/articles/data-centric-machine-learning-building-shopify-inbox-s-message-classification-89547c88c3590e73.md): Shopify Engineering · 2022-07-21 · Medium · 12 min read · Machine learning, Model training, Privacy, Communications
  Shopify Inbox's message classifier improved from ~70% to ~90% accuracy, and from ~35% to ~80% high-confidence coverage, by investing in better training data rather than model tweaks.
- [Federated Learning using Hugging Face and Flower](https://swe.quest/articles/federated-learning-using-hugging-face-and-flower-51b18eb47f6bd9fe.md): Hugging Face · 2023-03-27 · Medium · 5 min read · Model training, Machine learning, Python, Privacy
  Federated fine-tuning of a pre-trained distilBERT model for IMDB sentiment classification uses Flower to coordinate multiple clients and a server, so raw data never leaves the clients.
- [Getting Started with Sentiment Analysis on Twitter](https://swe.quest/articles/getting-started-with-sentiment-analysis-on-twitter-905f15e14131b6f1.md): Hugging Face · 2022-07-07 · Medium · 15 min read · Machine learning, Python, Inference & serving
  Twitter sentiment analysis can be done with an off-the-shelf model instead of manual labeling, and two routes are shown: code and no-code.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
