Kajtson commited on
Commit
ef40c7c
1 Parent(s): f502bb0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -15
app.py CHANGED
@@ -13,40 +13,38 @@ device = "cuda:0" if torch.cuda.is_available() else "cpu"
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("Kajtson/speecht5_finetuned_voxpopuli_pl")
17
-
18
- model = SpeechT5ForTextToSpeech.from_pretrained("Kajtson/speecht5_finetuned_voxpopuli_pl").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[1337]["xvector"]).unsqueeze(0)
23
-
24
- target_dtype = np.int16
25
- max_range = np.iinfo(target_dtype).max
26
 
27
  def translate(audio):
28
- outputs = asr_pipe(audio, max_new_tokens=200, generate_kwargs={"task": "transcribe", "language": "pl"})
29
  return outputs["text"]
30
 
31
 
32
  def synthesise(text):
33
- inputs = processor(text=text, return_tensors="pt")
34
- speech = model.generate_speech(inputs["input_ids"].to(device), speaker_embeddings.to(device), vocoder=vocoder)
 
 
 
 
 
35
  return speech.cpu()
36
 
37
 
38
  def speech_to_speech_translation(audio):
39
  translated_text = translate(audio)
40
  synthesised_speech = synthesise(translated_text)
41
- synthesised_speech = (synthesised_speech.numpy() * max_range).astype(np.int16)
42
  return 16000, synthesised_speech
43
 
44
 
45
  title = "Cascaded STST"
46
  description = """
47
- Demo for cascaded speech-to-speech translation (STST), mapping from source speech in any language to target speech in English. Demo uses OpenAI's [Whisper Base](https://huggingface.co/openai/whisper-base) model for speech translation, and fine-tuned Microsoft's
48
- [SpeechT5 TTS](https://huggingface.co/Kajtson/speecht5_finetuned_voxpopuli_pl) model for text-to-speech:
49
-
50
  ![Cascaded STST](https://huggingface.co/datasets/huggingface-course/audio-course-images/resolve/main/s2st_cascaded.png "Diagram of cascaded speech to speech translation")
51
  """
52
 
 
13
  asr_pipe = pipeline("automatic-speech-recognition", model="openai/whisper-base", device=device)
14
 
15
  # load text-to-speech checkpoint and speaker embeddings
16
+ model = VitsModel.from_pretrained("Matthijs/mms-tts-deu")
17
+ tokenizer = VitsTokenizer.from_pretrained("Matthijs/mms-tts-deu")
18
+ # vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan").to(device)
 
19
 
20
  embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")
21
+ speaker_embeddings = torch.tensor(embeddings_dataset[7306]["xvector"]).unsqueeze(0)
 
 
 
22
 
23
  def translate(audio):
24
+ outputs = asr_pipe(audio, max_new_tokens=256, generate_kwargs={"task": "transcribe", "language": "de"})
25
  return outputs["text"]
26
 
27
 
28
  def synthesise(text):
29
+ inputs = tokenizer(text=text, return_tensors="pt")
30
+ input_ids = inputs["input_ids"]
31
+
32
+ with torch.no_grad():
33
+ outputs = model(input_ids)
34
+
35
+ speech = outputs.audio[0]
36
  return speech.cpu()
37
 
38
 
39
  def speech_to_speech_translation(audio):
40
  translated_text = translate(audio)
41
  synthesised_speech = synthesise(translated_text)
42
+ synthesised_speech = (synthesised_speech.numpy() * 32767).astype(np.int16)
43
  return 16000, synthesised_speech
44
 
45
 
46
  title = "Cascaded STST"
47
  description = """
 
 
 
48
  ![Cascaded STST](https://huggingface.co/datasets/huggingface-course/audio-course-images/resolve/main/s2st_cascaded.png "Diagram of cascaded speech to speech translation")
49
  """
50