Browse Source

pre-commit lint

master
雨泓 3 years ago
parent
commit
56cb04cefe
26 changed files with 100 additions and 96 deletions
  1. +4
    -10
      modelscope/models/__init__.py
  2. +3
    -3
      modelscope/models/nlp/__init__.py
  3. +2
    -2
      modelscope/models/nlp/masked_language_model.py
  4. +5
    -3
      modelscope/models/nlp/sbert_for_nli.py
  5. +4
    -2
      modelscope/models/nlp/sbert_for_sentence_similarity.py
  6. +6
    -5
      modelscope/models/nlp/sbert_for_sentiment_classification.py
  7. +15
    -11
      modelscope/models/nlp/sbert_for_sequence_classification.py
  8. +4
    -3
      modelscope/models/nlp/sbert_for_token_classification.py
  9. +3
    -4
      modelscope/models/nlp/sbert_for_zero_shot_classification.py
  10. +1
    -2
      modelscope/models/nlp/space/model/gen_unified_transformer.py
  11. +2
    -3
      modelscope/models/nlp/space/model/unified_transformer.py
  12. +1
    -2
      modelscope/models/nlp/space/modules/transformer_block.py
  13. +6
    -6
      modelscope/pipelines/nlp/fill_mask_pipeline.py
  14. +8
    -10
      modelscope/pipelines/nlp/nli_pipeline.py
  15. +6
    -4
      modelscope/pipelines/nlp/sentence_similarity_pipeline.py
  16. +8
    -6
      modelscope/pipelines/nlp/sentiment_classification_pipeline.py
  17. +5
    -2
      modelscope/pipelines/nlp/text_generation_pipeline.py
  18. +4
    -1
      modelscope/pipelines/nlp/word_segmentation_pipeline.py
  19. +4
    -3
      modelscope/pipelines/nlp/zero_shot_classification_pipeline.py
  20. +1
    -2
      modelscope/preprocessors/nlp.py
  21. +1
    -2
      modelscope/preprocessors/space/dialog_intent_prediction_preprocessor.py
  22. +3
    -4
      modelscope/preprocessors/space/dialog_modeling_preprocessor.py
  23. +1
    -1
      modelscope/preprocessors/space/fields/gen_field.py
  24. +1
    -1
      modelscope/preprocessors/space/fields/intent_field.py
  25. +1
    -2
      modelscope/trainers/nlp/space/trainers/intent_trainer.py
  26. +1
    -2
      tests/pipelines/test_word_segmentation.py

+ 4
- 10
modelscope/models/__init__.py View File

@@ -5,13 +5,7 @@ from .audio.tts.vocoder import Hifigan16k
from .base import Model from .base import Model
from .builder import MODELS, build_model from .builder import MODELS, build_model
from .multi_model import OfaForImageCaptioning from .multi_model import OfaForImageCaptioning
from .nlp import (
BertForSequenceClassification,
SbertForNLI,
SbertForSentenceSimilarity,
SbertForSentimentClassification,
SbertForZeroShotClassification,
StructBertForMaskedLM,
VecoForMaskedLM,
SbertForTokenClassification,
)
from .nlp import (BertForSequenceClassification, SbertForNLI,
SbertForSentenceSimilarity, SbertForSentimentClassification,
SbertForTokenClassification, SbertForZeroShotClassification,
StructBertForMaskedLM, VecoForMaskedLM)

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

@@ -1,10 +1,10 @@
from .bert_for_sequence_classification import * # noqa F403 from .bert_for_sequence_classification import * # noqa F403
from .masked_language_model import * # noqa F403 from .masked_language_model import * # noqa F403
from .sbert_for_nli import * # noqa F403
from .palm_for_text_generation import * # noqa F403 from .palm_for_text_generation import * # noqa F403
from .sbert_for_nli import * # noqa F403
from .sbert_for_sentence_similarity import * # noqa F403 from .sbert_for_sentence_similarity import * # noqa F403
from .sbert_for_token_classification import * # noqa F403
from .sbert_for_sentiment_classification import * # noqa F403 from .sbert_for_sentiment_classification import * # noqa F403
from .sbert_for_token_classification import * # noqa F403
from .sbert_for_zero_shot_classification import * # noqa F403
from .space.dialog_intent_prediction_model import * # noqa F403 from .space.dialog_intent_prediction_model import * # noqa F403
from .space.dialog_modeling_model import * # noqa F403 from .space.dialog_modeling_model import * # noqa F403
from .sbert_for_zero_shot_classification import * # noqa F403

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

