File size: 881 Bytes
b0143d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d91e0f8
b0143d9
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# -*- 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)