Spaces:
Running
Running
Commit
·
1f189b0
1
Parent(s):
b90039b
fix error
Browse files
app.py
CHANGED
@@ -38,11 +38,11 @@ def translate(text, source_lang, target_lang):
|
|
38 |
|
39 |
# Example texts in different languages
|
40 |
examples = {
|
41 |
-
"English": ["Hello, how are you today?", "I would like to visit Japan someday."],
|
42 |
-
"French": ["Bonjour, comment allez-vous aujourd'hui?", "J'aimerais visiter le Japon un jour."],
|
43 |
-
"Japanese": ["こんにちは、今日はお元気ですか?", "いつか日本を訪れたいです。"],
|
44 |
-
"Spanish": ["Hola, ¿cómo estás hoy?", "Me gustaría visitar Japón algún día."],
|
45 |
-
"Mandarin": ["你好,今天好吗?", "我希望有一天能去日本。"]
|
46 |
}
|
47 |
|
48 |
# Create Gradio interface
|
@@ -69,17 +69,61 @@ with gr.Blocks() as demo:
|
|
69 |
with gr.Column():
|
70 |
target_text = gr.Textbox(label="Translation Result", lines=5)
|
71 |
|
72 |
-
#
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
|
78 |
-
#
|
79 |
-
|
80 |
-
|
|
|
|
|
|
|
|
|
|
|
81 |
|
82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
|
84 |
# Set up translation function
|
85 |
translate_btn.click(
|
|
|
38 |
|
39 |
# Example texts in different languages
|
40 |
examples = {
|
41 |
+
"English": [["Hello, how are you today?"], ["I would like to visit Japan someday."]],
|
42 |
+
"French": [["Bonjour, comment allez-vous aujourd'hui?"], ["J'aimerais visiter le Japon un jour."]],
|
43 |
+
"Japanese": [["こんにちは、今日はお元気ですか?"], ["いつか日本を訪れたいです。"]],
|
44 |
+
"Spanish": [["Hola, ¿cómo estás hoy?"], ["Me gustaría visitar Japón algún día."]],
|
45 |
+
"Mandarin": [["你好,今天好吗?"], ["我希望有一天能去日本。"]]
|
46 |
}
|
47 |
|
48 |
# Create Gradio interface
|
|
|
69 |
with gr.Column():
|
70 |
target_text = gr.Textbox(label="Translation Result", lines=5)
|
71 |
|
72 |
+
# Add examples for each language but only display the relevant ones
|
73 |
+
with gr.Accordion("Examples", open=True):
|
74 |
+
english_examples = gr.Examples(
|
75 |
+
examples=examples["English"],
|
76 |
+
inputs=source_text,
|
77 |
+
label="English Examples",
|
78 |
+
visible=True
|
79 |
+
)
|
80 |
+
french_examples = gr.Examples(
|
81 |
+
examples=examples["French"],
|
82 |
+
inputs=source_text,
|
83 |
+
label="French Examples",
|
84 |
+
visible=False
|
85 |
+
)
|
86 |
+
japanese_examples = gr.Examples(
|
87 |
+
examples=examples["Japanese"],
|
88 |
+
inputs=source_text,
|
89 |
+
label="Japanese Examples",
|
90 |
+
visible=False
|
91 |
+
)
|
92 |
+
spanish_examples = gr.Examples(
|
93 |
+
examples=examples["Spanish"],
|
94 |
+
inputs=source_text,
|
95 |
+
label="Spanish Examples",
|
96 |
+
visible=False
|
97 |
+
)
|
98 |
+
mandarin_examples = gr.Examples(
|
99 |
+
examples=examples["Mandarin"],
|
100 |
+
inputs=source_text,
|
101 |
+
label="Mandarin Examples",
|
102 |
+
visible=False
|
103 |
+
)
|
104 |
|
105 |
+
# Dictionary to map language names to their example components
|
106 |
+
example_components = {
|
107 |
+
"English": english_examples,
|
108 |
+
"French": french_examples,
|
109 |
+
"Japanese": japanese_examples,
|
110 |
+
"Spanish": spanish_examples,
|
111 |
+
"Mandarin": mandarin_examples
|
112 |
+
}
|
113 |
|
114 |
+
# Function to update visibility of examples
|
115 |
+
def update_example_visibility(lang):
|
116 |
+
outputs = []
|
117 |
+
for language, component in example_components.items():
|
118 |
+
outputs.append(gr.Examples.update(visible=(language == lang)))
|
119 |
+
return outputs
|
120 |
+
|
121 |
+
# Connect source language change to example visibility update
|
122 |
+
source_lang.change(
|
123 |
+
update_example_visibility,
|
124 |
+
inputs=source_lang,
|
125 |
+
outputs=list(example_components.values())
|
126 |
+
)
|
127 |
|
128 |
# Set up translation function
|
129 |
translate_btn.click(
|