Spaces:
Build error
Build error
JackismyShephard
commited on
Commit
·
77862e1
1
Parent(s):
dbfdf1a
implement audio translation to danish speech
Browse files
app.py
CHANGED
@@ -1,72 +1,114 @@
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
import torch
|
4 |
-
from datasets import load_dataset
|
5 |
|
6 |
-
from transformers import
|
7 |
|
|
|
8 |
|
|
|
9 |
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
10 |
|
11 |
# load speech translation checkpoint
|
12 |
-
asr_pipe = pipeline(
|
|
|
|
|
|
|
|
|
13 |
|
14 |
# load text-to-speech checkpoint and speaker embeddings
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
-
|
18 |
-
|
|
|
19 |
|
20 |
-
|
21 |
-
|
22 |
|
23 |
|
24 |
def translate(audio):
|
25 |
-
outputs = asr_pipe(
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
return outputs["text"]
|
27 |
|
28 |
|
29 |
def synthesise(text):
|
30 |
-
|
31 |
-
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
|
35 |
def speech_to_speech_translation(audio):
|
36 |
translated_text = translate(audio)
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
[
|
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 |
|
50 |
-
demo = gr.
|
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,
|
58 |
-
)
|
59 |
-
|
60 |
-
file_translate = gr.Interface(
|
61 |
-
fn=speech_to_speech_translation,
|
62 |
-
inputs=gr.Audio(source="upload", type="filepath"),
|
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"])
|
71 |
-
|
72 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
import torch
|
|
|
4 |
|
5 |
+
from transformers import pipeline
|
6 |
|
7 |
+
checkpoint_finetuned = "JackismyShephard/speecht5_tts-finetuned-nst-da"
|
8 |
|
9 |
+
revision = "5af228df418092b681cf31c31e413bdd2b5f9c8c"
|
10 |
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
11 |
|
12 |
# load speech translation checkpoint
|
13 |
+
asr_pipe = pipeline(
|
14 |
+
"automatic-speech-recognition",
|
15 |
+
model="openai/whisper-base",
|
16 |
+
device=device,
|
17 |
+
)
|
18 |
|
19 |
# load text-to-speech checkpoint and speaker embeddings
|
20 |
+
pipe = pipeline(
|
21 |
+
"text-to-speech",
|
22 |
+
model=checkpoint_finetuned,
|
23 |
+
use_fast=True,
|
24 |
+
device=device,
|
25 |
+
revision=revision,
|
26 |
+
)
|
27 |
|
28 |
+
speaker_embedding_path = "female_23_vestjylland.npy"
|
29 |
+
speaker_embedding = np.load(speaker_embedding_path)
|
30 |
+
speaker_embedding_tensor = torch.tensor(speaker_embedding).unsqueeze(0)
|
31 |
|
32 |
+
target_dtype = np.int16
|
33 |
+
max_range = np.iinfo(target_dtype).max
|
34 |
|
35 |
|
36 |
def translate(audio):
|
37 |
+
outputs = asr_pipe(
|
38 |
+
audio,
|
39 |
+
max_new_tokens=256,
|
40 |
+
batch_size=8,
|
41 |
+
chunk_length_s=30,
|
42 |
+
generate_kwargs={"task": "translate", "language": "danish"},
|
43 |
+
)
|
44 |
return outputs["text"]
|
45 |
|
46 |
|
47 |
def synthesise(text):
|
48 |
+
if len(text.strip()) == 0:
|
49 |
+
return (16000, np.zeros(0))
|
50 |
+
|
51 |
+
text = replace_danish_letters(text)
|
52 |
+
|
53 |
+
forward_params = {"speaker_embeddings": speaker_embedding}
|
54 |
+
speech = pipe(text, forward_params=forward_params)
|
55 |
+
|
56 |
+
sr, audio = speech["sampling_rate"], speech["audio"]
|
57 |
+
|
58 |
+
audio = (audio * max_range).astype(np.int16)
|
59 |
+
|
60 |
+
return sr, audio
|
61 |
|
62 |
|
63 |
def speech_to_speech_translation(audio):
|
64 |
translated_text = translate(audio)
|
65 |
+
return synthesise(translated_text)
|
66 |
+
|
67 |
+
|
68 |
+
def replace_danish_letters(text):
|
69 |
+
for src, dst in replacements:
|
70 |
+
text = text.replace(src, dst)
|
71 |
+
return text
|
72 |
+
|
73 |
+
|
74 |
+
replacements = [
|
75 |
+
("&", "og"),
|
76 |
+
("\r", " "),
|
77 |
+
("´", ""),
|
78 |
+
("\\", ""),
|
79 |
+
("¨", " "),
|
80 |
+
("Å", "AA"),
|
81 |
+
("Æ", "AE"),
|
82 |
+
("É", "E"),
|
83 |
+
("Ö", "OE"),
|
84 |
+
("Ø", "OE"),
|
85 |
+
("á", "a"),
|
86 |
+
("ä", "ae"),
|
87 |
+
("å", "aa"),
|
88 |
+
("è", "e"),
|
89 |
+
("î", "i"),
|
90 |
+
("ô", "oe"),
|
91 |
+
("ö", "oe"),
|
92 |
+
("ø", "oe"),
|
93 |
+
("ü", "y"),
|
94 |
+
]
|
95 |
|
96 |
|
97 |
title = "Cascaded STST"
|
98 |
description = """
|
99 |
+
Demo for cascaded speech-to-speech translation (STST), mapping from source speech in any language to target speech in Danish. Demo uses OpenAI's [Whisper Base](https://huggingface.co/openai/whisper-base) model for speech translation, and JackismyShephard's
|
100 |
+
[speecht5_tts-finetuned-nst-da](https://huggingface.co/JackismyShephard/speecht5_tts-finetuned-nst-da) model for text-to-speech:
|
101 |
|
102 |
![Cascaded STST](https://huggingface.co/datasets/huggingface-course/audio-course-images/resolve/main/s2st_cascaded.png "Diagram of cascaded speech to speech translation")
|
103 |
"""
|
104 |
|
105 |
+
demo = gr.Interface(
|
|
|
|
|
106 |
fn=speech_to_speech_translation,
|
107 |
+
inputs=gr.Audio(type="filepath"),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
outputs=gr.Audio(label="Generated Speech", type="numpy"),
|
109 |
examples=[["./example.wav"]],
|
110 |
title=title,
|
111 |
description=description,
|
112 |
)
|
113 |
|
|
|
|
|
|
|
114 |
demo.launch()
|