@@ -2,10 +2,10 @@ from typing import Any, Dict, Optional, Union


import numpy as np import numpy as np


from ...metainfo import Models
from ...utils.constant import Tasks from ...utils.constant import Tasks
from ..base import Model, Tensor from ..base import Model, Tensor
from ..builder import MODELS from ..builder import MODELS
from ...metainfo import Models


__all__ = ['StructBertForMaskedLM', 'VecoForMaskedLM', 'MaskedLMModelBase'] __all__ = ['StructBertForMaskedLM', 'VecoForMaskedLM', 'MaskedLMModelBase']


@@ -27,7 +27,7 @@ class MaskedLMModelBase(Model):


@property @property
def config(self): def config(self):
if hasattr(self.model, "config"):
if hasattr(self.model, 'config'):
return self.model.config return self.model.config
return None return None




+ 5
- 3
modelscope/models/nlp/sbert_for_nli.py View File

@@ -1,7 +1,8 @@
from ...metainfo import Models
from ...utils.constant import Tasks from ...utils.constant import Tasks
from .sbert_for_sequence_classification import SbertForSequenceClassificationBase
from ..builder import MODELS from ..builder import MODELS
from ...metainfo import Models
from .sbert_for_sequence_classification import \
SbertForSequenceClassificationBase


__all__ = ['SbertForNLI'] __all__ = ['SbertForNLI']


@@ -17,5 +18,6 @@ class SbertForNLI(SbertForSequenceClassificationBase):
model_cls (Optional[Any], optional): model loader, if None, use the model_cls (Optional[Any], optional): model loader, if None, use the
default loader to load model weights, by default None. default loader to load model weights, by default None.
""" """
super().__init__(model_dir, *args, model_args={"num_labels": 3}, **kwargs)
super().__init__(
model_dir, *args, model_args={'num_labels': 3}, **kwargs)
assert self.model.config.num_labels == 3 assert self.model.config.num_labels == 3

+ 4
- 2
modelscope/models/nlp/sbert_for_sentence_similarity.py View File

@@ -1,7 +1,8 @@
from modelscope.metainfo import Models from modelscope.metainfo import Models
from modelscope.utils.constant import Tasks from modelscope.utils.constant import Tasks
from .sbert_for_sequence_classification import SbertForSequenceClassificationBase
from ..builder import MODELS from ..builder import MODELS
from .sbert_for_sequence_classification import \
SbertForSequenceClassificationBase


__all__ = ['SbertForSentenceSimilarity'] __all__ = ['SbertForSentenceSimilarity']


@@ -18,6 +19,7 @@ class SbertForSentenceSimilarity(SbertForSequenceClassificationBase):
model_cls (Optional[Any], optional): model loader, if None, use the model_cls (Optional[Any], optional): model loader, if None, use the
default loader to load model weights, by default None. default loader to load model weights, by default None.
""" """
super().__init__(model_dir, *args, model_args={"num_labels": 2}, **kwargs)
super().__init__(
model_dir, *args, model_args={'num_labels': 2}, **kwargs)
self.model_dir = model_dir self.model_dir = model_dir
assert self.model.config.num_labels == 2 assert self.model.config.num_labels == 2

+ 6
- 5
modelscope/models/nlp/sbert_for_sentiment_classification.py View File

@@ -1,14 +1,14 @@
from modelscope.metainfo import Models
from modelscope.utils.constant import Tasks from modelscope.utils.constant import Tasks
from .sbert_for_sequence_classification import SbertForSequenceClassificationBase
from ..builder import MODELS from ..builder import MODELS
from modelscope.metainfo import Models
from .sbert_for_sequence_classification import \
SbertForSequenceClassificationBase


