-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwilensapi.py
More file actions
45 lines (39 loc) · 1.42 KB
/
twilensapi.py
File metadata and controls
45 lines (39 loc) · 1.42 KB
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
from transformers import pipeline
class TwiLens:
def __init__(self) -> None:
self.classifiermodel = pipeline(
"sentiment-analysis", model="ClassifierModels/BERT", device=0
)
self.summarizermodel = pipeline(
"summarization", model="SummarizerModels/BART", device=0
)
def get_labels(self, pred):
label_map = {
"LABEL_0": "negative",
"LABEL_1": "positive",
"LABEL_2": "neutral",
}
return label_map[pred["label"]]
def classifier(self, dataset):
if type(dataset) != list:
dataset = dataset.tolist()
preds = self.classifiermodel(dataset)
preds = [self.get_labels(pred) for pred in preds]
return preds
def summarizer(self, dataset):
text = " ".join(text for text in dataset)
summary = self.summarizermodel(text)
return summary
def SentimentSummary(self, dataset, sentiment):
sentiment = sentiment.lower()
preds = self.classifier(dataset)
filtered_text = [
text for text, pred in zip(dataset, preds) if pred == sentiment
]
if len(filtered_text) == 0:
print(
f"There are no tweets of {sentiment} sentiment among the ones provided as input."
)
else:
summary = self.summarizer(filtered_text)
return summary[0]["summary_text"]