Remove ASR pipeline, use model directly to set forced decoder ids
Browse files
app.py
CHANGED
@@ -4,13 +4,18 @@ import numpy as np
|
|
4 |
import torch
|
5 |
from datasets import load_dataset
|
6 |
|
7 |
-
from transformers import SpeechT5ForTextToSpeech, SpeechT5HifiGan, SpeechT5Processor
|
|
|
8 |
|
9 |
|
10 |
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
11 |
|
|
|
12 |
# load speech translation checkpoint
|
13 |
-
|
|
|
|
|
|
|
14 |
|
15 |
# load text-to-speech checkpoint and speaker embeddings
|
16 |
processor = SpeechT5Processor.from_pretrained("microsoft/speecht5_tts")
|
@@ -23,8 +28,10 @@ speaker_embeddings = torch.tensor(embeddings_dataset[7306]["xvector"]).unsqueeze
|
|
23 |
|
24 |
|
25 |
def translate(audio):
|
26 |
-
|
27 |
-
|
|
|
|
|
28 |
|
29 |
|
30 |
def synthesise(text):
|
|
|
4 |
import torch
|
5 |
from datasets import load_dataset
|
6 |
|
7 |
+
from transformers import SpeechT5ForTextToSpeech, SpeechT5HifiGan, SpeechT5Processor
|
8 |
+
from transformers import WhisperForConditionalGeneration, WhisperProcessor
|
9 |
|
10 |
|
11 |
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
12 |
|
13 |
+
target_language = "french"
|
14 |
# load speech translation checkpoint
|
15 |
+
whisper_model_name = "openai/whisper-base"
|
16 |
+
whisper_processor = WhisperProcessor.from_pretrained(whisper_model_name)
|
17 |
+
whisper_model = WhisperForConditionalGeneration.from_pretrained(whisper_model_name)
|
18 |
+
decoder_ids = whisper_processor.get_decoder_prompt_ids(language=target_language, task="transcribe")
|
19 |
|
20 |
# load text-to-speech checkpoint and speaker embeddings
|
21 |
processor = SpeechT5Processor.from_pretrained("microsoft/speecht5_tts")
|
|
|
28 |
|
29 |
|
30 |
def translate(audio):
|
31 |
+
input_features = whisper_processor(audio["array"], sampling_rate=16000, return_tensors="pt").input_features
|
32 |
+
predicted_ids = whisper_model.generate(input_features, forced_decoder_ids=decoder_ids)
|
33 |
+
translated_text = whisper_processor.batch_decode(predicted_ids, skip_special_tokens=True)[0]
|
34 |
+
return translated_text
|
35 |
|
36 |
|
37 |
def synthesise(text):
|