Skip to content

Commit d6fad86

Browse files
authored
[serve] guard imports (#39825)
guard imports
1 parent 7a0ba0d commit d6fad86

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

src/transformers/commands/serving.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232

3333
from huggingface_hub import model_info
3434
from huggingface_hub.constants import HF_HUB_OFFLINE
35-
from PIL import Image
3635

3736
import transformers
3837
from transformers.models.auto.modeling_auto import (
@@ -45,6 +44,7 @@
4544
is_openai_available,
4645
is_pydantic_available,
4746
is_uvicorn_available,
47+
is_vision_available,
4848
)
4949

5050
from .. import (
@@ -54,7 +54,6 @@
5454
ProcessorMixin,
5555
TextIteratorStreamer,
5656
)
57-
from ..generation.continuous_batching import ContinuousBatchingManager, RequestStatus
5857
from ..utils import is_torch_available, logging
5958
from . import BaseTransformersCLICommand
6059

@@ -69,9 +68,14 @@
6968
PreTrainedModel,
7069
)
7170

71+
from ..generation.continuous_batching import ContinuousBatchingManager, RequestStatus
72+
7273
if is_librosa_available():
7374
import librosa
7475

76+
if is_vision_available():
77+
from PIL import Image
78+
7579
serve_dependencies_available = (
7680
is_pydantic_available() and is_fastapi_available() and is_uvicorn_available() and is_openai_available()
7781
)
@@ -811,7 +815,7 @@ def stream_chat_completion(_inputs):
811815
return stream_chat_completion(inputs[0])
812816

813817
@staticmethod
814-
def get_model_modality(model: PreTrainedModel) -> Modality:
818+
def get_model_modality(model: "PreTrainedModel") -> Modality:
815819
model_classname = model.__class__.__name__
816820
if model_classname in MODEL_FOR_IMAGE_TEXT_TO_TEXT_MAPPING_NAMES.values():
817821
modality = Modality.VLM
@@ -1545,7 +1549,9 @@ def _load_model_and_data_processor(self, model_id_and_revision: str):
15451549
logger.info(f"Loaded model {model_id_and_revision}")
15461550
return model, data_processor
15471551

1548-
def load_model_and_processor(self, model_id_and_revision: str) -> tuple[PreTrainedModel, PreTrainedTokenizerFast]:
1552+
def load_model_and_processor(
1553+
self, model_id_and_revision: str
1554+
) -> tuple["PreTrainedModel", PreTrainedTokenizerFast]:
15491555
"""
15501556
Loads the text model and processor from the given model ID and revision into the ServeCommand instance.
15511557
@@ -1570,7 +1576,7 @@ def load_model_and_processor(self, model_id_and_revision: str) -> tuple[PreTrain
15701576

15711577
return model, processor
15721578

1573-
def load_audio_model_and_processor(self, model_id_and_revision: str) -> tuple[PreTrainedModel, ProcessorMixin]:
1579+
def load_audio_model_and_processor(self, model_id_and_revision: str) -> tuple["PreTrainedModel", ProcessorMixin]:
15741580
"""
15751581
Loads the audio model and processor from the given model ID and revision into the ServeCommand instance.
15761582

0 commit comments

Comments
 (0)