Browse Source

local gen ready

master
ly119399 3 years ago
parent
commit
caadf4a39d
12 changed files with 57 additions and 53 deletions
  1. +2
    -2
      modelscope/models/nlp/__init__.py
  2. +2
    -1
      modelscope/models/nlp/space/dialog_intent_prediction_model.py
  3. +3
    -4
      modelscope/models/nlp/space/dialog_modeling_model.py
  4. +2
    -2
      modelscope/pipelines/nlp/__init__.py
  5. +6
    -5
      modelscope/pipelines/nlp/space/dialog_intent_prediction_pipeline.py
  6. +7
    -7
      modelscope/pipelines/nlp/space/dialog_modeling_pipeline.py
  7. +2
    -2
      modelscope/preprocessors/__init__.py
  8. +3
    -3
      modelscope/preprocessors/space/dialog_intent_prediction_preprocessor.py
  9. +3
    -3
      modelscope/preprocessors/space/dialog_modeling_preprocessor.py
  10. +2
    -2
      modelscope/utils/constant.py
  11. +13
    -10
      tests/pipelines/nlp/test_dialog_intent_prediction.py
  12. +12
    -12
      tests/pipelines/nlp/test_dialog_modeling.py

+ 2
- 2
modelscope/models/nlp/__init__.py View File

@@ -1,4 +1,4 @@
from .sequence_classification_model import * # noqa F403
from .space.dialog_generation_model import * # noqa F403
from .space.dialog_intent_model import * # noqa F403
from .space.dialog_intent_prediction_model import * # noqa F403
from .space.dialog_modeling_model import * # noqa F403
from .text_generation_model import * # noqa F403

modelscope/models/nlp/space/dialog_intent_model.py → modelscope/models/nlp/space/dialog_intent_prediction_model.py View File

@@ -14,7 +14,8 @@ from .model.model_base import ModelBase
__all__ = ['DialogIntentModel']


@MODELS.register_module(Tasks.dialog_intent, module_name=r'space')
@MODELS.register_module(
Tasks.dialog_intent_prediction, module_name=r'space-intent')
class DialogIntentModel(Model):

def __init__(self, model_dir: str, *args, **kwargs):

modelscope/models/nlp/space/dialog_generation_model.py → modelscope/models/nlp/space/dialog_modeling_model.py View File

@@ -11,12 +11,11 @@ from ...builder import MODELS
from .model.generator import Generator
from .model.model_base import ModelBase

__all__ = ['DialogGenerationModel']
__all__ = ['DialogModelingModel']


@MODELS.register_module(
Tasks.dialog_generation, module_name=r'space-generation')
class DialogGenerationModel(Model):
@MODELS.register_module(Tasks.dialog_modeling, module_name=r'space-modeling')
class DialogModelingModel(Model):

def __init__(self, model_dir: str, *args, **kwargs):
"""initialize the test generation model from the `model_dir` path.

+ 2
- 2
modelscope/pipelines/nlp/__init__.py View File

@@ -1,4 +1,4 @@
from .sequence_classification_pipeline import * # noqa F403
from .space.dialog_generation_pipeline import * # noqa F403
from .space.dialog_intent_pipeline import * # noqa F403
from .space.dialog_intent_prediction_pipeline import * # noqa F403
from .space.dialog_modeling_pipeline import * # noqa F403
from .text_generation_pipeline import * # noqa F403

modelscope/pipelines/nlp/space/dialog_intent_pipeline.py → modelscope/pipelines/nlp/space/dialog_intent_prediction_pipeline.py View File

@@ -1,19 +1,20 @@
from typing import Any, Dict, Optional

from modelscope.models.nlp import DialogIntentModel
from modelscope.preprocessors import DialogIntentPreprocessor
from modelscope.preprocessors import DialogIntentPredictionPreprocessor
from modelscope.utils.constant import Tasks
from ...base import Input, Pipeline
from ...builder import PIPELINES

__all__ = ['DialogIntentPipeline']
__all__ = ['DialogIntentPredictionPipeline']


@PIPELINES.register_module(Tasks.dialog_intent, module_name=r'space')
class DialogIntentPipeline(Pipeline):
@PIPELINES.register_module(
Tasks.dialog_intent_prediction, module_name=r'space-intent')
class DialogIntentPredictionPipeline(Pipeline):

def __init__(self, model: DialogIntentModel,
preprocessor: DialogIntentPreprocessor, **kwargs):
preprocessor: DialogIntentPredictionPreprocessor, **kwargs):
"""use `model` and `preprocessor` to create a nlp text classification pipeline for prediction