__all__ = ['SbertForSentimentClassification'] __all__ = ['SbertForSentimentClassification']




@MODELS.register_module( @MODELS.register_module(
Tasks.sentiment_classification,
module_name=Models.structbert)
Tasks.sentiment_classification, module_name=Models.structbert)
class SbertForSentimentClassification(SbertForSequenceClassificationBase): class SbertForSentimentClassification(SbertForSequenceClassificationBase):


def __init__(self, model_dir: str, *args, **kwargs): def __init__(self, model_dir: str, *args, **kwargs):
@@ -19,5 +19,6 @@ class SbertForSentimentClassification(SbertForSequenceClassificationBase):
model_cls (Optional[Any], optional): model loader, if None, use the model_cls (Optional[Any], optional): model loader, if None, use the
default loader to load model weights, by default None. default loader to load model weights, by default None.
""" """
super().__init__(model_dir, *args, model_args={"num_labels": 2}, **kwargs)
super().__init__(
model_dir, *args, model_args={'num_labels': 2}, **kwargs)
assert self.model.config.num_labels == 2 assert self.model.config.num_labels == 2

+ 15
- 11
modelscope/models/nlp/sbert_for_sequence_classification.py View File

@@ -1,11 +1,13 @@
from torch import nn
import os
from typing import Any, Dict from typing import Any, Dict
from ..base import Model
import numpy as np

import json import json
import os
import numpy as np
import torch import torch
from sofa.models.sbert.modeling_sbert import SbertPreTrainedModel, SbertModel
from sofa.models.sbert.modeling_sbert import SbertModel, SbertPreTrainedModel
from torch import nn

from ..base import Model




class SbertTextClassfier(SbertPreTrainedModel): class SbertTextClassfier(SbertPreTrainedModel):
@@ -27,9 +29,7 @@ class SbertTextClassfier(SbertPreTrainedModel):
pooled_output = outputs[1] pooled_output = outputs[1]
pooled_output = self.dropout(pooled_output) pooled_output = self.dropout(pooled_output)
logits = self.classifier(pooled_output) logits = self.classifier(pooled_output)
return {
"logits": logits
}
return {'logits': logits}




class SbertForSequenceClassificationBase(Model): class SbertForSequenceClassificationBase(Model):
@@ -38,13 +38,17 @@ class SbertForSequenceClassificationBase(Model):
super().__init__(model_dir, *args, **kwargs) super().__init__(model_dir, *args, **kwargs)
if model_args is None: if model_args is None:
model_args = {} model_args = {}
self.model = SbertTextClassfier.from_pretrained(model_dir, **model_args)
self.model = SbertTextClassfier.from_pretrained(
model_dir, **model_args)
self.id2label = {} self.id2label = {}
self.label_path = os.path.join(self.model_dir, 'label_mapping.json') self.label_path = os.path.join(self.model_dir, 'label_mapping.json')
if os.path.exists(self.label_path): if os.path.exists(self.label_path):
with open(self.label_path) as f: with open(self.label_path) as f:
self.label_mapping = json.load(f) self.label_mapping = json.load(f)
self.id2label = {idx: name for name, idx in self.label_mapping.items()}
self.id2label = {
idx: name
for name, idx in self.label_mapping.items()
}


def train(self): def train(self):
return self.model.train() return self.model.train()
@@ -59,7 +63,7 @@ class SbertForSequenceClassificationBase(Model):
return self.model.forward(input_ids, token_type_ids) return self.model.forward(input_ids, token_type_ids)


def postprocess(self, input, **kwargs): def postprocess(self, input, **kwargs):
logits = input["logits"]
logits = input['logits']
probs = logits.softmax(-1).numpy() probs = logits.softmax(-1).numpy()
pred = logits.argmax(-1).numpy() pred = logits.argmax(-1).numpy()
logits = logits.numpy() logits = logits.numpy()


