Spaces:
Sleeping
Sleeping
App.py to use Meta MMS for TTS model
Browse files
app.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
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 |
+
from transformers import VitsModel, VitsTokenizer, pipeline
|
| 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("microsoft/speecht5_tts")
|
| 16 |
+
#model = SpeechT5ForTextToSpeech.from_pretrained("microsoft/speecht5_tts").to(device)
|
| 17 |
+
#vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan").to(device)
|
| 18 |
+
#embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")
|
| 19 |
+
#speaker_embeddings = torch.tensor(embeddings_dataset[7306]["xvector"]).unsqueeze(0)
|
| 20 |
+
|
| 21 |
+
model = VitsModel.from_pretrained("Matthijs/mms-tts-deu")
|
| 22 |
+
tokenizer = VitsTokenizer.from_pretrained("Matthijs/mms-tts-deu")
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def translate(audio):
|
| 26 |
+
outputs = asr_pipe(audio, max_new_tokens=256, generate_kwargs={"task": "translate", "language": "de"})
|
| 27 |
+
return outputs["text"]
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def synthesise(text):
|
| 31 |
+
#inputs = processor(text=text, return_tensors="pt")
|
| 32 |
+
#speech = model.generate_speech(inputs["input_ids"].to(device), speaker_embeddings.to(device), vocoder=vocoder)
|
| 33 |
+
#return speech.cpu()
|
| 34 |
+
inputs = tokenizer(text, return_tensors="pt")
|
| 35 |
+
input_ids = inputs["input_ids"]
|
| 36 |
+
|
| 37 |
+
with torch.no_grad():
|
| 38 |
+
outputs = model(input_ids)
|
| 39 |
+
|
| 40 |
+
speech = outputs.audio[0]
|
| 41 |
+
return speech.cpu()
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def speech_to_speech_translation(audio):
|
| 45 |
+
translated_text = translate(audio)
|
| 46 |
+
synthesised_speech = synthesise(translated_text)
|
| 47 |
+
synthesised_speech = (synthesised_speech.numpy() * 32767).astype(np.int16)
|
| 48 |
+
return 16000, synthesised_speech
|
| 49 |
+
#return 16000, synthesised_speech
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
title = "Cascaded STST"
|
| 53 |
+
description = """
|
| 54 |
+
Demo for cascaded speech-to-speech translation (STST), mapping from source speech in any language to target speech in German. Demo uses OpenAI's [Whisper Base](https://huggingface.co/openai/whisper-base) model for speech translation, and Meta's
|
| 55 |
+
MMS model for text-to-speech:
|
| 56 |
+
|
| 57 |
+

|
| 58 |
+
"""
|
| 59 |
+
|
| 60 |
+
demo = gr.Blocks()
|
| 61 |
+
|
| 62 |
+
mic_translate = gr.Interface(
|
| 63 |
+
fn=speech_to_speech_translation,
|
| 64 |
+
inputs=gr.Audio(source="microphone", type="filepath"),
|
| 65 |
+
outputs=gr.Audio(label="Generated Speech", type="numpy"),
|
| 66 |
+
title=title,
|
| 67 |
+
description=description,
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
file_translate = gr.Interface(
|
| 71 |
+
fn=speech_to_speech_translation,
|
| 72 |
+
inputs=gr.Audio(source="upload", type="filepath"),
|
| 73 |
+
outputs=gr.Audio(label="Generated Speech", type="numpy"),
|
| 74 |
+
examples=[["./example.wav"]],
|
| 75 |
+
title=title,
|
| 76 |
+
description=description,
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
with demo:
|
| 80 |
+
gr.TabbedInterface([mic_translate, file_translate], ["Microphone", "Audio File"])
|
| 81 |
+
|
| 82 |
+
demo.launch()
|