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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # Copyright (c) Alibaba, Inc. and its affiliates.
  2. import shutil
  3. import unittest
  4. import zipfile
  5. from pathlib import Path
  6. from modelscope.fileio import File
  7. from modelscope.models import Model
  8. from modelscope.models.nlp import BertForSequenceClassification
  9. from modelscope.pipelines import SequenceClassificationPipeline, pipeline
  10. from modelscope.preprocessors import SequenceClassificationPreprocessor
  11. from modelscope.pydatasets import PyDataset
  12. from modelscope.utils.constant import Hubs, Tasks
  13. from modelscope.utils.test_utils import test_level
  14. class SequenceClassificationTest(unittest.TestCase):
  15. def setUp(self) -> None:
  16. self.model_id = 'damo/bert-base-sst2'
  17. def predict(self, pipeline_ins: SequenceClassificationPipeline):
  18. from easynlp.appzoo import load_dataset
  19. set = load_dataset('glue', 'sst2')
  20. data = set['test']['sentence'][:3]
  21. results = pipeline_ins(data[0])
  22. print(results)
  23. results = pipeline_ins(data[1])
  24. print(results)
  25. print(data)
  26. def printDataset(self, dataset: PyDataset):
  27. for i, r in enumerate(dataset):
  28. if i > 10:
  29. break
  30. print(r)
  31. @unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
  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. self.predict(pipeline_ins)
  41. @unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
  42. def test_run_with_model_name(self):
  43. text_classification = pipeline(
  44. task=Tasks.text_classification, model=self.model_id)
  45. result = text_classification(
  46. PyDataset.load(
  47. 'glue',
  48. subset_name='sst2',
  49. split='train',
  50. target='sentence',
  51. hub=Hubs.huggingface))
  52. self.printDataset(result)
  53. @unittest.skipUnless(test_level() >= 1, 'skip test in current test level')
  54. def test_run_with_default_model(self):
  55. text_classification = pipeline(task=Tasks.text_classification)
  56. result = text_classification(
  57. PyDataset.load(
  58. 'glue',
  59. subset_name='sst2',
  60. split='train',
  61. target='sentence',
  62. hub=Hubs.huggingface))
  63. self.printDataset(result)
  64. @unittest.skipUnless(test_level() >= 1, 'skip test in current test level')
  65. def test_run_with_dataset(self):
  66. model = Model.from_pretrained(self.model_id)
  67. preprocessor = SequenceClassificationPreprocessor(
  68. model.model_dir, first_sequence='sentence', second_sequence=None)
  69. text_classification = pipeline(
  70. Tasks.text_classification, model=model, preprocessor=preprocessor)
  71. # loaded from huggingface dataset
  72. dataset = PyDataset.load(
  73. 'glue',
  74. subset_name='sst2',
  75. split='train',
  76. target='sentence',
  77. hub=Hubs.huggingface)
  78. result = text_classification(dataset)
  79. self.printDataset(result)
  80. @unittest.skipUnless(test_level() >= 1, 'skip test in current test level')
  81. def test_run_with_modelscope_dataset(self):
  82. text_classification = pipeline(task=Tasks.text_classification)
  83. # loaded from modelscope dataset
  84. dataset = PyDataset.load(
  85. 'squad', split='train', target='context', hub=Hubs.modelscope)
  86. result = text_classification(dataset)
  87. self.printDataset(result)
  88. if __name__ == '__main__':
  89. unittest.main()