levente-murgas
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -5,6 +5,35 @@ 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 |
|
@@ -12,17 +41,17 @@ device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
|
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("
|
16 |
|
17 |
-
model = SpeechT5ForTextToSpeech.from_pretrained("
|
18 |
vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan").to(device)
|
19 |
|
20 |
-
|
21 |
-
speaker_embeddings = torch.tensor(
|
22 |
|
23 |
|
24 |
def translate(audio):
|
25 |
-
outputs = asr_pipe(audio, max_new_tokens=256, generate_kwargs={"task": "
|
26 |
return outputs["text"]
|
27 |
|
28 |
|
@@ -34,6 +63,8 @@ def synthesise(text):
|
|
34 |
|
35 |
def speech_to_speech_translation(audio):
|
36 |
translated_text = translate(audio)
|
|
|
|
|
37 |
synthesised_speech = synthesise(translated_text)
|
38 |
synthesised_speech = (synthesised_speech.numpy() * 32767).astype(np.int16)
|
39 |
return 16000, synthesised_speech
|
@@ -42,8 +73,7 @@ def speech_to_speech_translation(audio):
|
|
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 |
|
@@ -51,7 +81,7 @@ demo = gr.Blocks()
|
|
51 |
|
52 |
mic_translate = gr.Interface(
|
53 |
fn=speech_to_speech_translation,
|
54 |
-
inputs=gr.Audio(
|
55 |
outputs=gr.Audio(label="Generated Speech", type="numpy"),
|
56 |
title=title,
|
57 |
description=description,
|
@@ -59,7 +89,7 @@ mic_translate = gr.Interface(
|
|
59 |
|
60 |
file_translate = gr.Interface(
|
61 |
fn=speech_to_speech_translation,
|
62 |
-
inputs=gr.Audio(
|
63 |
outputs=gr.Audio(label="Generated Speech", type="numpy"),
|
64 |
examples=[["./example.wav"]],
|
65 |
title=title,
|
@@ -69,4 +99,4 @@ file_translate = gr.Interface(
|
|
69 |
with demo:
|
70 |
gr.TabbedInterface([mic_translate, file_translate], ["Microphone", "Audio File"])
|
71 |
|
72 |
-
demo.launch()
|
|
|
5 |
|
6 |
from transformers import SpeechT5ForTextToSpeech, SpeechT5HifiGan, SpeechT5Processor, pipeline
|
7 |
|
8 |
+
speaker_embedding_path = "./speaker_embedding.npy"
|
9 |
+
|
10 |
+
replacements = [
|
11 |
+
("&", "og"),
|
12 |
+
("\r", " "),
|
13 |
+
("´", ""),
|
14 |
+
("\\", ""),
|
15 |
+
("¨", " "),
|
16 |
+
("Å", "AA"),
|
17 |
+
("Æ", "AE"),
|
18 |
+
("É", "E"),
|
19 |
+
("Ö", "OE"),
|
20 |
+
("Ø", "OE"),
|
21 |
+
("á", "a"),
|
22 |
+
("ä", "ae"),
|
23 |
+
("å", "aa"),
|
24 |
+
("è", "e"),
|
25 |
+
("î", "i"),
|
26 |
+
("ô", "oe"),
|
27 |
+
("ö", "oe"),
|
28 |
+
("ø", "oe"),
|
29 |
+
("ü", "y"),
|
30 |
+
]
|
31 |
+
|
32 |
+
|
33 |
+
def replace_danish_letters(text):
|
34 |
+
for src, dst in replacements:
|
35 |
+
text = text.replace(src, dst)
|
36 |
+
return text
|
37 |
|
38 |
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
39 |
|
|
|
41 |
asr_pipe = pipeline("automatic-speech-recognition", model="openai/whisper-base", device=device)
|
42 |
|
43 |
# load text-to-speech checkpoint and speaker embeddings
|
44 |
+
processor = SpeechT5Processor.from_pretrained("JackismyShephard/speecht5_tts-finetuned-nst-da")
|
45 |
|
46 |
+
model = SpeechT5ForTextToSpeech.from_pretrained("JackismyShephard/speecht5_tts-finetuned-nst-da").to(device)
|
47 |
vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan").to(device)
|
48 |
|
49 |
+
speaker_embedding = np.load(speaker_embedding_path)
|
50 |
+
speaker_embeddings = torch.tensor(speaker_embedding).unsqueeze(0)
|
51 |
|
52 |
|
53 |
def translate(audio):
|
54 |
+
outputs = asr_pipe(audio, max_new_tokens=256, generate_kwargs={"task": "transcribe", "language": "da"})
|
55 |
return outputs["text"]
|
56 |
|
57 |
|
|
|
63 |
|
64 |
def speech_to_speech_translation(audio):
|
65 |
translated_text = translate(audio)
|
66 |
+
translated_text = replace_danish_letters(translated_text)
|
67 |
+
print(translated_text)
|
68 |
synthesised_speech = synthesise(translated_text)
|
69 |
synthesised_speech = (synthesised_speech.numpy() * 32767).astype(np.int16)
|
70 |
return 16000, synthesised_speech
|
|
|
73 |
title = "Cascaded STST"
|
74 |
description = """
|
75 |
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
|
76 |
+
[SpeechT5 TTS](https://huggingface.co/microsoft/speecht5_tts) model fine-tuned by [JackismyShephard](https://huggingface.co/JackismyShephard) for Danish for text-to-speech:
|
|
|
77 |
![Cascaded STST](https://huggingface.co/datasets/huggingface-course/audio-course-images/resolve/main/s2st_cascaded.png "Diagram of cascaded speech to speech translation")
|
78 |
"""
|
79 |
|
|
|
81 |
|
82 |
mic_translate = gr.Interface(
|
83 |
fn=speech_to_speech_translation,
|
84 |
+
inputs=gr.Audio(sources=["microphone"], type="filepath"),
|
85 |
outputs=gr.Audio(label="Generated Speech", type="numpy"),
|
86 |
title=title,
|
87 |
description=description,
|
|
|
89 |
|
90 |
file_translate = gr.Interface(
|
91 |
fn=speech_to_speech_translation,
|
92 |
+
inputs=gr.Audio(sources=["upload"], type="filepath"),
|
93 |
outputs=gr.Audio(label="Generated Speech", type="numpy"),
|
94 |
examples=[["./example.wav"]],
|
95 |
title=title,
|
|
|
99 |
with demo:
|
100 |
gr.TabbedInterface([mic_translate, file_translate], ["Microphone", "Audio File"])
|
101 |
|
102 |
+
demo.launch()
|