jackietung commited on
Commit
315679e
·
verified ·
1 Parent(s): 0d8e442

Upload folder using huggingface_hub

Browse files
README.md ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ language: zh
3
+ license: mit
4
+ tags:
5
+ - bert
6
+ - sentiment-analysis
7
+ - chinese
8
+ datasets:
9
+ - custom
10
+ ---
11
+
12
+ # 中文情感分析模型
13
+
14
+ 這是一個基於 BERT 的中文情感分析模型,可用於判斷文本的情感傾向(正面、負面或中性)。
15
+
16
+ ## 模型描述
17
+
18
+ - 模型基於 bert-base-chinese 微調
19
+ - 適用於中文文本的情感分析
20
+ - 輸出標籤:0(負面),1(正面),2(中性)
21
+ - 使用 Focal Loss 訓練,以處理類別不平衡問題
22
+
23
+ ## 使用方法
24
+
25
+ ```python
26
+ from transformers import AutoModelForSequenceClassification, AutoTokenizer
27
+ import torch
28
+
29
+ # 載入模型和分詞器
30
+ model = AutoModelForSequenceClassification.from_pretrained("jackietung/bert-base-chinese-sentiment-finetuned")
31
+ tokenizer = AutoTokenizer.from_pretrained("jackietung/bert-base-chinese-sentiment-finetuned")
32
+
33
+ # 準備輸入
34
+ text = "這個App使用體驗很差!"
35
+ inputs = tokenizer(text, return_tensors="pt")
36
+
37
+ # 進行預測
38
+ with torch.no_grad():
39
+ outputs = model(**inputs)
40
+ predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
41
+
42
+ # 獲取預測結果
43
+ label_names = ["負面", "正面", "中性"]
44
+ predicted_class = torch.argmax(predictions, dim=1).item()
45
+
46
+ print(f"預測類別: {label_names[predicted_class]}")
47
+ print(f"預測分數: {predictions[0][predicted_class].item():.4f}")
48
+
49
+ # 顯示所有類別的分數
50
+ for i, label in enumerate(label_names):
51
+ print(f"{label} 分數: {predictions[0][i].item():.4f}")
config.json ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_name_or_path": "bert-base-chinese",
3
+ "architectures": [
4
+ "BertForSequenceClassification"
5
+ ],
6
+ "attention_probs_dropout_prob": 0.1,
7
+ "classifier_dropout": null,
8
+ "directionality": "bidi",
9
+ "hidden_act": "gelu",
10
+ "hidden_dropout_prob": 0.1,
11
+ "hidden_size": 768,
12
+ "id2label": {
13
+ "0": "\u8ca0\u9762",
14
+ "1": "\u6b63\u9762",
15
+ "2": "\u4e2d\u6027"
16
+ },
17
+ "initializer_range": 0.02,
18
+ "intermediate_size": 3072,
19
+ "label2id": {
20
+ "\u4e2d\u6027": 2,
21
+ "\u6b63\u9762": 1,
22
+ "\u8ca0\u9762": 0
23
+ },
24
+ "layer_norm_eps": 1e-12,
25
+ "max_position_embeddings": 512,
26
+ "model_type": "bert",
27
+ "num_attention_heads": 12,
28
+ "num_hidden_layers": 12,
29
+ "pad_token_id": 0,
30
+ "pooler_fc_size": 768,
31
+ "pooler_num_attention_heads": 12,
32
+ "pooler_num_fc_layers": 3,
33
+ "pooler_size_per_head": 128,
34
+ "pooler_type": "first_token_transform",
35
+ "position_embedding_type": "absolute",
36
+ "torch_dtype": "float32",
37
+ "transformers_version": "4.48.3",
38
+ "type_vocab_size": 2,
39
+ "use_cache": true,
40
+ "vocab_size": 21128
41
+ }
examples.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ from transformers import pipeline
3
+
4
+ # 載入情感分析管道
5
+ classifier = pipeline(
6
+ "sentiment-analysis",
7
+ model="jackietung/bert-base-chinese-sentiment-finetuned",
8
+ return_all_scores=True
9
+ )
10
+
11
+ # 測試文本
12
+ texts = [
13
+ "這款 App 的界面設計非常直觀,使用起來很順暢!",
14
+ "客服回應速度太慢,問題遲遲得不到解決,很失望。",
15
+ "功能還算齊全,但偶爾會閃退,希望能改進。",
16
+ "雖然有些小bug,但整體來說是個實用的工具App。",
17
+ "完全不推薦下載,廣告太多而且耗電量驚人。"
18
+ ]
19
+
20
+ # 進行預測
21
+ for text in texts:
22
+ result = classifier(text)[0]
23
+ print(f"文本: {text}")
24
+
25
+ # 按分數排序
26
+ sorted_scores = sorted(result, key=lambda x: x['score'], reverse=True)
27
+
28
+ # 獲取最高分數的情感
29
+ top_sentiment = sorted_scores[0]
30
+ print(f"預測情感: {top_sentiment['label']} (分數: {top_sentiment['score']:.4f})")
31
+
32
+ # 顯示所有情感分數
33
+ print("所有情感分數:")
34
+ for score_item in sorted_scores:
35
+ print(f" {score_item['label']}: {score_item['score']:.4f}")
36
+ print("-" * 50)
metadata.json ADDED
File without changes
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5ead3f78962d41b506f182b3fdc6ca023f9d72b3367d789b677014c198cf16b9
3
+ size 409103316
special_tokens_map.json ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ {
2
+ "cls_token": "[CLS]",
3
+ "mask_token": "[MASK]",
4
+ "pad_token": "[PAD]",
5
+ "sep_token": "[SEP]",
6
+ "unk_token": "[UNK]"
7
+ }
tokenizer_config.json ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "added_tokens_decoder": {
3
+ "0": {
4
+ "content": "[PAD]",
5
+ "lstrip": false,
6
+ "normalized": false,
7
+ "rstrip": false,
8
+ "single_word": false,
9
+ "special": true
10
+ },
11
+ "100": {
12
+ "content": "[UNK]",
13
+ "lstrip": false,
14
+ "normalized": false,
15
+ "rstrip": false,
16
+ "single_word": false,
17
+ "special": true
18
+ },
19
+ "101": {
20
+ "content": "[CLS]",
21
+ "lstrip": false,
22
+ "normalized": false,
23
+ "rstrip": false,
24
+ "single_word": false,
25
+ "special": true
26
+ },
27
+ "102": {
28
+ "content": "[SEP]",
29
+ "lstrip": false,
30
+ "normalized": false,
31
+ "rstrip": false,
32
+ "single_word": false,
33
+ "special": true
34
+ },
35
+ "103": {
36
+ "content": "[MASK]",
37
+ "lstrip": false,
38
+ "normalized": false,
39
+ "rstrip": false,
40
+ "single_word": false,
41
+ "special": true
42
+ }
43
+ },
44
+ "clean_up_tokenization_spaces": true,
45
+ "cls_token": "[CLS]",
46
+ "do_basic_tokenize": true,
47
+ "do_lower_case": false,
48
+ "extra_special_tokens": {},
49
+ "mask_token": "[MASK]",
50
+ "model_max_length": 512,
51
+ "never_split": null,
52
+ "pad_token": "[PAD]",
53
+ "sep_token": "[SEP]",
54
+ "strip_accents": null,
55
+ "tokenize_chinese_chars": true,
56
+ "tokenizer_class": "BertTokenizer",
57
+ "unk_token": "[UNK]"
58
+ }
vocab.txt ADDED
The diff for this file is too large to render. See raw diff