EurekaPotato commited on
Commit
d56c2d4
·
verified ·
1 Parent(s): eefac30

Upload folder using huggingface_hub

Browse files
Files changed (2) hide show
  1. handler.py +72 -7
  2. requirements.txt +1 -0
handler.py CHANGED
@@ -110,16 +110,81 @@ class AudioFeatureExtractorEndpoint:
110
  self.sr = 16000
111
  self.emotion_cnn = EmotionCNN()
112
 
113
- # Load Silero VAD
114
  try:
115
- self.vad_model, self.vad_utils = torch.hub.load(
116
- repo_or_dir="snakers4/silero-vad", model="silero_vad", trust_repo=True
 
 
 
 
117
  )
118
- self.get_speech_timestamps = self.vad_utils[0]
119
- print("✓ Silero VAD loaded")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
120
  except Exception as e:
121
- print(f"⚠ Silero VAD failed: {e}")
122
- self.vad_model = None
 
 
 
 
 
 
 
 
 
 
 
 
 
123
 
124
  # -------- V1: SNR --------
125
  def extract_snr(self, audio: np.ndarray) -> float:
 
110
  self.sr = 16000
111
  self.emotion_cnn = EmotionCNN()
112
 
113
+ # Load Silero VAD from HuggingFace Hub (more reliable on HF Spaces)
114
  try:
115
+ from huggingface_hub import hf_hub_download
116
+
117
+ # Download the model file from HF
118
+ model_path = hf_hub_download(
119
+ repo_id="snakers4/silero-vad",
120
+ filename="files/silero_vad.jit"
121
  )
122
+
123
+ # Load the JIT model
124
+ self.vad_model = torch.jit.load(model_path)
125
+ self.vad_model.eval()
126
+
127
+ # Get the get_speech_timestamps function
128
+ # The model itself has this as a method in newer versions
129
+ def get_speech_timestamps(audio, model, sampling_rate=16000, **kwargs):
130
+ """Wrapper for VAD speech detection"""
131
+ if not isinstance(audio, torch.Tensor):
132
+ audio = torch.FloatTensor(audio)
133
+
134
+ # Get speech timestamps using the model
135
+ speech_probs = []
136
+ chunk_size = 512
137
+ for i in range(0, len(audio), chunk_size):
138
+ chunk = audio[i:i + chunk_size]
139
+ if len(chunk) < chunk_size:
140
+ chunk = torch.nn.functional.pad(chunk, (0, chunk_size - len(chunk)))
141
+ with torch.no_grad():
142
+ speech_prob = model(chunk, sampling_rate).item()
143
+ speech_probs.append((i, speech_prob))
144
+
145
+ # Convert probabilities to timestamps
146
+ threshold = kwargs.get('threshold', 0.5)
147
+ min_speech_duration_ms = kwargs.get('min_speech_duration_ms', 250)
148
+ min_silence_duration_ms = kwargs.get('min_silence_duration_ms', 100)
149
+
150
+ timestamps = []
151
+ in_speech = False
152
+ speech_start = 0
153
+
154
+ for i, prob in speech_probs:
155
+ if prob > threshold and not in_speech:
156
+ speech_start = i
157
+ in_speech = True
158
+ elif prob <= threshold and in_speech:
159
+ duration_ms = (i - speech_start) / sampling_rate * 1000
160
+ if duration_ms >= min_speech_duration_ms:
161
+ timestamps.append({'start': speech_start, 'end': i})
162
+ in_speech = False
163
+
164
+ # Close last segment if still in speech
165
+ if in_speech:
166
+ timestamps.append({'start': speech_start, 'end': len(audio)})
167
+
168
+ return timestamps
169
+
170
+ self.get_speech_timestamps = get_speech_timestamps
171
+ print("✓ Silero VAD loaded from HuggingFace Hub")
172
  except Exception as e:
173
+ print(f"⚠ Silero VAD failed to load from HF Hub: {e}")
174
+ print(f" Trying fallback torch.hub.load...")
175
+ try:
176
+ # Fallback to torch.hub
177
+ self.vad_model, self.vad_utils = torch.hub.load(
178
+ repo_or_dir="snakers4/silero-vad",
179
+ model="silero_vad",
180
+ trust_repo=True,
181
+ force_reload=False
182
+ )
183
+ self.get_speech_timestamps = self.vad_utils[0]
184
+ print("✓ Silero VAD loaded via torch.hub (fallback)")
185
+ except Exception as e2:
186
+ print(f"⚠ Both HF Hub and torch.hub failed for Silero VAD: {e2}")
187
+ self.vad_model = None
188
 
189
  # -------- V1: SNR --------
190
  def extract_snr(self, audio: np.ndarray) -> float:
requirements.txt CHANGED
@@ -13,3 +13,4 @@ torchaudio
13
  fastapi==0.95.2
14
  uvicorn==0.22.0
15
  python-multipart==0.0.6
 
 
13
  fastapi==0.95.2
14
  uvicorn==0.22.0
15
  python-multipart==0.0.6
16
+ huggingface_hub>=0.19.0