|
|
@@ -3,7 +3,8 @@ import unittest |
|
|
|
|
|
|
|
|
from modelscope.hub.snapshot_download import snapshot_download |
|
|
from modelscope.hub.snapshot_download import snapshot_download |
|
|
from modelscope.models import Model |
|
|
from modelscope.models import Model |
|
|
from modelscope.models.nlp import SbertForSequenceClassification |
|
|
|
|
|
|
|
|
from modelscope.models.nlp.task_models.sequence_classification import \ |
|
|
|
|
|
SequenceClassificationModel |
|
|
from modelscope.pipelines import pipeline |
|
|
from modelscope.pipelines import pipeline |
|
|
from modelscope.pipelines.nlp import SingleSentenceClassificationPipeline |
|
|
from modelscope.pipelines.nlp import SingleSentenceClassificationPipeline |
|
|
from modelscope.preprocessors import SingleSentenceClassificationPreprocessor |
|
|
from modelscope.preprocessors import SingleSentenceClassificationPreprocessor |
|
|
@@ -11,15 +12,15 @@ from modelscope.utils.constant import Tasks |
|
|
from modelscope.utils.test_utils import test_level |
|
|
from modelscope.utils.test_utils import test_level |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SentimentClassificationTest(unittest.TestCase): |
|
|
|
|
|
model_id = 'damo/nlp_structbert_sentiment-classification_chinese-tiny' |
|
|
|
|
|
|
|
|
class SentimentClassificationTaskModelTest(unittest.TestCase): |
|
|
|
|
|
model_id = 'damo/nlp_structbert_sentiment-classification_chinese-base' |
|
|
sentence1 = '启动的时候很大声音,然后就会听到1.2秒的卡察的声音,类似齿轮摩擦的声音' |
|
|
sentence1 = '启动的时候很大声音,然后就会听到1.2秒的卡察的声音,类似齿轮摩擦的声音' |
|
|
|
|
|
|
|
|
@unittest.skipUnless(test_level() >= 2, 'skip test in current test level') |
|
|
@unittest.skipUnless(test_level() >= 2, 'skip test in current test level') |
|
|
def test_run_with_direct_file_download(self): |
|
|
def test_run_with_direct_file_download(self): |
|
|
cache_path = snapshot_download(self.model_id) |
|
|
cache_path = snapshot_download(self.model_id) |
|
|
tokenizer = SingleSentenceClassificationPreprocessor(cache_path) |
|
|
tokenizer = SingleSentenceClassificationPreprocessor(cache_path) |
|
|
model = SbertForSequenceClassification.from_pretrained( |
|
|
|
|
|
|
|
|
model = SequenceClassificationModel.from_pretrained( |
|
|
self.model_id, num_labels=2) |
|
|
self.model_id, num_labels=2) |
|
|
pipeline1 = SingleSentenceClassificationPipeline( |
|
|
pipeline1 = SingleSentenceClassificationPipeline( |
|
|
model, preprocessor=tokenizer) |
|
|
model, preprocessor=tokenizer) |
|
|
@@ -32,10 +33,6 @@ class SentimentClassificationTest(unittest.TestCase): |
|
|
print() |
|
|
print() |
|
|
print(f'sentence1: {self.sentence1}\n' |
|
|
print(f'sentence1: {self.sentence1}\n' |
|
|
f'pipeline1: {pipeline2(input=self.sentence1)}') |
|
|
f'pipeline1: {pipeline2(input=self.sentence1)}') |
|
|
self.assertTrue( |
|
|
|
|
|
isinstance(pipeline1.model, SbertForSequenceClassification)) |
|
|
|
|
|
self.assertTrue( |
|
|
|
|
|
isinstance(pipeline2.model, SbertForSequenceClassification)) |
|
|
|
|
|
|
|
|
|
|
|
@unittest.skipUnless(test_level() >= 2, 'skip test in current test level') |
|
|
@unittest.skipUnless(test_level() >= 2, 'skip test in current test level') |
|
|
def test_run_with_model_from_modelhub(self): |
|
|
def test_run_with_model_from_modelhub(self): |
|
|
@@ -47,23 +44,22 @@ class SentimentClassificationTest(unittest.TestCase): |
|
|
preprocessor=tokenizer) |
|
|
preprocessor=tokenizer) |
|
|
print(pipeline_ins(input=self.sentence1)) |
|
|
print(pipeline_ins(input=self.sentence1)) |
|
|
self.assertTrue( |
|
|
self.assertTrue( |
|
|
isinstance(pipeline_ins.model, SbertForSequenceClassification)) |
|
|
|
|
|
|
|
|
isinstance(pipeline_ins.model, SequenceClassificationModel)) |
|
|
|
|
|
|
|
|
@unittest.skipUnless(test_level() >= 0, 'skip test in current test level') |
|
|
@unittest.skipUnless(test_level() >= 0, 'skip test in current test level') |
|
|
def test_run_with_model_name(self): |
|
|
def test_run_with_model_name(self): |
|
|
pipeline_ins = pipeline( |
|
|
pipeline_ins = pipeline( |
|
|
task=Tasks.sentiment_classification, model=self.model_id) |
|
|
task=Tasks.sentiment_classification, model=self.model_id) |
|
|
print(pipeline_ins(input=self.sentence1)) |
|
|
print(pipeline_ins(input=self.sentence1)) |
|
|
print(pipeline_ins.model.__class__) |
|
|
|
|
|
self.assertTrue( |
|
|
self.assertTrue( |
|
|
isinstance(pipeline_ins.model, SbertForSequenceClassification)) |
|
|
|
|
|
|
|
|
isinstance(pipeline_ins.model, SequenceClassificationModel)) |
|
|
|
|
|
|
|
|
@unittest.skipUnless(test_level() >= 2, 'skip test in current test level') |
|
|
|
|
|
|
|
|
@unittest.skipUnless(test_level() >= 0, 'skip test in current test level') |
|
|
def test_run_with_default_model(self): |
|
|
def test_run_with_default_model(self): |
|
|
pipeline_ins = pipeline(task=Tasks.sentiment_classification) |
|
|
pipeline_ins = pipeline(task=Tasks.sentiment_classification) |
|
|
print(pipeline_ins(input=self.sentence1)) |
|
|
print(pipeline_ins(input=self.sentence1)) |
|
|
self.assertTrue( |
|
|
self.assertTrue( |
|
|
isinstance(pipeline_ins.model, SbertForSequenceClassification)) |
|
|
|
|
|
|
|
|
isinstance(pipeline_ins.model, SequenceClassificationModel)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
if __name__ == '__main__': |
|
|
|