자체 호스팅 Discourse AI 감성: GPU 및 CPU 옵션

# 자가 호스팅된 Discourse AI 감정 분석: GPU 및 CPU 옵션

Discourse AI 포스트의 감정 및 톤 분석을 자가 호스팅된 HuggingFace Text Embeddings Inference (TEI)를 통해 설정합니다.

무엇인가요?

Discourse AI의 Sentiment는 chat/completion LLM이 아닙니다. 내부에는 각각 약 125M 파라미터를 가진 두 개의 작은 클래식 분류 RoBERTa 모델이 있으며, 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_emotions 모델의 tokenizer.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 시나리오에서 CPU 사용량을 제한할 수 있습니다:

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

CPU 성능

지표
Sentiment 추론 ~270ms
Emotion 추론 ~205ms
컨테이너 RAM 사용량 ~500 MB
–cpus=0.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 - 두 개의 객체:
필드 모델 1 모델 2
model_name cardiffnlp/twitter-roberta-base-sentiment-latest SamLowe/roberta-base-go_emotions
endpoint http://\u003cyour-host\u003e:8081 http://\u003cyour-host\u003e:8082
api_key (빈 값) (빈 값)

대시보드

  • /admin/reports/overall_sentiment - 전체 감정 (긍정 - 부정)
  • /admin/reports/emotion_joy (다른 27가지 감정도 가능)
  • 백필: 약 2500 포스트/시간, 최대 60일 이내의 포스트만 처리

조건 및 위험 요소

  • 모델은 영어 데이터로 학습됨. 러시아어 텍스트에 대해서는 근사한 결과를 제공하지만, 기본 감정 분석은 작동 가능
  • 엔드포인트는 API 키 없이도 접근 가능 — 상업용 환경에서는 reverse proxy를 통해 접근을 제한해야 함
  • VRAM 모니터링: nvidia-smi --query-compute-apps=pid,name,used_memory --format=csv,noheader