Update app.py
Browse files
app.py
CHANGED
@@ -1,34 +1,128 @@
|
|
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 |
-
|
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 |
return speech.cpu()
|
33 |
|
34 |
|
@@ -38,12 +132,10 @@ def speech_to_speech_translation(audio):
|
|
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 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 |
|
@@ -56,7 +148,6 @@ mic_translate = gr.Interface(
|
|
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"),
|
@@ -69,4 +160,4 @@ file_translate = gr.Interface(
|
|
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 (
|
6 |
+
VitsModel,
|
7 |
+
VitsTokenizer,
|
8 |
+
pipeline
|
9 |
+
)
|
10 |
|
11 |
|
12 |
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
13 |
|
14 |
# load speech translation checkpoint
|
15 |
+
asr_pipe = pipeline(
|
16 |
+
"automatic-speech-recognition",
|
17 |
+
model="openai/whisper-base",
|
18 |
+
device=device
|
19 |
+
)
|
20 |
+
|
21 |
+
model = VitsModel.from_pretrained("Matthijs/mms-tts-deu")
|
22 |
+
tokenizer = VitsTokenizer.from_pretrained("Matthijs/mms-tts-deu")
|
23 |
+
|
24 |
+
|
25 |
+
def translate(audio):
|
26 |
+
outputs = asr_pipe(
|
27 |
+
audio,
|
28 |
+
max_new_tokens=256,
|
29 |
+
generate_kwargs={"task": "transcribe", "language": "de"}
|
30 |
+
)
|
31 |
+
return outputs["text"]
|
32 |
+
|
33 |
+
|
34 |
+
def synthesise(text):
|
35 |
+
if len(text.strip()) == 0:
|
36 |
+
return (16000, np.zeros(0).astype(np.int16))
|
37 |
+
|
38 |
+
inputs = tokenizer(text, return_tensors="pt")
|
39 |
+
input_ids = inputs["input_ids"]
|
40 |
+
|
41 |
+
with torch.no_grad():
|
42 |
+
outputs = model(input_ids)
|
43 |
+
|
44 |
+
speech = outputs.audio[0]
|
45 |
+
return speech.cpu()
|
46 |
+
|
47 |
+
|
48 |
+
def speech_to_speech_translation(audio):
|
49 |
+
translated_text = translate(audio)
|
50 |
+
synthesised_speech = synthesise(translated_text)
|
51 |
+
synthesised_speech = (synthesised_speech.numpy() * 32767).astype(np.int16)
|
52 |
+
return 16000, synthesised_speech
|
53 |
+
|
54 |
+
title = "Cascaded STST"
|
55 |
+
description = """
|
56 |
+
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
|
57 |
+
[SpeechT5 TTS](https://huggingface.co/microsoft/speecht5_tts) model for text-to-speech:
|
58 |
+
![Cascaded STST](https://huggingface.co/datasets/huggingface-course/audio-course-images/resolve/main/s2st_cascaded.png "Diagram of cascaded speech to speech translation")
|
59 |
+
"""
|
60 |
+
|
61 |
+
demo = gr.Blocks()
|
62 |
+
|
63 |
+
mic_translate = gr.Interface(
|
64 |
+
fn=speech_to_speech_translation,
|
65 |
+
inputs=gr.Audio(source="microphone", type="filepath"),
|
66 |
+
outputs=gr.Audio(label="Generated Speech", type="numpy"),
|
67 |
+
title=title,
|
68 |
+
description=description,
|
69 |
+
)
|
70 |
+
file_translate = gr.Interface(
|
71 |
+
fn=speech_to_speech_translation,
|
72 |
+
inputs=gr.Audio(source="upload", type="filepath"),
|
73 |
+
outputs=gr.Audio(label="Generated Speech", type="numpy"),
|
74 |
+
examples=[["./example.wav"]],
|
75 |
+
title=title,
|
76 |
+
description=description,
|
77 |
+
)
|
78 |
+
|
79 |
+
with demo:
|
80 |
+
gr.TabbedInterface([mic_translate, file_translate], ["Microphone", "Audio File"])
|
81 |
+
|
82 |
+
demo.launch()import gradio as gr
|
83 |
+
import numpy as np
|
84 |
+
import torch
|
85 |
|
86 |
+
from transformers import (
|
87 |
+
VitsModel,
|
88 |
+
VitsTokenizer,
|
89 |
+
pipeline
|
90 |
+
)
|
91 |
+
|
92 |
+
|
93 |
+
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
94 |
|
95 |
+
# load speech translation checkpoint
|
96 |
+
asr_pipe = pipeline(
|
97 |
+
"automatic-speech-recognition",
|
98 |
+
model="openai/whisper-base",
|
99 |
+
device=device
|
100 |
+
)
|
101 |
|
102 |
+
model = VitsModel.from_pretrained("Matthijs/mms-tts-deu")
|
103 |
+
tokenizer = VitsTokenizer.from_pretrained("Matthijs/mms-tts-deu")
|
104 |
|
105 |
|
106 |
def translate(audio):
|
107 |
+
outputs = asr_pipe(
|
108 |
+
audio,
|
109 |
+
max_new_tokens=256,
|
110 |
+
generate_kwargs={"task": "transcribe", "language": "de"}
|
111 |
+
)
|
112 |
return outputs["text"]
|
113 |
|
114 |
|
115 |
def synthesise(text):
|
116 |
+
if len(text.strip()) == 0:
|
117 |
+
return (16000, np.zeros(0).astype(np.int16))
|
118 |
+
|
119 |
+
inputs = tokenizer(text, return_tensors="pt")
|
120 |
+
input_ids = inputs["input_ids"]
|
121 |
+
|
122 |
+
with torch.no_grad():
|
123 |
+
outputs = model(input_ids)
|
124 |
+
|
125 |
+
speech = outputs.audio[0]
|
126 |
return speech.cpu()
|
127 |
|
128 |
|
|
|
132 |
synthesised_speech = (synthesised_speech.numpy() * 32767).astype(np.int16)
|
133 |
return 16000, synthesised_speech
|
134 |
|
|
|
135 |
title = "Cascaded STST"
|
136 |
description = """
|
137 |
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
|
138 |
[SpeechT5 TTS](https://huggingface.co/microsoft/speecht5_tts) model for text-to-speech:
|
|
|
139 |
![Cascaded STST](https://huggingface.co/datasets/huggingface-course/audio-course-images/resolve/main/s2st_cascaded.png "Diagram of cascaded speech to speech translation")
|
140 |
"""
|
141 |
|
|
|
148 |
title=title,
|
149 |
description=description,
|
150 |
)
|
|
|
151 |
file_translate = gr.Interface(
|
152 |
fn=speech_to_speech_translation,
|
153 |
inputs=gr.Audio(source="upload", type="filepath"),
|
|
|
160 |
with demo:
|
161 |
gr.TabbedInterface([mic_translate, file_translate], ["Microphone", "Audio File"])
|
162 |
|
163 |
+
demo.launch()
|