+ 4
- 3
modelscope/models/nlp/sbert_for_token_classification.py View File

@@ -34,7 +34,7 @@ class SbertForTokenClassification(Model):


def eval(self): def eval(self):
return self.model.eval() return self.model.eval()
def forward(self, input: Dict[str, def forward(self, input: Dict[str,
Any]) -> Dict[str, Union[str, np.ndarray]]: Any]) -> Dict[str, Union[str, np.ndarray]]:
"""return the result by the model """return the result by the model
@@ -54,8 +54,9 @@ class SbertForTokenClassification(Model):
input_ids = torch.tensor(input['input_ids']).unsqueeze(0) input_ids = torch.tensor(input['input_ids']).unsqueeze(0)
return {**self.model(input_ids), 'text': input['text']} return {**self.model(input_ids), 'text': input['text']}


def postprocess(self, input: Dict[str, Tensor], **kwargs) -> Dict[str, Tensor]:
logits = input["logits"]
def postprocess(self, input: Dict[str, Tensor],
**kwargs) -> Dict[str, Tensor]:
logits = input['logits']
pred = torch.argmax(logits[0], dim=-1) pred = torch.argmax(logits[0], dim=-1)
pred = pred.numpy() pred = pred.numpy()
rst = {'predictions': pred, 'logits': logits, 'text': input['text']} rst = {'predictions': pred, 'logits': logits, 'text': input['text']}


+ 3
- 4
modelscope/models/nlp/sbert_for_zero_shot_classification.py View File

@@ -3,16 +3,15 @@ from typing import Any, Dict
import numpy as np import numpy as np


from modelscope.utils.constant import Tasks from modelscope.utils.constant import Tasks
from ...metainfo import Models
from ..base import Model from ..base import Model
from ..builder import MODELS from ..builder import MODELS
from ...metainfo import Models


__all__ = ['SbertForZeroShotClassification'] __all__ = ['SbertForZeroShotClassification']




@MODELS.register_module( @MODELS.register_module(
Tasks.zero_shot_classification,
module_name=Models.structbert)
Tasks.zero_shot_classification, module_name=Models.structbert)
class SbertForZeroShotClassification(Model): class SbertForZeroShotClassification(Model):


def __init__(self, model_dir: str, *args, **kwargs): def __init__(self, model_dir: str, *args, **kwargs):
@@ -31,7 +30,7 @@ class SbertForZeroShotClassification(Model):


def eval(self): def eval(self):
return self.model.eval() return self.model.eval()
def forward(self, input: Dict[str, Any]) -> Dict[str, np.ndarray]: def forward(self, input: Dict[str, Any]) -> Dict[str, np.ndarray]:
"""return the result by the model """return the result by the model




+ 1
- 2
modelscope/models/nlp/space/model/gen_unified_transformer.py View File

@@ -3,8 +3,7 @@ IntentUnifiedTransformer
""" """
import torch import torch


from .unified_transformer import \
UnifiedTransformer
from .unified_transformer import UnifiedTransformer




class GenUnifiedTransformer(UnifiedTransformer): class GenUnifiedTransformer(UnifiedTransformer):


+ 2
- 3
modelscope/models/nlp/space/model/unified_transformer.py View File

@@ -7,10 +7,9 @@ import torch
import torch.nn as nn import torch.nn as nn
import torch.nn.functional as F import torch.nn.functional as F


from .model_base import ModelBase
from ..modules.embedder import Embedder from ..modules.embedder import Embedder
from ..modules.transformer_block import \
TransformerBlock
from ..modules.transformer_block import TransformerBlock
from .model_base import ModelBase




class UnifiedTransformer(ModelBase): class UnifiedTransformer(ModelBase):


+ 1
- 2
modelscope/models/nlp/space/modules/transformer_block.py View File

@@ -6,8 +6,7 @@ import torch
import torch.nn as nn import torch.nn as nn


from .feedforward import FeedForward from .feedforward import FeedForward
from .multihead_attention import \
MultiheadAttention
from .multihead_attention import MultiheadAttention




