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

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