File size: 1,483 Bytes
d477079
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch.nn.functional as F

MODEL_PATH = "vishnuexe/TamilSense-model"
ID2LABEL = {0: "positive", 1: "negative"}

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
model = AutoModelForSequenceClassification.from_pretrained(MODEL_PATH)
model.to(device)
model.eval()

def predict(text: str):
    inputs = tokenizer(
        text,
        return_tensors="pt",
        truncation=True,
        max_length=128,
        padding=True
    ).to(device)

    with torch.no_grad():
        outputs = model(**inputs)

    probs = F.softmax(outputs.logits, dim=-1).cpu().numpy()[0]
    predicted_id = probs.argmax()
    predicted_label = ID2LABEL[predicted_id]
    confidence = float(probs[predicted_id])

    return {
        "text": text,
        "sentiment": predicted_label,
        "confidence": round(confidence, 4),
        "scores": {
            "positive": round(float(probs[0]), 4),
            "negative": round(float(probs[1]), 4),
        }
    }

if __name__ == "__main__":
    test_sentences = [
        "Super da machan, vera level!",
        "Worst movie ever, total waste of time",
    ]
    for sentence in test_sentences:
        result = predict(sentence)
        print(f"Text: {result['text']}")
        print(f"Sentiment: {result['sentiment']} ({result['confidence']*100:.1f}% confident)")
        print()