Spaces:
Build error
Build error
File size: 3,320 Bytes
cc91688 77ed5d2 cc91688 77ed5d2 96a246f cc91688 e1a87b1 cc91688 e1a87b1 cc91688 1924bd6 ea340dc 619b2a8 96a246f cc91688 96a246f cc91688 6c68b70 ea340dc 6c68b70 cc91688 6c68b70 77ed5d2 cc91688 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
import gradio as gr
from translator import ServerTranslator
from LanguageTranslator.utils.constants import set_api_key, stop_api_key
import json
def apply_api_key(api_key):
api_key = set_api_key(api_key=api_key)
return f'Successfully set {api_key}'
def clear_all(input_json, input_text, input_dest_lang, translated_text_json, translted_text_text):
return None, "", "", "", ""
def translate_text_json(input_json):
# Translation code for JSON input
try:
json_file_path = input_json.name
with open(json_file_path, 'r') as f:
file_content = f.read()
json_input = json.loads(file_content)
translation = ServerTranslator.language_translator(inputs_data=json_input).translate()
translate_text = translation['text']
return translate_text
except Exception as e:
translate_text = f"Error: {str(e)}"
return translate_text
def translate_text_text(input_text, input_dest_lang):
# Translation code for text input
try:
translation = ServerTranslator.language_translator(text=input_text, dest_language=input_dest_lang).translate()
translate_text = translation['text']
return translate_text
except Exception as e:
translate_text = f"Error: {str(e)}"
return translate_text
with gr.Blocks() as demo:
with gr.Column():
gr.Markdown("# Translator For Upload JSON file")
translated_text_json = gr.Textbox(label="Result Translate Text")
input_json = gr.File(label="Upload JSON file")
submit_json = gr.Button("Submit Json")
submit_json.click(
translate_text_json,
[input_json], # Pass all three inputs
[translated_text_json]
)
with gr.Column():
gr.Markdown("# Translator For Input Text and Destination Language")
translated_text_text = gr.Textbox(label="Result Translate Text")
input_dest_lang = gr.Textbox(placeholder='Example input: vi', label="Destination Language")
input_text = gr.Textbox(placeholder='Example inputs: I love you, I love you than myself',
label="Enter Text")
submit_text = gr.Button("Submit Text")
submit_text.click(
translate_text_text,
[input_text, input_dest_lang], # Pass all three inputs
[translated_text_text]
)
clear = gr.Button("Clear")
clear.click(
clear_all,
[input_json, input_text, input_dest_lang, translated_text_json, translated_text_text],
[input_json, input_text, input_dest_lang, translated_text_json, translated_text_text]
)
with gr.Row():
with gr.Column():
status = gr.Textbox(label='API KEY STATUS')
api_key_set = gr.Textbox(label='Set your OPENAI API KEY')
api_key_set_button = gr.Button("Set API key")
api_key_set_button.click(
apply_api_key,
[api_key_set],
[status]
)
with gr.Column():
status_2 = gr.Textbox(label='STOP API KEY STATUS')
stop_api_button = gr.Button('Stop API key')
stop_api_button.click(
stop_api_key,
[],
[status_2])
demo.launch(share=True)
|