Sandiago21 commited on
Commit
e767916
1 Parent(s): 126c324

Upload folder using huggingface_hub

Browse files
Files changed (5) hide show
  1. README.md +3 -9
  2. app.py +143 -0
  3. example.wav +0 -0
  4. packages.txt +2 -0
  5. requirements.txt +6 -0
README.md CHANGED
@@ -1,12 +1,6 @@
1
  ---
2
- title: Speech To Speech Translation German
3
- emoji: 🌖
4
- colorFrom: purple
5
- colorTo: indigo
6
- sdk: gradio
7
- sdk_version: 3.38.0
8
  app_file: app.py
9
- pinned: false
 
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
+