Spaces:
Runtime error
Runtime error
Sandiago21
commited on
Commit
•
8b17ca6
1
Parent(s):
2e2f394
Upload folder using huggingface_hub
Browse files- README.md +3 -9
- app.py +168 -0
- example.wav +0 -0
- example_in_greek.wav +0 -0
- requirements.txt +6 -0
README.md
CHANGED
@@ -1,12 +1,6 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji: 📊
|
4 |
-
colorFrom: green
|
5 |
-
colorTo: blue
|
6 |
-
sdk: gradio
|
7 |
-
sdk_version: 3.39.0
|
8 |
app_file: app.py
|
9 |
-
|
|
|
10 |
---
|
11 |
-
|
12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
+
title: speech-to-speech-translation-german-2
|
|
|
|
|
|
|
|
|
|
|
3 |
app_file: app.py
|
4 |
+
sdk: gradio
|
5 |
+
sdk_version: 3.36.0
|
6 |
---
|
|
|
|
app.py
ADDED
@@ -0,0 +1,168 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import torch
|
4 |
+
from datasets import load_dataset
|
5 |
+
from transformers import SpeechT5ForTextToSpeech, SpeechT5HifiGan, SpeechT5Processor, pipeline
|
6 |
+
|
7 |
+
|
8 |
+
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
9 |
+
|
10 |
+
# load speech translation checkpoint
|
11 |
+
asr_pipe = pipeline("automatic-speech-recognition", model="openai/whisper-large-v2", device=device)
|
12 |
+
german_translation_pipe = pipeline("translation", model="Helsinki-NLP/opus-mt-en-de")
|
13 |
+
|
14 |
+
# load text-to-speech checkpoint and speaker embeddings
|
15 |
+
model_id = "microsoft/speecht5_tts" # update with your model id
|
16 |
+
# pipe = pipeline("automatic-speech-recognition", model=model_id)
|
17 |
+
model = SpeechT5ForTextToSpeech.from_pretrained(model_id)
|
18 |
+
vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan")
|
19 |
+
embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")
|
20 |
+
speaker_embeddings = torch.tensor(embeddings_dataset[7440]["xvector"]).unsqueeze(0)
|
21 |
+
|
22 |
+
processor = SpeechT5Processor.from_pretrained(model_id)
|
23 |
+
|
24 |
+
model_id_german = "Sandiago21/speecht5_finetuned_mozilla_foundation_common_voice_13_german"
|
25 |
+
model_german = SpeechT5ForTextToSpeech.from_pretrained(model_id_german)
|
26 |
+
processor_german = SpeechT5Processor.from_pretrained(model_id_german)
|
27 |
+
|
28 |
+
replacements = [
|
29 |
+
("Ä", "E"),
|
30 |
+
("Æ", "E"),
|
31 |
+
("Ç", "C"),
|
32 |
+
("É", "E"),
|
33 |
+
("Í", "I"),
|
34 |
+
("Ó", "O"),
|
35 |
+
("Ö", "E"),
|
36 |
+
("Ü", "Y"),
|
37 |
+
("ß", "S"),
|
38 |
+
("à", "a"),
|
39 |
+
("á", "a"),
|
40 |
+
("ã", "a"),
|
41 |
+
("ä", "e"),
|
42 |
+
("å", "a"),
|
43 |
+
("ë", "e"),
|
44 |
+
("í", "i"),
|
45 |
+
("ï", "i"),
|
46 |
+
("ð", "o"),
|
47 |
+
("ñ", "n"),
|
48 |
+
("ò", "o"),
|
49 |
+
("ó", "o"),
|
50 |
+
("ô", "o"),
|
51 |
+
("ö", "u"),
|
52 |
+
("ú", "u"),
|
53 |
+
("ü", "y"),
|
54 |
+
("ý", "y"),
|
55 |
+
("Ā", "A"),
|
56 |
+
("ā", "a"),
|
57 |
+
("ă", "a"),
|
58 |
+
("ą", "a"),
|
59 |
+
("ć", "c"),
|
60 |
+
("Č", "C"),
|
61 |
+
("č", "c"),
|
62 |
+
("ď", "d"),
|
63 |
+
("Đ", "D"),
|
64 |
+
("ę", "e"),
|
65 |
+
("ě", "e"),
|
66 |
+
("ğ", "g"),
|
67 |
+
("İ", "I"),
|
68 |
+
("О", "O"),
|
69 |
+
("Ł", "L"),
|
70 |
+
("ń", "n"),
|
71 |
+
("ň", "n"),
|
72 |
+
("Ō", "O"),
|
73 |
+
("ō", "o"),
|
74 |
+
("ő", "o"),
|
75 |
+
("ř", "r"),
|
76 |
+
("Ś", "S"),
|
77 |
+
("ś", "s"),
|
78 |
+
("Ş", "S"),
|
79 |
+
("ş", "s"),
|
80 |
+
("Š", "S"),
|
81 |
+
("š", "s"),
|
82 |
+
("ū", "u"),
|
83 |
+
("ź", "z"),
|
84 |
+
("Ż", "Z"),
|
85 |
+
("Ž", "Z"),
|
86 |
+
("ǐ", "i"),
|
87 |
+
("ǐ", "i"),
|
88 |
+
("ș", "s"),
|
89 |
+
("ț", "t"),
|
90 |
+
]
|
91 |
+
|
92 |
+
def cleanup_text(text):
|
93 |
+
for src, dst in replacements:
|
94 |
+
text = text.replace(src, dst)
|
95 |
+
return text
|
96 |
+
|
97 |
+
|
98 |
+
def synthesize_speech(text):
|
99 |
+
text = cleanup_text(text)
|
100 |
+
inputs = processor(text=text, return_tensors="pt")
|
101 |
+
speech = model.generate_speech(inputs["input_ids"].to(device), speaker_embeddings.to(device), vocoder=vocoder)
|
102 |
+
|
103 |
+
return gr.Audio.update(value=(16000, speech.cpu().numpy()))
|
104 |
+
|
105 |
+
|
106 |
+
def translate_to_english(audio):
|
107 |
+
outputs = asr_pipe(audio, max_new_tokens=256, generate_kwargs={"task": "translate", "language": "english"})
|
108 |
+
return outputs["text"]
|
109 |
+
|
110 |
+
|
111 |
+
def synthesise_from_english(text):
|
112 |
+
text = cleanup_text(text)
|
113 |
+
inputs = processor(text=text, return_tensors="pt")
|
114 |
+
speech = model.generate_speech(inputs["input_ids"].to(device), speaker_embeddings.to(device), vocoder=vocoder)
|
115 |
+
return speech.cpu().numpy()
|
116 |
+
|
117 |
+
|
118 |
+
def translate_from_english_to_german(text):
|
119 |
+
return german_translation_pipe(text)[0]["translation_text"]
|
120 |
+
|
121 |
+
|
122 |
+
def synthesise_from_german(text):
|
123 |
+
text = cleanup_text(text)
|
124 |
+
inputs = processor_german(text=text, return_tensors="pt")
|
125 |
+
speech = model_german.generate_speech(inputs["input_ids"].to(device), speaker_embeddings.to(device), vocoder=vocoder)
|
126 |
+
return speech.cpu()
|
127 |
+
|
128 |
+
|
129 |
+
def speech_to_speech_translation(audio):
|
130 |
+
translated_text = translate_to_english(audio)
|
131 |
+
translated_text = translate_from_english_to_german(translated_text)
|
132 |
+
# synthesised_speech = synthesise_from_english(translated_text)
|
133 |
+
# translated_text = translate_from_english_to_german(synthesised_speech)
|
134 |
+
synthesised_speech = synthesise_from_german(translated_text)
|
135 |
+
synthesised_speech = (synthesised_speech.numpy() * 32767).astype(np.int16)
|
136 |
+
return ((16000, synthesised_speech), translated_text)
|
137 |
+
|
138 |
+
|
139 |
+
title = "Cascaded STST"
|
140 |
+
description = """
|
141 |
+
Demo for cascaded speech-to-speech translation (STST), mapping from source speech in any language to target speech in German. Demo uses OpenAI's [Whisper Large v2](https://huggingface.co/openai/whisper-large-v2) model for speech translation, and [Sandiago21/speecht5_finetuned_google_fleurs_german](https://huggingface.co/Sandiago21/speecht5_finetuned_google_fleurs_german) checkpoint for text-to-speech, which is based on Microsoft's
|
142 |
+
[SpeechT5 TTS](https://huggingface.co/microsoft/speecht5_tts) model for text-to-speech, fine-tuned in German Audio dataset:
|
143 |
+
![Cascaded STST](https://huggingface.co/datasets/huggingface-course/audio-course-images/resolve/main/s2st_cascaded.png "Diagram of cascaded speech to speech translation")
|
144 |
+
"""
|
145 |
+
|
146 |
+
demo = gr.Blocks()
|
147 |
+
|
148 |
+
mic_translate = gr.Interface(
|
149 |
+
fn=speech_to_speech_translation,
|
150 |
+
inputs=gr.Audio(source="microphone", type="filepath"),
|
151 |
+
outputs=[gr.Audio(label="Generated Speech", type="numpy"), gr.outputs.Textbox()],
|
152 |
+
title=title,
|
153 |
+
description=description,
|
154 |
+
)
|
155 |
+
|
156 |
+
file_translate = gr.Interface(
|
157 |
+
fn=speech_to_speech_translation,
|
158 |
+
inputs=gr.Audio(source="upload", type="filepath"),
|
159 |
+
outputs=[gr.Audio(label="Generated Speech", type="numpy"), gr.outputs.Textbox()],
|
160 |
+
examples=[["./example.wav"]],
|
161 |
+
title=title,
|
162 |
+
description=description,
|
163 |
+
)
|
164 |
+
|
165 |
+
with demo:
|
166 |
+
gr.TabbedInterface([mic_translate, file_translate], ["Microphone", "Audio File"])
|
167 |
+
|
168 |
+
demo.launch()
|
example.wav
ADDED
Binary file (247 kB). View file
|
|
example_in_greek.wav
ADDED
Binary file (603 kB). View file
|
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
git+https://github.com/huggingface/transformers
|
3 |
+
datasets
|
4 |
+
torchaudio
|
5 |
+
sentencepiece
|
6 |
+
|