Spaces:
Runtime error
Runtime error
divyeshrajpura
commited on
Commit
•
fe7fc8b
1
Parent(s):
4c2c4b9
Unit 7 - Final working copy
Browse files
app.py
CHANGED
@@ -2,27 +2,25 @@ 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 |
|
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", model="openai/whisper-base", device=device)
|
13 |
|
14 |
# load text-to-speech checkpoint and speaker embeddings
|
15 |
-
processor = SpeechT5Processor.from_pretrained("
|
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[
|
22 |
-
|
23 |
|
24 |
def translate(audio):
|
25 |
-
outputs = asr_pipe(audio, max_new_tokens=256, generate_kwargs={"task": "
|
|
|
26 |
return outputs["text"]
|
27 |
|
28 |
|
@@ -31,7 +29,6 @@ def synthesise(text):
|
|
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 = synthesise(translated_text)
|
@@ -70,3 +67,4 @@ with demo:
|
|
70 |
gr.TabbedInterface([mic_translate, file_translate], ["Microphone", "Audio File"])
|
71 |
|
72 |
demo.launch()
|
|
|
|
2 |
import numpy as np
|
3 |
import torch
|
4 |
from datasets import load_dataset
|
|
|
5 |
from transformers import SpeechT5ForTextToSpeech, SpeechT5HifiGan, SpeechT5Processor, pipeline
|
6 |
|
|
|
7 |
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
8 |
|
9 |
# load speech translation checkpoint
|
10 |
asr_pipe = pipeline("automatic-speech-recognition", model="openai/whisper-base", device=device)
|
11 |
|
12 |
# load text-to-speech checkpoint and speaker embeddings
|
13 |
+
processor = SpeechT5Processor.from_pretrained("sanchit-gandhi/speecht5_tts_vox_nl")
|
14 |
|
15 |
+
model = SpeechT5ForTextToSpeech.from_pretrained("sanchit-gandhi/speecht5_tts_vox_nl").to(device)
|
16 |
vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan").to(device)
|
17 |
|
18 |
+
embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation", streaming = True)
|
19 |
+
speaker_embeddings = torch.tensor(next(iter(embeddings_dataset))["xvector"]).unsqueeze(0)
|
|
|
20 |
|
21 |
def translate(audio):
|
22 |
+
outputs = asr_pipe(audio, max_new_tokens=256, generate_kwargs={"task": "transcribe", "language": "nl"})
|
23 |
+
print(outputs["text"])
|
24 |
return outputs["text"]
|
25 |
|
26 |
|
|
|
29 |
speech = model.generate_speech(inputs["input_ids"].to(device), speaker_embeddings.to(device), vocoder=vocoder)
|
30 |
return speech.cpu()
|
31 |
|
|
|
32 |
def speech_to_speech_translation(audio):
|
33 |
translated_text = translate(audio)
|
34 |
synthesised_speech = synthesise(translated_text)
|
|
|
67 |
gr.TabbedInterface([mic_translate, file_translate], ["Microphone", "Audio File"])
|
68 |
|
69 |
demo.launch()
|
70 |
+
|