leenag/Multilingual_TTS
8
1import gradio as gr2import torch3import numpy as np4from transformers import VitsModel, AutoTokenizer5 6LANG_MODEL_MAP = {7 "English": "facebook/mms-tts-eng",8 "Hindi": "facebook/mms-tts-hin",9 "Tamil": "facebook/mms-tts-tam",10 "Malayalam": "facebook/mms-tts-mal",11 "Kannada": "facebook/mms-tts-kan",12 "Telugu": "facebook/mms-tts-tel"13}14 15device = torch.device("cuda" if torch.cuda.is_available() else "cpu")16cache = {}17 18def load_model_and_tokenizer(language):19 model_name = LANG_MODEL_MAP[language]20 if model_name not in cache:21 tokenizer = AutoTokenizer.from_pretrained(model_name)22 model = VitsModel.from_pretrained(model_name).to(device)23 cache[model_name] = (tokenizer, model)24 return cache[model_name]25 26def tts(language, text):27 if not text.strip():28 return 16000, np.zeros(1) # empty waveform if no text29 30 tokenizer, model = load_model_and_tokenizer(language)31 inputs = tokenizer(text, return_tensors="pt").to(device)32 33 with torch.no_grad():34 output = model(**inputs)35 36 waveform = output.waveform.squeeze().cpu().numpy()37 return 16000, waveform38 39iface = gr.Interface(40 fn=tts,41 inputs=[42 gr.Dropdown(choices=list(LANG_MODEL_MAP.keys()), label="Select Language"),43 gr.Textbox(label="Enter Text")44 ],45 outputs=gr.Audio(label="Synthesized Speech", type="numpy"),46 title="Multilingual Text-to-Speech (MMS)",47 description="Generate speech from text using Meta's MMS models for English, Hindi, Tamil, Malayalam, Kannada and Telugu."48)49 50if __name__ == "__main__":51 iface.launch()