Update app.py
Browse files
app.py
CHANGED
@@ -5,7 +5,6 @@ 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
|
@@ -20,51 +19,43 @@ vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan").to(devic
|
|
20 |
embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")
|
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()
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
synthesised_speech = synthesise(translated_text)
|
38 |
synthesised_speech = (synthesised_speech.numpy() * 32767).astype(np.int16)
|
39 |
return 16000, synthesised_speech
|
40 |
|
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
|
45 |
[SpeechT5 TTS](https://huggingface.co/microsoft/speecht5_tts) model for text-to-speech:
|
46 |
-
|
47 |

|
48 |
"""
|
49 |
|
50 |
demo = gr.Blocks()
|
51 |
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
outputs=gr.Audio(label="Generated Speech", type="numpy"),
|
64 |
-
examples=[["./example.wav"]],
|
65 |
-
title=title,
|
66 |
-
description=description,
|
67 |
-
)
|
68 |
|
69 |
with demo:
|
70 |
gr.TabbedInterface([mic_translate, file_translate], ["Microphone", "Audio File"])
|
|
|
5 |
|
6 |
from transformers import SpeechT5ForTextToSpeech, SpeechT5HifiGan, SpeechT5Processor, pipeline
|
7 |
|
|
|
8 |
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
9 |
|
10 |
# load speech translation checkpoint
|
|
|
19 |
embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")
|
20 |
speaker_embeddings = torch.tensor(embeddings_dataset[7306]["xvector"]).unsqueeze(0)
|
21 |
|
22 |
+
languages = ["id"]
|
23 |
|
24 |
+
def translate(audio, target_language):
|
25 |
+
outputs = asr_pipe(audio, max_new_tokens=256, generate_kwargs={"task": "translate", "language": target_language})
|
26 |
return outputs["text"]
|
27 |
|
28 |
+
def synthesise(text, target_language):
|
|
|
29 |
inputs = processor(text=text, return_tensors="pt")
|
30 |
speech = model.generate_speech(inputs["input_ids"].to(device), speaker_embeddings.to(device), vocoder=vocoder)
|
31 |
return speech.cpu()
|
32 |
|
33 |
+
def speech_to_speech_translation(audio, target_language):
|
34 |
+
translated_text = translate(audio, target_language)
|
35 |
+
synthesised_speech = synthesise(translated_text, target_language)
|
|
|
36 |
synthesised_speech = (synthesised_speech.numpy() * 32767).astype(np.int16)
|
37 |
return 16000, synthesised_speech
|
38 |
|
39 |
+
title = "Multilingual Cascaded STST"
|
|
|
40 |
description = """
|
41 |
+
Demo for cascaded speech-to-speech translation (STST), mapping from source speech in any language to target speech in another language. Demo uses OpenAI's [Whisper Base](https://huggingface.co/openai/whisper-base) model for speech translation, and Microsoft's
|
42 |
[SpeechT5 TTS](https://huggingface.co/microsoft/speecht5_tts) model for text-to-speech:
|
|
|
43 |

|
44 |
"""
|
45 |
|
46 |
demo = gr.Blocks()
|
47 |
|
48 |
+
def create_interface(source):
|
49 |
+
return gr.Interface(
|
50 |
+
fn=speech_to_speech_translation,
|
51 |
+
inputs=[gr.Audio(source=source, type="filepath"), gr.Dropdown(choices=languages, label="Target Language")],
|
52 |
+
outputs=gr.Audio(label="Generated Speech", type="numpy"),
|
53 |
+
title=title,
|
54 |
+
description=description,
|
55 |
+
)
|
56 |
+
|
57 |
+
mic_translate = create_interface("microphone")
|
58 |
+
file_translate = create_interface("upload")
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
with demo:
|
61 |
gr.TabbedInterface([mic_translate, file_translate], ["Microphone", "Audio File"])
|