# from responses import start import gradio as gr from language_directions import * from examples import example_sentences import os import asyncio from translator import translate_text target_lang_dict = get_all_source_languages() target_languages = target_lang_dict.keys() def get_target_dropdown(current_value=None): target_languages = list(target_lang_dict.keys()) default_target_value = current_value if current_value in target_languages else None if default_target_value is None: if "Spanish" in target_languages or "spanish" in target_languages: default_target_value = "Spanish" else: default_target_value = target_languages[0] target_dropdown = gr.Dropdown(choices=target_languages, value=default_target_value, label="Target Language") return target_dropdown def get_dropdown_value(dropdown): if isinstance(dropdown, gr.Dropdown): dropdown_value = dropdown.constructor_args.get('value') elif isinstance(dropdown, str): dropdown_value = dropdown return dropdown_value def get_dropdowns(target_dropdown): target_language_name = get_dropdown_value(target_dropdown) target_dropdown = get_target_dropdown(current_value=target_language_name) return target_dropdown def input_changed(target_language_dropdown): return get_dropdowns(target_dropdown=target_language_dropdown) async def translate(input_text, target): print(f"Translating text: {input_text}") print(f"Target language: {target}") if target in target_lang_dict.keys(): target = target_lang_dict[target] else: raise KeyError(f"Target language {target} not found in target_lang_dict") print(f"Resolved target language: {target}") translation = await translate_text(input_text, target) print(f"Translation result: {translation}") # Ensure newlines are properly parsed translation = translation.replace("\\n", "\n") return translation, "" with gr.Blocks() as demo: gr.HTML("""