Spaces:
Runtime error
Runtime error
mahimairaja
commited on
Commit
•
b3b9d29
1
Parent(s):
7c6d1bf
Added Italian to French Feature (#1)
Browse files- Added Italian to French Feature (b08b273c06fcf98751168eea77a01fecffd2ed22)
app.py
CHANGED
@@ -3,7 +3,12 @@ import numpy as np
|
|
3 |
import torch
|
4 |
from datasets import load_dataset
|
5 |
|
|
|
|
|
|
|
|
|
6 |
from transformers import SpeechT5ForTextToSpeech, SpeechT5HifiGan, SpeechT5Processor, pipeline
|
|
|
7 |
|
8 |
|
9 |
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
@@ -12,18 +17,34 @@ device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
|
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 =
|
16 |
|
17 |
-
model = SpeechT5ForTextToSpeech.from_pretrained("
|
18 |
vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan").to(device)
|
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, generate_kwargs={"task": "translate"})
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
|
29 |
def synthesise(text):
|
@@ -43,7 +64,6 @@ title = "Cascaded STST"
|
|
43 |
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 "Diagram of cascaded speech to speech translation")
|
48 |
"""
|
49 |
|
@@ -61,7 +81,7 @@ file_translate = gr.Interface(
|
|
61 |
fn=speech_to_speech_translation,
|
62 |
inputs=gr.Audio(source="upload", type="filepath"),
|
63 |
outputs=gr.Audio(label="Generated Speech", type="numpy"),
|
64 |
-
examples=[["./example.wav"]],
|
65 |
title=title,
|
66 |
description=description,
|
67 |
)
|
@@ -69,4 +89,4 @@ file_translate = gr.Interface(
|
|
69 |
with demo:
|
70 |
gr.TabbedInterface([mic_translate, file_translate], ["Microphone", "Audio File"])
|
71 |
|
72 |
-
demo.launch()
|
|
|
3 |
import torch
|
4 |
from datasets import load_dataset
|
5 |
|
6 |
+
|
7 |
+
from transformers import AutoTokenizer, TFMarianMTModel
|
8 |
+
from typing import List
|
9 |
+
|
10 |
from transformers import SpeechT5ForTextToSpeech, SpeechT5HifiGan, SpeechT5Processor, pipeline
|
11 |
+
from transformers import SpeechT5Processor, SpeechT5ForTextToSpeech, SpeechT5HifiGan, AutoProcessor
|
12 |
|
13 |
|
14 |
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
|
|
17 |
asr_pipe = pipeline("automatic-speech-recognition", model="openai/whisper-base", device=device)
|
18 |
|
19 |
# load text-to-speech checkpoint and speaker embeddings
|
20 |
+
processor = AutoProcessor.from_pretrained("Sandiago21/speecht5_finetuned_facebook_voxpopuli_french")
|
21 |
|
22 |
+
model = SpeechT5ForTextToSpeech.from_pretrained("Sandiago21/speecht5_finetuned_facebook_voxpopuli_french").to(device)
|
23 |
vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan").to(device)
|
24 |
|
25 |
+
|
26 |
embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")
|
27 |
speaker_embeddings = torch.tensor(embeddings_dataset[7306]["xvector"]).unsqueeze(0)
|
28 |
|
29 |
|
30 |
+
|
31 |
+
|
32 |
def translate(audio):
|
33 |
outputs = asr_pipe(audio, max_new_tokens=256, generate_kwargs={"task": "translate"})
|
34 |
+
english = outputs["text"]
|
35 |
+
|
36 |
+
src = "en" # source language
|
37 |
+
trg = "fr" # target language
|
38 |
+
model_name = f"Helsinki-NLP/opus-mt-{src}-{trg}"
|
39 |
+
|
40 |
+
model = TFMarianMTModel.from_pretrained(model_name)
|
41 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
42 |
+
|
43 |
+
batch = tokenizer([english], return_tensors="tf")
|
44 |
+
gen = model.generate(**batch)
|
45 |
+
return tokenizer.batch_decode(gen, skip_special_tokens=True)[0]
|
46 |
+
|
47 |
+
|
48 |
|
49 |
|
50 |
def synthesise(text):
|
|
|
64 |
description = """
|
65 |
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
|
66 |
[SpeechT5 TTS](https://huggingface.co/microsoft/speecht5_tts) model for text-to-speech:
|
|
|
67 |
![Cascaded STST](https://huggingface.co/datasets/huggingface-course/audio-course-images/resolve/main/s2st_cascaded.png "Diagram of cascaded speech to speech translation")
|
68 |
"""
|
69 |
|
|
|
81 |
fn=speech_to_speech_translation,
|
82 |
inputs=gr.Audio(source="upload", type="filepath"),
|
83 |
outputs=gr.Audio(label="Generated Speech", type="numpy"),
|
84 |
+
# examples=[["./example.wav"]],
|
85 |
title=title,
|
86 |
description=description,
|
87 |
)
|
|
|
89 |
with demo:
|
90 |
gr.TabbedInterface([mic_translate, file_translate], ["Microphone", "Audio File"])
|
91 |
|
92 |
+
demo.launch()
|