Sandiago21 commited on
Commit
084b7bd
1 Parent(s): 920d115

Upload folder using huggingface_hub

Browse files
Files changed (5) hide show
  1. README.md +3 -9
  2. app.py +165 -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 Greek With Transcription
3
- emoji: 📉
4
- colorFrom: yellow
5
- colorTo: indigo
6
- sdk: gradio
7
- sdk_version: 3.37.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-greek-with-transcription
 
 
 
 
 
3
  app_file: app.py
4
+ sdk: gradio
5
+ sdk_version: 3.36.0
6
  ---
 
 
app.py ADDED
@@ -0,0 +1,165 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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_google_fleurs_greek" # 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
+ ("ου", "u"),
25
+ ("αυ", "af"),
26
+ ("ευ", "ef"),
27
+ ("ει", "i"),
28
+ ("οι", "i"),
29
+ ("αι", "e"),
30
+ ("ού", "u"),
31
+ ("εί", "i"),
32
+ ("οί", "i"),
33
+ ("αί", "e"),
34
+ ("Ά", "A"),
35
+ ("Έ", "E"),
36
+ ("Ή", "H"),
37
+ ("Ί", "I"),
38
+ ("Ό", "O"),
39
+ ("Ύ", "Y"),
40
+ ("Ώ", "O"),
41
+ ("ΐ", "i"),
42
+ ("Α", "A"),
43
+ ("Β", "B"),
44
+ ("Γ", "G"),
45
+ ("Δ", "L"),
46
+ ("Ε", "Ε"),
47
+ ("Ζ", "Z"),
48
+ ("Η", "I"),
49
+ ("Θ", "Th"),
50
+ ("Ι", "I"),
51
+ ("Κ", "K"),
52
+ ("Λ", "L"),
53
+ ("Μ", "M"),
54
+ ("Ν", "N"),
55
+ ("Ξ", "Ks"),
56
+ ("Ο", "O"),
57
+ ("Π", "P"),
58
+ ("Ρ", "R"),
59
+ ("Σ", "S"),
60
+ ("Τ", "T"),
61
+ ("Υ", "Y"),
62
+ ("Φ", "F"),
63
+ ("Χ", "X"),
64
+ ("Ω", "O"),
65
+ ("ά", "a"),
66
+ ("έ", "e"),
67
+ ("ή", "i"),
68
+ ("ί", "i"),
69
+ ("α", "a"),
70
+ ("β", "v"),
71
+ ("γ", "g"),
72
+ ("δ", "d"),
73
+ ("ε", "e"),
74
+ ("ζ", "z"),
75
+ ("η", "i"),
76
+ ("θ", "th"),
77
+ ("ι", "i"),
78
+ ("κ", "k"),
79
+ ("λ", "l"),
80
+ ("μ", "m"),
81
+ ("ν", "n"),
82
+ ("ξ", "ks"),
83
+ ("ο", "o"),
84
+ ("π", "p"),
85
+ ("ρ", "r"),
86
+ ("ς", "s"),
87
+ ("σ", "s"),
88
+ ("τ", "t"),
89
+ ("υ", "i"),
90
+ ("φ", "f"),
91
+ ("χ", "h"),
92
+ ("ψ", "ps"),
93
+ ("ω", "o"),
94
+ ("ϊ", "i"),
95
+ ("ϋ", "i"),
96
+ ("ό", "o"),
97
+ ("ύ", "i"),
98
+ ("ώ", "o"),
99
+ ("í", "i"),
100
+ ("õ", "o"),
101
+ ("Ε", "E"),
102
+ ("Ψ", "Ps"),
103
+ ]
104
+
105
+ def cleanup_text(text):
106
+ for src, dst in replacements:
107
+ text = text.replace(src, dst)
108
+ return text
109
+
110
+ def synthesize_speech(text):
111
+ text = cleanup_text(text)
112
+ inputs = processor(text=text, return_tensors="pt")
113
+ speech = model.generate_speech(inputs["input_ids"].to(device), speaker_embeddings.to(device), vocoder=vocoder)
114
+
115
+ return gr.Audio.update(value=(16000, speech.cpu().numpy()))
116
+
117
+ def translate(audio):
118
+ outputs = asr_pipe(audio, max_new_tokens=256, generate_kwargs={"task": "transcribe", "language": "greek"})
119
+ return outputs["text"]
120
+
121
+
122
+ def synthesise(text):
123
+ text = cleanup_text(text)
124
+ inputs = processor(text=text, return_tensors="pt")
125
+ speech = model.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(audio)
131
+ synthesised_speech = synthesise(translated_text)
132
+ synthesised_speech = (synthesised_speech.numpy() * 32767).astype(np.int16)
133
+ return ((16000, synthesised_speech), translated_text)
134
+
135
+
136
+ title = "Cascaded STST"
137
+ description = """
138
+ 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_google_fleurs_greek](https://huggingface.co/Sandiago21/speecht5_finetuned_google_fleurs_greek) checkpoint for text-to-speech, which is based on Microsoft's
139
+ [SpeechT5 TTS](https://huggingface.co/microsoft/speecht5_tts) model for text-to-speech, fine-tuned in Greek Audio dataset:
140
+ ![Cascaded STST](https://huggingface.co/datasets/huggingface-course/audio-course-images/resolve/main/s2st_cascaded.png "Diagram of cascaded speech to speech translation")
141
+ """
142
+
143
+ demo = gr.Blocks()
144
+
145
+ mic_translate = gr.Interface(
146
+ fn=speech_to_speech_translation,
147
+ inputs=gr.Audio(source="microphone", type="filepath"),
148
+ outputs=[gr.Audio(label="Generated Speech", type="numpy"), gr.outputs.Textbox()],
149
+ title=title,
150
+ description=description,
151
+ )
152
+
153
+ file_translate = gr.Interface(
154
+ fn=speech_to_speech_translation,
155
+ inputs=gr.Audio(source="upload", type="filepath"),
156
+ outputs=[gr.Audio(label="Generated Speech", type="numpy"), gr.outputs.Textbox()],
157
+ examples=[["./example.wav"]],
158
+ title=title,
159
+ description=description,
160
+ )
161
+
162
+ with demo:
163
+ gr.TabbedInterface([mic_translate, file_translate], ["Microphone", "Audio File"])
164
+
165
+ demo.launch()
example.wav ADDED
Binary file (263 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
+