|
import gradio as gr |
|
from bs4 import BeautifulSoup |
|
|
|
|
|
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 |
|
|
|
|
|
file_path = '1100_lan.htm' |
|
language_dict = parse_html_to_dict(file_path) |
|
languages = list(language_dict.keys()) |
|
|
|
|
|
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]) |
|
|
|
|
|
def get_language_code(selected_language): |
|
return f"Language Code: {language_dict.get(selected_language, 'Unknown')}" |
|
|
|
|
|
def create_searchable_dropdown(): |
|
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(): |
|
language_code_output = gr.Textbox( |
|
label="Language Code", |
|
interactive=False |
|
) |
|
|
|
|
|
search_input.change( |
|
fn=filter_languages, |
|
inputs=[search_input], |
|
outputs=language_dropdown |
|
) |
|
|
|
|
|
language_dropdown.change( |
|
fn=get_language_code, |
|
inputs=[language_dropdown], |
|
outputs=language_code_output |
|
) |
|
|
|
return demo |
|
|
|
|
|
if __name__ == "__main__": |
|
demo = create_searchable_dropdown() |
|
demo.launch() |
|
|