Browse Source

add test level

master
智丞 3 years ago
parent
commit
1009d54d22
3 changed files with 18 additions and 4 deletions
  1. +6
    -2
      tests/pipelines/test_nli.py
  2. +6
    -1
      tests/pipelines/test_sentiment_classification.py
  3. +6
    -1
      tests/pipelines/test_zero_shot_classification.py

+ 6
- 2
tests/pipelines/test_nli.py View File

@@ -7,6 +7,7 @@ from modelscope.models.nlp import SbertForNLI
from modelscope.pipelines import NLIPipeline, pipeline from modelscope.pipelines import NLIPipeline, pipeline
from modelscope.preprocessors import NLIPreprocessor from modelscope.preprocessors import NLIPreprocessor
from modelscope.utils.constant import Tasks from modelscope.utils.constant import Tasks
from modelscope.utils.test_utils import test_level




class NLITest(unittest.TestCase): class NLITest(unittest.TestCase):
@@ -14,8 +15,8 @@ class NLITest(unittest.TestCase):
sentence1 = '四川商务职业学院和四川财经职业学院哪个好?' sentence1 = '四川商务职业学院和四川财经职业学院哪个好?'
sentence2 = '四川商务职业学院商务管理在哪个校区?' sentence2 = '四川商务职业学院商务管理在哪个校区?'


@unittest.skip('skip temporarily to save test time')
def test_run_from_local(self):
@unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
def test_run_with_direct_file_download(self):
cache_path = snapshot_download(self.model_id) cache_path = snapshot_download(self.model_id)
tokenizer = NLIPreprocessor(cache_path) tokenizer = NLIPreprocessor(cache_path)
model = SbertForNLI(cache_path, tokenizer=tokenizer) model = SbertForNLI(cache_path, tokenizer=tokenizer)
@@ -28,6 +29,7 @@ class NLITest(unittest.TestCase):
f'sentence1: {self.sentence1}\nsentence2: {self.sentence2}\n' f'sentence1: {self.sentence1}\nsentence2: {self.sentence2}\n'
f'pipeline1: {pipeline2(input=(self.sentence1, self.sentence2))}') f'pipeline1: {pipeline2(input=(self.sentence1, self.sentence2))}')


@unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
def test_run_with_model_from_modelhub(self): def test_run_with_model_from_modelhub(self):
model = Model.from_pretrained(self.model_id) model = Model.from_pretrained(self.model_id)
tokenizer = NLIPreprocessor(model.model_dir) tokenizer = NLIPreprocessor(model.model_dir)
@@ -35,10 +37,12 @@ class NLITest(unittest.TestCase):
task=Tasks.nli, model=model, preprocessor=tokenizer) task=Tasks.nli, model=model, preprocessor=tokenizer)
print(pipeline_ins(input=(self.sentence1, self.sentence2))) print(pipeline_ins(input=(self.sentence1, self.sentence2)))


@unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
def test_run_with_model_name(self): def test_run_with_model_name(self):
pipeline_ins = pipeline(task=Tasks.nli, model=self.model_id) pipeline_ins = pipeline(task=Tasks.nli, model=self.model_id)
print(pipeline_ins(input=(self.sentence1, self.sentence2))) print(pipeline_ins(input=(self.sentence1, self.sentence2)))


@unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
def test_run_with_default_model(self): def test_run_with_default_model(self):
pipeline_ins = pipeline(task=Tasks.nli) pipeline_ins = pipeline(task=Tasks.nli)
print(pipeline_ins(input=(self.sentence1, self.sentence2))) print(pipeline_ins(input=(self.sentence1, self.sentence2)))


+ 6
- 1
tests/pipelines/test_sentiment_classification.py View File

@@ -7,13 +7,15 @@ from modelscope.models.nlp import SbertForSentimentClassification
from modelscope.pipelines import SentimentClassificationPipeline, pipeline from modelscope.pipelines import SentimentClassificationPipeline, pipeline
from modelscope.preprocessors import SentimentClassificationPreprocessor from modelscope.preprocessors import SentimentClassificationPreprocessor
from modelscope.utils.constant import Tasks from modelscope.utils.constant import Tasks
from modelscope.utils.test_utils import test_level




class SentimentClassificationTest(unittest.TestCase): class SentimentClassificationTest(unittest.TestCase):
model_id = 'damo/nlp_structbert_sentiment-classification_chinese-base' model_id = 'damo/nlp_structbert_sentiment-classification_chinese-base'
sentence1 = '启动的时候很大声音,然后就会听到1.2秒的卡察的声音,类似齿轮摩擦的声音' sentence1 = '启动的时候很大声音,然后就会听到1.2秒的卡察的声音,类似齿轮摩擦的声音'


