Views
No views yet
喜极而泣 [Joy] -> [Sadness] -> [Relief] 或 先抑后扬 [Annoyance] -> [Surprise] -> [Admiration]),引擎会在海量小说库中找到完美符合该情绪走向的章节。liudev/roberta-multilabel-28-3-classes) 是一个基于 RoBERTa 架构的多标签文本分类模型。专门用于小说、对话或长文本段落的情感和基调分析。text_a: 历史上下文(如当前段落的前 3 段)text_b: 当前需要预测的段落文本
这种设计使得模型能够结合前文语境,做出更准确的判断。0.5 阈值往往不是最优的。为了在生产环境中确保**“宁愿漏报,也不误报”(高查准率,Precision >= 80% 为目标)**,我们对每个标签进行了严格的阈值调优(正如我们的官方引擎中所使用的那样)。1PRODUCTION_THRESHOLDS = {
2 "anger": 0.71, "annoyance": 0.66, "disapproval": 0.65, "disgust": 0.75,
3 "fear": 0.73, "nervousness": 0.74, "embarrassment": 0.82, "disappointment": 0.69,
4 "gratitude": 0.75, "joy": 0.59, "amusement": 0.65, "excitement": 0.61,
5 "optimism": 0.73, "pride": 0.73, "relief": 0.75, "admiration": 0.71,
6 "approval": 0.69, "love": 0.77, "caring": 0.73, "desire": 0.78,
7 "neutral": 0.63, "sadness": 0.68, "grief": 0.80, "remorse": 0.81,
8 "surprise": 0.59, "realization": 0.61, "curiosity": 0.78, "confusion": 0.77,
9 "tone_positive": 0.49, "tone_negative": 0.47, "tone_neutral": 0.62
10}1import torch
2from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
4model_id = "liudev/roberta-multilabel-28-3-classes"
5tokenizer = AutoTokenizer.from_pretrained(model_id)
6model = AutoModelForSequenceClassification.from_pretrained(model_id)
7model.eval()
8
9# 推荐的生产环境阈值 (High Precision)
10THETA_FINAL_TENSOR = torch.tensor([
11 0.71, 0.66, 0.65, 0.75, 0.73, 0.74, 0.82, 0.69, 0.75, 0.59,
12 0.65, 0.61, 0.73, 0.73, 0.75, 0.71, 0.69, 0.77, 0.73, 0.78,
13 0.63, 0.68, 0.80, 0.81, 0.59, 0.61, 0.78, 0.77, 0.49, 0.47, 0.62
14])
15
16# 标签映射
17id2label = model.config.id2label
18
19# 构建输入 (Context + Current Paragraph)
20context_paragraphs = [
21 "夜幕低垂,狂风在破败的庙宇外肆虐,吹得半掩的残门嘎吱作响。",
22 "李青死死握紧了手中的长剑,手心满是冷汗,连呼吸都变得极其小心翼翼。"
23]
24current_paragraph = "突然,黑暗中传来一声凄厉的惨叫,紧接着,一双血红色的眼睛在神像背后缓缓睁开!"
25
26text_a = "\n".join(context_paragraphs) # 前文历史(提供语境)
27text_b = current_paragraph # 当前需要分析情绪的段落
28
29inputs = tokenizer(
30 text_a,
31 text_b,
32 padding=True,
33 truncation=True,
34 max_length=256,
35 return_tensors="pt"
36)
37
38with torch.no_grad():
39 logits = model(**inputs).logits
40 probs = torch.sigmoid(logits).squeeze(0) # 转换为概率
41
42# 使用自定义阈值进行过滤
43predictions = []
44for idx, prob in enumerate(probs):
45 if prob >= THETA_FINAL_TENSOR[idx]:
46 predictions.append({
47 "label": id2label[idx],
48 "score": round(prob.item(), 4)
49 })
50
51print(predictions)
52# Expected Output format:[{'label': 'fear', 'score': 0.8855}, {'label': 'nervousness', 'score': 0.7645}, {'label': 'surprise', 'score': 0.8146}, {'label': 'tone_negative', 'score': 0.8112}]
53| Label | Precision | Recall | F1-Score | Support |
|---|---|---|---|---|
| anger | 0.75 | 0.77 | 0.76 | 887 |
| annoyance | 0.76 | 0.73 | 0.74 | 1874 |
| disapproval | 0.70 | 0.74 | 0.72 | 2013 |
| disgust | 0.75 | 0.67 | 0.71 | 691 |
| fear | 0.71 | 0.68 | 0.70 | 1165 |
| nervousness | 0.64 | 0.70 | 0.67 | 1134 |
| embarrassment | 0.48 | 0.62 | 0.54 | 417 |
| disappointment | 0.66 | 0.80 | 0.73 | 1805 |
| gratitude | 0.88 | 0.76 | 0.82 | 683 |
| joy | 0.85 | 0.88 | 0.87 | 2329 |
| amusement | 0.72 | 0.68 | 0.70 | 1546 |
| excitement | 0.81 | 0.81 | 0.81 | 2073 |
| optimism | 0.68 | 0.68 | 0.68 | 1464 |
| pride | 0.62 | 0.67 | 0.65 | 1151 |
| relief | 0.69 | 0.64 | 0.67 | 1023 |
| admiration | 0.61 | 0.75 | 0.67 | 1529 |
| approval | 0.67 | 0.74 | 0.70 | 1917 |
| love | 0.65 | 0.68 | 0.67 | 922 |
| caring | 0.67 | 0.71 | 0.69 | 1630 |
| desire | 0.53 | 0.62 | 0.57 | 1132 |
| neutral | 0.74 | 0.75 | 0.75 | 1810 |
| sadness | 0.84 | 0.82 | 0.83 | 1585 |
| grief | 0.64 | 0.77 | 0.70 | 612 |
| remorse | 0.67 | 0.63 | 0.65 | 323 |
| surprise | 0.76 | 0.79 | 0.77 | 2355 |
| realization | 0.65 | 0.79 | 0.71 | 3563 |
| curiosity | 0.69 | 0.69 | 0.69 | 762 |
| confusion | 0.64 | 0.72 | 0.68 | 839 |
| tone_positive | 0.87 | 0.86 | 0.86 | 4306 |
| tone_negative | 0.87 | 0.90 | 0.89 | 4772 |
| tone_neutral | 0.75 | 0.77 | 0.76 | 2093 |
| Micro Avg | 0.73 | 0.77 | 0.75 | 50405 |