Update README.md
Browse files
README.md
CHANGED
|
@@ -47,6 +47,61 @@ tags:
|
|
| 47 |
|
| 48 |
# Code
|
| 49 |
|
|
|
|
|
|
|
| 50 |
```python
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
```
|
|
|
|
| 47 |
|
| 48 |
# Code
|
| 49 |
|
| 50 |
+
To run the model, you need to first evaluate the binary classification model, as shown below:
|
| 51 |
+
|
| 52 |
```python
|
| 53 |
+
# Models
|
| 54 |
+
MAX_LEN = 256
|
| 55 |
+
BINARY_MODEL_DIR = "crarojasca/BinaryAugmentedCARDS"
|
| 56 |
+
TAXONOMY_MODEL_DIR = "crarojasca/TaxonomyAugmentedCARDS"
|
| 57 |
+
|
| 58 |
+
# Loading tokenizer
|
| 59 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 60 |
+
BINARY_MODEL_DIR,
|
| 61 |
+
max_length = MAX_LEN, padding = "max_length",
|
| 62 |
+
return_token_type_ids = True
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
# Loading Models
|
| 66 |
+
## 1. Binary Model
|
| 67 |
+
print("Loading binary model: {}".format(BINARY_MODEL_DIR))
|
| 68 |
+
config = AutoConfig.from_pretrained(BINARY_MODEL_DIR)
|
| 69 |
+
binary_model = AutoModelForSequenceClassification.from_pretrained(BINARY_MODEL_DIR, config=config)
|
| 70 |
+
binary_model.to(device)
|
| 71 |
+
|
| 72 |
+
## 2. Taxonomy Model
|
| 73 |
+
print("Loading taxonomy model: {}".format(TAXONOMY_MODEL_DIR))
|
| 74 |
+
config = AutoConfig.from_pretrained(TAXONOMY_MODEL_DIR)
|
| 75 |
+
taxonomy_model = AutoModelForSequenceClassification.from_pretrained(TAXONOMY_MODEL_DIR, config=config)
|
| 76 |
+
taxonomy_model.to(device)
|
| 77 |
+
|
| 78 |
+
# Load Dataset
|
| 79 |
+
id2label = {
|
| 80 |
+
0: '1_1', 1: '1_2', 2: '1_3', 3: '1_4', 4: '1_6', 5: '1_7', 6: '2_1',
|
| 81 |
+
7: '2_3', 8: '3_1', 9: '3_2', 10: '3_3', 11: '4_1', 12: '4_2', 13: '4_4',
|
| 82 |
+
14: '4_5', 15: '5_1', 16: '5_2', 17: '5_3'
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
text = "Climate change is just a natural phenomenon"
|
| 87 |
+
|
| 88 |
+
tokenized_text = tokenizer(text, return_tensors = "pt")
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
# Running Binary Model
|
| 92 |
+
outputs = binary_model(**tokenized_text)
|
| 93 |
+
binary_score = outputs.logits.softmax(dim = 1)
|
| 94 |
+
binary_prediction = torch.argmax(outputs.logits, axis=1)
|
| 95 |
+
binary_predictions = binary_prediction.to('cpu').item()
|
| 96 |
+
|
| 97 |
+
# Running Taxonomy Model
|
| 98 |
+
outputs = taxonomy_model(**tokenized_text)
|
| 99 |
+
taxonomy_score = outputs.logits.softmax(dim = 1)
|
| 100 |
+
taxonomy_prediction = torch.argmax(outputs.logits, axis=1)
|
| 101 |
+
taxonomy_prediction = taxonomy_prediction.to('cpu').item()
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
prediction = "0_0" if binary_prediction==0 else id2label[taxonomy_prediction]
|
| 105 |
+
prediction
|
| 106 |
+
|
| 107 |
```
|