Args:

modelscope/pipelines/nlp/space/dialog_generation_pipeline.py → modelscope/pipelines/nlp/space/dialog_modeling_pipeline.py View File

@@ -1,20 +1,20 @@
from typing import Any, Dict, Optional

from modelscope.models.nlp import DialogGenerationModel
from modelscope.preprocessors import DialogGenerationPreprocessor
from modelscope.models.nlp import DialogModelingModel
from modelscope.preprocessors import DialogModelingPreprocessor
from modelscope.utils.constant import Tasks
from ...base import Pipeline, Tensor
from ...builder import PIPELINES

__all__ = ['DialogGenerationPipeline']
__all__ = ['DialogModelingPipeline']


@PIPELINES.register_module(
Tasks.dialog_generation, module_name=r'space-generation')
class DialogGenerationPipeline(Pipeline):
Tasks.dialog_modeling, module_name=r'space-modeling')
class DialogModelingPipeline(Pipeline):

def __init__(self, model: DialogGenerationModel,
preprocessor: DialogGenerationPreprocessor, **kwargs):
def __init__(self, model: DialogModelingModel,
preprocessor: DialogModelingPreprocessor, **kwargs):
"""use `model` and `preprocessor` to create a nlp text classification pipeline for prediction

Args:

+ 2
- 2
modelscope/preprocessors/__init__.py View File

@@ -6,5 +6,5 @@ from .common import Compose
from .image import LoadImage, load_image
from .nlp import * # noqa F403
from .nlp import TextGenerationPreprocessor
from .space.dialog_generation_preprocessor import * # noqa F403
from .space.dialog_intent_preprocessor import * # noqa F403
from .space.dialog_intent_prediction_preprocessor import * # noqa F403
from .space.dialog_modeling_preprocessor import * # noqa F403

modelscope/preprocessors/space/dialog_intent_preprocessor.py → modelscope/preprocessors/space/dialog_intent_prediction_preprocessor.py View File

@@ -11,11 +11,11 @@ from modelscope.utils.type_assert import type_assert
from ..base import Preprocessor
from ..builder import PREPROCESSORS

__all__ = ['DialogIntentPreprocessor']
__all__ = ['DialogIntentPredictionPreprocessor']


@PREPROCESSORS.register_module(Fields.nlp, module_name=r'space')
class DialogIntentPreprocessor(Preprocessor):
@PREPROCESSORS.register_module(Fields.nlp, module_name=r'space-intent')
class DialogIntentPredictionPreprocessor(Preprocessor):

def __init__(self, model_dir: str, *args, **kwargs):
"""preprocess the data via the vocab.txt from the `model_dir` path

modelscope/preprocessors/space/dialog_generation_preprocessor.py → modelscope/preprocessors/space/dialog_modeling_preprocessor.py View File

@@ -12,11 +12,11 @@ from modelscope.utils.type_assert import type_assert
from ..base import Preprocessor
from ..builder import PREPROCESSORS

__all__ = ['DialogGenerationPreprocessor']
__all__ = ['DialogModelingPreprocessor']


@PREPROCESSORS.register_module(Fields.nlp, module_name=r'space-generation')
class DialogGenerationPreprocessor(Preprocessor):
@PREPROCESSORS.register_module(Fields.nlp, module_name=r'space-modeling')
class DialogModelingPreprocessor(Preprocessor):