class TransformerBlock(nn.Module): class TransformerBlock(nn.Module):


+ 6
- 6
modelscope/pipelines/nlp/fill_mask_pipeline.py View File

@@ -1,15 +1,14 @@
from typing import Dict, Optional, Union, Any
from typing import Any, Dict, Optional, Union


import torch import torch


from ...metainfo import Pipelines
from ...models import Model from ...models import Model
from ...models.nlp.masked_language_model import \
MaskedLMModelBase
from ...models.nlp.masked_language_model import MaskedLMModelBase
from ...preprocessors import FillMaskPreprocessor from ...preprocessors import FillMaskPreprocessor
from ...utils.constant import Tasks from ...utils.constant import Tasks
from ..base import Pipeline, Tensor from ..base import Pipeline, Tensor
from ..builder import PIPELINES from ..builder import PIPELINES
from ...metainfo import Pipelines


__all__ = ['FillMaskPipeline'] __all__ = ['FillMaskPipeline']


@@ -20,7 +19,7 @@ class FillMaskPipeline(Pipeline):
def __init__(self, def __init__(self,
model: Union[MaskedLMModelBase, str], model: Union[MaskedLMModelBase, str],
preprocessor: Optional[FillMaskPreprocessor] = None, preprocessor: Optional[FillMaskPreprocessor] = None,
first_sequence="sentense",
first_sequence='sentense',
**kwargs): **kwargs):
"""use `model` and `preprocessor` to create a nlp fill mask pipeline for prediction """use `model` and `preprocessor` to create a nlp fill mask pipeline for prediction


@@ -38,7 +37,8 @@ class FillMaskPipeline(Pipeline):
first_sequence=first_sequence, first_sequence=first_sequence,
second_sequence=None) second_sequence=None)
fill_mask_model.eval() fill_mask_model.eval()
super().__init__(model=fill_mask_model, preprocessor=preprocessor, **kwargs)
super().__init__(
model=fill_mask_model, preprocessor=preprocessor, **kwargs)
self.preprocessor = preprocessor self.preprocessor = preprocessor
self.tokenizer = preprocessor.tokenizer self.tokenizer = preprocessor.tokenizer
self.mask_id = {'veco': 250001, 'sbert': 103} self.mask_id = {'veco': 250001, 'sbert': 103}


+ 8
- 10
modelscope/pipelines/nlp/nli_pipeline.py View File

@@ -1,31 +1,28 @@
import uuid import uuid
from typing import Any, Dict, Union from typing import Any, Dict, Union
import torch
import uuid
from typing import Any, Dict, Union


import numpy as np import numpy as np
import torch


from ..base import Pipeline
from ..builder import PIPELINES
from ...metainfo import Pipelines from ...metainfo import Pipelines
from ...models import Model from ...models import Model
from ...models.nlp import SbertForNLI from ...models.nlp import SbertForNLI
from ...preprocessors import NLIPreprocessor from ...preprocessors import NLIPreprocessor
from ...utils.constant import Tasks from ...utils.constant import Tasks
from ..base import Pipeline
from ..builder import PIPELINES


__all__ = ['NLIPipeline'] __all__ = ['NLIPipeline']




@PIPELINES.register_module(
Tasks.nli, module_name=Pipelines.nli)
@PIPELINES.register_module(Tasks.nli, module_name=Pipelines.nli)
class NLIPipeline(Pipeline): class NLIPipeline(Pipeline):


def __init__(self, def __init__(self,
model: Union[SbertForNLI, str], model: Union[SbertForNLI, str],
preprocessor: NLIPreprocessor = None, preprocessor: NLIPreprocessor = None,
first_sequence="first_sequence",
second_sequence="second_sequence",
first_sequence='first_sequence',
second_sequence='second_sequence',
**kwargs): **kwargs):
"""use `model` and `preprocessor` to create a nlp text classification pipeline for prediction """use `model` and `preprocessor` to create a nlp text classification pipeline for prediction