def test_run_from_local(self):
@unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
def test_run_with_direct_file_download(self):
cache_path = snapshot_download(self.model_id) cache_path = snapshot_download(self.model_id)
tokenizer = SentimentClassificationPreprocessor(cache_path) tokenizer = SentimentClassificationPreprocessor(cache_path)
model = SbertForSentimentClassification( model = SbertForSentimentClassification(
@@ -30,6 +32,7 @@ class SentimentClassificationTest(unittest.TestCase):
print(f'sentence1: {self.sentence1}\n' print(f'sentence1: {self.sentence1}\n'
f'pipeline1: {pipeline2(input=self.sentence1)}') f'pipeline1: {pipeline2(input=self.sentence1)}')


@unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
def test_run_with_model_from_modelhub(self): def test_run_with_model_from_modelhub(self):
model = Model.from_pretrained(self.model_id) model = Model.from_pretrained(self.model_id)
tokenizer = SentimentClassificationPreprocessor(model.model_dir) tokenizer = SentimentClassificationPreprocessor(model.model_dir)
@@ -39,11 +42,13 @@ class SentimentClassificationTest(unittest.TestCase):
preprocessor=tokenizer) preprocessor=tokenizer)
print(pipeline_ins(input=self.sentence1)) print(pipeline_ins(input=self.sentence1))


@unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
def test_run_with_model_name(self): def test_run_with_model_name(self):
pipeline_ins = pipeline( pipeline_ins = pipeline(
task=Tasks.sentiment_classification, model=self.model_id) task=Tasks.sentiment_classification, model=self.model_id)
print(pipeline_ins(input=self.sentence1)) print(pipeline_ins(input=self.sentence1))


@unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
def test_run_with_default_model(self): def test_run_with_default_model(self):
pipeline_ins = pipeline(task=Tasks.sentiment_classification) pipeline_ins = pipeline(task=Tasks.sentiment_classification)
print(pipeline_ins(input=self.sentence1)) print(pipeline_ins(input=self.sentence1))


+ 6
- 1
tests/pipelines/test_zero_shot_classification.py View File

@@ -7,6 +7,7 @@ from modelscope.models.nlp import SbertForZeroShotClassification
from modelscope.pipelines import ZeroShotClassificationPipeline, pipeline from modelscope.pipelines import ZeroShotClassificationPipeline, pipeline
from modelscope.preprocessors import ZeroShotClassificationPreprocessor from modelscope.preprocessors import ZeroShotClassificationPreprocessor
from modelscope.utils.constant import Tasks from modelscope.utils.constant import Tasks
from modelscope.utils.test_utils import test_level




class ZeroShotClassificationTest(unittest.TestCase): class ZeroShotClassificationTest(unittest.TestCase):
@@ -15,7 +16,8 @@ class ZeroShotClassificationTest(unittest.TestCase):
labels = ['文化', '体育', '娱乐', '财经', '家居', '汽车', '教育', '科技', '军事'] labels = ['文化', '体育', '娱乐', '财经', '家居', '汽车', '教育', '科技', '军事']
template = '这篇文章的标题是{}' template = '这篇文章的标题是{}'


def test_run_from_local(self):
@unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
def test_run_with_direct_file_download(self):
cache_path = snapshot_download(self.model_id) cache_path = snapshot_download(self.model_id)
tokenizer = ZeroShotClassificationPreprocessor(cache_path) tokenizer = ZeroShotClassificationPreprocessor(cache_path)
model = SbertForZeroShotClassification(cache_path, tokenizer=tokenizer) model = SbertForZeroShotClassification(cache_path, tokenizer=tokenizer)
@@ -36,6 +38,7 @@ class ZeroShotClassificationTest(unittest.TestCase):
f'pipeline2: {pipeline2(self.sentence,candidate_labels=self.labels,hypothesis_template=self.template)}' f'pipeline2: {pipeline2(self.sentence,candidate_labels=self.labels,hypothesis_template=self.template)}'
) )


@unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
def test_run_with_model_from_modelhub(self): def test_run_with_model_from_modelhub(self):
model = Model.from_pretrained(self.model_id) model = Model.from_pretrained(self.model_id)
tokenizer = ZeroShotClassificationPreprocessor(model.model_dir) tokenizer = ZeroShotClassificationPreprocessor(model.model_dir)
@@ -45,11 +48,13 @@ class ZeroShotClassificationTest(unittest.TestCase):
preprocessor=tokenizer) preprocessor=tokenizer)
print(pipeline_ins(input=self.sentence, candidate_labels=self.labels)) print(pipeline_ins(input=self.sentence, candidate_labels=self.labels))


@unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
def test_run_with_model_name(self): def test_run_with_model_name(self):
pipeline_ins = pipeline( pipeline_ins = pipeline(
task=Tasks.zero_shot_classification, model=self.model_id) task=Tasks.zero_shot_classification, model=self.model_id)
print(pipeline_ins(input=self.sentence, candidate_labels=self.labels)) print(pipeline_ins(input=self.sentence, candidate_labels=self.labels))


@unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
def test_run_with_default_model(self): def test_run_with_default_model(self):
pipeline_ins = pipeline(task=Tasks.zero_shot_classification) pipeline_ins = pipeline(task=Tasks.zero_shot_classification)
print(pipeline_ins(input=self.sentence, candidate_labels=self.labels)) print(pipeline_ins(input=self.sentence, candidate_labels=self.labels))


Loading…
Cancel
Save