Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,21 @@
|
|
| 1 |
-
|
| 2 |
-
from transformers import
|
| 3 |
|
| 4 |
-
|
|
|
|
| 5 |
|
| 6 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from transformers import AutoTokenizer, BertForSequenceClassification
|
| 3 |
|
| 4 |
+
tokenizer = AutoTokenizer.from_pretrained("textattack/bert-base-uncased-yelp-polarity")
|
| 5 |
+
model = BertForSequenceClassification.from_pretrained("textattack/bert-base-uncased-yelp-polarity")
|
| 6 |
|
| 7 |
+
inputs = tokenizer("Hello, my dog is cute", return_tensors="pt")
|
| 8 |
+
|
| 9 |
+
with torch.no_grad():
|
| 10 |
+
logits = model(**inputs).logits
|
| 11 |
+
|
| 12 |
+
predicted_class_id = logits.argmax().item()
|
| 13 |
+
model.config.id2label[predicted_class_id]
|
| 14 |
+
|
| 15 |
+
# To train a model on `num_labels` classes, you can pass `num_labels=num_labels` to `.from_pretrained(...)`
|
| 16 |
+
num_labels = len(model.config.id2label)
|
| 17 |
+
model = BertForSequenceClassification.from_pretrained("textattack/bert-base-uncased-yelp-polarity", num_labels=num_labels)
|
| 18 |
+
|
| 19 |
+
labels = torch.tensor([1])
|
| 20 |
+
loss = model(**inputs, labels=labels).loss
|
| 21 |
+
round(loss.item(), 2)
|