Update README.md
Browse files
README.md
CHANGED
|
@@ -45,33 +45,19 @@ This model is a fine-tuned version of `roberta-large` trained for logical fallac
|
|
| 45 |
To use the model for quick classification with a text pipeline:
|
| 46 |
|
| 47 |
```python
|
| 48 |
-
from transformers import pipeline
|
| 49 |
import torch
|
| 50 |
|
| 51 |
-
#
|
| 52 |
model_path = "MidhunKanadan/roberta-large-fallacy-classification"
|
|
|
|
| 53 |
|
| 54 |
-
#
|
| 55 |
-
device = 0 if torch.cuda.is_available() else -1
|
| 56 |
-
|
| 57 |
-
# Initialize the text classification pipeline with use_fast=False for the tokenizer
|
| 58 |
-
tokenizer = AutoTokenizer.from_pretrained(model_path, use_fast=False)
|
| 59 |
-
model = AutoModelForSequenceClassification.from_pretrained(model_path)
|
| 60 |
-
|
| 61 |
-
# Use GPU if available
|
| 62 |
-
pipe = pipeline("text-classification", model=model, tokenizer=tokenizer, device=device)
|
| 63 |
-
|
| 64 |
-
# Define a sample text
|
| 65 |
text = "The rooster crows always before the sun rises, therefore the crowing rooster causes the sun to rise."
|
|
|
|
| 66 |
|
| 67 |
-
#
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
# Print the predicted label and score
|
| 71 |
-
print(f"Predicted Label: {result[0]['label']}")
|
| 72 |
-
print(f"Score: {result[0]['score']:.4f}")
|
| 73 |
-
|
| 74 |
-
|
| 75 |
```
|
| 76 |
|
| 77 |
Expected Output:
|
|
@@ -88,33 +74,20 @@ For more control, load the model and tokenizer directly and perform classificati
|
|
| 88 |
import torch
|
| 89 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 90 |
|
| 91 |
-
# Load
|
| 92 |
model_path = "MidhunKanadan/roberta-large-fallacy-classification"
|
| 93 |
-
model = AutoModelForSequenceClassification.from_pretrained(model_path)
|
| 94 |
-
tokenizer = AutoTokenizer.from_pretrained(model_path, use_fast=False)
|
| 95 |
-
|
| 96 |
-
# Automatically move model to GPU if available, else use CPU
|
| 97 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 98 |
-
model.to(device)
|
|
|
|
| 99 |
|
| 100 |
-
#
|
| 101 |
text = "The rooster crows always before the sun rises, therefore the crowing rooster causes the sun to rise."
|
| 102 |
-
|
| 103 |
-
# Tokenize the input text and move tensors to the appropriate device
|
| 104 |
inputs = tokenizer(text, return_tensors="pt").to(device)
|
| 105 |
-
|
| 106 |
-
# Run the model and get logits
|
| 107 |
with torch.no_grad():
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
# Apply softmax to get probabilities
|
| 111 |
-
probabilities = torch.nn.functional.softmax(logits, dim=1)[0]
|
| 112 |
-
|
| 113 |
-
# Pair each label with its score and sort in descending order
|
| 114 |
-
sorted_results = sorted(zip(model.config.id2label.values(), probabilities), key=lambda x: x[1], reverse=True)
|
| 115 |
|
| 116 |
-
#
|
| 117 |
-
for label, score in
|
| 118 |
print(f"{label}: {score.item():.4f}")
|
| 119 |
```
|
| 120 |
|
|
|
|
| 45 |
To use the model for quick classification with a text pipeline:
|
| 46 |
|
| 47 |
```python
|
| 48 |
+
from transformers import pipeline
|
| 49 |
import torch
|
| 50 |
|
| 51 |
+
# Initialize the text classification pipeline with the specified model and tokenizer
|
| 52 |
model_path = "MidhunKanadan/roberta-large-fallacy-classification"
|
| 53 |
+
pipe = pipeline("text-classification", model=model_path, tokenizer=model_path, use_fast=False, device=0 if torch.cuda.is_available() else -1)
|
| 54 |
|
| 55 |
+
# Sample text to analyze for logical fallacies
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
text = "The rooster crows always before the sun rises, therefore the crowing rooster causes the sun to rise."
|
| 57 |
+
result = pipe(text)[0] # Retrieve the first result (main prediction)
|
| 58 |
|
| 59 |
+
# Output the predicted label and confidence score
|
| 60 |
+
print(f"Predicted Label: {result['label']}\nScore: {result['score']:.4f}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
```
|
| 62 |
|
| 63 |
Expected Output:
|
|
|
|
| 74 |
import torch
|
| 75 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 76 |
|
| 77 |
+
# Load model and tokenizer, set device
|
| 78 |
model_path = "MidhunKanadan/roberta-large-fallacy-classification"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 80 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_path).to(device).eval()
|
| 81 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path, use_fast=False)
|
| 82 |
|
| 83 |
+
# Tokenize input and get probabilities
|
| 84 |
text = "The rooster crows always before the sun rises, therefore the crowing rooster causes the sun to rise."
|
|
|
|
|
|
|
| 85 |
inputs = tokenizer(text, return_tensors="pt").to(device)
|
|
|
|
|
|
|
| 86 |
with torch.no_grad():
|
| 87 |
+
probabilities = torch.nn.functional.softmax(model(**inputs).logits, dim=1)[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
+
# Output sorted results
|
| 90 |
+
for label, score in sorted(zip(model.config.id2label.values(), probabilities), key=lambda x: x[1], reverse=True):
|
| 91 |
print(f"{label}: {score.item():.4f}")
|
| 92 |
```
|
| 93 |
|