File size: 1,629 Bytes
efb0817
5d28776
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89c20b1
5d28776
 
 
 
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
52
53
54
55
56
57
58
59
60

import gradio as gr
from transformers import pipeline
import torch
from transformers import RobertaForSequenceClassification, RobertaTokenizer
id2label = {
    "0": "admiration",
    "1": "amusement",
    "2": "anger",
    "3": "annoyance",
    "4": "approval",
    "5": "caring",
    "6": "confusion",
    "7": "curiosity",
    "8": "desire",
    "9": "disappointment",
    "10": "disapproval",
    "11": "disgust",
    "12": "embarrassment",
    "13": "excitement",
    "14": "fear",
    "15": "gratitude",
    "16": "grief",
    "17": "joy",
    "18": "love",
    "19": "nervousness",
    "20": "optimism",
    "21": "pride",
    "22": "realization",
    "23": "relief",
    "24": "remorse",
    "25": "sadness",
    "26": "surprise",
    "27": "neutral"
}
def predict(text):
    model = RobertaForSequenceClassification.from_pretrained("Pavan97/emotion_analysis_new", num_labels = 28)
    tokenizer = RobertaTokenizer.from_pretrained("roberta-base")
    tokenized_text = tokenizer(text, return_tensors = 'pt')
    with torch.no_grad():
        labels = model(**tokenized_text)
    labels = torch.nn.functional.softmax(labels.logits[0], dim = -1)

    predicted_labels = {}
    for idx, label in enumerate(labels):
        
        predicted_labels[id2label[str(idx)]] = label.numpy().item()
            

    

    return predicted_labels

iface = gr.Interface(fn = predict,
                     inputs = "text",
                     outputs = gr.Label(num_top_classes=5),
                     title = 'Emotion Analysis',
                     description = "Get the emotion of the input text",)

iface.launch(inline = False)