thuyentruong
commited on
Commit
•
56b97ab
1
Parent(s):
183519b
Update app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
|
|
2 |
import numpy as np
|
3 |
import torch
|
4 |
from datasets import load_dataset
|
|
|
5 |
|
6 |
from transformers import SpeechT5ForTextToSpeech, SpeechT5HifiGan, SpeechT5Processor, pipeline
|
7 |
|
@@ -10,29 +11,23 @@ device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
|
10 |
|
11 |
# load speech translation checkpoint
|
12 |
asr_pipe = pipeline("automatic-speech-recognition", model="openai/whisper-base", device=device)
|
|
|
13 |
|
14 |
-
# load text-to-speech checkpoint and speaker embeddings
|
15 |
-
processor = SpeechT5Processor.from_pretrained("sanchit-gandhi/speecht5_tts_vox_nl")
|
16 |
|
17 |
-
model =
|
18 |
-
|
19 |
|
20 |
-
embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")
|
21 |
-
speaker_embeddings = torch.tensor(embeddings_dataset[7306]["xvector"]).unsqueeze(0)
|
22 |
|
23 |
|
24 |
def translate(audio):
|
25 |
-
outputs = asr_pipe(audio, max_new_tokens=256
|
26 |
-
|
27 |
-
return outputs["text"]
|
28 |
|
29 |
|
30 |
def synthesise(text):
|
31 |
-
inputs =
|
32 |
-
|
33 |
-
|
34 |
-
)
|
35 |
-
speech = model.generate_speech(inputs["input_ids"].to(device), speaker_embeddings.to(device), vocoder=vocoder)
|
36 |
return speech.cpu()
|
37 |
|
38 |
|
@@ -40,13 +35,12 @@ def speech_to_speech_translation(audio):
|
|
40 |
translated_text = translate(audio)
|
41 |
synthesised_speech = synthesise(translated_text)
|
42 |
synthesised_speech = (synthesised_speech.numpy() * 32767).astype(np.int16)
|
43 |
-
return 16000, synthesised_speech
|
44 |
|
45 |
|
46 |
title = "Cascaded STST"
|
47 |
description = """
|
48 |
-
Demo for cascaded speech-to-speech translation (STST), mapping from source speech in any language to target speech in
|
49 |
-
[SpeechT5 TTS](https://huggingface.co/microsoft/speecht5_tts) model for text-to-speech:
|
50 |
![Cascaded STST](https://huggingface.co/datasets/huggingface-course/audio-course-images/resolve/main/s2st_cascaded.png "Diagram of cascaded speech to speech translation")
|
51 |
"""
|
52 |
|
@@ -68,8 +62,3 @@ file_translate = gr.Interface(
|
|
68 |
title=title,
|
69 |
description=description,
|
70 |
)
|
71 |
-
|
72 |
-
with demo:
|
73 |
-
gr.TabbedInterface([mic_translate, file_translate], ["Microphone", "Audio File"])
|
74 |
-
|
75 |
-
demo.launch(share=True)
|
|
|
2 |
import numpy as np
|
3 |
import torch
|
4 |
from datasets import load_dataset
|
5 |
+
from transformers import VitsModel, VitsTokenizer
|
6 |
|
7 |
from transformers import SpeechT5ForTextToSpeech, SpeechT5HifiGan, SpeechT5Processor, pipeline
|
8 |
|
|
|
11 |
|
12 |
# load speech translation checkpoint
|
13 |
asr_pipe = pipeline("automatic-speech-recognition", model="openai/whisper-base", device=device)
|
14 |
+
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-en-ru")
|
15 |
|
|
|
|
|
16 |
|
17 |
+
model = VitsModel.from_pretrained('facebook/mms-tts-rus').to(device)
|
18 |
+
tokenizer = VitsTokenizer.from_pretrained('facebook/mms-tts-rus')
|
19 |
|
|
|
|
|
20 |
|
21 |
|
22 |
def translate(audio):
|
23 |
+
outputs = asr_pipe(audio, max_new_tokens=256, generate_kwargs={"task": "translate"})
|
24 |
+
return translator(outputs['text'])[0]['translation_text']
|
|
|
25 |
|
26 |
|
27 |
def synthesise(text):
|
28 |
+
inputs = tokenizer(text=text, return_tensors="pt")
|
29 |
+
with torch.no_grad():
|
30 |
+
speech = model(**inputs).waveform
|
|
|
|
|
31 |
return speech.cpu()
|
32 |
|
33 |
|
|
|
35 |
translated_text = translate(audio)
|
36 |
synthesised_speech = synthesise(translated_text)
|
37 |
synthesised_speech = (synthesised_speech.numpy() * 32767).astype(np.int16)
|
38 |
+
return 16000, synthesised_speech[0]
|
39 |
|
40 |
|
41 |
title = "Cascaded STST"
|
42 |
description = """
|
43 |
+
Demo for cascaded speech-to-speech translation (STST), mapping from source speech in any language to target speech in Russian
|
|
|
44 |
![Cascaded STST](https://huggingface.co/datasets/huggingface-course/audio-course-images/resolve/main/s2st_cascaded.png "Diagram of cascaded speech to speech translation")
|
45 |
"""
|
46 |
|
|
|
62 |
title=title,
|
63 |
description=description,
|
64 |
)
|
|
|
|
|
|
|
|
|
|