thuyentruong commited on
Commit
ba496cd
1 Parent(s): 0bd83b4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -12
app.py CHANGED
@@ -2,7 +2,6 @@ import gradio as gr
2
  import numpy as np
3
  import torch
4
  from datasets import load_dataset
5
- from transformers import VitsModel, VitsTokenizer
6
 
7
  from transformers import SpeechT5ForTextToSpeech, SpeechT5HifiGan, SpeechT5Processor, pipeline
8
 
@@ -11,24 +10,38 @@ 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
- translator = pipeline("translation", model="Helsinki-NLP/opus-mt-en-ru")
15
 
 
 
 
16
 
17
- model = VitsModel.from_pretrained('facebook/mms-tts-rus').to(device)
18
- tokenizer = VitsTokenizer.from_pretrained('facebook/mms-tts-rus')
19
 
 
 
20
 
21
 
22
  def translate(audio):
23
- outputs = asr_pipe(audio, max_new_tokens=256, generate_kwargs={"task": "translate"})
24
- return translator(outputs['text'])[0]['translation_text']
 
 
25
 
26
 
27
  def synthesise(text):
28
- inputs = tokenizer(text=text, return_tensors="pt",max_length=598,
29
- truncation=True,)
30
- with torch.no_grad():
31
- speech = model(**inputs).waveform
 
 
 
 
 
 
 
 
32
  return speech.cpu()
33
 
34
 
@@ -36,12 +49,13 @@ def speech_to_speech_translation(audio):
36
  translated_text = translate(audio)
37
  synthesised_speech = synthesise(translated_text)
38
  synthesised_speech = (synthesised_speech.numpy() * 32767).astype(np.int16)
39
- return 16000, synthesised_speech[0]
40
 
41
 
42
  title = "Cascaded STST"
43
  description = """
44
- Demo for cascaded speech-to-speech translation (STST), mapping from source speech in any language to target speech in Russian
 
45
  ![Cascaded STST](https://huggingface.co/datasets/huggingface-course/audio-course-images/resolve/main/s2st_cascaded.png "Diagram of cascaded speech to speech translation")
46
  """
47
 
 
2
  import numpy as np
3
  import torch
4
  from datasets import load_dataset
 
5
 
6
  from transformers import SpeechT5ForTextToSpeech, SpeechT5HifiGan, SpeechT5Processor, pipeline
7
 
 
10
 
11
  # load speech translation checkpoint
12
  asr_pipe = pipeline("automatic-speech-recognition", model="openai/whisper-base", device=device)
 
13
 
14
+ # load text-to-speech checkpoint and speaker embeddings
15
+ tts_checkpoint = "sanchit-gandhi/speecht5_tts_vox_nl"
16
+ processor = SpeechT5Processor.from_pretrained(tts_checkpoint)
17
 
18
+ model = SpeechT5ForTextToSpeech.from_pretrained(tts_checkpoint).to(device)
19
+ vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan").to(device)
20
 
21
+ embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")
22
+ speaker_embeddings = torch.tensor(embeddings_dataset[7306]["xvector"]).unsqueeze(0)
23
 
24
 
25
  def translate(audio):
26
+ # Trick Whisper to translate from any language to Dutch.
27
+ # Note that using task=translate will translate to English instead.
28
+ outputs = asr_pipe(audio, max_new_tokens=256, generate_kwargs={"task": "transcribe", "language": "dutch"})
29
+ return outputs["text"]
30
 
31
 
32
  def synthesise(text):
33
+ # Need to specific truncate to max text positions.
34
+ # Otherwise model.generate_speech will throw errors.
35
+ inputs = processor(
36
+ text=text,
37
+ # max_length=200,
38
+ max_length=598,
39
+ truncation=True,
40
+ # padding=True,
41
+ return_tensors="pt"
42
+ )
43
+ # inputs = processor(text=text, return_tensors="pt")
44
+ speech = model.generate_speech(inputs["input_ids"].to(device), speaker_embeddings.to(device), vocoder=vocoder)
45
  return speech.cpu()
46
 
47
 
 
49
  translated_text = translate(audio)
50
  synthesised_speech = synthesise(translated_text)
51
  synthesised_speech = (synthesised_speech.numpy() * 32767).astype(np.int16)
52
+ return 16000, synthesised_speech
53
 
54
 
55
  title = "Cascaded STST"
56
  description = """
57
+ Demo for cascaded speech-to-speech translation (STST), mapping from source speech in any language to target speech in Dutch. Demo uses OpenAI's [Whisper Tiny](https://huggingface.co/openai/whisper-tiny) model for speech translation, and a finetuned
58
+ [SpeechT5 TTS](https://huggingface.co/sanchit-gandhi/speecht5_tts_vox_nl) model for text-to-speech:
59
  ![Cascaded STST](https://huggingface.co/datasets/huggingface-course/audio-course-images/resolve/main/s2st_cascaded.png "Diagram of cascaded speech to speech translation")
60
  """
61