自己ホスティング Discourse AI センチメント:GPU と CPU のオプション

# 自主ホスティング Discourse AI 感情分析:GPU と CPU の選択肢

Discourse AI の投稿のトーンと感情分析を、self-hosted HuggingFace Text Embeddings Inference (TEI) を通じて設定します。

これは何ですか?

Discourse AI の Sentiment は チャット/コンプリーション LLM ではありません。内部では、2つの小さな 分類用 RoBERTa モデル(それぞれ約 125M パラメータ)が HuggingFace TEI を通じて実行されます。モデル名は Discourse のダッシュボードレポートの SQL クエリで ハードコード されており、変更できません。

出典: Self-Hosting Sentiment and Emotion for DiscourseAI(Falco、Discourse チーム)

モデル model_name(コード内での正確な名前) 用途
Sentiment cardiffnlp/twitter-roberta-base-sentiment-latest positive / negative / neutral
Emotion SamLowe/roberta-base-go_emotions 28種類の感情(joy, anger, surprise…)

APIフォーマット: POST {\"inputs\": \"text\", \"truncate\": true} → 配列 [{\"label\": \"...\", \"score\": 0.95}, ...]

特徴:cardiffnlp モデルには tokenizer.json が存在しない

TEI は tokenizer.json を必要としますが、cardiffnlp/twitter-roberta-base-sentiment-latest にはそのファイルがありません(古い形式: vocab.json + merges.txt)。解決策: モデルファイルをローカルにダウンロードし、SamLowe/roberta-base-go_emotionstokenizer.json をコピーします(同じ RoBERTa-base テキストエンコーダーを使用)。

事前準備(一度だけ)

sudo mkdir -p /opt/tei-sentiment-cache/model
cd /opt/tei-sentiment-cache/model

for f in config.json vocab.json merges.txt special_tokens_map.json pytorch_model.bin; do
  sudo curl -sL -o "$f"     "https://huggingface.co/cardiffnlp/twitter-roberta-base-sentiment-latest/resolve/main/$f"
done

sudo curl -sL -o tokenizer.json   "https://huggingface.co/SamLowe/roberta-base-go_emotions/resolve/main/tokenizer.json"
sudo curl -sL -o tokenizer_config.json   "https://huggingface.co/SamLowe/roberta-base-go_emotions/resolve/main/tokenizer_config.json"

オプション A: GPU

イメージ: ghcr.io/huggingface/text-embeddings-inference:cuda-1.9.3

標準のタグ :latest(および :1.9)は compute cap 80(Ampere)向けにコンパイルされており、Blackwell(RTX 50x0、compute cap 120)では動作しません。必ず cuda-1.9.3 を使用してください。

docker pull ghcr.io/huggingface/text-embeddings-inference:cuda-1.9.3
sudo mkdir -p /opt/tei-emotion-cache

docker run -d --name tei-sentiment   --gpus all --shm-size 1g   -p 8081:80   -v /opt/tei-sentiment-cache/model:/data/model   --restart unless-stopped   ghcr.io/huggingface/text-embeddings-inference:cuda-1.9.3   --model-id /data/model

docker run -d --name tei-emotion   --gpus all --shm-size 1g   -p 8082:80   -v /opt/tei-emotion-cache:/data   --restart unless-stopped   ghcr.io/huggingface/text-embeddings-inference:cuda-1.9.3   --model-id SamLowe/roberta-base-go_emotions

Blackwell での最初の起動では、CUDA コアの JIT コンパイルがコンテナ内で約 5 分かかります。これは一度だけです。

GPU(RTX 5060 Ti)の性能

メトリック
Sentiment 推論 約 14ms
Emotion 推論 約 60ms
コンテナの VRAM 約 428 MB
両方の VRAM 約 856 MB

オプション B: CPU(バックアップ)

イメージ: ghcr.io/huggingface/text-embeddings-inference:cpu-1.9

GPU が利用できない場合や VRAM が不足している場合に使用。NVIDIA ドライバ不要。

docker pull ghcr.io/huggingface/text-embeddings-inference:cpu-1.9
sudo mkdir -p /opt/tei-emotion-cache

docker run -d --name tei-sentiment   --shm-size 1g   -p 8081:80   -v /opt/tei-sentiment-cache/model:/data/model   --restart unless-stopped   ghcr.io/huggingface/text-embeddings-inference:cpu-1.9   --model-id /data/model --dtype float32

docker run -d --name tei-emotion   --shm-size 1g   -p 8082:80   -v /opt/tei-emotion-cache:/data   --restart unless-stopped   ghcr.io/huggingface/text-embeddings-inference:cpu-1.9   --model-id SamLowe/roberta-base-go_emotions --dtype float32
```高LA(Load Average)の場合はCPUを制限できます:

```bash
docker update --cpus=0.1 tei-sentiment tei-emotion

CPU性能

メトリック
Sentiment推論 約270ms
Emotion推論 約205ms
コンテナのRAM使用量 約500MB
–cpus=0.1の場合 1投稿あたり約2〜3秒

GPU ↔ CPUの切り替え

docker stop tei-sentiment tei-emotion
docker rm tei-sentiment tei-emotion

次に、必要な設定でコンテナを再起動します。Discourseの設定は変更不要です(エンドポイントは同じです)。

チェック

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

curl -s http://localhost:8082/   -X POST -H 'Content-Type: application/json'   -d '{"inputs": "I am happy"}'

期待されるSentimentの応答:[{"label":"positive","score":0.96},...]

Discourseの設定

/admin/plugins/discourse-ai/settings?filter=sentiment で:

  1. discourse_ai_enabled = true
  2. ai_sentiment_enabled = true
  3. ai_sentiment_model_configs - 2つのオブジェクト:
フィールド モデル1 モデル2
model_name cardiffnlp/twitter-roberta-base-sentiment-latest SamLowe/roberta-base-go_emotions
endpoint http://<your-host>:8081 http://<your-host>:8082
api_key (空) (空)

ダッシュボード

  • /admin/reports/overall_sentiment - 一般的な感情(positive - negative)
  • /admin/reports/emotion_joy(その他の27種類の感情も同様)
  • バックフィル:約2500投稿/時間、60日以内の投稿のみ

条件とリスク

  • モデルは英語で学習されています。ロシア語のテキストについては、結果は近似値ですが、基本的なSentimentは機能します。
  • APIエンドポイントはAPIキーなしで公開されています。商用利用の場合は、リバースプロキシで制御してください。
  • VRAMのモニタリング:nvidia-smi --query-compute-apps=pid,name,used_memory --format=csv,noheader