|
import json |
|
import torch |
|
from TTS.api import TTS |
|
import gradio as gr |
|
|
|
|
|
with open("language_dictionary.json", "r") as f: |
|
language_dict = json.load(f) |
|
|
|
|
|
device = "cuda" if torch.cuda.is_available() else "cpu" |
|
|
|
|
|
def generate_audio(text, language): |
|
model_name = f"tts_models/{language}/fairseq/vits" |
|
tts = TTS(model_name=model_name, progress_bar=False).to(device) |
|
output_path = "output_file.wav" |
|
tts.tts_to_file(text=text, file_path=output_path) |
|
return output_path |
|
|
|
|
|
languages = list(language_dict.values()) |
|
iso_codes = list(language_dict.keys()) |
|
|
|
|
|
def run_interface(): |
|
def update_iso_code(language): |
|
|
|
iso_code = iso_codes[languages.index(language)] |
|
return iso_code |
|
|
|
with gr.Blocks() as demo: |
|
with gr.Row(): |
|
text_input = gr.Textbox(label="Enter Text", lines=4, placeholder="Type your text here...") |
|
language_dropdown = gr.Dropdown(label="Select Language", choices=languages) |
|
|
|
generate_button = gr.Button("Generate Audio") |
|
output_audio = gr.Audio(label="Generated Audio", type="filepath") |
|
|
|
generate_button.click( |
|
fn=lambda text, lang: generate_audio(text, update_iso_code(lang)), |
|
inputs=[text_input, language_dropdown], |
|
outputs=[output_audio] |
|
) |
|
|
|
demo.launch() |
|
|
|
|
|
if __name__ == "__main__": |
|
run_interface() |
|
|