Система модерации контента на основе ИИ: Python + LLM + OpenCV для корпоративной безопасности контента

技术架构

Почему модерация контента — главное применение ИИ

ByteDance обрабатывает миллиарды единиц контента ежедневно — каждая единица должна быть проверена за миллисекунды. Это самый жёсткий производственный сценарий ИИ — не «общение», а «оценивание».

Модерация контента — это не опция, а красная линия соответствия. Один пропущенный отзыв может привести к удалению приложения.


Трёхуровневая архитектура модерации

┌──────────────────────────────────────────────────┐
│              Content Moderation Architecture       │
├──────────────────────────────────────────────────┤
│  Layer 1: Text Moderation                         │
│  ├── Sensitive word detection (AC automaton)      │
│  ├── Semantic understanding (LLM classification)  │
│  └── Sentiment analysis                           │
├──────────────────────────────────────────────────┤
│  Layer 2: Image Moderation                        │
│  ├── OCR text extraction → Text moderation        │
│  ├── Object detection (violence/porn/politics)    │
│  └── Face detection (public figures/minors)       │
├──────────────────────────────────────────────────┤
│  Layer 3: Video Moderation                        │
│  ├── Keyframe extraction → Image moderation       │
│  ├── Audio to text → Text moderation              │
│  └── Behavior recognition                         │
└──────────────────────────────────────────────────┘

Модерация текста

Обнаружение чувствительных слов (AC-автомат)

from pyahocorasick import Automaton

class SensitiveWordDetector:
    def __init__(self):
        self.automaton = Automaton()
        self._load_words()

    def _load_words(self):
        with open("sensitive_words.txt", "r") as f:
            for idx, word in enumerate(f):
                self.automaton.add_word(word.strip(), (idx, word.strip()))
        self.automaton.make_automaton()

    def detect(self, text: str) -> list:
        results = []
        for end_idx, (word_idx, word) in self.automaton.iter(text):
            start_idx = end_idx - len(word) + 1
            results.append({"word": word, "start": start_idx, "end": end_idx + 1})
        return results

Семантическая модерация LLM

from openai import OpenAI

class SemanticModerator:
    def __init__(self):
        self.client = OpenAI()
        self.system_prompt = """You are a content moderation expert. Determine if the content violates rules.
Output JSON: {"is_violation": true/false, "category": "...", "confidence": 0.0-1.0, "reason": "..."}"""

    def moderate(self, text: str) -> dict:
        response = self.client.chat.completions.create(
            model="gpt-4o-mini",
            messages=[
                {"role": "system", "content": self.system_prompt},
                {"role": "user", "content": text}
            ],
            response_format={"type": "json_object"},
            temperature=0
        )
        return json.loads(response.choices[0].message.content)

Модерация изображений

import easyocr

class ImageModerator:
    def __init__(self):
        self.ocr_reader = easyocr.Reader(['ch_sim', 'en'])
        self.text_moderator = SemanticModerator()

    def moderate(self, image_path: str) -> dict:
        ocr_results = self.ocr_reader.readtext(image_path)
        text = " ".join([result[1] for result in ocr_results])

        if text.strip():
            text_result = self.text_moderator.moderate(text)
        else:
            text_result = {"is_violation": False}

        image_result = self._detect_objects(image_path)

        return {
            "ocr_text": text,
            "text_moderation": text_result,
            "image_moderation": image_result,
            "is_violation": text_result["is_violation"] or image_result["is_violation"]
        }

Многоуровневый конвейер модерации

class ModerationPipeline:
    def __init__(self):
        self.sensitive_detector = SensitiveWordDetector()
        self.semantic_moderator = SemanticModerator()
        self.image_moderator = ImageModerator()

    def moderate(self, content: dict) -> dict:
        # Level 1: Fast rule filtering (<10ms)
        sensitive_hits = self.sensitive_detector.detect(content.get("text", ""))
        if sensitive_hits:
            return {"is_violation": True, "level": "high", "details": sensitive_hits}

        # Level 2: AI semantic moderation (<500ms)
        results = {}
        if content["type"] == "text":
            results["semantic"] = self.semantic_moderator.moderate(content["text"])
        elif content["type"] == "image":
            results["image"] = self.image_moderator.moderate(content["url"])

        # Level 3: Human review (low confidence triggers)
        for key, result in results.items():
            if result.get("confidence", 1.0) < 0.85:
                results[key]["need_human_review"] = True

        is_violation = any(r.get("is_violation", False) for r in results.values())
        return {"is_violation": is_violation, "results": results}

Заключение

Основной дизайн корпоративной системы модерации контента:

  1. Многоуровневая фильтрация: Правила (быстро) → ИИ (точно) → Ручная проверка (страховка)
  2. Мультимодальное покрытие: Текст + Изображения + Видео + Аудио
  3. Экосистема Python: OpenCV, easyocr, whisper готовы к использованию
  4. Усиление LLM: От «совпадения ключевых слов» к «семантическому пониманию»

Модерация контента — самый жёсткий производственный сценарий ИИ — не «может общаться», а «может оценивать».

Попробуйте эти локальные браузерные инструменты — регистрация не требуется →

#AI审核#Python#大模型#OpenCV#内容安全