Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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 |
-
|
17 |
-
|
18 |
-
|
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[
|
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=
|
29 |
return outputs["text"]
|
30 |
|
31 |
|
32 |
def synthesise(text):
|
33 |
-
inputs =
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
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() *
|
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 |
|