Реализация AI-автоматической разметки данных
Auto-labeling — это использование существующих моделей для автоматической разметки новых данных. Цель: сократить объём ручной работы на 60-80%, сохранив качество датасета выше порога, необходимого для обучения. Ключевой вопрос — какие примеры принять автоматически, а какие отправить на ручную проверку.
Стратегии авторазметки по типу задачи
from anthropic import Anthropic
import numpy as np
import pandas as pd
from dataclasses import dataclass
from typing import Optional
@dataclass
class AutoLabelResult:
text: str
predicted_label: str
confidence: float
auto_accepted: bool
method: str # 'weak_model', 'llm', 'rules', 'ensemble'
class AutoLabelingPipeline:
def __init__(self, task_type: str, confidence_threshold: float = 0.85):
self.task_type = task_type
self.threshold = confidence_threshold
self.llm = Anthropic()
self.stats = {'auto_accepted': 0, 'sent_to_review': 0}
def label_batch(self, texts: list[str],
label_schema: list[str],
method: str = 'ensemble') -> list[AutoLabelResult]:
"""Авторазметка батча текстов"""
if method == 'llm':
return self._llm_labeling(texts, label_schema)
elif method == 'weak_model':
return self._weak_model_labeling(texts, label_schema)
elif method == 'ensemble':
return self._ensemble_labeling(texts, label_schema)
else:
raise ValueError(f"Unknown method: {method}")
def _llm_labeling(self, texts: list[str],
label_schema: list[str]) -> list[AutoLabelResult]:
"""LLM-разметка с оценкой уверенности"""
results = []
batch_size = 10
for i in range(0, len(texts), batch_size):
batch = texts[i:i + batch_size]
texts_formatted = "\n".join([f"{j+1}. {t[:300]}" for j, t in enumerate(batch)])
labels_str = ", ".join(label_schema)
response = self.llm.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=400,
messages=[{
"role": "user",
"content": f"""Classify each text. Labels: {labels_str}
Texts:
{texts_formatted}
Return JSON array: [{{"label": "...", "confidence": 0.0-1.0}}]
confidence = how certain you are (0.9+ for obvious cases, 0.5-0.7 for ambiguous)."""
}]
)
try:
import json
preds = json.loads(response.content[0].text)
for text, pred in zip(batch, preds):
confidence = pred.get('confidence', 0.5)
results.append(AutoLabelResult(
text=text,
predicted_label=pred['label'],
confidence=confidence,
auto_accepted=confidence >= self.threshold,
method='llm'
))
except Exception:
# Fallback: отправить на ручную разметку
for text in batch:
results.append(AutoLabelResult(
text=text,
predicted_label='unknown',
confidence=0.0,
auto_accepted=False,
method='llm_failed'
))
return results
def _weak_model_labeling(self, texts: list[str],
label_schema: list[str]) -> list[AutoLabelResult]:
"""Быстрая разметка через zero-shot модель"""
from transformers import pipeline
classifier = pipeline(
"zero-shot-classification",
model="facebook/bart-large-mnli",
device=0
)
results = []
predictions = classifier(texts, candidate_labels=label_schema, batch_size=32)
for text, pred in zip(texts, predictions):
confidence = pred['scores'][0]
# Штраф за близкие scores (неопределённость между лейблами)
if len(pred['scores']) > 1 and pred['scores'][1] > 0.3:
confidence *= 0.9
results.append(AutoLabelResult(
text=text,
predicted_label=pred['labels'][0],
confidence=confidence,
auto_accepted=confidence >= self.threshold,
method='weak_model'
))
return results
def _ensemble_labeling(self, texts: list[str],
label_schema: list[str]) -> list[AutoLabelResult]:
"""Комбинация: быстрая модель + LLM для неопределённых случаев"""
# Шаг 1: Быстрая разметка
weak_results = self._weak_model_labeling(texts, label_schema)
# Шаг 2: LLM для неопределённых
uncertain_indices = [
i for i, r in enumerate(weak_results)
if not r.auto_accepted and r.confidence > 0.5 # Не совсем провал
]
uncertain_texts = [texts[i] for i in uncertain_indices]
if uncertain_texts:
llm_results = self._llm_labeling(uncertain_texts, label_schema)
for idx, llm_result in zip(uncertain_indices, llm_results):
# Если модели согласны — повышаем уверенность
if llm_result.predicted_label == weak_results[idx].predicted_label:
combined_confidence = (weak_results[idx].confidence + llm_result.confidence) / 2 + 0.1
weak_results[idx].confidence = min(combined_confidence, 1.0)
weak_results[idx].auto_accepted = combined_confidence >= self.threshold
weak_results[idx].method = 'ensemble_agree'
else:
# Разногласие — отправить человеку
weak_results[idx].auto_accepted = False
weak_results[idx].method = 'ensemble_disagree'
return weak_results
Правилa-based авторазметка (Programmatic Labeling)
from snorkel.labeling import labeling_function, PandasLFApplier
from snorkel.labeling.model import LabelModel
import re
# Константы меток
NEGATIVE, ABSTAIN, POSITIVE = -1, -2, 0
@labeling_function()
def lf_contains_positive_words(x):
positive_words = ['excellent', 'great', 'amazing', 'love', 'perfect', 'отлично', 'супер', 'замечательно']
return POSITIVE if any(w in x.text.lower() for w in positive_words) else ABSTAIN
@labeling_function()
def lf_contains_negative_words(x):
negative_words = ['terrible', 'awful', 'worst', 'hate', 'horrible', 'ужасно', 'плохо', 'отстой']
return NEGATIVE if any(w in x.text.lower() for w in negative_words) else ABSTAIN
@labeling_function()
def lf_rating_pattern(x):
match = re.search(r'(\d)[/из]\s*5', x.text)
if match:
rating = int(match.group(1))
if rating >= 4:
return POSITIVE
elif rating <= 2:
return NEGATIVE
return ABSTAIN
@labeling_function()
def lf_exclamation_positive(x):
if x.text.count('!') >= 2 and len(x.text) < 100:
return POSITIVE
return ABSTAIN
def train_label_model(df: pd.DataFrame) -> pd.Series:
"""Snorkel: объединение слабых labeling functions"""
lfs = [lf_contains_positive_words, lf_contains_negative_words,
lf_rating_pattern, lf_exclamation_positive]
applier = PandasLFApplier(lfs=lfs)
L_train = applier.apply(df=df)
# Обучение generative model
label_model = LabelModel(cardinality=2, verbose=True)
label_model.fit(L_train=L_train, n_epochs=500, lr=0.001)
return label_model.predict(L=L_train)
Мониторинг качества авторазметки
class AutoLabelQualityMonitor:
"""Контроль качества через золотые примеры"""
def __init__(self, gold_samples: list[dict]):
"""gold_samples: [{text, true_label}]"""
self.gold = gold_samples
def evaluate_accuracy(self, pipeline: AutoLabelingPipeline) -> dict:
"""Точность авторазметки на золотых примерах"""
texts = [g['text'] for g in self.gold]
true_labels = [g['true_label'] for g in self.gold]
label_schema = list(set(true_labels))
results = pipeline.label_batch(texts, label_schema, method='ensemble')
correct = sum(
1 for r, true in zip(results, true_labels)
if r.predicted_label == true
)
auto_accepted_correct = sum(
1 for r, true in zip(results, true_labels)
if r.auto_accepted and r.predicted_label == true
)
auto_accepted_total = sum(1 for r in results if r.auto_accepted)
return {
'overall_accuracy': correct / len(results),
'auto_accepted_accuracy': (
auto_accepted_correct / auto_accepted_total
if auto_accepted_total > 0 else 0
),
'auto_acceptance_rate': auto_accepted_total / len(results),
'review_queue_size': len(results) - auto_accepted_total
}
Экономия ресурсов при разных порогах
| Порог confidence | Auto-accept rate | Точность автопринятых | Ручная работа |
|---|---|---|---|
| 0.95 | 35% | 98.5% | 65% задач |
| 0.90 | 52% | 97.2% | 48% задач |
| 0.85 | 68% | 95.8% | 32% задач |
| 0.80 | 78% | 93.1% | 22% задач |
| 0.70 | 89% | 88.4% | 11% задач |
Оптимальный порог для большинства задач классификации — 0.85-0.90: сокращение ручной работы на 65-70% при точности автопринятых примеров 95-97%. Финальный датасет требует случайной выборочной проверки 3-5% авторазмеченных примеров для валидации качества.







