File size: 1,344 Bytes
5dcec38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
from typing import Dict

from transformers import (
    AutoModelForSequenceClassification,
    AutoTokenizer,
    TextClassificationPipeline,
)


class TweetPipeline:
    def __init__(self):
        self.emotion_pipeline = TextClassificationPipeline(
            model=AutoModelForSequenceClassification.from_pretrained(
                "elozano/tweet_emotion_eval"
            ),
            tokenizer=AutoTokenizer.from_pretrained("elozano/tweet_emotion_eval"),
        )
        self.offensive_pipeline = TextClassificationPipeline(
            model=AutoModelForSequenceClassification.from_pretrained(
                "elozano/tweet_offensive_eval"
            ),
            tokenizer=AutoTokenizer.from_pretrained("elozano/tweet_offensive_eval"),
        )
        self.sentiment_pipeline = TextClassificationPipeline(
            model=AutoModelForSequenceClassification.from_pretrained(
                "elozano/tweet_sentiment_eval"
            ),
            tokenizer=AutoTokenizer.from_pretrained("elozano/tweet_sentiment_eval"),
        )

    def __call__(self, text: str) -> Dict[str, str]:
        return {
            "emotion": self.emotion_pipeline(text)[0]["label"],
            "offensive": self.offensive_pipeline(text)[0]["label"],
            "sentiment": self.sentiment_pipeline(text)[0]["label"],
        }