Update README.md
Browse files
README.md
CHANGED
|
@@ -83,22 +83,22 @@ For more detailed control over predictions:
|
|
| 83 |
```python
|
| 84 |
import torch
|
| 85 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
|
|
|
| 86 |
|
| 87 |
-
|
| 88 |
-
model_path = "MidhunKanadan/roberta-large-fallacy-classification"
|
| 89 |
-
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 90 |
-
model = AutoModelForSequenceClassification.from_pretrained(model_path).to(device).eval()
|
| 91 |
-
tokenizer = AutoTokenizer.from_pretrained(model_path, use_fast=False)
|
| 92 |
-
|
| 93 |
-
# Tokenize input and get probabilities
|
| 94 |
text = "The rooster crows always before the sun rises, therefore the crowing rooster causes the sun to rise."
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
with torch.no_grad():
|
| 97 |
-
|
|
|
|
| 98 |
|
| 99 |
-
#
|
| 100 |
-
for label, score in sorted(
|
| 101 |
-
print(f"{label}: {score
|
| 102 |
```
|
| 103 |
|
| 104 |
Expected Output:
|
|
|
|
| 83 |
```python
|
| 84 |
import torch
|
| 85 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 86 |
+
import torch.nn.functional as F
|
| 87 |
|
| 88 |
+
model_path = "MidhunKanadan/roberta-large-logical-fallacy-classifier"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
text = "The rooster crows always before the sun rises, therefore the crowing rooster causes the sun to rise."
|
| 90 |
+
|
| 91 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 92 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_path).to("cuda")
|
| 93 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=128).to("cuda")
|
| 94 |
+
|
| 95 |
with torch.no_grad():
|
| 96 |
+
probs = F.softmax(model(**inputs).logits, dim=-1)
|
| 97 |
+
results = {model.config.id2label[i]: score.item() for i, score in enumerate(probs[0])}
|
| 98 |
|
| 99 |
+
# Print scores for all labels
|
| 100 |
+
for label, score in sorted(results.items(), key=lambda x: x[1], reverse=True):
|
| 101 |
+
print(f"{label}: {score:.4f}")
|
| 102 |
```
|
| 103 |
|
| 104 |
Expected Output:
|