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

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