please
Browse files- app.py +32 -15
- requirements.txt +1 -1
app.py
CHANGED
@@ -3,38 +3,53 @@ import numpy as np
|
|
3 |
import torch
|
4 |
from datasets import load_dataset
|
5 |
|
6 |
-
from transformers import
|
7 |
|
8 |
|
9 |
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
10 |
|
11 |
# load speech translation checkpoint
|
12 |
-
asr_pipe = pipeline("automatic-speech-recognition",
|
|
|
13 |
|
14 |
# load text-to-speech checkpoint and speaker embeddings
|
15 |
-
processor = SpeechT5Processor.from_pretrained("microsoft/speecht5_tts")
|
16 |
|
17 |
-
model = SpeechT5ForTextToSpeech.from_pretrained(
|
18 |
-
|
|
|
|
|
19 |
|
20 |
-
embeddings_dataset = load_dataset(
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
|
24 |
def translate(audio):
|
25 |
-
outputs = asr_pipe(audio, max_new_tokens=256,
|
|
|
26 |
return outputs["text"]
|
27 |
|
28 |
|
29 |
-
def
|
30 |
-
inputs = processor(text=text, return_tensors="pt")
|
31 |
-
speech = model.generate_speech(inputs["input_ids"].to(device), speaker_embeddings.to(device), vocoder=vocoder)
|
|
|
|
|
|
|
|
|
|
|
32 |
return speech.cpu()
|
33 |
|
34 |
|
35 |
def speech_to_speech_translation(audio):
|
36 |
translated_text = translate(audio)
|
37 |
-
synthesised_speech =
|
38 |
synthesised_speech = (synthesised_speech.numpy() * 32767).astype(np.int16)
|
39 |
return 16000, synthesised_speech
|
40 |
|
@@ -44,9 +59,10 @@ description = """
|
|
44 |
Demo for cascaded speech-to-speech translation (STST), mapping from source speech in any language to target speech in English. Demo uses OpenAI's [Whisper Base](https://huggingface.co/openai/whisper-base) model for speech translation, and Microsoft's
|
45 |
[SpeechT5 TTS](https://huggingface.co/microsoft/speecht5_tts) model for text-to-speech:
|
46 |
|
47 |
-
![Cascaded STST](https://huggingface.co/datasets/huggingface-course/audio-course-images/resolve/main/s2st_cascaded.png "
|
48 |
"""
|
49 |
|
|
|
50 |
demo = gr.Blocks()
|
51 |
|
52 |
mic_translate = gr.Interface(
|
@@ -67,6 +83,7 @@ file_translate = gr.Interface(
|
|
67 |
)
|
68 |
|
69 |
with demo:
|
70 |
-
gr.TabbedInterface([mic_translate, file_translate],
|
|
|
71 |
|
72 |
-
demo.launch()
|
|
|
3 |
import torch
|
4 |
from datasets import load_dataset
|
5 |
|
6 |
+
from transformers import pipeline, AutoTokenizer, VitsModel, VitsTokenizer
|
7 |
|
8 |
|
9 |
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
10 |
|
11 |
# load speech translation checkpoint
|
12 |
+
asr_pipe = pipeline("automatic-speech-recognition",
|
13 |
+
model="openai/whisper-base", device=device)
|
14 |
|
15 |
# load text-to-speech checkpoint and speaker embeddings
|
16 |
+
# processor = SpeechT5Processor.from_pretrained("microsoft/speecht5_tts")
|
17 |
|
18 |
+
# model = SpeechT5ForTextToSpeech.from_pretrained(
|
19 |
+
# "microsoft/speecht5_tts").to(device)
|
20 |
+
# vocoder = SpeechT5HifiGan.from_pretrained(
|
21 |
+
# "microsoft/speecht5_hifigan").to(device)
|
22 |
|
23 |
+
# embeddings_dataset = load_dataset(
|
24 |
+
# "Matthijs/cmu-arctic-xvectors", split="validation")
|
25 |
+
# speaker_embeddings = torch.tensor(
|
26 |
+
# embeddings_dataset[7306]["xvector"]).unsqueeze(0)
|
27 |
+
|
28 |
+
|
29 |
+
model_nld = VitsModel.from_pretrained("facebook/mms-tts-nld")
|
30 |
+
tokenizer = VitsTokenizer.from_pretrained("facebook/mms-tts-nld")
|
31 |
|
32 |
|
33 |
def translate(audio):
|
34 |
+
outputs = asr_pipe(audio, max_new_tokens=256,
|
35 |
+
generate_kwargs={"task": "translate"})
|
36 |
return outputs["text"]
|
37 |
|
38 |
|
39 |
+
def synthesise2(text):
|
40 |
+
# inputs = processor(text=text, return_tensors="pt")
|
41 |
+
# speech = model.generate_speech(inputs["input_ids"].to(device), speaker_embeddings.to(device), vocoder=vocoder)
|
42 |
+
inputs = tokenizer(text, return_tensors="pt")
|
43 |
+
input_ids = inputs["input_ids"]
|
44 |
+
with torch.no_grad():
|
45 |
+
outputs = model_nld(input_ids)
|
46 |
+
speech = outputs["waveform"]
|
47 |
return speech.cpu()
|
48 |
|
49 |
|
50 |
def speech_to_speech_translation(audio):
|
51 |
translated_text = translate(audio)
|
52 |
+
synthesised_speech = synthesise2(translated_text)
|
53 |
synthesised_speech = (synthesised_speech.numpy() * 32767).astype(np.int16)
|
54 |
return 16000, synthesised_speech
|
55 |
|
|
|
59 |
Demo for cascaded speech-to-speech translation (STST), mapping from source speech in any language to target speech in English. Demo uses OpenAI's [Whisper Base](https://huggingface.co/openai/whisper-base) model for speech translation, and Microsoft's
|
60 |
[SpeechT5 TTS](https://huggingface.co/microsoft/speecht5_tts) model for text-to-speech:
|
61 |
|
62 |
+
![Cascaded STST](https://huggingface.co/datasets/huggingface-course/audio-course-images/resolve/main/s2st_cascaded.png "Digram of cascaded speech to speech translation")
|
63 |
"""
|
64 |
|
65 |
+
|
66 |
demo = gr.Blocks()
|
67 |
|
68 |
mic_translate = gr.Interface(
|
|
|
83 |
)
|
84 |
|
85 |
with demo:
|
86 |
+
gr.TabbedInterface([mic_translate, file_translate],
|
87 |
+
["Microphone", "Audio File"])
|
88 |
|
89 |
+
demo.launch()
|
requirements.txt
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
torch
|
2 |
-
git+https://github.com/
|
3 |
datasets
|
4 |
sentencepiece
|
|
|
1 |
torch
|
2 |
+
git+https://github.com/hollance/transformers.git@6900e8ba6532162a8613d2270ec2286c3f58f57b
|
3 |
datasets
|
4 |
sentencepiece
|