Update app.py
Browse filesAdded translating to French
app.py
CHANGED
@@ -8,22 +8,31 @@ from transformers import SpeechT5ForTextToSpeech, SpeechT5HifiGan, SpeechT5Proce
|
|
8 |
|
9 |
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
10 |
|
11 |
-
# load speech
|
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("microsoft/speecht5_tts")
|
16 |
-
|
17 |
model = SpeechT5ForTextToSpeech.from_pretrained("microsoft/speecht5_tts").to(device)
|
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 |
-
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
|
29 |
def synthesise(text):
|
|
|
8 |
|
9 |
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
10 |
|
11 |
+
# load speech recognition checkpoint
|
12 |
asr_pipe = pipeline("automatic-speech-recognition", model="openai/whisper-base", device=device)
|
13 |
|
14 |
+
# load translation model for translating transcribed text to French
|
15 |
+
translation_model_name = "Helsinki-NLP/opus-mt-en-fr"
|
16 |
+
translation_model = MarianMTModel.from_pretrained(translation_model_name).to(device)
|
17 |
+
translation_tokenizer = MarianTokenizer.from_pretrained(translation_model_name)
|
18 |
+
|
19 |
# load text-to-speech checkpoint and speaker embeddings
|
20 |
processor = SpeechT5Processor.from_pretrained("microsoft/speecht5_tts")
|
|
|
21 |
model = SpeechT5ForTextToSpeech.from_pretrained("microsoft/speecht5_tts").to(device)
|
22 |
vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan").to(device)
|
23 |
|
24 |
embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")
|
25 |
speaker_embeddings = torch.tensor(embeddings_dataset[7306]["xvector"]).unsqueeze(0)
|
26 |
|
|
|
27 |
def translate(audio):
|
28 |
+
# Transcribe speech to text (Whisper ASR)
|
29 |
+
transcription = asr_pipe(audio)["text"]
|
30 |
+
|
31 |
+
# Translate the transcribed text from English to French
|
32 |
+
translated = translation_model.generate(**translation_tokenizer(transcription, return_tensors="pt", padding=True).to(device))
|
33 |
+
translated_text = translation_tokenizer.decode(translated[0], skip_special_tokens=True)
|
34 |
+
|
35 |
+
return translated_text
|
36 |
|
37 |
|
38 |
def synthesise(text):
|