Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,17 @@
|
|
1 |
import gradio as gr
|
2 |
from deep_translator import GoogleTranslator, MyMemoryTranslator
|
3 |
|
4 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
def translate_with_multiple_ais(text, target_language):
|
6 |
try:
|
7 |
# GoogleTranslatorを使用
|
@@ -15,31 +25,17 @@ def translate_with_multiple_ais(text, target_language):
|
|
15 |
except Exception as e:
|
16 |
mymemory_translated = f"Error: {str(e)}"
|
17 |
|
18 |
-
#
|
19 |
-
return
|
20 |
-
"Google Translator": google_translated,
|
21 |
-
"MyMemory Translator": mymemory_translated,
|
22 |
-
}
|
23 |
-
|
24 |
-
# サポート言語(deep_translatorで使える言語コードの例)
|
25 |
-
LANGUAGES = {
|
26 |
-
"English": "en",
|
27 |
-
"Japanese": "ja",
|
28 |
-
"Spanish": "es",
|
29 |
-
"French": "fr",
|
30 |
-
"German": "de",
|
31 |
-
"Chinese (Simplified)": "zh-cn"
|
32 |
-
}
|
33 |
|
34 |
-
# Gradio
|
35 |
with gr.Blocks() as demo:
|
36 |
-
gr.Markdown("
|
37 |
-
|
38 |
-
with gr.Row():
|
39 |
-
input_text = gr.Textbox(label="Input Text", placeholder="Enter text to translate...")
|
40 |
|
41 |
with gr.Row():
|
42 |
-
|
|
|
43 |
|
44 |
with gr.Row():
|
45 |
google_translation = gr.Textbox(label="Google Translator Result", interactive=False)
|
@@ -47,7 +43,7 @@ with gr.Blocks() as demo:
|
|
47 |
|
48 |
translate_button = gr.Button("Translate")
|
49 |
|
50 |
-
#
|
51 |
translate_button.click(
|
52 |
fn=lambda text, lang: translate_with_multiple_ais(text, LANGUAGES[lang]),
|
53 |
inputs=[input_text, target_language],
|
@@ -55,5 +51,4 @@ with gr.Blocks() as demo:
|
|
55 |
)
|
56 |
|
57 |
# アプリを実行
|
58 |
-
|
59 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from deep_translator import GoogleTranslator, MyMemoryTranslator
|
3 |
|
4 |
+
# 言語のリストを定義(例: Google翻訳がサポートする言語)
|
5 |
+
LANGUAGES = {
|
6 |
+
"English": "en",
|
7 |
+
"Japanese": "ja",
|
8 |
+
"French": "fr",
|
9 |
+
"Spanish": "es",
|
10 |
+
"German": "de",
|
11 |
+
"Chinese (Simplified)": "zh-CN",
|
12 |
+
}
|
13 |
+
|
14 |
+
# 翻訳関数
|
15 |
def translate_with_multiple_ais(text, target_language):
|
16 |
try:
|
17 |
# GoogleTranslatorを使用
|
|
|
25 |
except Exception as e:
|
26 |
mymemory_translated = f"Error: {str(e)}"
|
27 |
|
28 |
+
# 2つの翻訳結果を返す
|
29 |
+
return google_translated, mymemory_translated
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
+
# Gradioインターフェースの設定
|
32 |
with gr.Blocks() as demo:
|
33 |
+
gr.Markdown("# Multi-AI Translator")
|
34 |
+
gr.Markdown("このアプリはGoogle TranslatorとMyMemory Translatorを使用してテキストを翻訳します。")
|
|
|
|
|
35 |
|
36 |
with gr.Row():
|
37 |
+
input_text = gr.Textbox(label="Text to Translate", placeholder="Enter text here...")
|
38 |
+
target_language = gr.Dropdown(choices=list(LANGUAGES.keys()), label="Target Language")
|
39 |
|
40 |
with gr.Row():
|
41 |
google_translation = gr.Textbox(label="Google Translator Result", interactive=False)
|
|
|
43 |
|
44 |
translate_button = gr.Button("Translate")
|
45 |
|
46 |
+
# 翻訳ボタンの動作を設定
|
47 |
translate_button.click(
|
48 |
fn=lambda text, lang: translate_with_multiple_ais(text, LANGUAGES[lang]),
|
49 |
inputs=[input_text, target_language],
|
|
|
51 |
)
|
52 |
|
53 |
# アプリを実行
|
54 |
+
demo.launch()
|
|