Ellight commited on
Commit
b9bb0a2
·
verified ·
1 Parent(s): 8cfd37e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -14
app.py CHANGED
@@ -2,31 +2,54 @@ 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("Ellight/speecht5_finetuned_voxpopuli_nl")
16
-
17
- model = SpeechT5ForTextToSpeech.from_pretrained("Ellight/speecht5_finetuned_voxpopuli_nl").to(device)
18
- vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan").to(device)
19
-
20
- embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation",trust_remote_code=True)
21
- speaker_embeddings = torch.tensor(embeddings_dataset[7306]["xvector"]).unsqueeze(0)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
 
23
 
24
  def translate(audio):
25
- outputs = asr_pipe(audio, max_new_tokens=256, generate_kwargs={"task": "translate"})
26
  return outputs["text"]
27
 
28
 
29
  def synthesise(text):
 
30
  inputs = processor(text=text, return_tensors="pt")
31
  speech = model.generate_speech(inputs["input_ids"].to(device), speaker_embeddings.to(device), vocoder=vocoder)
32
  return speech.cpu()
@@ -41,9 +64,8 @@ def speech_to_speech_translation(audio):
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 English. Demo uses OpenAI's [Whisper Base](https://huggingface.co/openai/whisper-base) model for speech translation, and Microsoft's
45
- [SpeechT5 TTS](https://huggingface.co/microsoft/speecht5_tts) model for text-to-speech:
46
-
47
  ![Cascaded STST](https://huggingface.co/datasets/huggingface-course/audio-course-images/resolve/main/s2st_cascaded.png "Diagram of cascaded speech to speech translation")
48
  """
49
 
@@ -69,4 +91,4 @@ file_translate = gr.Interface(
69
  with demo:
70
  gr.TabbedInterface([mic_translate, file_translate], ["Microphone", "Audio File"])
71
 
72
- demo.launch()
 
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 = "Ellight/speecht5_finetuned_voxpopuli_nl" # update with your model id
15
+ # pipe = pipeline("automatic-speech-recognition", model=model_id)
16
+ model = SpeechT5ForTextToSpeech.from_pretrained(model_id)
17
+ vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan")
18
+ embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")
19
+ speaker_embeddings = torch.tensor(embeddings_dataset[7440]["xvector"]).unsqueeze(0)
20
+
21
+ processor = SpeechT5Processor.from_pretrained(model_id)
22
+
23
+ replacements = [
24
+ ("à", "a"),
25
+ ("ç", "c"),
26
+ ("è", "e"),
27
+ ("ë", "e"),
28
+ ("í", "i"),
29
+ ("ï", "i"),
30
+ ("ö", "o"),
31
+ ("ü", "u"),
32
+ ]
33
+
34
+ def cleanup_text(text):
35
+ for src, dst in replacements:
36
+ text = text.replace(src, dst)
37
+ return text
38
+
39
+ def synthesize_speech(text):
40
+ text = cleanup_text(text)
41
+ inputs = processor(text=text, return_tensors="pt")
42
+ speech = model.generate_speech(inputs["input_ids"].to(device), speaker_embeddings.to(device), vocoder=vocoder)
43
 
44
+ return gr.Audio.update(value=(16000, speech.cpu().numpy()))
45
 
46
  def translate(audio):
47
+ outputs = asr_pipe(audio, max_new_tokens=256, generate_kwargs={"task": "transcribe", "language": "Dutch"})
48
  return outputs["text"]
49
 
50
 
51
  def synthesise(text):
52
+ text = cleanup_text(text)
53
  inputs = processor(text=text, return_tensors="pt")
54
  speech = model.generate_speech(inputs["input_ids"].to(device), speaker_embeddings.to(device), vocoder=vocoder)
55
  return speech.cpu()
 
64
 
65
  title = "Cascaded STST"
66
  description = """
67
+ 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 Large v2](https://huggingface.co/openai/whisper-large-v2) model for speech translation, and [Sandiago21/speecht5_finetuned_voxpopuli_it](https://huggingface.co/Sandiago21/speecht5_finetuned_voxpopuli_it) checkpoint for text-to-speech, which is based on Microsoft's
68
+ [SpeechT5 TTS](https://huggingface.co/microsoft/speecht5_tts) model for text-to-speech, fine-tuned in Dutch Audio dataset:
 
69
  ![Cascaded STST](https://huggingface.co/datasets/huggingface-course/audio-course-images/resolve/main/s2st_cascaded.png "Diagram of cascaded speech to speech translation")
70
  """
71
 
 
91
  with demo:
92
  gr.TabbedInterface([mic_translate, file_translate], ["Microphone", "Audio File"])
93
 
94
+ demo.launch()