liudev commited on
Commit
aa4b9ce
·
verified ·
1 Parent(s): 77a1ee8

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +166 -188
README.md CHANGED
@@ -1,199 +1,177 @@
1
  ---
2
- library_name: transformers
3
- tags: []
 
 
 
 
 
 
 
 
 
 
 
4
  ---
5
 
6
- # Model Card for Model ID
7
 
8
- <!-- Provide a quick summary of what the model is/does. -->
 
9
 
 
10
 
 
11
 
12
- ## Model Details
13
 
14
- ### Model Description
 
 
 
 
15
 
16
- <!-- Provide a longer summary of what this model is. -->
17
 
18
- This is the model card of a 🤗 transformers model that has been pushed on the Hub. This model card has been automatically generated.
19
-
20
- - **Developed by:** [More Information Needed]
21
- - **Funded by [optional]:** [More Information Needed]
22
- - **Shared by [optional]:** [More Information Needed]
23
- - **Model type:** [More Information Needed]
24
- - **Language(s) (NLP):** [More Information Needed]
25
- - **License:** [More Information Needed]
26
- - **Finetuned from model [optional]:** [More Information Needed]
27
-
28
- ### Model Sources [optional]
29
-
30
- <!-- Provide the basic links for the model. -->
31
-
32
- - **Repository:** [More Information Needed]
33
- - **Paper [optional]:** [More Information Needed]
34
- - **Demo [optional]:** [More Information Needed]
35
-
36
- ## Uses
37
-
38
- <!-- Address questions around how the model is intended to be used, including the foreseeable users of the model and those affected by the model. -->
39
-
40
- ### Direct Use
41
-
42
- <!-- This section is for the model use without fine-tuning or plugging into a larger ecosystem/app. -->
43
-
44
- [More Information Needed]
45
-
46
- ### Downstream Use [optional]
47
-
48
- <!-- This section is for the model use when fine-tuned for a task, or when plugged into a larger ecosystem/app -->
49
-
50
- [More Information Needed]
51
-
52
- ### Out-of-Scope Use
53
-
54
- <!-- This section addresses misuse, malicious use, and uses that the model will not work well for. -->
55
-
56
- [More Information Needed]
57
-
58
- ## Bias, Risks, and Limitations
59
-
60
- <!-- This section is meant to convey both technical and sociotechnical limitations. -->
61
-
62
- [More Information Needed]
63
-
64
- ### Recommendations
65
-
66
- <!-- This section is meant to convey recommendations with respect to the bias, risk, and technical limitations. -->
67
-
68
- Users (both direct and downstream) should be made aware of the risks, biases and limitations of the model. More information needed for further recommendations.
69
-
70
- ## How to Get Started with the Model
71
-
72
- Use the code below to get started with the model.
73
-
74
- [More Information Needed]
75
-
76
- ## Training Details
77
-
78
- ### Training Data
79
-
80
- <!-- This should link to a Dataset Card, perhaps with a short stub of information on what the training data is all about as well as documentation related to data pre-processing or additional filtering. -->
81
-
82
- [More Information Needed]
83
-
84
- ### Training Procedure
85
-
86
- <!-- This relates heavily to the Technical Specifications. Content here should link to that section when it is relevant to the training procedure. -->
87
-
88
- #### Preprocessing [optional]
89
-
90
- [More Information Needed]
91
-
92
-
93
- #### Training Hyperparameters
94
-
95
- - **Training regime:** [More Information Needed] <!--fp32, fp16 mixed precision, bf16 mixed precision, bf16 non-mixed precision, fp16 non-mixed precision, fp8 mixed precision -->
96
-
97
- #### Speeds, Sizes, Times [optional]
98
-
99
- <!-- This section provides information about throughput, start/end time, checkpoint size if relevant, etc. -->
100
-
101
- [More Information Needed]
102
-
103
- ## Evaluation
104
-
105
- <!-- This section describes the evaluation protocols and provides the results. -->
106
-
107
- ### Testing Data, Factors & Metrics
108
-
109
- #### Testing Data
110
-
111
- <!-- This should link to a Dataset Card if possible. -->
112
-
113
- [More Information Needed]
114
-
115
- #### Factors
116
-
117
- <!-- These are the things the evaluation is disaggregating by, e.g., subpopulations or domains. -->
118
-
119
- [More Information Needed]
120
-
121
- #### Metrics
122
-
123
- <!-- These are the evaluation metrics being used, ideally with a description of why. -->
124
-
125
- [More Information Needed]
126
-
127
- ### Results
128
-
129
- [More Information Needed]
130
-
131
- #### Summary
132
-
133
-
134
-
135
- ## Model Examination [optional]
136
-
137
- <!-- Relevant interpretability work for the model goes here -->
138
-
139
- [More Information Needed]
140
-
141
- ## Environmental Impact
142
-
143
- <!-- Total emissions (in grams of CO2eq) and additional considerations, such as electricity usage, go here. Edit the suggested text below accordingly -->
144
-
145
- Carbon emissions can be estimated using the [Machine Learning Impact calculator](https://mlco2.github.io/impact#compute) presented in [Lacoste et al. (2019)](https://arxiv.org/abs/1910.09700).
146
-
147
- - **Hardware Type:** [More Information Needed]
148
- - **Hours used:** [More Information Needed]
149
- - **Cloud Provider:** [More Information Needed]
150
- - **Compute Region:** [More Information Needed]
151
- - **Carbon Emitted:** [More Information Needed]
152
-
153
- ## Technical Specifications [optional]
154
-
155
- ### Model Architecture and Objective
156
-
157
- [More Information Needed]
158
-
159
- ### Compute Infrastructure
160
-
161
- [More Information Needed]
162
-
163
- #### Hardware
164
-
165
- [More Information Needed]
166
-
167
- #### Software
168
-
169
- [More Information Needed]
170
-
171
- ## Citation [optional]
172
-
173
- <!-- If there is a paper or blog post introducing the model, the APA and Bibtex information for that should go in this section. -->
174
-
175
- **BibTeX:**
176
-
177
- [More Information Needed]
178
-
179
- **APA:**
180
-
181
- [More Information Needed]
182
-
183
- ## Glossary [optional]
184
-
185
- <!-- If relevant, include terms and calculations in this section that can help readers understand the model or model card. -->
186
-
187
- [More Information Needed]
188
-
189
- ## More Information [optional]
190
-
191
- [More Information Needed]
192
-
193
- ## Model Card Authors [optional]
194
-
195
- [More Information Needed]
196
-
197
- ## Model Card Contact
198
 
199
- [More Information Needed]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ language:
3
+ - zh
4
+ tags:
5
+ - roberta
6
+ - text-classification
7
+ - multi-label-classification
8
+ - emotion-detection
9
+ - sentiment-analysis
10
+ - pytorch
11
+ metrics:
12
+ - f1
13
+ - precision
14
+ - recall
15
  ---
16
 
17
+ # RoBERTa Multi-Label Emotion & Tone Classifier (28 Emotions + 3 Tones)
18
 
19
+ [![Try it Live](https://img.shields.io/badge/🚀_Try_it_Live-Novel_Emotion_Search_Engine-blue?style=for-the-badge)](https://semo.liudev.com)
20
+ [![Author](https://img.shields.io/badge/Author-liudev-orange?style=for-the-badge)](https://huggingface.co/liudev)
21
 
22
+ ## 🚀 官方应用展示 (Powered by this model)
23
 
24
+ 本模型目前已在生产环境中部署。我们基于此模型开发了一款强大的 **“小说情绪起伏搜索引擎”**。
25
 
26
+ 👉 **[点击这里立即体验在线 Demo:semo.liudev.com](https://semo.liudev.com)**
27
 
28
+ 在这个应用中,我们展示了该模型的高阶用法:
29
+ 1. **情绪轨迹检索 (Emotion Trajectory Search)**:不再是单纯的关键词搜索,你可以拼装一个情绪链条(例如:`喜极而泣 [Joy] -> [Sadness] -> [Relief]` 或 `先抑后扬 [Annoyance] -> [Surprise] -> [Admiration]`),引擎会在海量小说库中找到完美符合该情绪走向的章节。
30
+ 2. **序列匹配算法**:底层结合了该模型的 31 维向量输出与 DTW (动态时间规整) 算法,实现长文本情绪子序列的模糊匹配。
31
+ 3. **上下文感知高亮**:精准定位并渲染命中情绪的段落。
32
+ 4. **AI 链条生成**:支持使用自然语言描述情绪走向,自动转化为模型的查询向量。
33
 
34
+ 如果你对长文本情感分析、网文数据挖掘感兴趣,强烈建议试用该应用!
35
 
36
+ ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
+ ## 模型简介 (Model Description)
39
+ 本模型 (`liudev/roberta-multilabel-28-3-classes`) 是一个基于 RoBERTa 架构的多标签文本分类模型。专门用于小说、对话或长文本段落的情感和基调分析。
40
+
41
+ 模型共支持 **31 个类别**,包括:
42
+ - **28 种细粒度情感**(如 anger, joy, love, surprise 等)
43
+ - **3 种情感基调**(tone_positive, tone_negative, tone_neutral)
44
+
45
+ ### 特殊的输入格式 (Context-Aware)
46
+ 为了更好地理解小说/长文本中的上下文连贯性,**本模型在训练和推理时使用了双句输入(Pair Input)策略**:
47
+ - `text_a`: 历史上下文(如当前段落的前 3 段)
48
+ - `text_b`: 当前需要预测的段落文本
49
+ 这种设计使得模型能够结合前文语境,做出更准确的判断。
50
+
51
+ ## 生产环境推荐阈值 (High-Precision Thresholds)
52
+ 在多标签分类中,默认的 `0.5` 阈值往往不是最优的。为了在生产环境中确保**“宁愿漏报,也不误报”(高查准率,Precision >= 80% 为目标)**,我们对每个标签进行了严格的阈值调优(正如我们的官方引擎中所使用的那样)。
53
+
54
+ 强烈建议在推理时使用以下独立阈值字典:
55
+
56
+ ```python
57
+ PRODUCTION_THRESHOLDS = {
58
+ "anger": 0.71, "annoyance": 0.66, "disapproval": 0.65, "disgust": 0.75,
59
+ "fear": 0.73, "nervousness": 0.74, "embarrassment": 0.82, "disappointment": 0.69,
60
+ "gratitude": 0.75, "joy": 0.59, "amusement": 0.65, "excitement": 0.61,
61
+ "optimism": 0.73, "pride": 0.73, "relief": 0.75, "admiration": 0.71,
62
+ "approval": 0.69, "love": 0.77, "caring": 0.73, "desire": 0.78,
63
+ "neutral": 0.63, "sadness": 0.68, "grief": 0.80, "remorse": 0.81,
64
+ "surprise": 0.59, "realization": 0.61, "curiosity": 0.78, "confusion": 0.77,
65
+ "tone_positive": 0.49, "tone_negative": 0.47, "tone_neutral": 0.62
66
+ }
67
+ ```
68
+
69
+ ## 如何使用 (How to Use)
70
+
71
+ 以下是一个开箱即用的推理示例,包含了上下文组装和自定义阈值过滤:
72
+
73
+ ```python
74
+ import torch
75
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
76
+
77
+ model_id = "liudev/roberta-multilabel-28-3-classes"
78
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
79
+ model = AutoModelForSequenceClassification.from_pretrained(model_id)
80
+ model.eval()
81
+
82
+ # 推荐的生产环境阈值 (High Precision)
83
+ THETA_FINAL_TENSOR = torch.tensor([
84
+ 0.71, 0.66, 0.65, 0.75, 0.73, 0.74, 0.82, 0.69, 0.75, 0.59,
85
+ 0.65, 0.61, 0.73, 0.73, 0.75, 0.71, 0.69, 0.77, 0.73, 0.78,
86
+ 0.63, 0.68, 0.80, 0.81, 0.59, 0.61, 0.78, 0.77, 0.49, 0.47, 0.62
87
+ ])
88
+
89
+ # 标签映射
90
+ id2label = model.config.id2label
91
+
92
+ # 构建输入 (Context + Current Paragraph)
93
+ context_paragraphs = [
94
+ "夜幕低垂,狂风在破败的庙宇外肆虐,吹得半掩的残门嘎吱作响。",
95
+ "李青死死握紧了手中的长剑,手心满是冷汗,连呼吸都变得极其小心翼翼。"
96
+ ]
97
+ current_paragraph = "突然,黑暗中传来一声凄厉的惨叫,紧接着,一双血红色的眼睛在神像背后缓缓睁开!"
98
+
99
+ text_a = "\n".join(context_paragraphs) # 前文历史(提供语境)
100
+ text_b = current_paragraph # 当前需要分析情绪的段落
101
+
102
+ inputs = tokenizer(
103
+ text_a,
104
+ text_b,
105
+ padding=True,
106
+ truncation=True,
107
+ max_length=256,
108
+ return_tensors="pt"
109
+ )
110
+
111
+ with torch.no_grad():
112
+ logits = model(**inputs).logits
113
+ probs = torch.sigmoid(logits).squeeze(0) # 转换为概率
114
+
115
+ # 使用自定义阈值进行过滤
116
+ predictions = []
117
+ for idx, prob in enumerate(probs):
118
+ if prob >= THETA_FINAL_TENSOR[idx]:
119
+ predictions.append({
120
+ "label": id2label[idx],
121
+ "score": round(prob.item(), 4)
122
+ })
123
+
124
+ print(predictions)
125
+ # Expected Output format:[{'label': 'fear', 'score': 0.8855}, {'label': 'nervousness', 'score': 0.7645}, {'label': 'surprise', 'score': 0.8146}, {'label': 'tone_negative', 'score': 0.8112}]
126
+
127
+ ```
128
+
129
+ ## 评估指标 (Evaluation Results)
130
+
131
+ 本模型在验证集上的综合表现如下:
132
+ - **Micro F1:** 0.75
133
+ - **Macro F1:** 0.72
134
+ - **Samples F1:** 0.75
135
+
136
+ ### 详细分类报告 (基于 Best F1 阈值)
137
+
138
+ 模型在各个标签上的查准率(Precision)和召回率(Recall)表现:
139
+
140
+ | Label | Precision | Recall | F1-Score | Support |
141
+ | :--- | :---: | :---: | :---: | :---: |
142
+ | anger | 0.75 | 0.77 | 0.76 | 887 |
143
+ | annoyance | 0.76 | 0.73 | 0.74 | 1874 |
144
+ | disapproval | 0.70 | 0.74 | 0.72 | 2013 |
145
+ | disgust | 0.75 | 0.67 | 0.71 | 691 |
146
+ | fear | 0.71 | 0.68 | 0.70 | 1165 |
147
+ | nervousness | 0.64 | 0.70 | 0.67 | 1134 |
148
+ | embarrassment| 0.48 | 0.62 | 0.54 | 417 |
149
+ | disappointment| 0.66 | 0.80 | 0.73 | 1805 |
150
+ | gratitude | 0.88 | 0.76 | 0.82 | 683 |
151
+ | joy | 0.85 | 0.88 | 0.87 | 2329 |
152
+ | amusement | 0.72 | 0.68 | 0.70 | 1546 |
153
+ | excitement | 0.81 | 0.81 | 0.81 | 2073 |
154
+ | optimism | 0.68 | 0.68 | 0.68 | 1464 |
155
+ | pride | 0.62 | 0.67 | 0.65 | 1151 |
156
+ | relief | 0.69 | 0.64 | 0.67 | 1023 |
157
+ | admiration | 0.61 | 0.75 | 0.67 | 1529 |
158
+ | approval | 0.67 | 0.74 | 0.70 | 1917 |
159
+ | love | 0.65 | 0.68 | 0.67 | 922 |
160
+ | caring | 0.67 | 0.71 | 0.69 | 1630 |
161
+ | desire | 0.53 | 0.62 | 0.57 | 1132 |
162
+ | neutral | 0.74 | 0.75 | 0.75 | 1810 |
163
+ | sadness | 0.84 | 0.82 | 0.83 | 1585 |
164
+ | grief | 0.64 | 0.77 | 0.70 | 612 |
165
+ | remorse | 0.67 | 0.63 | 0.65 | 323 |
166
+ | surprise | 0.76 | 0.79 | 0.77 | 2355 |
167
+ | realization | 0.65 | 0.79 | 0.71 | 3563 |
168
+ | curiosity | 0.69 | 0.69 | 0.69 | 762 |
169
+ | confusion | 0.64 | 0.72 | 0.68 | 839 |
170
+ | tone_positive | 0.87 | 0.86 | 0.86 | 4306 |
171
+ | tone_negative | 0.87 | 0.90 | 0.89 | 4772 |
172
+ | tone_neutral | 0.75 | 0.77 | 0.76 | 2093 |
173
+ | **Micro Avg** | **0.73** | **0.77** | **0.75** | **50405** |
174
+
175
+ *(注意:若采用前文提供的生产环境高精度阈值,Precision 将普遍提升至 0.7~0.85 以上,代价是 Recall 会按预期下降。)*
176
+
177
+ 以上内容撰写使用LLM生成,以上代码可直接在kaggle上运行,以上数据均为实际运行结果,已测试通过。