|
import gradio as gr |
|
from deep_translator import GoogleTranslator, MyMemoryTranslator |
|
|
|
|
|
LANGUAGES = { |
|
"English": "en", |
|
"Japanese": "ja", |
|
"French": "fr", |
|
"Spanish": "es", |
|
"German": "de", |
|
"Chinese (Simplified)": "zh-CN", |
|
} |
|
|
|
|
|
def translate_with_multiple_ais(text, target_language): |
|
try: |
|
|
|
google_translated = GoogleTranslator(source='auto', target=target_language).translate(text) |
|
except Exception as e: |
|
google_translated = f"Error: {str(e)}" |
|
|
|
try: |
|
|
|
mymemory_translated = MyMemoryTranslator(source='auto', target=target_language).translate(text) |
|
except Exception as e: |
|
mymemory_translated = f"Error: {str(e)}" |
|
|
|
|
|
return google_translated, mymemory_translated |
|
|
|
|
|
with gr.Blocks() as demo: |
|
gr.Markdown("# Multi-AI Translator") |
|
gr.Markdown("このアプリはGoogle TranslatorとMyMemory Translatorを使用してテキストを翻訳します。") |
|
|
|
with gr.Row(): |
|
input_text = gr.Textbox(label="Text to Translate", placeholder="Enter text here...") |
|
target_language = gr.Dropdown(choices=list(LANGUAGES.keys()), label="Target Language") |
|
|
|
with gr.Row(): |
|
google_translation = gr.Textbox(label="Google Translator Result", interactive=False) |
|
mymemory_translation = gr.Textbox(label="MyMemory Translator Result", interactive=False) |
|
|
|
translate_button = gr.Button("Translate") |
|
|
|
|
|
translate_button.click( |
|
fn=lambda text, lang: translate_with_multiple_ais(text, LANGUAGES[lang]), |
|
inputs=[input_text, target_language], |
|
outputs=[google_translation, mymemory_translation] |
|
) |
|
|
|
|
|
demo.launch() |
|
|