File size: 3,034 Bytes
cf92f5c
 
 
 
4e243b0
cf92f5c
4e243b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cf92f5c
 
 
 
 
4e243b0
 
cf92f5c
 
 
 
 
4e243b0
 
 
 
 
 
cf92f5c
4e243b0
 
 
cf92f5c
4e243b0
 
cf92f5c
 
4e243b0
 
 
 
 
 
2a7ff02
4e243b0
 
 
 
2a7ff02
4e243b0
 
 
 
cf92f5c
4e243b0
 
 
 
 
 
cf92f5c
4e243b0
cf92f5c
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import json
import torch
from TTS.api import TTS
import gradio as gr
from bs4 import BeautifulSoup

# Function to parse HTML and create a language dictionary
def parse_html_to_dict(file_path):
    with open(file_path, 'r', encoding='utf-8') as file:
        html_content = file.read()

    soup = BeautifulSoup(html_content, 'html.parser')
    language_dict = {}
    for p in soup.find_all('p')[1:]:
        parts = p.get_text().split()
        if len(parts) >= 2:
            code = parts[0].strip()
            name = " ".join(parts[1:]).strip()
            language_dict[name] = code
    return language_dict

# Load the language dictionary from the HTML file
file_path = '1100_lan.htm'  # Update this with your actual file path
language_dict = parse_html_to_dict(file_path)
languages = list(language_dict.keys())
iso_codes = list(language_dict.values())

# Get device
device = "cuda" if torch.cuda.is_available() else "cpu"

# Function to generate audio
def generate_audio(text, language_code):
    model_name = f"tts_models/{language_code}/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

# Function to filter languages based on search term
def filter_languages(search_term):
    if not search_term:
        return gr.update(choices=languages, value=None)
    filtered = [lang for lang in languages if search_term.lower() in lang.lower()]
    return gr.update(choices=filtered, value=None if not filtered else filtered[0])

# Function to map selected language to ISO code
def update_iso_code(selected_language):
    return language_dict.get(selected_language, "Unknown")

# Gradio interface
def run_interface():
    with gr.Blocks() as demo:
        with gr.Row():
            search_input = gr.Textbox(
                label="Search Language",
                placeholder="Type to search...",
                show_label=True
            )
        with gr.Row():
            language_dropdown = gr.Dropdown(
                choices=languages,
                label="Select Language",
                show_label=True,
                interactive=True
            )
        with gr.Row():
            text_input = gr.Textbox(label="Enter Text", lines=4, placeholder="Type your text here...")
            generate_button = gr.Button("Generate Audio")
            output_audio = gr.Audio(label="Generated Audio", type="filepath")

        # Update dropdown choices when search input changes
        search_input.change(
            fn=filter_languages,
            inputs=[search_input],
            outputs=language_dropdown
        )

        # Generate audio when button is clicked
        generate_button.click(
            fn=lambda text, lang: generate_audio(text, update_iso_code(lang)),
            inputs=[text_input, language_dropdown],
            outputs=[output_audio]
        )

    demo.launch()

# Run the Gradio interface
if __name__ == "__main__":
    run_interface()