File size: 1,882 Bytes
d2ed505
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from transformers import pipeline

zeroshot_classifier = pipeline("zero-shot-classification", model="MoritzLaurer/deberta-v3-large-zeroshot-v1.1-all-33")





def english_information_extraction(text: str):





  hypothesis_template_domain = "This text is about {}"
  domain_classes = ["women" , "muslims" , "tamil" , "sinhala" , "other"]
  domains_output= zeroshot_classifier(text, domain_classes , hypothesis_template=hypothesis_template_domain, multi_label=False)

  sentiment_discrimination_prompt = f"the content of this text about {domains_output['labels'][0]} "
  hypothesis_template_sentiment = "is {} sentiment"
  hypothesis_template_sentiment = sentiment_discrimination_prompt + hypothesis_template_sentiment

  sentiment_classes = ["positive" ,"neutral", "negative"]
  sentiment_output= zeroshot_classifier(text, sentiment_classes , hypothesis_template=hypothesis_template_sentiment, multi_label=False)

  hypothesis_template_discrimination = "is {}"
  hypothesis_template_discrimination = sentiment_discrimination_prompt + hypothesis_template_discrimination

  discrimination_classes = ["hateful" , "not hateful"]

  discrimination_output= zeroshot_classifier(text, discrimination_classes , hypothesis_template=hypothesis_template_discrimination, multi_label=False)

  domain_label , domain_score = domains_output["labels"][0] , domains_output["scores"][0]
  sentiment_label , sentiment_score = sentiment_output["labels"][0] , sentiment_output["scores"][0]
  discrimination_label , discrimination_score = discrimination_output["labels"][0] , discrimination_output["scores"][0]

  return {"domain_label" : domain_label,
          "domain_score" : domain_score,
          "sentiment_label" : sentiment_label,
          "sentiment_score" : sentiment_score,
          "discrimination_label" : discrimination_label,
          "discrimination_score": discrimination_score}