import gradio as gr from transformers import SpeechT5Processor, SpeechT5ForTextToSpeech, SpeechT5HifiGan from transformers import AutoProcessor, AutoModelForTextToSpectrogram from datasets import load_dataset import torch import soundfile as sf import os # Load models and processors processor = AutoProcessor.from_pretrained("ayush2607/speecht5_tts_technical_data") model = AutoModelForTextToSpectrogram.from_pretrained("ayush2607/speecht5_tts_technical_data") vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan") # Load xvector containing speaker's voice characteristics from a dataset embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation") speaker_embeddings = torch.tensor(embeddings_dataset[7306]["xvector"]).unsqueeze(0) def text_to_speech(text): inputs = processor(text=text, return_tensors="pt") speech = model.generate_speech(inputs["input_ids"], speaker_embeddings, vocoder=vocoder) output_path = "output.wav" sf.write(output_path, speech.numpy(), samplerate=16000) return output_path # Create Gradio interface iface = gr.Interface( fn=text_to_speech, inputs=gr.Textbox(label="Enter text to convert to speech"), outputs=gr.Audio(label="Generated Speech"), title="Text-to-Speech Converter", description="Convert text to speech using the SpeechT5 model." ) # Launch the app iface.launch()