Update README.md
Browse files
README.md
CHANGED
@@ -1 +1,22 @@
|
|
1 |
# 繁體中文情緒分類: 負面(0)、正面(1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
# 繁體中文情緒分類: 負面(0)、正面(1)
|
2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
3 |
+
tokenizer = AutoTokenizer.from_pretrained("Cheng-Lung/bert-sentiment")
|
4 |
+
model = AutoModelForSequenceClassification.from_pretrained("Cheng-Lung/bert-sentiment")
|
5 |
+
|
6 |
+
## Pediction
|
7 |
+
target_names=['Negative','Positive']
|
8 |
+
max_length = 200 # 最多字數 若超出模型訓練時的字數,以模型最大字數為依據
|
9 |
+
def get_sentiment_proba(text):
|
10 |
+
# prepare our text into tokenized sequence
|
11 |
+
inputs = tokenizer(text, padding=True, truncation=True, max_length=max_length, return_tensors="pt")
|
12 |
+
# perform inference to our model
|
13 |
+
outputs = model(**inputs)
|
14 |
+
# get output probabilities by doing softmax
|
15 |
+
probs = outputs[0].softmax(1)
|
16 |
+
|
17 |
+
response = {'Negative': round(float(probs[0, 0]), 2), 'Positive': round(float(probs[0, 1]), 2)}
|
18 |
+
# executing argmax function to get the candidate label
|
19 |
+
#return probs.argmax()
|
20 |
+
return response
|
21 |
+
|
22 |
+
get_sentiment_proba('不喜歡這款產品')
|