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_sentiment_classification.py 2.8 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # Copyright (c) Alibaba, Inc. and its affiliates.
  2. import unittest
  3. from modelscope.hub.snapshot_download import snapshot_download
  4. from modelscope.models import Model
  5. from modelscope.models.nlp import (SbertForSentimentClassification,
  6. SequenceClassificationModel)
  7. from modelscope.pipelines import SentimentClassificationPipeline, pipeline
  8. from modelscope.preprocessors import SentimentClassificationPreprocessor
  9. from modelscope.utils.constant import Tasks
  10. from modelscope.utils.test_utils import test_level
  11. class SentimentClassificationTest(unittest.TestCase):
  12. model_id = 'damo/nlp_structbert_sentiment-classification_chinese-base'
  13. sentence1 = '启动的时候很大声音,然后就会听到1.2秒的卡察的声音,类似齿轮摩擦的声音'
  14. @unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
  15. def test_run_with_direct_file_download(self):
  16. cache_path = snapshot_download(self.model_id)
  17. tokenizer = SentimentClassificationPreprocessor(cache_path)
  18. model = SequenceClassificationModel.from_pretrained(
  19. self.model_id, num_labels=2)
  20. pipeline1 = SentimentClassificationPipeline(
  21. model, preprocessor=tokenizer)
  22. pipeline2 = pipeline(
  23. Tasks.sentiment_classification,
  24. model=model,
  25. preprocessor=tokenizer,
  26. model_revision='beta')
  27. print(f'sentence1: {self.sentence1}\n'
  28. f'pipeline1:{pipeline1(input=self.sentence1)}')
  29. print()
  30. print(f'sentence1: {self.sentence1}\n'
  31. f'pipeline1: {pipeline2(input=self.sentence1)}')
  32. @unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
  33. def test_run_with_model_from_modelhub(self):
  34. model = Model.from_pretrained(self.model_id)
  35. tokenizer = SentimentClassificationPreprocessor(model.model_dir)
  36. pipeline_ins = pipeline(
  37. task=Tasks.sentiment_classification,
  38. model=model,
  39. preprocessor=tokenizer,
  40. model_revision='beta')
  41. print(pipeline_ins(input=self.sentence1))
  42. @unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
  43. def test_run_with_model_name(self):
  44. pipeline_ins = pipeline(
  45. task=Tasks.sentiment_classification,
  46. model=self.model_id,
  47. model_revision='beta')
  48. print(pipeline_ins(input=self.sentence1))
  49. @unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
  50. def test_run_with_default_model(self):
  51. pipeline_ins = pipeline(
  52. task=Tasks.sentiment_classification, model_revision='beta')
  53. print(pipeline_ins(input=self.sentence1))
  54. if __name__ == '__main__':
  55. unittest.main()