preetam8 commited on
Commit
32e9053
·
1 Parent(s): 49c1298

Remove ASR pipeline, use model directly to set forced decoder ids

Browse files
Files changed (1) hide show
  1. app.py +11 -4
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, pipeline
 
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", device=device)
 
 
 
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
- outputs = asr_pipe(audio, max_new_tokens=256, generate_kwargs={"task": "transcribe", "language": "french"})
27
- return outputs["text"]
 
 
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):