Update README.md
Browse files
README.md
CHANGED
@@ -1,3 +1,44 @@
|
|
1 |
-
---
|
2 |
-
license: gpl-3.0
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
license: gpl-3.0
|
3 |
+
widget:
|
4 |
+
- text: 宵凉百念集孤灯,暗雨鸣廊睡未能。生计坐怜秋一叶,归程冥想浪千层。寒心国事浑难料,堆眼官资信可憎。此去梦中应不忘,顺承门内近觚棱。
|
5 |
+
pipeline_tag: text-classification
|
6 |
+
---
|
7 |
+
|
8 |
+
使用方法如下:
|
9 |
+
```python
|
10 |
+
from transformers import BertTokenizer, BertForSequenceClassification
|
11 |
+
import torch
|
12 |
+
|
13 |
+
# 加载已训练的模型和分词器
|
14 |
+
model_path = 'qixun/tangsong_qilv_classify'
|
15 |
+
tokenizer = BertTokenizer.from_pretrained(model_path)
|
16 |
+
model = BertForSequenceClassification.from_pretrained(model_path)
|
17 |
+
|
18 |
+
# 预处理函数
|
19 |
+
def preprocess_text(text):
|
20 |
+
inputs = tokenizer(text, padding='max_length', truncation=True, max_length=128, return_tensors='pt')
|
21 |
+
return inputs
|
22 |
+
|
23 |
+
# 分类函数
|
24 |
+
def classify_text(text):
|
25 |
+
model.eval() # 切换到评估模式
|
26 |
+
inputs = preprocess_text(text)
|
27 |
+
with torch.no_grad():
|
28 |
+
outputs = model(**inputs)
|
29 |
+
logits = outputs.logits
|
30 |
+
probabilities = torch.softmax(logits, dim=1)
|
31 |
+
predicted_label = torch.argmax(probabilities, dim=1).item()
|
32 |
+
return predicted_label, probabilities
|
33 |
+
|
34 |
+
# 示例文本
|
35 |
+
text = "宵凉百念集孤灯,暗雨鸣廊睡未能。生计坐怜秋一叶,归程冥想浪千层。寒心国事浑难料,堆眼官资信可憎。此去梦中应不忘,顺承门内近觚棱。"
|
36 |
+
|
37 |
+
# 调用分类函数
|
38 |
+
predicted_label, probabilities = classify_text(text)
|
39 |
+
|
40 |
+
# 输出结果
|
41 |
+
print(f"预测标签: {predicted_label}")
|
42 |
+
print(f"概率分布: {probabilities}")
|
43 |
+
|
44 |
+
```
|