DarwinAnim8or
commited on
Commit
•
9c944b5
1
Parent(s):
430b7ae
Update README.md
Browse files
README.md
CHANGED
@@ -88,11 +88,42 @@ Or Python API:
|
|
88 |
```python
|
89 |
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
90 |
|
91 |
-
model
|
92 |
-
|
93 |
-
tokenizer = AutoTokenizer.from_pretrained("KoalaAI/Text-Moderation"
|
94 |
|
|
|
95 |
inputs = tokenizer("I love AutoTrain", return_tensors="pt")
|
96 |
-
|
97 |
outputs = model(**inputs)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
```
|
|
|
88 |
```python
|
89 |
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
90 |
|
91 |
+
# Load the model and tokenizer
|
92 |
+
model = AutoModelForSequenceClassification.from_pretrained("KoalaAI/Text-Moderation")
|
93 |
+
tokenizer = AutoTokenizer.from_pretrained("KoalaAI/Text-Moderation")
|
94 |
|
95 |
+
# Run the model on your input
|
96 |
inputs = tokenizer("I love AutoTrain", return_tensors="pt")
|
|
|
97 |
outputs = model(**inputs)
|
98 |
+
|
99 |
+
# Get the predicted logits
|
100 |
+
logits = outputs.logits
|
101 |
+
|
102 |
+
# Apply softmax to get probabilities (scores)
|
103 |
+
probabilities = logits.softmax(dim=-1).squeeze()
|
104 |
+
|
105 |
+
# Retrieve the labels
|
106 |
+
id2label = model.config.id2label
|
107 |
+
labels = [id2label[idx] for idx in range(len(probabilities))]
|
108 |
+
|
109 |
+
# Combine labels and probabilities, then sort
|
110 |
+
label_prob_pairs = list(zip(labels, probabilities))
|
111 |
+
label_prob_pairs.sort(key=lambda item: item[1], reverse=True)
|
112 |
+
|
113 |
+
# Print the sorted results
|
114 |
+
for label, probability in label_prob_pairs:
|
115 |
+
print(f"Label: {label} - Probability: {probability:.4f}")
|
116 |
+
```
|
117 |
+
|
118 |
+
The output of the above Python code will look like this:
|
119 |
+
```
|
120 |
+
Label: OK - Probability: 0.9840
|
121 |
+
Label: H - Probability: 0.0043
|
122 |
+
Label: SH - Probability: 0.0039
|
123 |
+
Label: V - Probability: 0.0019
|
124 |
+
Label: S - Probability: 0.0018
|
125 |
+
Label: HR - Probability: 0.0015
|
126 |
+
Label: V2 - Probability: 0.0011
|
127 |
+
Label: S3 - Probability: 0.0010
|
128 |
+
Label: H2 - Probability: 0.0006
|
129 |
```
|