File size: 2,676 Bytes
4b75840
 
 
 
 
 
e99a699
620af8b
 
 
 
 
 
 
4b75840
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
620af8b
 
 
 
 
 
 
 
 
4b75840
 
 
 
 
 
 
620af8b
 
 
 
 
 
 
 
 
4b75840
 
 
 
 
 
 
 
 
 
620af8b
 
 
 
 
 
 
 
 
4b75840
 
 
 
 
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
from transformers import AutoTokenizer, AutoModelForSequenceClassification
from transformers_interpret import SequenceClassificationExplainer
import torch
import pandas as pd


class SentimentAnalysis:
    """ This class is an example

    Attributes:
        class_attribute (str): (class attribute) The class attribute
        instance_attribute (str): The instance attribute
    """

    def __init__(self):
        # Load Tokenizer & Model
        hub_location = 'cardiffnlp/twitter-roberta-base-sentiment'
        self.tokenizer = AutoTokenizer.from_pretrained(hub_location)
        self.model = AutoModelForSequenceClassification.from_pretrained(hub_location)

        # Change model labels in config
        self.model.config.id2label[0] = "Negative"
        self.model.config.id2label[1] = "Neutral"
        self.model.config.id2label[2] = "Positive"
        self.model.config.label2id["Negative"] = self.model.config.label2id.pop("LABEL_0")
        self.model.config.label2id["Neutral"] = self.model.config.label2id.pop("LABEL_1")
        self.model.config.label2id["Positive"] = self.model.config.label2id.pop("LABEL_2")

        # Instantiate explainer
        self.explainer = SequenceClassificationExplainer(self.model, self.tokenizer)

    def justify(self, text):
        """
        The function to add two Complex Numbers.

        Parameters:
            num (ComplexNumber): The complex number to be added.

        Returns:
            ComplexNumber: A complex number which contains the sum.
        """

        word_attributions = self.explainer(text)
        html = self.explainer.visualize("example.html")

        return html

    def classify(self, text):
        """
        The function to add two Complex Numbers.

        Parameters:
            num (ComplexNumber): The complex number to be added.

        Returns:
            ComplexNumber: A complex number which contains the sum.
        """

        tokens = self.tokenizer.encode_plus(text, add_special_tokens=False, return_tensors='pt')
        outputs = self.model(**tokens)
        probs = torch.nn.functional.softmax(outputs[0], dim=-1)
        probs = probs.mean(dim=0).detach().numpy()
        preds = pd.Series(probs, index=["Negative", "Neutral", "Positive"], name='Predicted Probability')

        return preds

    def run(self, text):
        """
        The function to add two Complex Numbers.

        Parameters:
            num (ComplexNumber): The complex number to be added.

        Returns:
            ComplexNumber: A complex number which contains the sum.
        """

        preds = self.classify(text)
        html = self.justify(text)

        return preds, html