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_text_classification.py 3.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. # Copyright (c) Alibaba, Inc. and its affiliates.
  2. import unittest
  3. from modelscope.models import Model
  4. from modelscope.msdatasets import MsDataset
  5. from modelscope.pipelines import pipeline
  6. from modelscope.pipelines.nlp import SequenceClassificationPipeline
  7. from modelscope.preprocessors import SequenceClassificationPreprocessor
  8. from modelscope.utils.constant import Tasks
  9. from modelscope.utils.demo_utils import DemoCompatibilityCheck
  10. from modelscope.utils.test_utils import test_level
  11. class SequenceClassificationTest(unittest.TestCase, DemoCompatibilityCheck):
  12. sentence1 = 'i like this wonderful place'
  13. def setUp(self) -> None:
  14. self.model_id = 'damo/bert-base-sst2'
  15. self.task = Tasks.text_classification
  16. def predict(self, pipeline_ins: SequenceClassificationPipeline):
  17. from easynlp.appzoo import load_dataset
  18. set = load_dataset('glue', 'sst2')
  19. data = set['test']['sentence'][:3]
  20. results = pipeline_ins(data[0])
  21. print(results)
  22. results = pipeline_ins(data[1])
  23. print(results)
  24. print(data)
  25. def printDataset(self, dataset: MsDataset):
  26. for i, r in enumerate(dataset):
  27. if i > 10:
  28. break
  29. print(r)
  30. # @unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
  31. @unittest.skip('nlp model does not support tensor input, skipped')
  32. def test_run_with_model_from_modelhub(self):
  33. model = Model.from_pretrained(self.model_id)
  34. preprocessor = SequenceClassificationPreprocessor(
  35. model.model_dir, first_sequence='sentence', second_sequence=None)
  36. pipeline_ins = pipeline(
  37. task=Tasks.text_classification,
  38. model=model,
  39. preprocessor=preprocessor)
  40. print(f'sentence1: {self.sentence1}\n'
  41. f'pipeline1:{pipeline_ins(input=self.sentence1)}')
  42. # @unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
  43. @unittest.skip('nlp model does not support tensor input, skipped')
  44. def test_run_with_model_name(self):
  45. text_classification = pipeline(
  46. task=Tasks.text_classification, model=self.model_id)
  47. result = text_classification(
  48. MsDataset.load(
  49. 'xcopa',
  50. subset_name='translation-et',
  51. namespace='damotest',
  52. split='test',
  53. target='premise'))
  54. self.printDataset(result)
  55. # @unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
  56. @unittest.skip('nlp model does not support tensor input, skipped')
  57. def test_run_with_default_model(self):
  58. text_classification = pipeline(task=Tasks.text_classification)
  59. result = text_classification(
  60. MsDataset.load(
  61. 'xcopa',
  62. subset_name='translation-et',
  63. namespace='damotest',
  64. split='test',
  65. target='premise'))
  66. self.printDataset(result)
  67. # @unittest.skipUnless(test_level() >= 1, 'skip test in current test level')
  68. @unittest.skip('nlp model does not support tensor input, skipped')
  69. def test_run_with_modelscope_dataset(self):
  70. text_classification = pipeline(task=Tasks.text_classification)
  71. # loaded from modelscope dataset
  72. dataset = MsDataset.load(
  73. 'xcopa',
  74. subset_name='translation-et',
  75. namespace='damotest',
  76. split='test',
  77. target='premise')
  78. result = text_classification(dataset)
  79. self.printDataset(result)
  80. @unittest.skip('demo compatibility test is only enabled on a needed-basis')
  81. def test_demo_compatibility(self):
  82. self.compatibility_check()
  83. if __name__ == '__main__':
  84. unittest.main()