import gradio as gr import numpy as np import torch from datasets import load_dataset import librosa from transformers import SpeechT5ForTextToSpeech, SpeechT5HifiGan, SpeechT5Processor, pipeline from transformers import WhisperForConditionalGeneration, WhisperProcessor from transformers import VitsModel, VitsTokenizer target_dtype = np.int16 max_range = np.iinfo(target_dtype).max device = "cuda:0" if torch.cuda.is_available() else "cpu" model_mms = VitsModel.from_pretrained("facebook/mms-tts-nld") tokenizer_mms = VitsTokenizer.from_pretrained("facebook/mms-tts-nld") processor = WhisperProcessor.from_pretrained("openai/whisper-base") model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-base") forced_decoder_ids = processor.get_decoder_prompt_ids(language="nl", task="transcribe") sampling_rate = processor.feature_extractor.sampling_rate def translate(audio): input_features = processor(audio,sampling_rate=sampling_rate,return_tensors="pt").input_features predicted_ids = model.generate(input_features,forced_decoder_ids=forced_decoder_ids) transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True) return transcription def synthesise(text): print("text",text) inputs = tokenizer_mms(text[0], return_tensors="pt") input_ids = inputs["input_ids"] with torch.no_grad(): outputs = model_mms(input_ids) speech = outputs["waveform"] return speech def speech_to_speech_translation(audio): sampling_rate = 16000 data_array,samplerate = librosa.load(audio) data_16 = librosa.resample(data_array, orig_sr=samplerate, target_sr=sampling_rate) translated_text = translate(data_16) synthesised_speech = synthesise(translated_text) print("max_range",max_range) synthesised_speech = (synthesised_speech.numpy() * max_range).astype(np.int16) print("sampling_rate",sampling_rate) print("synthesised_speech",synthesised_speech) return sampling_rate, synthesised_speech.T title = "Cascaded STST" description = """ 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 [SpeechT5 TTS](https://huggingface.co/microsoft/speecht5_tts) model for text-to-speech: ![Cascaded STST](https://huggingface.co/datasets/huggingface-course/audio-course-images/resolve/main/s2st_cascaded.png "Diagram of cascaded speech to speech translation") """ demo = gr.Blocks() mic_translate = gr.Interface( fn=speech_to_speech_translation, inputs=gr.Audio(source="microphone", type="filepath"), outputs=gr.Audio(label="Generated Speech", type="numpy"), title=title, description=description, ) file_translate = gr.Interface( fn=speech_to_speech_translation, inputs=gr.Audio(source="upload", type="filepath"), outputs=gr.Audio(label="Generated Speech", type="numpy"), examples=[["./example.wav"]], title=title, description=description, ) with demo: gr.TabbedInterface([mic_translate, file_translate], ["Microphone", "Audio File"]) demo.launch()