@@ -51,7 +48,8 @@ class NLIPipeline(Pipeline):
with torch.no_grad(): with torch.no_grad():
return super().forward(inputs, **forward_params) return super().forward(inputs, **forward_params)


def postprocess(self, inputs: Dict[str, Any], **postprocess_params) -> Dict[str, str]:
def postprocess(self, inputs: Dict[str, Any],
**postprocess_params) -> Dict[str, str]:
"""process the prediction results """process the prediction results


Args: Args:


+ 6
- 4
modelscope/pipelines/nlp/sentence_similarity_pipeline.py View File

@@ -2,11 +2,12 @@ from typing import Any, Dict, Union


import numpy as np import numpy as np
import torch import torch

from ...metainfo import Pipelines from ...metainfo import Pipelines
from ...models import Model
from ...models.nlp import SbertForSentenceSimilarity from ...models.nlp import SbertForSentenceSimilarity
from ...preprocessors import SequenceClassificationPreprocessor from ...preprocessors import SequenceClassificationPreprocessor
from ...utils.constant import Tasks from ...utils.constant import Tasks
from ...models import Model
from ..base import Input, Pipeline from ..base import Input, Pipeline
from ..builder import PIPELINES from ..builder import PIPELINES


@@ -20,8 +21,8 @@ class SentenceSimilarityPipeline(Pipeline):
def __init__(self, def __init__(self,
model: Union[Model, str], model: Union[Model, str],
preprocessor: SequenceClassificationPreprocessor = None, preprocessor: SequenceClassificationPreprocessor = None,
first_sequence="first_sequence",
second_sequence="second_sequence",
first_sequence='first_sequence',
second_sequence='second_sequence',
**kwargs): **kwargs):
"""use `model` and `preprocessor` to create a nlp sentence similarity pipeline for prediction """use `model` and `preprocessor` to create a nlp sentence similarity pipeline for prediction


@@ -50,7 +51,8 @@ class SentenceSimilarityPipeline(Pipeline):
with torch.no_grad(): with torch.no_grad():
return super().forward(inputs, **forward_params) return super().forward(inputs, **forward_params)


def postprocess(self, inputs: Dict[str, Any], **postprocess_params) -> Dict[str, str]:
def postprocess(self, inputs: Dict[str, Any],
**postprocess_params) -> Dict[str, str]:
"""process the prediction results """process the prediction results


Args: Args:


+ 8
- 6
modelscope/pipelines/nlp/sentiment_classification_pipeline.py View File

@@ -1,17 +1,18 @@
import os import os
import uuid import uuid
from typing import Any, Dict, Union from typing import Any, Dict, Union
import torch
import json import json
import numpy as np import numpy as np
import torch


from ...metainfo import Pipelines
from ...models import Model
from ...models.nlp import SbertForSentimentClassification from ...models.nlp import SbertForSentimentClassification
from ...preprocessors import SentimentClassificationPreprocessor from ...preprocessors import SentimentClassificationPreprocessor
from ...utils.constant import Tasks from ...utils.constant import Tasks
from ...models import Model
from ..base import Input, Pipeline from ..base import Input, Pipeline
from ..builder import PIPELINES from ..builder import PIPELINES
from ...metainfo import Pipelines


__all__ = ['SentimentClassificationPipeline'] __all__ = ['SentimentClassificationPipeline']


@@ -24,8 +25,8 @@ class SentimentClassificationPipeline(Pipeline):
def __init__(self, def __init__(self,
model: Union[SbertForSentimentClassification, str], model: Union[SbertForSentimentClassification, str],
preprocessor: SentimentClassificationPreprocessor = None, preprocessor: SentimentClassificationPreprocessor = None,
first_sequence="first_sequence",
second_sequence="second_sequence",
first_sequence='first_sequence',
second_sequence='second_sequence',
**kwargs): **kwargs):
"""use `model` and `preprocessor` to create a nlp text classification pipeline for prediction """use `model` and `preprocessor` to create a nlp text classification pipeline for prediction


