1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
| from transformers import ( pipeline, AutoTokenizer, AutoModelForSequenceClassification ) import torch
class ContentModerator: def __init__(self): self.text_classifier = pipeline( "text-classification", model="martin-ha/toxic-comment-model" ) self.sentiment_analyzer = pipeline( "sentiment-analysis", model="cardiffnlp/twitter-roberta-base-emotion" ) self.topic_classifier = pipeline( "text-classification", model="facebook/bart-large-mnli" ) def moderate_content(self, content, content_type="text"): """内容审核""" toxicity = self.text_classifier(content)[0] emotion = self.sentiment_analyzer(content)[0] topics = ["政治讨论", "商业内容", "技术分享", "娱乐内容", "其他"] topic_result = self.topic_classifier( content, candidate_labels=topics ) score = self._calculate_comprehensive_score( toxicity, emotion, topic_result, content_type ) result = { "content": content, "toxicity": { "label": toxicity["label"], "score": toxicity["score"] }, "emotion": { "label": emotion["label"], "score": emotion["score"] }, "topic": { "primary": topic_result["labels"][0], "confidence": topic_result["scores"][0] }, "comprehensive_score": score, "recommendation": self._get_recommendation(score), "review_required": score > 0.7 } return result def _calculate_comprehensive_score(self, toxicity, emotion, topic, content_type): """计算综合风险评分""" score = 0.0 if toxicity["label"] == "toxic": score += toxicity["score"] * 0.5 if emotion["label"] in ["anger", "disgust", "fear"]: score += emotion["score"] * 0.3 if topic["labels"][0] in ["政治讨论"]: score += topic["scores"][0] * 0.2 if content_type == "comment": score *= 1.2 elif content_type == "post": score *= 1.0 return min(score, 1.0) def _get_recommendation(self, score): """获取处理建议""" if score > 0.8: return "立即删除并封禁用户" elif score > 0.6: return "标记为敏感内容,需要人工审核" elif score > 0.4: return "添加内容警告" else: return "正常发布"
moderator = ContentModerator()
contents = [ ("这个产品简直是垃圾!", "comment"), ("今天天气真好,心情愉快", "post"), ("分享一个技术经验...", "post") ]
for content, content_type in contents: result = moderator.moderate_content(content, content_type) print(f"内容: {content}") print(f"风险评分: {result['comprehensive_score']:.4f}") print(f"建议措施: {result['recommendation']}") print(f"需要审核: {result['review_required']}") print("---")
|