Spaces:
Sleeping
Sleeping
Update speech_processing.py
Browse files- speech_processing.py +43 -40
speech_processing.py
CHANGED
|
@@ -1,40 +1,43 @@
|
|
| 1 |
-
from transformers import SeamlessM4Tv2Model, AutoProcessor
|
| 2 |
-
import numpy as np
|
| 3 |
-
import torch
|
| 4 |
-
from pydub import AudioSegment
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
processor
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
#
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
#
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import SeamlessM4Tv2Model, AutoProcessor
|
| 2 |
+
import numpy as np
|
| 3 |
+
import torch
|
| 4 |
+
from pydub import AudioSegment
|
| 5 |
+
import spaces
|
| 6 |
+
|
| 7 |
+
# Load processor and model
|
| 8 |
+
processor = AutoProcessor.from_pretrained("facebook/seamless-m4t-v2-large")
|
| 9 |
+
model = SeamlessM4Tv2Model.from_pretrained("facebook/seamless-m4t-v2-large")
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
@spaces.GPU()
|
| 13 |
+
def translate_audio(audio_file):
|
| 14 |
+
if audio_file is None:
|
| 15 |
+
return "No audio file detected. Please try again."
|
| 16 |
+
|
| 17 |
+
try:
|
| 18 |
+
# Set the device (use GPU if available)
|
| 19 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 20 |
+
model.to(device)
|
| 21 |
+
|
| 22 |
+
# Reset audio file pointer and load audio
|
| 23 |
+
audio = AudioSegment.from_file(audio_file, format="wav")
|
| 24 |
+
audio = audio.set_frame_rate(16000).set_channels(1)
|
| 25 |
+
|
| 26 |
+
# Convert audio to float32 NumPy array
|
| 27 |
+
audio_array = np.array(audio.get_array_of_samples()).astype(np.float32) / 32768.0
|
| 28 |
+
|
| 29 |
+
# Process input
|
| 30 |
+
audio_inputs = processor(audios=audio_array, sampling_rate=16000, return_tensors="pt")
|
| 31 |
+
audio_inputs = {key: val.to(device) for key, val in audio_inputs.items()} # Ensure tensors are on the correct device
|
| 32 |
+
|
| 33 |
+
# Generate translation
|
| 34 |
+
output_tokens = model.generate(**audio_inputs, tgt_lang="eng", generate_speech=False)
|
| 35 |
+
|
| 36 |
+
# Extract token IDs from the generated output
|
| 37 |
+
token_ids = output_tokens.sequences
|
| 38 |
+
# Decode token IDs to text
|
| 39 |
+
translated_text_from_audio = processor.batch_decode(token_ids, skip_special_tokens=True)[0]
|
| 40 |
+
|
| 41 |
+
return translated_text_from_audio
|
| 42 |
+
except Exception as e:
|
| 43 |
+
return f"Error during audio translation: {e}"
|