Spaces:
Runtime error
Runtime error
Sandiago21
commited on
Commit
β’
e767916
1
Parent(s):
126c324
Upload folder using huggingface_hub
Browse files- README.md +3 -9
- app.py +143 -0
- example.wav +0 -0
- packages.txt +2 -0
- requirements.txt +6 -0
README.md
CHANGED
@@ -1,12 +1,6 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji: π
|
4 |
-
colorFrom: purple
|
5 |
-
colorTo: indigo
|
6 |
-
sdk: gradio
|
7 |
-
sdk_version: 3.38.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
|
|
|
|
|
|
|
|
|
|
|
3 |
app_file: app.py
|
4 |
+
sdk: gradio
|
5 |
+
sdk_version: 3.36.0
|
6 |
---
|
|
|
|
app.py
ADDED
@@ -0,0 +1,143 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
|
13 |
+
# load text-to-speech checkpoint and speaker embeddings
|
14 |
+
model_id = "Sandiago21/speecht5_finetuned_mozilla_foundation_common_voice_13_german" # update with your model id
|
15 |
+
# pipe = pipeline("automatic-speech-recognition", model=model_id)
|
16 |
+
model = SpeechT5ForTextToSpeech.from_pretrained(model_id)
|
17 |
+
vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan")
|
18 |
+
embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")
|
19 |
+
speaker_embeddings = torch.tensor(embeddings_dataset[7440]["xvector"]).unsqueeze(0)
|
20 |
+
|
21 |
+
processor = SpeechT5Processor.from_pretrained(model_id)
|
22 |
+
|
23 |
+
replacements = [
|
24 |
+
("Γ", "E"),
|
25 |
+
("Γ", "E"),
|
26 |
+
("Γ", "C"),
|
27 |
+
("Γ", "E"),
|
28 |
+
("Γ", "I"),
|
29 |
+
("Γ", "O"),
|
30 |
+
("Γ", "E"),
|
31 |
+
("Γ", "Y"),
|
32 |
+
("Γ", "S"),
|
33 |
+
("Γ ", "a"),
|
34 |
+
("Γ‘", "a"),
|
35 |
+
("Γ£", "a"),
|
36 |
+
("Γ€", "e"),
|
37 |
+
("Γ₯", "a"),
|
38 |
+
("Γ«", "e"),
|
39 |
+
("Γ", "i"),
|
40 |
+
("Γ―", "i"),
|
41 |
+
("Γ°", "o"),
|
42 |
+
("Γ±", "n"),
|
43 |
+
("Γ²", "o"),
|
44 |
+
("Γ³", "o"),
|
45 |
+
("Γ΄", "o"),
|
46 |
+
("ΓΆ", "u"),
|
47 |
+
("ΓΊ", "u"),
|
48 |
+
("ΓΌ", "y"),
|
49 |
+
("Γ½", "y"),
|
50 |
+
("Δ", "A"),
|
51 |
+
("Δ", "a"),
|
52 |
+
("Δ", "a"),
|
53 |
+
("Δ
", "a"),
|
54 |
+
("Δ", "c"),
|
55 |
+
("Δ", "C"),
|
56 |
+
("Δ", "c"),
|
57 |
+
("Δ", "d"),
|
58 |
+
("Δ", "D"),
|
59 |
+
("Δ", "e"),
|
60 |
+
("Δ", "e"),
|
61 |
+
("Δ", "g"),
|
62 |
+
("Δ°", "I"),
|
63 |
+
("Π", "O"),
|
64 |
+
("Ε", "L"),
|
65 |
+
("Ε", "n"),
|
66 |
+
("Ε", "n"),
|
67 |
+
("Ε", "O"),
|
68 |
+
("Ε", "o"),
|
69 |
+
("Ε", "o"),
|
70 |
+
("Ε", "r"),
|
71 |
+
("Ε", "S"),
|
72 |
+
("Ε", "s"),
|
73 |
+
("Ε", "S"),
|
74 |
+
("Ε", "s"),
|
75 |
+
("Ε ", "S"),
|
76 |
+
("Ε‘", "s"),
|
77 |
+
("Ε«", "u"),
|
78 |
+
("ΕΊ", "z"),
|
79 |
+
("Ε»", "Z"),
|
80 |
+
("Ε½", "Z"),
|
81 |
+
("Η", "i"),
|
82 |
+
("Η", "i"),
|
83 |
+
("Θ", "s"),
|
84 |
+
("Θ", "t"),
|
85 |
+
]
|
86 |
+
|
87 |
+
|
88 |
+
def cleanup_text(text):
|
89 |
+
for src, dst in replacements:
|
90 |
+
text = text.replace(src, dst)
|
91 |
+
return text
|
92 |
+
|
93 |
+
|
94 |
+
def transcribe_to_german(audio):
|
95 |
+
outputs = asr_pipe(audio, max_new_tokens=256, generate_kwargs={"task": "transcribe", "language": "german"})
|
96 |
+
return outputs["text"]
|
97 |
+
|
98 |
+
|
99 |
+
def synthesise_from_german(text):
|
100 |
+
text = cleanup_text(text)
|
101 |
+
inputs = processor(text=text, return_tensors="pt")
|
102 |
+
speech = model.generate_speech(inputs["input_ids"].to(device), speaker_embeddings.to(device), vocoder=vocoder)
|
103 |
+
return speech.cpu()
|
104 |
+
|
105 |
+
|
106 |
+
def speech_to_speech_translation(audio):
|
107 |
+
translated_text = transcribe_to_german(audio)
|
108 |
+
synthesised_speech = synthesise_from_german(translated_text)
|
109 |
+
synthesised_speech = (synthesised_speech.numpy() * 32767).astype(np.int16)
|
110 |
+
return ((16000, synthesised_speech), translated_text)
|
111 |
+
|
112 |
+
|
113 |
+
title = "Cascaded STST"
|
114 |
+
description = """
|
115 |
+
Demo for cascaded speech-to-speech translation (STST), mapping from source speech in any language to target speech in Greek. Demo uses OpenAI's [Whisper Large v2](https://huggingface.co/openai/whisper-large-v2) model for speech translation, and [Sandiago21/speecht5_finetuned_mozilla_foundation_common_voice_13_german](https://huggingface.co/Sandiago21/speecht5_finetuned_mozilla_foundation_common_voice_13_german) checkpoint for text-to-speech, which is based on Microsoft's
|
116 |
+
[SpeechT5 TTS](https://huggingface.co/microsoft/speecht5_tts) model for text-to-speech, fine-tuned in Greek Audio dataset:
|
117 |
+
![Cascaded STST](https://huggingface.co/datasets/huggingface-course/audio-course-images/resolve/main/s2st_cascaded.png "Diagram of cascaded speech to speech translation")
|
118 |
+
"""
|
119 |
+
|
120 |
+
demo = gr.Blocks()
|
121 |
+
|
122 |
+
mic_translate = gr.Interface(
|
123 |
+
fn=speech_to_speech_translation,
|
124 |
+
inputs=gr.Audio(source="microphone", type="filepath"),
|
125 |
+
outputs=[gr.Audio(label="Generated Speech", type="numpy"), gr.outputs.Textbox()],
|
126 |
+
title=title,
|
127 |
+
description=description,
|
128 |
+
)
|
129 |
+
|
130 |
+
file_translate = gr.Interface(
|
131 |
+
fn=speech_to_speech_translation,
|
132 |
+
inputs=gr.Audio(source="upload", type="filepath"),
|
133 |
+
outputs=[gr.Audio(label="Generated Speech", type="numpy"), gr.outputs.Textbox()],
|
134 |
+
examples=[["./example.wav"]],
|
135 |
+
title=title,
|
136 |
+
description=description,
|
137 |
+
)
|
138 |
+
|
139 |
+
with demo:
|
140 |
+
gr.TabbedInterface([mic_translate, file_translate], ["Microphone", "Audio File"])
|
141 |
+
|
142 |
+
demo.launch()
|
143 |
+
|
example.wav
ADDED
Binary file (603 kB). View file
|
|
packages.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
ffmpeg
|
2 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
git+https://github.com/huggingface/transformers
|
3 |
+
datasets
|
4 |
+
torchaudio
|
5 |
+
sentencepiece
|
6 |
+
|