|
| 1 | +import pytest |
| 2 | + |
| 3 | +from flair.data import Sentence |
| 4 | +from flair.datasets import ClassificationCorpus |
| 5 | +from flair.embeddings import TransformerDocumentEmbeddings |
| 6 | +from flair.models import TARSClassifier |
| 7 | +from tests.model_test_utils import BaseModelTest |
| 8 | + |
| 9 | + |
| 10 | +class TestTarsClassifier(BaseModelTest): |
| 11 | + model_cls = TARSClassifier |
| 12 | + train_label_type = "class" |
| 13 | + model_args = dict(task_name="2_CLASS") |
| 14 | + training_args = dict(mini_batch_size=1, max_epochs=2) |
| 15 | + pretrained_model = "tars-base" |
| 16 | + |
| 17 | + @pytest.fixture |
| 18 | + def corpus(self, tasks_base_path): |
| 19 | + yield ClassificationCorpus(tasks_base_path / "imdb_underscore") |
| 20 | + |
| 21 | + @pytest.fixture |
| 22 | + def embeddings(self): |
| 23 | + yield TransformerDocumentEmbeddings("distilbert-base-uncased") |
| 24 | + |
| 25 | + @pytest.fixture |
| 26 | + def example_sentence(self): |
| 27 | + yield Sentence("This is great!") |
| 28 | + |
| 29 | + def build_model(self, embeddings, label_dict, **kwargs): |
| 30 | + model_args = dict(self.model_args) |
| 31 | + for k in kwargs.keys(): |
| 32 | + if k in model_args: |
| 33 | + del model_args[k] |
| 34 | + return self.model_cls( |
| 35 | + embeddings=embeddings, |
| 36 | + label_type=self.train_label_type, |
| 37 | + **model_args, |
| 38 | + **kwargs, |
| 39 | + ) |
| 40 | + |
| 41 | + def transform_corpus(self, model, corpus): |
| 42 | + model.add_and_switch_to_new_task( |
| 43 | + task_name="2_CLASS", |
| 44 | + label_dictionary=corpus.make_label_dictionary(self.train_label_type), |
| 45 | + label_type=self.train_label_type, |
| 46 | + ) |
| 47 | + return corpus |
| 48 | + |
| 49 | + @pytest.mark.integration |
| 50 | + def test_predict_zero_shot(self, loaded_pretrained_model): |
| 51 | + sentence = Sentence("I am so glad you liked it!") |
| 52 | + loaded_pretrained_model.predict_zero_shot(sentence, ["happy", "sad"]) |
| 53 | + assert len(sentence.get_labels(loaded_pretrained_model.label_type)) == 1 |
| 54 | + assert sentence.get_labels(loaded_pretrained_model.label_type)[0].value == "happy" |
| 55 | + |
| 56 | + @pytest.mark.integration |
| 57 | + def test_predict_zero_shot_single_label_always_predicts(self, loaded_pretrained_model): |
| 58 | + sentence = Sentence("I hate it") |
| 59 | + loaded_pretrained_model.predict_zero_shot(sentence, ["happy", "sad"]) |
| 60 | + # Ensure this is an example that predicts no classes in multilabel |
| 61 | + assert len(sentence.get_labels(loaded_pretrained_model.label_type)) == 0 |
| 62 | + loaded_pretrained_model.predict_zero_shot(sentence, ["happy", "sad"], multi_label=False) |
| 63 | + assert len(sentence.get_labels(loaded_pretrained_model.label_type)) == 1 |
| 64 | + assert sentence.get_labels(loaded_pretrained_model.label_type)[0].value == "sad" |
| 65 | + |
| 66 | + @pytest.mark.integration |
| 67 | + def test_init_tars_and_switch(self, tasks_base_path, corpus): |
| 68 | + tars = TARSClassifier( |
| 69 | + task_name="2_CLASS", |
| 70 | + label_dictionary=corpus.make_label_dictionary(label_type="class"), |
| 71 | + label_type="class", |
| 72 | + ) |
| 73 | + |
| 74 | + # check if right number of classes |
| 75 | + assert len(tars.get_current_label_dictionary()) == 2 |
| 76 | + |
| 77 | + # switch to task with only one label |
| 78 | + tars.add_and_switch_to_new_task("1_CLASS", "one class", "testlabel") |
| 79 | + |
| 80 | + # check if right number of classes |
| 81 | + assert len(tars.get_current_label_dictionary()) == 1 |
| 82 | + |
| 83 | + # switch to task with three labels provided as list |
| 84 | + tars.add_and_switch_to_new_task("3_CLASS", ["list 1", "list 2", "list 3"], "testlabel") |
| 85 | + |
| 86 | + # check if right number of classes |
| 87 | + assert len(tars.get_current_label_dictionary()) == 3 |
| 88 | + |
| 89 | + # switch to task with four labels provided as set |
| 90 | + tars.add_and_switch_to_new_task("4_CLASS", {"set 1", "set 2", "set 3", "set 4"}, "testlabel") |
| 91 | + |
| 92 | + # check if right number of classes |
| 93 | + assert len(tars.get_current_label_dictionary()) == 4 |
| 94 | + |
| 95 | + # switch to task with two labels provided as Dictionary |
| 96 | + tars.add_and_switch_to_new_task("2_CLASS_AGAIN", corpus.make_label_dictionary(label_type="class"), "testlabel") |
| 97 | + |
| 98 | + # check if right number of classes |
| 99 | + assert len(tars.get_current_label_dictionary()) == 2 |
| 100 | + |
| 101 | + @pytest.mark.skip("embeddings are not supported in tars") |
| 102 | + def test_load_use_model_keep_embedding(self): |
| 103 | + pass |
| 104 | + |
| 105 | + @pytest.mark.skip("tars needs additional setup after loading") |
| 106 | + def test_load_use_model(self): |
| 107 | + pass |
0 commit comments