flairNLP / flair

A very simple framework for state-of-the-art Natural Language Processing (NLP)
https://flairnlp.github.io/flair/
Other
13.81k stars 2.09k forks source link

TARS Zero Shot Classifier Predictions #2997

Closed nlproc-007 closed 1 year ago

nlproc-007 commented 1 year ago

Here is the example code to use TARS Zero Shot Classifier

from flair.models import TARSClassifier
from flair.data import Sentence

# 1. Load our pre-trained TARS model for English
tars = TARSClassifier.load('tars-base')

# 2. Prepare a test sentence
sentence = Sentence("I am so glad you liked it!")

# 3. Define some classes that you want to predict using descriptive names
classes = ["happy", "sad"]

#4. Predict for these classes
tars.predict_zero_shot(sentence, classes)

# Print sentence with predicted labels
print(sentence)
print(sentence.labels[0].value)
print(round(sentence.labels[0].score,2))

Now this code is wrapped into the following function so that I can use it to get predictions for multiple sentences in a dataset.

def tars_zero(example):
  sentence = Sentence(example)
  tars.predict_zero_shot(sentence,classes)
  print(sentence)

inputs = ["I am so glad you liked it!", "I hate it"]
for input in inputs:
  tars_zero(input)

#output: 
Sentence: "I am so glad you liked it !" → happy (0.8667)
Sentence: "I hate it"

Here the model is giving predictions only for the first instance.

helpmefindaname commented 1 year ago

Hi @nlproc-007 does tars.predict_zero_shot(sentence, classes, multi_label=False) work?

stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.