TestApp / components /english_information_extraction.py
menikev's picture
Upload 9 files
d2ed505 verified
raw
history blame
1.88 kB
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}