def __init__(self, model_dir: str, *args, **kwargs):
"""preprocess the data via the vocab.txt from the `model_dir` path

+ 2
- 2
modelscope/utils/constant.py View File

@@ -38,8 +38,8 @@ class Tasks(object):
token_classification = 'token-classification'
conversational = 'conversational'
text_generation = 'text-generation'
dialog_generation = 'dialog-generation'
dialog_intent = 'dialog-intent'
dialog_modeling = 'dialog_modeling'
dialog_intent_prediction = 'dialog-intent-prediction'
table_question_answering = 'table-question-answering'
feature_extraction = 'feature-extraction'
sentence_similarity = 'sentence-similarity'


tests/pipelines/nlp/test_dialog_intent.py → tests/pipelines/nlp/test_dialog_intent_prediction.py View File

@@ -5,13 +5,13 @@ from maas_hub.snapshot_download import snapshot_download

from modelscope.models import Model
from modelscope.models.nlp import DialogIntentModel
from modelscope.pipelines import DialogIntentPipeline, pipeline
from modelscope.preprocessors import DialogIntentPreprocessor
from modelscope.pipelines import DialogIntentPredictionPipeline, pipeline
from modelscope.preprocessors import DialogIntentPredictionPreprocessor
from modelscope.utils.constant import Tasks


class DialogGenerationTest(unittest.TestCase):
model_id = 'damo/nlp_space_dialog-intent'
class DialogIntentPredictionTest(unittest.TestCase):
model_id = 'damo/nlp_space_dialog-intent-prediction'
test_case = [
'How do I locate my card?',
'I still have not received my new card, I ordered over a week ago.'
@@ -20,16 +20,17 @@ class DialogGenerationTest(unittest.TestCase):
@unittest.skip('test with snapshot_download')
def test_run(self):
cache_path = snapshot_download(self.model_id)
preprocessor = DialogIntentPreprocessor(model_dir=cache_path)
preprocessor = DialogIntentPredictionPreprocessor(model_dir=cache_path)
model = DialogIntentModel(
model_dir=cache_path,
text_field=preprocessor.text_field,
config=preprocessor.config)

pipelines = [
DialogIntentPipeline(model=model, preprocessor=preprocessor),
DialogIntentPredictionPipeline(
model=model, preprocessor=preprocessor),
pipeline(
task=Tasks.dialog_intent,
task=Tasks.dialog_intent_prediction,
model=model,
preprocessor=preprocessor)
]
@@ -39,12 +40,14 @@ class DialogGenerationTest(unittest.TestCase):

def test_run_with_model_from_modelhub(self):
model = Model.from_pretrained(self.model_id)
preprocessor = DialogIntentPreprocessor(model_dir=model.model_dir)
preprocessor = DialogIntentPredictionPreprocessor(
model_dir=model.model_dir)

pipelines = [
DialogIntentPipeline(model=model, preprocessor=preprocessor),
DialogIntentPredictionPipeline(
model=model, preprocessor=preprocessor),
pipeline(
task=Tasks.dialog_intent,
task=Tasks.dialog_intent_prediction,
model=model,
preprocessor=preprocessor)
]

tests/pipelines/nlp/test_dialog_generation.py → tests/pipelines/nlp/test_dialog_modeling.py View File

@@ -7,14 +7,14 @@ import unittest
from maas_hub.snapshot_download import snapshot_download

from modelscope.models import Model
from modelscope.models.nlp import DialogGenerationModel
from modelscope.pipelines import DialogGenerationPipeline, pipeline
from modelscope.preprocessors import DialogGenerationPreprocessor
from modelscope.models.nlp import DialogModelingModel
from modelscope.pipelines import DialogModelingPipeline, pipeline
from modelscope.preprocessors import DialogModelingPreprocessor
from modelscope.utils.constant import Tasks


class DialogGenerationTest(unittest.TestCase):
model_id = 'damo/nlp_space_dialog-generation'
class DialogModelingTest(unittest.TestCase):
model_id = 'damo/nlp_space_dialog-modeling'
test_case = {
'sng0073': {
'goal': {
@@ -92,21 +92,21 @@ class DialogGenerationTest(unittest.TestCase):
}
}

@unittest.skip('test with snapshot_download')
# @unittest.skip('test with snapshot_download')
def test_run(self):

cache_path = '/Users/yangliu/Space/maas_model/nlp_space_dialog-generation'
# cache_path = snapshot_download(self.model_id)
# cache_path = '/Users/yangliu/Space/maas_model/nlp_space_dialog-modeling'
cache_path = snapshot_download(self.model_id)

preprocessor = DialogGenerationPreprocessor(model_dir=cache_path)
model = DialogGenerationModel(
preprocessor = DialogModelingPreprocessor(model_dir=cache_path)
model = DialogModelingModel(
model_dir=cache_path,
text_field=preprocessor.text_field,
config=preprocessor.config)
pipelines = [
DialogGenerationPipeline(model=model, preprocessor=preprocessor),
DialogModelingPipeline(model=model, preprocessor=preprocessor),
pipeline(
task=Tasks.dialog_generation,
task=Tasks.dialog_modeling,
model=model,
preprocessor=preprocessor)
]

Loading…
Cancel
Save