drewThomasson commited on
Commit
4e243b0
1 Parent(s): 2a7ff02

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -18
app.py CHANGED
@@ -2,43 +2,80 @@ 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(
36
- label="Select Language", choices=languages, value="English"
 
 
 
37
  )
 
 
 
 
38
 
39
- generate_button = gr.Button("Generate Audio")
40
- output_audio = gr.Audio(label="Generated Audio", type="filepath")
 
 
 
 
41
 
 
42
  generate_button.click(
43
  fn=lambda text, lang: generate_audio(text, update_iso_code(lang)),
44
  inputs=[text_input, language_dropdown],
 
2
  import torch
3
  from TTS.api import TTS
4
  import gradio as gr
5
+ from bs4 import BeautifulSoup
6
 
7
+ # Function to parse HTML and create a language dictionary
8
+ def parse_html_to_dict(file_path):
9
+ with open(file_path, 'r', encoding='utf-8') as file:
10
+ html_content = file.read()
11
+
12
+ soup = BeautifulSoup(html_content, 'html.parser')
13
+ language_dict = {}
14
+ for p in soup.find_all('p')[1:]:
15
+ parts = p.get_text().split()
16
+ if len(parts) >= 2:
17
+ code = parts[0].strip()
18
+ name = " ".join(parts[1:]).strip()
19
+ language_dict[name] = code
20
+ return language_dict
21
+
22
+ # Load the language dictionary from the HTML file
23
+ file_path = '1100_lan.htm' # Update this with your actual file path
24
+ language_dict = parse_html_to_dict(file_path)
25
+ languages = list(language_dict.keys())
26
+ iso_codes = list(language_dict.values())
27
 
28
  # Get device
29
  device = "cuda" if torch.cuda.is_available() else "cpu"
30
 
31
  # Function to generate audio
32
+ def generate_audio(text, language_code):
33
+ model_name = f"tts_models/{language_code}/fairseq/vits"
34
  tts = TTS(model_name=model_name, progress_bar=False).to(device)
35
  output_path = "output_file.wav"
36
  tts.tts_to_file(text=text, file_path=output_path)
37
  return output_path
38
 
39
+ # Function to filter languages based on search term
40
+ def filter_languages(search_term):
41
+ if not search_term:
42
+ return gr.update(choices=languages, value=None)
43
+ filtered = [lang for lang in languages if search_term.lower() in lang.lower()]
44
+ return gr.update(choices=filtered, value=None if not filtered else filtered[0])
45
 
46
+ # Function to map selected language to ISO code
47
+ def update_iso_code(selected_language):
48
+ return language_dict.get(selected_language, "Unknown")
 
 
 
49
 
50
+ # Gradio interface
51
+ def run_interface():
52
  with gr.Blocks() as demo:
53
  with gr.Row():
54
+ search_input = gr.Textbox(
55
+ label="Search Language",
56
+ placeholder="Type to search...",
57
+ show_label=True
58
+ )
59
+ with gr.Row():
60
  language_dropdown = gr.Dropdown(
61
+ choices=languages,
62
+ label="Select Language",
63
+ show_label=True,
64
+ interactive=True
65
  )
66
+ with gr.Row():
67
+ text_input = gr.Textbox(label="Enter Text", lines=4, placeholder="Type your text here...")
68
+ generate_button = gr.Button("Generate Audio")
69
+ output_audio = gr.Audio(label="Generated Audio", type="filepath")
70
 
71
+ # Update dropdown choices when search input changes
72
+ search_input.change(
73
+ fn=filter_languages,
74
+ inputs=[search_input],
75
+ outputs=language_dropdown
76
+ )
77
 
78
+ # Generate audio when button is clicked
79
  generate_button.click(
80
  fn=lambda text, lang: generate_audio(text, update_iso_code(lang)),
81
  inputs=[text_input, language_dropdown],