-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.py
More file actions
49 lines (40 loc) · 2.16 KB
/
Copy pathinit.py
File metadata and controls
49 lines (40 loc) · 2.16 KB
1
2
3
4
5
6
7
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
41
42
43
44
45
46
47
48
# init.py
# note install with:
# pip install git+SeamlessM4Tv2Model, AutoProcessorM4Tv2Model, AutoProcessorgithub.com/huggingface/transformers.git sentencepiece
from transformers import SeamlessM4Tv2Model, AutoProcessor
import torch
import scipy
import torchaudio
# Only enable for x86 ARM machines, ignores error thrown when running on ARM
# Proceeds to generate the final audio file, but still throws NNPACK! not supported error
torch.backends.nnpack.enabled = False
def initialize_model():
if torch.backends.mps.is_available():
device = torch.device("mps")
print("running on mps")
elif torch.cuda.is_available():
device = torch.device("cuda")
print("running on cuda")
else:
device = torch.device("cpu")
print("running on cpu")
device = "cpu" #temp fix for cuda issue
processor = AutoProcessor.from_pretrained("facebook/seamless-m4t-v2-large")
model = SeamlessM4Tv2Model.from_pretrained("facebook/seamless-m4t-v2-large")
model = model.to(torch.device(device))
return model, processor, device
model, processor, device = initialize_model()
# t2s translation
def text_to_speech(text: str, src_lang: str, tgt_lang: str, output_path: str):
text_inputs = processor(text = text, src_lang=src_lang, return_tensors="pt").to(device)
audio_array_from_text = model.generate(**text_inputs, tgt_lang=tgt_lang)[0].cpu().numpy().squeeze()
scipy.io.wavfile.write(output_path, rate=16000, data=audio_array_from_text)
# s2s translation
def speech_to_speech(file_path: str, tgt_lang: str, output_path: str):
### note audio is a tensor of the .wav, and also returns the inital sample rate
audio, orig_freq = torchaudio.load(uri=file_path, format='wav', backend="soundfile") # pip install soundfile
if orig_freq!= 16000:
audio = torchaudio.functional.resample(audio, orig_freq, new_freq=16000) # must be a 16 kHz waveform array
audio_inputs = processor(audios=audio , sampling_rate=16000,return_tensors="pt").to(device)
audio_array_from_audio = model.generate(**audio_inputs, tgt_lang=tgt_lang)[0].cpu().numpy().squeeze()
scipy.io.wavfile.write(output_path, rate=16000, data=audio_array_from_audio)