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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 SequenceClassificationPipeline, pipeline
  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. def test_run_with_model_from_modelhub(self):
  29. model = Model.from_pretrained(self.model_id)
  30. preprocessor = SequenceClassificationPreprocessor(
  31. model.model_dir, first_sequence='sentence', second_sequence=None)
  32. pipeline_ins = pipeline(
  33. task=Tasks.text_classification,
  34. model=model,
  35. preprocessor=preprocessor)
  36. self.predict(pipeline_ins)
  37. @unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
  38. def test_run_with_model_name(self):
  39. text_classification = pipeline(
  40. task=Tasks.text_classification, model=self.model_id)
  41. result = text_classification(
  42. MsDataset.load(
  43. 'glue',
  44. subset_name='sst2',
  45. split='train',
  46. target='sentence',
  47. hub=Hubs.huggingface))
  48. self.printDataset(result)
  49. @unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
  50. def test_run_with_default_model(self):
  51. text_classification = pipeline(task=Tasks.text_classification)
  52. result = text_classification(
  53. MsDataset.load(
  54. 'glue',
  55. subset_name='sst2',
  56. split='train',
  57. target='sentence',
  58. hub=Hubs.huggingface))
  59. self.printDataset(result)
  60. @unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
  61. def test_run_with_dataset(self):
  62. model = Model.from_pretrained(self.model_id)
  63. preprocessor = SequenceClassificationPreprocessor(
  64. model.model_dir, first_sequence='sentence', second_sequence=None)
  65. text_classification = pipeline(
  66. Tasks.text_classification, model=model, preprocessor=preprocessor)
  67. # loaded from huggingface dataset
  68. dataset = MsDataset.load(
  69. 'glue',
  70. subset_name='sst2',
  71. split='train',
  72. target='sentence',
  73. hub=Hubs.huggingface)
  74. result = text_classification(dataset)
  75. self.printDataset(result)
  76. @unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
  77. def test_run_with_modelscope_dataset(self):
  78. text_classification = pipeline(task=Tasks.text_classification)
  79. # loaded from modelscope dataset
  80. dataset = MsDataset.load(
  81. 'squad', split='train', target='context', hub=Hubs.modelscope)
  82. result = text_classification(dataset)
  83. self.printDataset(result)
  84. if __name__ == '__main__':
  85. unittest.main()