burraco135
commited on
Commit
•
54c7ef9
1
Parent(s):
6068b47
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,141 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
processor = SpeechT5Processor.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 |
+
replacements = [
|
23 |
+
("Ä", "E"),
|
24 |
+
("Æ", "E"),
|
25 |
+
("Ç", "C"),
|
26 |
+
("É", "E"),
|
27 |
+
("Í", "I"),
|
28 |
+
("Ó", "O"),
|
29 |
+
("Ö", "E"),
|
30 |
+
("Ü", "Y"),
|
31 |
+
("ß", "S"),
|
32 |
+
("à", "a"),
|
33 |
+
("á", "a"),
|
34 |
+
("ã", "a"),
|
35 |
+
("ä", "e"),
|
36 |
+
("å", "a"),
|
37 |
+
("ë", "e"),
|
38 |
+
("í", "i"),
|
39 |
+
("ï", "i"),
|
40 |
+
("ð", "o"),
|
41 |
+
("ñ", "n"),
|
42 |
+
("ò", "o"),
|
43 |
+
("ó", "o"),
|
44 |
+
("ô", "o"),
|
45 |
+
("ö", "u"),
|
46 |
+
("ú", "u"),
|
47 |
+
("ü", "y"),
|
48 |
+
("ý", "y"),
|
49 |
+
("Ā", "A"),
|
50 |
+
("ā", "a"),
|
51 |
+
("ă", "a"),
|
52 |
+
("ą", "a"),
|
53 |
+
("ć", "c"),
|
54 |
+
("Č", "C"),
|
55 |
+
("č", "c"),
|
56 |
+
("ď", "d"),
|
57 |
+
("Đ", "D"),
|
58 |
+
("ę", "e"),
|
59 |
+
("ě", "e"),
|
60 |
+
("ğ", "g"),
|
61 |
+
("İ", "I"),
|
62 |
+
("О", "O"),
|
63 |
+
("Ł", "L"),
|
64 |
+
("ń", "n"),
|
65 |
+
("ň", "n"),
|
66 |
+
("Ō", "O"),
|
67 |
+
("ō", "o"),
|
68 |
+
("ő", "o"),
|
69 |
+
("ř", "r"),
|
70 |
+
("Ś", "S"),
|
71 |
+
("ś", "s"),
|
72 |
+
("Ş", "S"),
|
73 |
+
("ş", "s"),
|
74 |
+
("Š", "S"),
|
75 |
+
("š", "s"),
|
76 |
+
("ū", "u"),
|
77 |
+
("ź", "z"),
|
78 |
+
("Ż", "Z"),
|
79 |
+
("Ž", "Z"),
|
80 |
+
("ǐ", "i"),
|
81 |
+
("ǐ", "i"),
|
82 |
+
("ș", "s"),
|
83 |
+
("ț", "t"),
|
84 |
+
]
|
85 |
+
|
86 |
+
|
87 |
+
def cleanup_text(text):
|
88 |
+
for src, dst in replacements:
|
89 |
+
text = text.replace(src, dst)
|
90 |
+
return text
|
91 |
+
|
92 |
+
|
93 |
+
def transcribe_to_german(audio):
|
94 |
+
outputs = asr_pipe(audio, max_new_tokens=256, generate_kwargs={"task": "transcribe", "language": "german"})
|
95 |
+
return outputs["text"]
|
96 |
+
|
97 |
+
|
98 |
+
def synthesise_from_german(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 |
+
return speech.cpu()
|
103 |
+
|
104 |
+
|
105 |
+
def speech_to_speech_translation(audio):
|
106 |
+
translated_text = transcribe_to_german(audio)
|
107 |
+
synthesised_speech = synthesise_from_german(translated_text)
|
108 |
+
synthesised_speech = (synthesised_speech.numpy() * 32767).astype(np.int16)
|
109 |
+
return ((16000, synthesised_speech), translated_text)
|
110 |
+
|
111 |
+
|
112 |
+
title = "Cascaded STST"
|
113 |
+
description = """
|
114 |
+
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_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
|
115 |
+
[SpeechT5 TTS](https://huggingface.co/microsoft/speecht5_tts) model for text-to-speech, fine-tuned in German Audio dataset:
|
116 |
+
![Cascaded STST](https://huggingface.co/datasets/huggingface-course/audio-course-images/resolve/main/s2st_cascaded.png "Diagram of cascaded speech to speech translation")
|
117 |
+
"""
|
118 |
+
|
119 |
+
demo = gr.Blocks()
|
120 |
+
|
121 |
+
mic_translate = gr.Interface(
|
122 |
+
fn=speech_to_speech_translation,
|
123 |
+
inputs=gr.Audio(source="microphone", type="filepath"),
|
124 |
+
outputs=[gr.Audio(label="Generated Speech", type="numpy"), gr.outputs.Textbox()],
|
125 |
+
title=title,
|
126 |
+
description=description,
|
127 |
+
)
|
128 |
+
|
129 |
+
file_translate = gr.Interface(
|
130 |
+
fn=speech_to_speech_translation,
|
131 |
+
inputs=gr.Audio(source="upload", type="filepath"),
|
132 |
+
outputs=[gr.Audio(label="Generated Speech", type="numpy"), gr.outputs.Textbox()],
|
133 |
+
examples=[["./example.wav"]],
|
134 |
+
title=title,
|
135 |
+
description=description,
|
136 |
+
)
|
137 |
+
|
138 |
+
with demo:
|
139 |
+
gr.TabbedInterface([mic_translate, file_translate], ["Microphone", "Audio File"])
|
140 |
+
|
141 |
+
demo.launch()
|