# -*- coding: utf-8 -*- | |
""" | |
Spyder Editor | |
This is a temporary script file. | |
""" | |
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline | |
import torch | |
tokenizer = AutoTokenizer.from_pretrained("nomsgadded/opt_RestaurantReview") | |
model = AutoModelForSequenceClassification.from_pretrained("nomsgadded/opt_RestaurantReview", num_labels=9) | |
prefix = "##Rating: " | |
text1 = "Bad" | |
text2 = "It was really nice to dine there, however the waiter is very mean." | |
text3 = "Nice" | |
def inference(text): | |
text = prefix + text | |
inputs = tokenizer(text, return_tensors="pt") | |
classifier = pipeline("text-classification", model=model, tokenizer=tokenizer) | |
with torch.no_grad(): | |
logits = model(**inputs).logits | |
predicted_class_id = logits.argmax().item() | |
print((predicted_class_id+2)/2) | |
inference(text1) | |
inference(text2) | |
inference(text3) |