# Self-Hosting Sentiment and Emotion for DiscourseAI

**URL:** https://meta.discourse.org/t/self-hosting-sentiment-and-emotion-for-discourseai/334451
**Category:** Self-Hosting
**Tags:** ai, ai-sentiment
**Created:** [November 4, 2024, 5:56pm UTC](https://meta.discourse.org/t/self-hosting-sentiment-and-emotion-for-discourseai/334451 "2024-11-04T17:56:42Z")
**Posts on this page:** 1
**Showing post:** 1

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [November 4, 2024, 5:56pm UTC](https://meta.discourse.org/t/self-hosting-sentiment-and-emotion-for-discourseai/334451/1 "2024-11-04T17:56:42Z")

</div>

The [Discourse AI](https://meta.discourse.org/t/discourse-ai/259214) plugin has support for requesting emotion/sentiment classification of new posts, which is stored in the database and can be used in reports and admin dashboards.

Discourse AI supports two types of classification, each requiring its own model:

- **Sentiment** — classifies posts as positive, negative, or neutral (using `cardiffnlp/twitter-roberta-base-sentiment-latest`)
- **Emotion** — classifies posts across 28 emotion labels like joy, anger, surprise, etc. (using `SamLowe/roberta-base-go_emotions`)

To get both sentiment and emotion data in your dashboards, you need to run **both** models.

### Running with HuggingFace TEI

HuggingFace provides an awesome container image that can get you running quickly.

#### Sentiment model

```plaintext
mkdir -p /opt/tei-sentiment-cache
docker run --rm --gpus all --shm-size 1g -p 8081:80 \
  -v /opt/tei-sentiment-cache:/data \
  ghcr.io/huggingface/text-embeddings-inference:latest \
  --model-id cardiffnlp/twitter-roberta-base-sentiment-latest \
  --revision refs/pr/30

```

This should get you up and running with a local instance of `cardiffnlp/twitter-roberta-base-sentiment-latest`, an open model that can classify posts into positive/negative/neutral.

You can check if it’s working with

```bash
curl http://localhost:8081/ \
    -X POST \
    -H 'Content-Type: application/json' \
    -d "{ \"inputs\": \"I am happy\" }"

```

Which should return an array of confidence for each label under normal operation.

#### Emotion model

To also get emotion classification, run a second container with the emotion model:

```plaintext
mkdir -p /opt/tei-emotion-cache
docker run --rm --gpus all --shm-size 1g -p 8082:80 \
  -v /opt/tei-emotion-cache:/data \
  ghcr.io/huggingface/text-embeddings-inference:latest \
  --model-id SamLowe/roberta-base-go_emotions

```

### Supported models

- [cardiffnlp/twitter-roberta-base-sentiment-latest · Hugging Face](https://huggingface.co/cardiffnlp/twitter-roberta-base-sentiment-latest/) — sentiment (positive/negative/neutral)
- [SamLowe/roberta-base-go\_emotions · Hugging Face](https://huggingface.co/SamLowe/roberta-base-go_emotions) — emotion (28 emotion labels)

### Making it available for your Discourse instance

Most of the time, you will be running this on a dedicated server because of the GPU speed-up. When doing so, I recommend running a reverse proxy, doing TLS termination, and securing the endpoint so it can only be connected by your Discourse instance.

### Configuring Discourse AI

Discourse AI includes site settings to configure the inference server for open-source models. You should point it to your server using the **ai\_sentiment\_model\_configs** setting.

This setting accepts a JSON array of model configurations. Each entry requires:

| Field | Description |
| --- | --- |
| `model_name` | The HuggingFace model ID (e.g. `cardiffnlp/twitter-roberta-base-sentiment-latest`) |
| `endpoint` | The URL of your TEI instance (e.g. `https://your-server:8081`) |
| `api_key` | API key for the endpoint (can be left blank if not required) |

To get both sentiment and emotion dashboards, add an entry for **each** model you are running. For example, if you’re running both models locally:

- Entry 1: model\_name `cardiffnlp/twitter-roberta-base-sentiment-latest`, endpoint `https://your-server:8081`
- Entry 2: model\_name `SamLowe/roberta-base-go_emotions`, endpoint `https://your-server:8082`

After that, enable the classification by toggling `ai_sentiment_enabled`.

---

_[View the full topic](https://meta.discourse.org/t/self-hosting-sentiment-and-emotion-for-discourseai/334451)._
