Skip to content

Commit

Permalink
[Siglip] protect from imports if sentencepiece not installed (#28737)
Browse files Browse the repository at this point in the history
[Siglip] protect from imports if sentencepiece not installed
  • Loading branch information
amyeroberts authored Jan 28, 2024
1 parent 03cc177 commit f1cc615
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/transformers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,6 @@
"SiglipConfig",
"SiglipProcessor",
"SiglipTextConfig",
"SiglipTokenizer",
"SiglipVisionConfig",
],
"models.speech_encoder_decoder": ["SpeechEncoderDecoderConfig"],
Expand Down Expand Up @@ -1125,6 +1124,7 @@
_import_structure["models.reformer"].append("ReformerTokenizer")
_import_structure["models.rembert"].append("RemBertTokenizer")
_import_structure["models.seamless_m4t"].append("SeamlessM4TTokenizer")
_import_structure["models.siglip"].append("SiglipTokenizer")
_import_structure["models.speech_to_text"].append("Speech2TextTokenizer")
_import_structure["models.speecht5"].append("SpeechT5Tokenizer")
_import_structure["models.t5"].append("T5Tokenizer")
Expand Down Expand Up @@ -5503,7 +5503,6 @@
SiglipConfig,
SiglipProcessor,
SiglipTextConfig,
SiglipTokenizer,
SiglipVisionConfig,
)
from .models.speech_encoder_decoder import SpeechEncoderDecoderConfig
Expand Down Expand Up @@ -5852,6 +5851,7 @@
from .models.reformer import ReformerTokenizer
from .models.rembert import RemBertTokenizer
from .models.seamless_m4t import SeamlessM4TTokenizer
from .models.siglip import SiglipTokenizer
from .models.speech_to_text import Speech2TextTokenizer
from .models.speecht5 import SpeechT5Tokenizer
from .models.t5 import T5Tokenizer
Expand Down
20 changes: 18 additions & 2 deletions src/transformers/models/siglip/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from ...utils import (
OptionalDependencyNotAvailable,
_LazyModule,
is_sentencepiece_available,
is_torch_available,
is_vision_available,
)
Expand All @@ -29,9 +30,17 @@
"SiglipVisionConfig",
],
"processing_siglip": ["SiglipProcessor"],
"tokenization_siglip": ["SiglipTokenizer"],
}

try:
if not is_sentencepiece_available():
raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
pass
else:
_import_structure["tokenization_siglip"] = ["SiglipTokenizer"]


try:
if not is_vision_available():
raise OptionalDependencyNotAvailable()
Expand Down Expand Up @@ -63,7 +72,14 @@
SiglipVisionConfig,
)
from .processing_siglip import SiglipProcessor
from .tokenization_siglip import SiglipTokenizer

try:
if not is_sentencepiece_available():
raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
pass
else:
from .tokenization_siglip import SiglipTokenizer

try:
if not is_vision_available():
Expand Down
7 changes: 7 additions & 0 deletions src/transformers/utils/dummy_sentencepiece_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,13 @@ def __init__(self, *args, **kwargs):
requires_backends(self, ["sentencepiece"])


class SiglipTokenizer(metaclass=DummyObject):
_backends = ["sentencepiece"]

def __init__(self, *args, **kwargs):
requires_backends(self, ["sentencepiece"])


class Speech2TextTokenizer(metaclass=DummyObject):
_backends = ["sentencepiece"]

Expand Down

0 comments on commit f1cc615

Please sign in to comment.