shahukareem commited on
Commit
50cf541
1 Parent(s): ec3d253

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -12
app.py CHANGED
@@ -2,23 +2,22 @@ import gradio as gr
2
  import numpy as np
3
  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", device=device)
13
 
14
  # load text-to-speech checkpoint and speaker embeddings
15
- processor = SpeechT5Processor.from_pretrained("sanchit-gandhi/speecht5_tts_vox_nl")
16
-
17
- model = SpeechT5ForTextToSpeech.from_pretrained("sanchit-gandhi/speecht5_tts_vox_nl")
18
  vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan")
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
  replacements = [
24
  ("à", "a"),
@@ -48,7 +47,6 @@ def synthesise(text):
48
  speech = model.generate_speech(inputs["input_ids"].to(device), speaker_embeddings.to(device), vocoder=vocoder)
49
  return speech.cpu()
50
 
51
-
52
  def speech_to_speech_translation(audio):
53
  translated_text = translate(audio)
54
  synthesised_speech = synthesise(translated_text)
@@ -58,9 +56,8 @@ def speech_to_speech_translation(audio):
58
 
59
  title = "Cascaded STST"
60
  description = """
61
- 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 Microsoft's
62
- [SpeechT5 TTS](https://huggingface.co/microsoft/speecht5_tts) model for text-to-speech:
63
-
64
  ![Cascaded STST](https://huggingface.co/datasets/huggingface-course/audio-course-images/resolve/main/s2st_cascaded.png "Diagram of cascaded speech to speech translation")
65
  """
66
 
@@ -78,7 +75,7 @@ file_translate = gr.Interface(
78
  fn=speech_to_speech_translation,
79
  inputs=gr.Audio(source="upload", type="filepath"),
80
  outputs=gr.Audio(label="Generated Speech", type="numpy"),
81
- examples=[["./example.wav"]],
82
  title=title,
83
  description=description,
84
  )
 
2
  import numpy as np
3
  import torch
4
  from datasets import load_dataset
 
5
  from transformers import SpeechT5ForTextToSpeech, SpeechT5HifiGan, SpeechT5Processor, pipeline
6
 
7
 
8
  device = "cuda:0" if torch.cuda.is_available() else "cpu"
9
 
10
  # load speech translation checkpoint
11
+ asr_pipe = pipeline("automatic-speech-recognition", model="openai/whisper-large-v2", device=device)
12
 
13
  # load text-to-speech checkpoint and speaker embeddings
14
+ model_id = "sanchit-gandhi/speecht5_tts_vox_nl" # update with your model id
15
+ model = SpeechT5ForTextToSpeech.from_pretrained(model_id)
 
16
  vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan")
 
17
  embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")
18
+ speaker_embeddings = torch.tensor(embeddings_dataset[7440]["xvector"]).unsqueeze(0)
19
+
20
+ processor = SpeechT5Processor.from_pretrained(model_id)
21
 
22
  replacements = [
23
  ("à", "a"),
 
47
  speech = model.generate_speech(inputs["input_ids"].to(device), speaker_embeddings.to(device), vocoder=vocoder)
48
  return speech.cpu()
49
 
 
50
  def speech_to_speech_translation(audio):
51
  translated_text = translate(audio)
52
  synthesised_speech = synthesise(translated_text)
 
56
 
57
  title = "Cascaded STST"
58
  description = """
59
+ Demo for cascaded speech-to-speech translation (STST), mapping from source speech in any language to target speech in French. Demo uses OpenAI's [Whisper Large v2](https://huggingface.co/openai/whisper-large-v2) model for speech translation, and [ckandemir/speecht5_finetuned_voxpopuli_fr"](https://huggingface.co/ckandemir/speecht5_finetuned_voxpopuli_fr) checkpoint for text-to-speech, which is based on Microsoft's
60
+ [SpeechT5 TTS](https://huggingface.co/microsoft/speecht5_tts) model for text-to-speech, fine-tuned in French Audio dataset:
 
61
  ![Cascaded STST](https://huggingface.co/datasets/huggingface-course/audio-course-images/resolve/main/s2st_cascaded.png "Diagram of cascaded speech to speech translation")
62
  """
63
 
 
75
  fn=speech_to_speech_translation,
76
  inputs=gr.Audio(source="upload", type="filepath"),
77
  outputs=gr.Audio(label="Generated Speech", type="numpy"),
78
+ examples=[["/content/example.wav"]],
79
  title=title,
80
  description=description,
81
  )