-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #73 from Vela-zz/zh_model_config
model config manager class
- Loading branch information
Showing
13 changed files
with
642 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from ._model_management import ModelManager | ||
|
||
manager = ModelManager() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
from typing import Optional, Tuple | ||
|
||
from sentence_transformers import SentenceTransformer | ||
from transformers.models.auto.modeling_auto import ( | ||
AutoModelForSeq2SeqLM, AutoModelForSequenceClassification) | ||
from transformers.models.auto.tokenization_auto import AutoTokenizer | ||
|
||
|
||
def load_sentence_transformers( | ||
model_name: str, | ||
model_revision: Optional[str] = None, | ||
tokenizer_name: Optional[str] = None, | ||
tokenizer_revision: Optional[str] = None) -> SentenceTransformer: | ||
''' | ||
Loads a SentenceTransformer model. | ||
This function currently does not support specifying a tokenizer or a | ||
revision. If these arguments are provided, a warning message will be | ||
printed. | ||
Args: | ||
model_name: The name of the SentenceTransformer model to load. | ||
tokenizer_name: The name of the tokenizer to use. Currently not | ||
supported. | ||
model_revision: The model revision to load. Currently not supported. | ||
tokenizerl_revision: The tokenizedr revision to load. Currently not | ||
supported. | ||
Returns: | ||
model: The loaded SentenceTransformer model. | ||
''' | ||
if model_revision is not None or tokenizer_revision is not None: | ||
print("Warning: Specifying a revision is not currently supported.") | ||
if tokenizer_name is not None: | ||
print("Warning: Customizing the tokenizer is not currently supported.") | ||
|
||
model = SentenceTransformer(model_name) | ||
return model | ||
|
||
|
||
def load_auto_model_for_text_classification( | ||
model_name: str, | ||
model_revision: Optional[str] = None, | ||
tokenizer_name: Optional[str] = None, | ||
tokenizer_revision: Optional[str] = None | ||
) -> Tuple[AutoTokenizer, AutoModelForSequenceClassification]: | ||
''' | ||
Loads a sequence classification model and its tokenizer. | ||
Args: | ||
model_name: The name of the sequence-classification model to load. | ||
tokenizer_name: The name of the tokenizer to load. If None, the | ||
tokenizer associated with the model will be loaded. | ||
model_revision: The model revision to load. | ||
tokenizer_revision: the tokenizer revision to load. | ||
Returns: | ||
tokenizer: The loaded tokenizer. | ||
model: The loaded sequence classification model. | ||
''' | ||
if tokenizer_name is None: | ||
tokenizer_name = model_name | ||
tokenizer = AutoTokenizer.from_pretrained(tokenizer_name, | ||
revision=tokenizer_revision) | ||
model = AutoModelForSequenceClassification.from_pretrained( | ||
model_name, revision=model_revision) | ||
return tokenizer, model # type: ignore | ||
|
||
|
||
def load_auto_model_for_seq2seq( | ||
model_name: str, | ||
model_revision: Optional[str] = None, | ||
tokenizer_name: Optional[str] = None, | ||
tokenizer_revision: Optional[str] = None | ||
) -> Tuple[AutoTokenizer, AutoModelForSeq2SeqLM]: | ||
''' | ||
Loads a sequence-to-sequence model and its tokenizer. | ||
Args: | ||
model_name: The name of the sequence-classification model to load. | ||
tokenizer_name: The name of the tokenizer to load. If None, the | ||
tokenizer associated with the model will be loaded. | ||
model_revision: The model revision to load. | ||
tokenizer_revision: the tokenizer revision to load | ||
Returns: | ||
tokenizer: The loaded tokenizer. | ||
model: The loaded sequence-to-sequence model. | ||
''' | ||
if tokenizer_name is None: | ||
tokenizer_name = model_name | ||
tokenizer = AutoTokenizer.from_pretrained(tokenizer_name, | ||
revision=tokenizer_revision) | ||
model = AutoModelForSeq2SeqLM.from_pretrained(model_name, | ||
revision=model_revision) | ||
return tokenizer, model # type: ignore |
Oops, something went wrong.