@@ -52,7 +53,8 @@ class SentimentClassificationPipeline(Pipeline):
with torch.no_grad(): with torch.no_grad():
return super().forward(inputs, **forward_params) return super().forward(inputs, **forward_params)


def postprocess(self, inputs: Dict[str, Any], **postprocess_params) -> Dict[str, str]:
def postprocess(self, inputs: Dict[str, Any],
**postprocess_params) -> Dict[str, str]:
"""process the prediction results """process the prediction results


Args: Args:


+ 5
- 2
modelscope/pipelines/nlp/text_generation_pipeline.py View File

@@ -1,5 +1,7 @@
from typing import Dict, Optional, Union, Any
from typing import Any, Dict, Optional, Union

import torch import torch

from ...metainfo import Pipelines from ...metainfo import Pipelines
from ...models import Model from ...models import Model
from ...models.nlp import PalmForTextGeneration from ...models.nlp import PalmForTextGeneration
@@ -42,7 +44,8 @@ class TextGenerationPipeline(Pipeline):
with torch.no_grad(): with torch.no_grad():
return super().forward(inputs, **forward_params) return super().forward(inputs, **forward_params)


def postprocess(self, inputs: Dict[str, Tensor], **postprocess_params) -> Dict[str, str]:
def postprocess(self, inputs: Dict[str, Tensor],
**postprocess_params) -> Dict[str, str]:
"""process the prediction results """process the prediction results


Args: Args:


+ 4
- 1
modelscope/pipelines/nlp/word_segmentation_pipeline.py View File

@@ -1,5 +1,7 @@
from typing import Any, Dict, Optional, Union from typing import Any, Dict, Optional, Union

import torch import torch

from ...metainfo import Pipelines from ...metainfo import Pipelines
from ...models import Model from ...models import Model
from ...models.nlp import SbertForTokenClassification from ...models.nlp import SbertForTokenClassification
@@ -42,7 +44,8 @@ class WordSegmentationPipeline(Pipeline):
with torch.no_grad(): with torch.no_grad():
return super().forward(inputs, **forward_params) return super().forward(inputs, **forward_params)


def postprocess(self, inputs: Dict[str, Any], **postprocess_params) -> Dict[str, str]:
def postprocess(self, inputs: Dict[str, Any],
**postprocess_params) -> Dict[str, str]:
"""process the prediction results """process the prediction results


Args: Args:


+ 4
- 3
modelscope/pipelines/nlp/zero_shot_classification_pipeline.py View File

@@ -1,16 +1,17 @@
import os import os
import uuid import uuid
from typing import Any, Dict, Union from typing import Any, Dict, Union
import torch
import json import json
import numpy as np import numpy as np
import torch
from scipy.special import softmax from scipy.special import softmax


from ...metainfo import Pipelines
from ...models import Model
from ...models.nlp import SbertForZeroShotClassification from ...models.nlp import SbertForZeroShotClassification
from ...preprocessors import ZeroShotClassificationPreprocessor from ...preprocessors import ZeroShotClassificationPreprocessor
from ...utils.constant import Tasks from ...utils.constant import Tasks
from ...models import Model
from ...metainfo import Pipelines
from ..base import Input, Pipeline from ..base import Input, Pipeline
from ..builder import PIPELINES from ..builder import PIPELINES




+ 1
- 2
modelscope/preprocessors/nlp.py View File

@@ -5,8 +5,7 @@ from typing import Any, Dict, Union


from transformers import AutoTokenizer from transformers import AutoTokenizer


from ..metainfo import Preprocessors
from ..metainfo import Models
from ..metainfo import Models, Preprocessors
from ..utils.constant import Fields, InputFields from ..utils.constant import Fields, InputFields
from ..utils.type_assert import type_assert from ..utils.type_assert import type_assert
from .base import Preprocessor from .base import Preprocessor


+ 1
- 2
modelscope/preprocessors/space/dialog_intent_prediction_preprocessor.py View File

@@ -3,13 +3,12 @@
import os import os
from typing import Any, Dict from typing import Any, Dict


