magnustragardh commited on
Commit
c399ca6
1 Parent(s): 3be9059

Attempt to use German.

Browse files
Files changed (1) hide show
  1. app.py +22 -6
app.py CHANGED
@@ -8,11 +8,26 @@ from transformers import SpeechT5ForTextToSpeech, SpeechT5HifiGan, SpeechT5Proce
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
- tts_model = "sanchit-gandhi/speecht5_tts_vox_nl"
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  processor = SpeechT5Processor.from_pretrained(tts_model)
17
 
18
  model = SpeechT5ForTextToSpeech.from_pretrained(tts_model).to(device)
@@ -23,13 +38,14 @@ 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": "nl"})
27
  return outputs["text"]
28
 
29
 
30
  def synthesise(text):
31
  inputs = processor(text=text, return_tensors="pt")
32
- speech = model.generate_speech(inputs["input_ids"].to(device), speaker_embeddings.to(device), vocoder=vocoder)
 
33
  return speech.cpu()
34
 
35
 
@@ -41,8 +57,8 @@ def speech_to_speech_translation(audio):
41
 
42
 
43
  title = "Cascaded STST"
44
- description = """
45
- 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
46
  [SpeechT5 TTS](https://huggingface.co/microsoft/speecht5_tts) model for text-to-speech:
47
 
48
  ![Cascaded STST](https://huggingface.co/datasets/huggingface-course/audio-course-images/resolve/main/s2st_cascaded.png "Diagram of cascaded speech to speech translation")
@@ -70,4 +86,4 @@ file_translate = gr.Interface(
70
  with demo:
71
  gr.TabbedInterface([mic_translate, file_translate], ["Microphone", "Audio File"])
72
 
73
- demo.launch()
 
8
 
9
  device = "cuda:0" if torch.cuda.is_available() else "cpu"
10
 
11
+ language = "de"
12
+
13
  # load speech translation checkpoint
14
  asr_pipe = pipeline("automatic-speech-recognition", model="openai/whisper-base", device=device)
15
 
16
  # load text-to-speech checkpoint and speaker embeddings
17
+ if language == "nl":
18
+ tts_model = "sanchit-gandhi/speecht5_tts_vox_nl"
19
+ language_name = "Dutch"
20
+ elif language == "fi":
21
+ tts_model = "crcdng/speecht5_finetuned_voxpopuli_fi"
22
+ language_name = "Finnish"
23
+ elif language == "fr":
24
+ tts_model = "Sandiago21/speecht5_finetuned_facebook_voxpopuli_french"
25
+ language_name = "French"
26
+ elif language == "de":
27
+ tts_model = "Salama1429/TTS_German_Speecht5_finetuned_voxpopuli_nl"
28
+ language_name = "German"
29
+ else:
30
+ raise NotImplementedError(f"No support for language {language}")
31
  processor = SpeechT5Processor.from_pretrained(tts_model)
32
 
33
  model = SpeechT5ForTextToSpeech.from_pretrained(tts_model).to(device)
 
38
 
39
 
40
  def translate(audio):
41
+ outputs = asr_pipe(audio, max_new_tokens=256, generate_kwargs={"task": "transcribe", "language": language})
42
  return outputs["text"]
43
 
44
 
45
  def synthesise(text):
46
  inputs = processor(text=text, return_tensors="pt")
47
+ max_length = processor.tokenizer.model_max_length
48
+ speech = model.generate_speech(inputs["input_ids"][:, :max_length].to(device), speaker_embeddings.to(device), vocoder=vocoder)
49
  return speech.cpu()
50
 
51
 
 
57
 
58
 
59
  title = "Cascaded STST"
60
+ description = f"""
61
+ Demo for cascaded speech-to-speech translation (STST), mapping from source speech in any language to target speech in {language_name}. 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")
 
86
  with demo:
87
  gr.TabbedInterface([mic_translate, file_translate], ["Microphone", "Audio File"])
88
 
89
+ demo.launch(debug=True)