Spaces:
Running on Zero
Running on Zero
Daryl Lim Claude Opus 4.6 (1M context) commited on
Commit ·
2ffda3e
1
Parent(s): e71be84
feat: add UI improvements for usability
Browse filesTwo-column layout with English label and searchable dropdown with locale
codes, live character count, clear button, and Ctrl+Enter submit.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
app.py
CHANGED
|
@@ -42,7 +42,12 @@ def _load_model() -> AutoModelForSeq2SeqLM:
|
|
| 42 |
def _build_language_mappings() -> tuple[dict[str, str], list[str]]:
|
| 43 |
tokenizer = _load_tokenizer()
|
| 44 |
vocab = tokenizer.get_vocab()
|
| 45 |
-
name_to_code = {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
return name_to_code, sorted(name_to_code.keys())
|
| 47 |
|
| 48 |
|
|
@@ -80,41 +85,61 @@ def translate(
|
|
| 80 |
return result
|
| 81 |
|
| 82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
def _build_demo() -> gr.Blocks:
|
| 84 |
_, language_names = _build_language_mappings()
|
| 85 |
|
| 86 |
with gr.Blocks(title="MADLAD-400 Translation") as demo:
|
| 87 |
-
gr.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
with gr.Row():
|
| 90 |
-
gr.
|
| 91 |
-
|
| 92 |
-
choices=language_names,
|
| 93 |
-
value="French",
|
| 94 |
-
label="Target language",
|
| 95 |
-
scale=0,
|
| 96 |
-
)
|
| 97 |
-
|
| 98 |
-
with gr.Row():
|
| 99 |
-
input_text = gr.Textbox(
|
| 100 |
-
label="English",
|
| 101 |
-
placeholder="Enter English text here",
|
| 102 |
-
lines=4,
|
| 103 |
-
)
|
| 104 |
-
output_text = gr.Textbox(
|
| 105 |
-
label="Translation",
|
| 106 |
-
lines=4,
|
| 107 |
-
buttons=["copy"],
|
| 108 |
-
interactive=False,
|
| 109 |
-
)
|
| 110 |
-
|
| 111 |
-
translate_btn = gr.Button("Translate", variant="primary")
|
| 112 |
|
| 113 |
translate_btn.click(
|
| 114 |
fn=translate,
|
| 115 |
inputs=[input_text, target_language],
|
| 116 |
outputs=output_text,
|
| 117 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
return demo
|
| 120 |
|
|
|
|
| 42 |
def _build_language_mappings() -> tuple[dict[str, str], list[str]]:
|
| 43 |
tokenizer = _load_tokenizer()
|
| 44 |
vocab = tokenizer.get_vocab()
|
| 45 |
+
name_to_code = {}
|
| 46 |
+
for code, name in langid_to_language.items():
|
| 47 |
+
if code in vocab:
|
| 48 |
+
locale = code[2:-1] # <2fr> → fr
|
| 49 |
+
display_name = f"{name} ({locale})"
|
| 50 |
+
name_to_code[display_name] = code
|
| 51 |
return name_to_code, sorted(name_to_code.keys())
|
| 52 |
|
| 53 |
|
|
|
|
| 85 |
return result
|
| 86 |
|
| 87 |
|
| 88 |
+
def _update_char_count(text: str) -> str:
|
| 89 |
+
return f"{len(text)} characters"
|
| 90 |
+
|
| 91 |
+
|
| 92 |
def _build_demo() -> gr.Blocks:
|
| 93 |
_, language_names = _build_language_mappings()
|
| 94 |
|
| 95 |
with gr.Blocks(title="MADLAD-400 Translation") as demo:
|
| 96 |
+
with gr.Row(equal_height=True):
|
| 97 |
+
with gr.Column():
|
| 98 |
+
gr.Markdown("**English**")
|
| 99 |
+
input_text = gr.Textbox(
|
| 100 |
+
placeholder="Enter English text here",
|
| 101 |
+
lines=4,
|
| 102 |
+
show_label=False,
|
| 103 |
+
)
|
| 104 |
+
char_count = gr.Markdown("0 characters")
|
| 105 |
+
with gr.Column():
|
| 106 |
+
target_language = gr.Dropdown(
|
| 107 |
+
choices=language_names,
|
| 108 |
+
value="French (fr)",
|
| 109 |
+
show_label=False,
|
| 110 |
+
filterable=True,
|
| 111 |
+
)
|
| 112 |
+
output_text = gr.Textbox(
|
| 113 |
+
placeholder="Translation",
|
| 114 |
+
lines=4,
|
| 115 |
+
show_label=False,
|
| 116 |
+
interactive=False,
|
| 117 |
+
buttons=["copy"],
|
| 118 |
+
)
|
| 119 |
|
| 120 |
with gr.Row():
|
| 121 |
+
clear_btn = gr.Button("Clear")
|
| 122 |
+
translate_btn = gr.Button("Translate", variant="primary")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
|
| 124 |
translate_btn.click(
|
| 125 |
fn=translate,
|
| 126 |
inputs=[input_text, target_language],
|
| 127 |
outputs=output_text,
|
| 128 |
)
|
| 129 |
+
input_text.submit(
|
| 130 |
+
fn=translate,
|
| 131 |
+
inputs=[input_text, target_language],
|
| 132 |
+
outputs=output_text,
|
| 133 |
+
)
|
| 134 |
+
input_text.input(
|
| 135 |
+
fn=_update_char_count,
|
| 136 |
+
inputs=input_text,
|
| 137 |
+
outputs=char_count,
|
| 138 |
+
)
|
| 139 |
+
clear_btn.click(
|
| 140 |
+
fn=lambda: ("", "", "0 characters"),
|
| 141 |
+
outputs=[input_text, output_text, char_count],
|
| 142 |
+
)
|
| 143 |
|
| 144 |
return demo
|
| 145 |
|