Spaces:
Sleeping
Sleeping
Added thread lock to prevent overlapping executions
Browse files- src/app.py +24 -21
src/app.py
CHANGED
|
@@ -53,27 +53,30 @@ def translate_audio(
|
|
| 53 |
:yield: the tuple containing the sampling rate and the audio array
|
| 54 |
:rtype: tuple[int, np.ndarray]
|
| 55 |
"""
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
|
| 79 |
# Supported target languages for speech
|
|
|
|
| 53 |
:yield: the tuple containing the sampling rate and the audio array
|
| 54 |
:rtype: tuple[int, np.ndarray]
|
| 55 |
"""
|
| 56 |
+
with translate_lock:
|
| 57 |
+
orig_freq, np_array = audio
|
| 58 |
+
waveform = torch.from_numpy(np_array)
|
| 59 |
+
waveform = waveform.to(torch.float32)
|
| 60 |
+
waveform = waveform / 32768.0 # normalize int16 to [-1, 1]
|
| 61 |
+
|
| 62 |
+
audio = torchaudio.functional.resample(
|
| 63 |
+
waveform, orig_freq=orig_freq, new_freq=default_sampling_rate
|
| 64 |
+
) # must be a 16 kHz waveform array
|
| 65 |
+
|
| 66 |
+
audio_inputs = processor(
|
| 67 |
+
audios=audio,
|
| 68 |
+
return_tensors="pt",
|
| 69 |
+
sampling_rate=default_sampling_rate,
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
audio_array_from_audio = (
|
| 73 |
+
model.generate(**audio_inputs, tgt_lang=tgt_language)[0]
|
| 74 |
+
.cpu()
|
| 75 |
+
.numpy()
|
| 76 |
+
.squeeze()
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
yield (default_sampling_rate, audio_array_from_audio)
|
| 80 |
|
| 81 |
|
| 82 |
# Supported target languages for speech
|