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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 Hubs, Tasks
  9. from modelscope.utils.test_utils import test_level
  10. class SequenceClassificationTest(unittest.TestCase):
  11. def setUp(self) -> None:
  12. self.model_id = 'damo/bert-base-sst2'
  13. def predict(self, pipeline_ins: SequenceClassificationPipeline):
  14. from easynlp.appzoo import load_dataset
  15. set = load_dataset('glue', 'sst2')
  16. data = set['test']['sentence'][:3]
  17. results = pipeline_ins(data[0])
  18. print(results)
  19. results = pipeline_ins(data[1])
  20. print(results)
  21. print(data)
  22. def printDataset(self, dataset: MsDataset):
  23. for i, r in enumerate(dataset):
  24. if i > 10:
  25. break
  26. print(r)
  27. # @unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
  28. @unittest.skip('nlp model does not support tensor input, skipped')
  29. def test_run_with_model_from_modelhub(self):
  30. model = Model.from_pretrained(self.model_id)
  31. preprocessor = SequenceClassificationPreprocessor(
  32. model.model_dir, first_sequence='sentence', second_sequence=None)
  33. pipeline_ins = pipeline(
  34. task=Tasks.text_classification,
  35. model=model,
  36. preprocessor=preprocessor)
  37. self.predict(pipeline_ins)
  38. # @unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
  39. @unittest.skip('nlp model does not support tensor input, skipped')
  40. def test_run_with_model_name(self):
  41. text_classification = pipeline(
  42. task=Tasks.text_classification, model=self.model_id)
  43. result = text_classification(
  44. MsDataset.load(
  45. 'xcopa',
  46. subset_name='translation-et',
  47. namespace='damotest',
  48. split='test',
  49. target='premise'))
  50. self.printDataset(result)
  51. # @unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
  52. @unittest.skip('nlp model does not support tensor input, skipped')
  53. def test_run_with_default_model(self):
  54. text_classification = pipeline(task=Tasks.text_classification)
  55. result = text_classification(
  56. MsDataset.load(
  57. 'xcopa',
  58. subset_name='translation-et',
  59. namespace='damotest',
  60. split='test',
  61. target='premise'))
  62. self.printDataset(result)
  63. # @unittest.skipUnless(test_level() >= 1, 'skip test in current test level')
  64. @unittest.skip('nlp model does not support tensor input, skipped')
  65. def test_run_with_modelscope_dataset(self):
  66. text_classification = pipeline(task=Tasks.text_classification)
  67. # loaded from modelscope dataset
  68. dataset = MsDataset.load(
  69. 'xcopa',
  70. subset_name='translation-et',
  71. namespace='damotest',
  72. split='test',
  73. target='premise')
  74. result = text_classification(dataset)
  75. self.printDataset(result)
  76. if __name__ == '__main__':
  77. unittest.main()