Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import torch
|
3 |
+
from TTS.api import TTS
|
4 |
+
import gradio as gr
|
5 |
+
|
6 |
+
# Load the language dictionary from the JSON file
|
7 |
+
with open("language_dictionary.json", "r") as f:
|
8 |
+
language_dict = json.load(f)
|
9 |
+
|
10 |
+
# Get device
|
11 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
12 |
+
|
13 |
+
# Function to generate audio
|
14 |
+
def generate_audio(text, language):
|
15 |
+
model_name = f"tts_models/{language}/fairseq/vits"
|
16 |
+
tts = TTS(model_name=model_name, progress_bar=False).to(device)
|
17 |
+
output_path = "output_file.wav"
|
18 |
+
tts.tts_to_file(text=text, file_path=output_path)
|
19 |
+
return output_path
|
20 |
+
|
21 |
+
# Create a list of language names for the dropdown
|
22 |
+
languages = list(language_dict.values())
|
23 |
+
iso_codes = list(language_dict.keys())
|
24 |
+
|
25 |
+
# Create Gradio interface
|
26 |
+
def run_interface():
|
27 |
+
def update_iso_code(language):
|
28 |
+
# Map selected language to its ISO code
|
29 |
+
iso_code = iso_codes[languages.index(language)]
|
30 |
+
return iso_code
|
31 |
+
|
32 |
+
with gr.Blocks() as demo:
|
33 |
+
with gr.Row():
|
34 |
+
text_input = gr.Textbox(label="Enter Text", lines=4, placeholder="Type your text here...")
|
35 |
+
language_dropdown = gr.Dropdown(label="Select Language", choices=languages)
|
36 |
+
|
37 |
+
generate_button = gr.Button("Generate Audio")
|
38 |
+
output_audio = gr.Audio(label="Generated Audio", type="filepath")
|
39 |
+
|
40 |
+
generate_button.click(
|
41 |
+
fn=lambda text, lang: generate_audio(text, update_iso_code(lang)),
|
42 |
+
inputs=[text_input, language_dropdown],
|
43 |
+
outputs=[output_audio]
|
44 |
+
)
|
45 |
+
|
46 |
+
demo.launch()
|
47 |
+
|
48 |
+
# Run the Gradio interface
|
49 |
+
if __name__ == "__main__":
|
50 |
+
run_interface()
|