2022-06-10 10:17:27 +08:00
|
|
|
# Copyright (c) Alibaba, Inc. and its affiliates.
|
|
|
|
|
import unittest
|
|
|
|
|
|
2022-06-22 20:14:03 +08:00
|
|
|
from modelscope.hub.snapshot_download import snapshot_download
|
2022-06-12 23:27:26 +08:00
|
|
|
from modelscope.models import Model
|
2022-06-30 11:17:38 +08:00
|
|
|
from modelscope.models.nlp import SpaceForDialogIntent
|
2022-07-27 17:29:16 +08:00
|
|
|
from modelscope.pipelines import pipeline
|
|
|
|
|
from modelscope.pipelines.nlp import DialogIntentPredictionPipeline
|
2022-06-17 14:04:28 +08:00
|
|
|
from modelscope.preprocessors import DialogIntentPredictionPreprocessor
|
2022-06-12 14:55:32 +08:00
|
|
|
from modelscope.utils.constant import Tasks
|
2022-09-08 14:08:51 +08:00
|
|
|
from modelscope.utils.demo_utils import DemoCompatibilityCheck
|
2022-06-30 11:17:38 +08:00
|
|
|
from modelscope.utils.test_utils import test_level
|
2022-06-10 10:17:27 +08:00
|
|
|
|
|
|
|
|
|
2022-09-08 14:08:51 +08:00
|
|
|
class DialogIntentPredictionTest(unittest.TestCase, DemoCompatibilityCheck):
|
|
|
|
|
|
|
|
|
|
def setUp(self) -> None:
|
|
|
|
|
self.task = Tasks.task_oriented_conversation
|
|
|
|
|
self.model_id = 'damo/nlp_space_dialog-intent-prediction'
|
|
|
|
|
|
2022-06-12 23:27:26 +08:00
|
|
|
test_case = [
|
|
|
|
|
'How do I locate my card?',
|
|
|
|
|
'I still have not received my new card, I ordered over a week ago.'
|
|
|
|
|
]
|
2022-06-10 10:17:27 +08:00
|
|
|
|
2022-06-30 11:17:38 +08:00
|
|
|
@unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
|
2022-07-04 13:48:54 +08:00
|
|
|
def test_run_by_direct_model_download(self):
|
2022-08-10 14:27:47 +08:00
|
|
|
cache_path = snapshot_download(self.model_id, revision='update')
|
2022-06-17 14:04:28 +08:00
|
|
|
preprocessor = DialogIntentPredictionPreprocessor(model_dir=cache_path)
|
2022-06-30 11:17:38 +08:00
|
|
|
model = SpaceForDialogIntent(
|
2022-06-12 23:27:26 +08:00
|
|
|
model_dir=cache_path,
|
2022-06-10 10:17:27 +08:00
|
|
|
text_field=preprocessor.text_field,
|
|
|
|
|
config=preprocessor.config)
|
2022-06-12 14:14:26 +08:00
|
|
|
|
2022-06-12 23:27:26 +08:00
|
|
|
pipelines = [
|
2022-06-17 14:04:28 +08:00
|
|
|
DialogIntentPredictionPipeline(
|
|
|
|
|
model=model, preprocessor=preprocessor),
|
2022-06-12 23:27:26 +08:00
|
|
|
pipeline(
|
2022-08-10 14:27:47 +08:00
|
|
|
task=Tasks.task_oriented_conversation,
|
2022-06-12 23:27:26 +08:00
|
|
|
model=model,
|
|
|
|
|
preprocessor=preprocessor)
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
for my_pipeline, item in list(zip(pipelines, self.test_case)):
|
|
|
|
|
print(my_pipeline(item))
|
|
|
|
|
|
2022-06-30 11:17:38 +08:00
|
|
|
@unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
|
2022-06-12 23:27:26 +08:00
|
|
|
def test_run_with_model_from_modelhub(self):
|
2022-08-10 14:27:47 +08:00
|
|
|
model = Model.from_pretrained(self.model_id, revision='update')
|
2022-06-17 14:04:28 +08:00
|
|
|
preprocessor = DialogIntentPredictionPreprocessor(
|
|
|
|
|
model_dir=model.model_dir)
|
2022-06-12 23:27:26 +08:00
|
|
|
|
|
|
|
|
pipelines = [
|
2022-06-17 14:04:28 +08:00
|
|
|
DialogIntentPredictionPipeline(
|
|
|
|
|
model=model, preprocessor=preprocessor),
|
2022-06-12 23:27:26 +08:00
|
|
|
pipeline(
|
2022-08-10 14:27:47 +08:00
|
|
|
task=Tasks.task_oriented_conversation,
|
2022-06-12 23:27:26 +08:00
|
|
|
model=model,
|
|
|
|
|
preprocessor=preprocessor)
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
for my_pipeline, item in list(zip(pipelines, self.test_case)):
|
|
|
|
|
print(my_pipeline(item))
|
2022-06-10 10:17:27 +08:00
|
|
|
|
2022-07-04 13:48:54 +08:00
|
|
|
@unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
|
|
|
|
|
def test_run_with_model_name(self):
|
|
|
|
|
pipelines = [
|
2022-08-10 14:27:47 +08:00
|
|
|
pipeline(
|
2022-09-08 14:08:51 +08:00
|
|
|
task=self.task, model=self.model_id, model_revision='update')
|
2022-07-04 13:48:54 +08:00
|
|
|
]
|
|
|
|
|
for my_pipeline, item in list(zip(pipelines, self.test_case)):
|
|
|
|
|
print(my_pipeline(item))
|
|
|
|
|
|
2022-09-08 14:08:51 +08:00
|
|
|
@unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
|
|
|
|
|
def test_demo_compatibility(self):
|
|
|
|
|
self.compatibility_check()
|
|
|
|
|
|
2022-06-10 10:17:27 +08:00
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
unittest.main()
|