from .fields.intent_field import \
IntentBPETextField
from ...utils.config import Config from ...utils.config import Config
from ...utils.constant import Fields from ...utils.constant import Fields
from ...utils.type_assert import type_assert from ...utils.type_assert import type_assert
from ..base import Preprocessor from ..base import Preprocessor
from ..builder import PREPROCESSORS from ..builder import PREPROCESSORS
from .fields.intent_field import IntentBPETextField


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




+ 3
- 4
modelscope/preprocessors/space/dialog_modeling_preprocessor.py View File

@@ -3,13 +3,12 @@
import os import os
from typing import Any, Dict from typing import Any, Dict


from .fields.gen_field import \
MultiWOZBPETextField
from ..base import Preprocessor
from ..builder import PREPROCESSORS
from ...utils.config import Config from ...utils.config import Config
from ...utils.constant import Fields from ...utils.constant import Fields
from ...utils.type_assert import type_assert from ...utils.type_assert import type_assert
from ..base import Preprocessor
from ..builder import PREPROCESSORS
from .fields.gen_field import MultiWOZBPETextField


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




+ 1
- 1
modelscope/preprocessors/space/fields/gen_field.py View File

@@ -8,10 +8,10 @@ from itertools import chain


import numpy as np import numpy as np


from ..tokenizer import Tokenizer
from ....utils.nlp.space import ontology, utils from ....utils.nlp.space import ontology, utils
from ....utils.nlp.space.db_ops import MultiWozDB from ....utils.nlp.space.db_ops import MultiWozDB
from ....utils.nlp.space.utils import list2np from ....utils.nlp.space.utils import list2np
from ..tokenizer import Tokenizer




class BPETextField(object): class BPETextField(object):


+ 1
- 1
modelscope/preprocessors/space/fields/intent_field.py View File

@@ -14,10 +14,10 @@ import json
import numpy as np import numpy as np
from tqdm import tqdm from tqdm import tqdm


from ..tokenizer import Tokenizer
from ....utils.nlp.space import ontology, utils from ....utils.nlp.space import ontology, utils
from ....utils.nlp.space.scores import hierarchical_set_score from ....utils.nlp.space.scores import hierarchical_set_score
from ....utils.nlp.space.utils import list2np from ....utils.nlp.space.utils import list2np
from ..tokenizer import Tokenizer




class BPETextField(object): class BPETextField(object):


+ 1
- 2
modelscope/trainers/nlp/space/trainers/intent_trainer.py View File

@@ -14,8 +14,7 @@ import torch
from tqdm import tqdm from tqdm import tqdm
from transformers.optimization import AdamW, get_linear_schedule_with_warmup from transformers.optimization import AdamW, get_linear_schedule_with_warmup


from ..metrics.metrics_tracker import \
MetricsTracker
from ..metrics.metrics_tracker import MetricsTracker




def get_logger(log_path, name='default'): def get_logger(log_path, name='default'):


+ 1
- 2
tests/pipelines/test_word_segmentation.py View File

@@ -19,8 +19,7 @@ class WordSegmentationTest(unittest.TestCase):
def test_run_by_direct_model_download(self): def test_run_by_direct_model_download(self):
cache_path = snapshot_download(self.model_id) cache_path = snapshot_download(self.model_id)
tokenizer = TokenClassifcationPreprocessor(cache_path) tokenizer = TokenClassifcationPreprocessor(cache_path)
model = SbertForTokenClassification(
cache_path, tokenizer=tokenizer)
model = SbertForTokenClassification(cache_path, tokenizer=tokenizer)
pipeline1 = WordSegmentationPipeline(model, preprocessor=tokenizer) pipeline1 = WordSegmentationPipeline(model, preprocessor=tokenizer)
pipeline2 = pipeline( pipeline2 = pipeline(
Tasks.word_segmentation, model=model, preprocessor=tokenizer) Tasks.word_segmentation, model=model, preprocessor=tokenizer)


Loading…
Cancel
Save