Skip to content

Commit b3ee788

Browse files
committed
import torchaudio on a need basis
1 parent 5651138 commit b3ee788

1 file changed

Lines changed: 8 additions & 2 deletions

File tree

encoding/features/speech_model.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@
33
import torch
44
import numpy as np
55
from typing import Dict, Tuple, Optional
6-
from transformers import AutoModel, AutoProcessor, AutoFeatureExtractor
7-
import torchaudio
6+
from transformers import AutoModel, AutoProcessor, AutoFeatureExtractor
87
from tqdm import tqdm
98

9+
def import_torchaudio_gracefully():
10+
try:
11+
import torchaudio
12+
return torchaudio
13+
except ImportError:
14+
raise ImportError('torchaudio is required for SpeechFeatureExtractor. Please install it with this command:\npip install torchaudio')
1015

1116
def auto_device(fn):
1217
def wrapper(self, *args, **kwargs):
@@ -40,6 +45,7 @@ def __init__(
4045
target_sample_rate: int = 16000,
4146
disable_tqdm: bool = False,
4247
):
48+
import_torchaudio_gracefully()
4349
assert pool in {"last", "mean"}, "pool must be 'last' or 'mean'"
4450
self.model_name = model_name
4551
self.chunk_size = float(chunk_size)

0 commit comments

Comments
 (0)