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.8 kB

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