You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_zero_shot_classification.py 2.9 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # Copyright (c) Alibaba, Inc. and its affiliates.
  2. import unittest
  3. from modelscope.hub.snapshot_download import snapshot_download
  4. from modelscope.models import Model
  5. from modelscope.models.nlp import SbertForZeroShotClassification
  6. from modelscope.pipelines import pipeline
  7. from modelscope.pipelines.nlp import ZeroShotClassificationPipeline
  8. from modelscope.preprocessors import ZeroShotClassificationPreprocessor
  9. from modelscope.utils.constant import Tasks
  10. from modelscope.utils.test_utils import test_level
  11. class ZeroShotClassificationTest(unittest.TestCase):
  12. model_id = 'damo/nlp_structbert_zero-shot-classification_chinese-base'
  13. sentence = '全新突破 解放军运20版空中加油机曝光'
  14. labels = ['文化', '体育', '娱乐', '财经', '家居', '汽车', '教育', '科技', '军事']
  15. template = '这篇文章的标题是{}'
  16. @unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
  17. def test_run_with_direct_file_download(self):
  18. cache_path = snapshot_download(self.model_id)
  19. tokenizer = ZeroShotClassificationPreprocessor(cache_path)
  20. model = SbertForZeroShotClassification(cache_path, tokenizer=tokenizer)
  21. pipeline1 = ZeroShotClassificationPipeline(
  22. model, preprocessor=tokenizer)
  23. pipeline2 = pipeline(
  24. Tasks.zero_shot_classification,
  25. model=model,
  26. preprocessor=tokenizer)
  27. print(
  28. f'sentence: {self.sentence}\n'
  29. f'pipeline1:{pipeline1(input=self.sentence,candidate_labels=self.labels)}'
  30. )
  31. print()
  32. print(
  33. f'sentence: {self.sentence}\n'
  34. f'pipeline2: {pipeline2(self.sentence,candidate_labels=self.labels,hypothesis_template=self.template)}'
  35. )
  36. @unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
  37. def test_run_with_model_from_modelhub(self):
  38. model = Model.from_pretrained(self.model_id)
  39. tokenizer = ZeroShotClassificationPreprocessor(model.model_dir)
  40. pipeline_ins = pipeline(
  41. task=Tasks.zero_shot_classification,
  42. model=model,
  43. preprocessor=tokenizer)
  44. print(pipeline_ins(input=self.sentence, candidate_labels=self.labels))
  45. @unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
  46. def test_run_with_model_name(self):
  47. pipeline_ins = pipeline(
  48. task=Tasks.zero_shot_classification, model=self.model_id)
  49. print(pipeline_ins(input=self.sentence, candidate_labels=self.labels))
  50. @unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
  51. def test_run_with_default_model(self):
  52. pipeline_ins = pipeline(task=Tasks.zero_shot_classification)
  53. print(pipeline_ins(input=self.sentence, candidate_labels=self.labels))
  54. if __name__ == '__main__':
  55. unittest.main()