[Added] google_translate activate param (#2961)

- So you can quickly enable/disable it, otherwise you must select
  English to disable it, and then your language to enable it again.
This commit is contained in:
Salvador E. Tropea 2023-07-09 01:08:20 -03:00 committed by GitHub
parent 74ea7522a0
commit 463aac2d65
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,6 +2,7 @@ import gradio as gr
from deep_translator import GoogleTranslator from deep_translator import GoogleTranslator
params = { params = {
"activate": True,
"language string": "ja", "language string": "ja",
} }
@ -13,6 +14,8 @@ def input_modifier(string):
This function is applied to your text inputs before This function is applied to your text inputs before
they are fed into the model. they are fed into the model.
""" """
if not params['activate']:
return string
return GoogleTranslator(source=params['language string'], target='en').translate(string) return GoogleTranslator(source=params['language string'], target='en').translate(string)
@ -21,6 +24,8 @@ def output_modifier(string):
""" """
This function is applied to the model outputs. This function is applied to the model outputs.
""" """
if not params['activate']:
return string
return GoogleTranslator(source='en', target=params['language string']).translate(string) return GoogleTranslator(source='en', target=params['language string']).translate(string)
@ -40,7 +45,12 @@ def ui():
language_name = list(language_codes.keys())[list(language_codes.values()).index(params['language string'])] language_name = list(language_codes.keys())[list(language_codes.values()).index(params['language string'])]
# Gradio elements # Gradio elements
with gr.Row():
activate = gr.Checkbox(value=params['activate'], label='Activate translation')
with gr.Row():
language = gr.Dropdown(value=language_name, choices=[k for k in language_codes], label='Language') language = gr.Dropdown(value=language_name, choices=[k for k in language_codes], label='Language')
# Event functions to update the parameters in the backend # Event functions to update the parameters in the backend
activate.change(lambda x: params.update({"activate": x}), activate, None)
language.change(lambda x: params.update({"language string": language_codes[x]}), language, None) language.change(lambda x: params.update({"language string": language_codes[x]}), language, None)