Spaces:
Runtime error
Runtime error
# 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("""<html> | |
<head> | |
<style> | |
h1 { | |
text-align: center; | |
} | |
</style> | |
</head> | |
<body> | |
<h1>Jaga 3.1</h1> | |
</body> | |
</html>""") | |
with gr.Row(): | |
with gr.Column(): | |
input_textbox = gr.Textbox(lines=5, placeholder="Enter text to translate (source language will be auto-detected)", label="Input Text") | |
with gr.Column(): | |
target_language_dropdown = gr.Dropdown(choices=target_languages, | |
value="Spanish" if "Spanish" in target_languages else list(target_languages)[0], | |
label="Target Language") | |
translated_textbox = gr.Textbox(lines=5, placeholder="", label="Translated Text") | |
info_label = gr.HTML("") | |
btn = gr.Button("Translate") | |
input_textbox.change(input_changed, inputs=[target_language_dropdown], outputs=[target_language_dropdown]) | |
btn.click(lambda *args: asyncio.run(translate(*args)), inputs=[input_textbox, | |
target_language_dropdown], | |
outputs=[translated_textbox, info_label]) | |
gr.Examples(example_sentences, inputs=[input_textbox]) | |
if __name__ == "__main__": | |
print("Launching Gradio app...") | |
print(f"Environment Variables: OPENAI_API_KEY={os.getenv('OPENAI_API_KEY')}, LANGCHAIN_API_KEY={os.getenv('LANGCHAIN_API_KEY')}, LANGCHAIN_HUB_API_KEY={os.getenv('LANGCHAIN_HUB_API_KEY')}") | |
demo.launch(share=True, space="GetGloby/jaga-3") |