Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -4,12 +4,14 @@ import torch
|
|
4 |
from datasets import load_dataset
|
5 |
|
6 |
from transformers import SpeechT5ForTextToSpeech, SpeechT5HifiGan, SpeechT5Processor, pipeline
|
|
|
7 |
|
8 |
|
9 |
-
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
10 |
|
11 |
# load speech translation checkpoint
|
12 |
-
asr_pipe = pipeline("automatic-speech-recognition", model="openai/whisper-base"
|
|
|
13 |
|
14 |
# load text-to-speech checkpoint and speaker embeddings
|
15 |
processor = SpeechT5Processor.from_pretrained("microsoft/speecht5_tts")
|
@@ -20,18 +22,18 @@ vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan").to(devic
|
|
20 |
embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")
|
21 |
speaker_embeddings = torch.tensor(embeddings_dataset[7306]["xvector"]).unsqueeze(0)
|
22 |
|
23 |
-
|
24 |
def translate(audio):
|
25 |
outputs = asr_pipe(audio, max_new_tokens=256, generate_kwargs={"task": "translate"})
|
26 |
return outputs["text"]
|
27 |
|
28 |
-
|
29 |
def synthesise(text):
|
30 |
inputs = processor(text=text, return_tensors="pt")
|
31 |
speech = model.generate_speech(inputs["input_ids"].to(device), speaker_embeddings.to(device), vocoder=vocoder)
|
32 |
return speech.cpu()
|
33 |
|
34 |
-
|
35 |
def speech_to_speech_translation(audio):
|
36 |
translated_text = translate(audio)
|
37 |
synthesised_speech = synthesise(translated_text)
|
|
|
4 |
from datasets import load_dataset
|
5 |
|
6 |
from transformers import SpeechT5ForTextToSpeech, SpeechT5HifiGan, SpeechT5Processor, pipeline
|
7 |
+
import spaces
|
8 |
|
9 |
|
10 |
+
# device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
11 |
|
12 |
# load speech translation checkpoint
|
13 |
+
asr_pipe = pipeline("automatic-speech-recognition", model="openai/whisper-base")
|
14 |
+
asr_pipe.to('cuda')
|
15 |
|
16 |
# load text-to-speech checkpoint and speaker embeddings
|
17 |
processor = SpeechT5Processor.from_pretrained("microsoft/speecht5_tts")
|
|
|
22 |
embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")
|
23 |
speaker_embeddings = torch.tensor(embeddings_dataset[7306]["xvector"]).unsqueeze(0)
|
24 |
|
25 |
+
@spaces.GPU
|
26 |
def translate(audio):
|
27 |
outputs = asr_pipe(audio, max_new_tokens=256, generate_kwargs={"task": "translate"})
|
28 |
return outputs["text"]
|
29 |
|
30 |
+
@spaces.GPU
|
31 |
def synthesise(text):
|
32 |
inputs = processor(text=text, return_tensors="pt")
|
33 |
speech = model.generate_speech(inputs["input_ids"].to(device), speaker_embeddings.to(device), vocoder=vocoder)
|
34 |
return speech.cpu()
|
35 |
|
36 |
+
@spaces.GPU
|
37 |
def speech_to_speech_translation(audio):
|
38 |
translated_text = translate(audio)
|
39 |
synthesised_speech = synthesise(translated_text)
|