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_relation_extraction.py 2.7 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 InformationExtractionModel
  6. from modelscope.pipelines import pipeline
  7. from modelscope.pipelines.nlp import InformationExtractionPipeline
  8. from modelscope.preprocessors import RelationExtractionPreprocessor
  9. from modelscope.utils.constant import Tasks
  10. from modelscope.utils.demo_utils import DemoCompatibilityCheck
  11. from modelscope.utils.test_utils import test_level
  12. class RelationExtractionTest(unittest.TestCase, DemoCompatibilityCheck):
  13. def setUp(self) -> None:
  14. self.task = Tasks.relation_extraction
  15. self.model_id = 'damo/nlp_bert_relation-extraction_chinese-base'
  16. sentence = '高捷,祖籍江苏,本科毕业于东南大学'
  17. @unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
  18. def test_run_by_direct_model_download(self):
  19. cache_path = snapshot_download(self.model_id)
  20. tokenizer = RelationExtractionPreprocessor(cache_path)
  21. model = InformationExtractionModel.from_pretrained(cache_path)
  22. pipeline1 = InformationExtractionPipeline(
  23. model, preprocessor=tokenizer)
  24. pipeline2 = pipeline(
  25. Tasks.relation_extraction, model=model, preprocessor=tokenizer)
  26. print(f'sentence: {self.sentence}\n'
  27. f'pipeline1:{pipeline1(input=self.sentence)}')
  28. print()
  29. print(f'pipeline2: {pipeline2(input=self.sentence)}')
  30. @unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
  31. def test_run_with_model_from_modelhub(self):
  32. model = Model.from_pretrained(self.model_id)
  33. tokenizer = RelationExtractionPreprocessor(model.model_dir)
  34. pipeline_ins = pipeline(
  35. task=Tasks.relation_extraction,
  36. model=model,
  37. preprocessor=tokenizer)
  38. print(pipeline_ins(input=self.sentence))
  39. @unittest.skipUnless(test_level() >= 1, 'skip test in current test level')
  40. def test_run_with_model_name(self):
  41. pipeline_ins = pipeline(
  42. task=Tasks.relation_extraction, model=self.model_id)
  43. print(pipeline_ins(input=self.sentence))
  44. @unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
  45. def test_run_with_default_model(self):
  46. pipeline_ins = pipeline(task=Tasks.relation_extraction)
  47. print(pipeline_ins(input=self.sentence))
  48. @unittest.skip('demo compatibility test is only enabled on a needed-basis')
  49. def test_demo_compatibility(self):
  50. self.compatibility_check()
  51. if __name__ == '__main__':
  52. unittest.main()