Spaces:
Running
Running
Daryl Lim Claude Sonnet 4.6 commited on
Commit ·
6edcaed
1
Parent(s): ea7372a
feat: add loading feedback on Translate button
Browse filesWraps translate() in a generator that disables the button and shows "Translating..." on first yield, then restores the button and delivers the result on second yield. Also fixes a pre-existing extra blank line in tests/test_app.py flagged by ruff format.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- app.py +12 -2
- tests/test_app.py +0 -1
app.py
CHANGED
|
@@ -4,6 +4,7 @@ Translates English text to 22 production-ready languages from the MADLAD-400 pap
|
|
| 4 |
"""
|
| 5 |
|
| 6 |
import warnings
|
|
|
|
| 7 |
from functools import lru_cache
|
| 8 |
|
| 9 |
import gradio as gr
|
|
@@ -89,6 +90,15 @@ def _update_char_count(text: str) -> str:
|
|
| 89 |
return f"{len(text)} / 2,000"
|
| 90 |
|
| 91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
def _build_demo() -> gr.Blocks:
|
| 93 |
_, language_names = _build_language_mappings()
|
| 94 |
|
|
@@ -127,9 +137,9 @@ def _build_demo() -> gr.Blocks:
|
|
| 127 |
translate_btn = gr.Button("Translate", variant="primary")
|
| 128 |
|
| 129 |
translate_btn.click(
|
| 130 |
-
fn=
|
| 131 |
inputs=[input_text, target_language],
|
| 132 |
-
outputs=output_text,
|
| 133 |
)
|
| 134 |
input_text.submit(
|
| 135 |
fn=translate,
|
|
|
|
| 4 |
"""
|
| 5 |
|
| 6 |
import warnings
|
| 7 |
+
from collections.abc import Generator
|
| 8 |
from functools import lru_cache
|
| 9 |
|
| 10 |
import gradio as gr
|
|
|
|
| 90 |
return f"{len(text)} / 2,000"
|
| 91 |
|
| 92 |
|
| 93 |
+
def _translate_with_loading(
|
| 94 |
+
text: str,
|
| 95 |
+
target_language_name: str,
|
| 96 |
+
) -> Generator[tuple, None, None]:
|
| 97 |
+
yield gr.update(value="Translating...", interactive=False), gr.update()
|
| 98 |
+
result = translate(text, target_language_name)
|
| 99 |
+
yield gr.update(value="Translate", interactive=True), result
|
| 100 |
+
|
| 101 |
+
|
| 102 |
def _build_demo() -> gr.Blocks:
|
| 103 |
_, language_names = _build_language_mappings()
|
| 104 |
|
|
|
|
| 137 |
translate_btn = gr.Button("Translate", variant="primary")
|
| 138 |
|
| 139 |
translate_btn.click(
|
| 140 |
+
fn=_translate_with_loading,
|
| 141 |
inputs=[input_text, target_language],
|
| 142 |
+
outputs=[translate_btn, output_text],
|
| 143 |
)
|
| 144 |
input_text.submit(
|
| 145 |
fn=translate,
|
tests/test_app.py
CHANGED
|
@@ -127,7 +127,6 @@ def test_demo_has_translate_button(demo):
|
|
| 127 |
assert any(b.value == "Translate" for b in buttons), "Expected a 'Translate' button"
|
| 128 |
|
| 129 |
|
| 130 |
-
|
| 131 |
def test_translate_handlers_wire_text_and_language(demo):
|
| 132 |
"""Translate click and submit handlers should wire input text and target language."""
|
| 133 |
translate_fns = [fn for fn in demo.fns.values() if [type(i).__name__ for i in fn.inputs] == ["Textbox", "Dropdown"]]
|
|
|
|
| 127 |
assert any(b.value == "Translate" for b in buttons), "Expected a 'Translate' button"
|
| 128 |
|
| 129 |
|
|
|
|
| 130 |
def test_translate_handlers_wire_text_and_language(demo):
|
| 131 |
"""Translate click and submit handlers should wire input text and target language."""
|
| 132 |
translate_fns = [fn for fn in demo.fns.values() if [type(i).__name__ for i in fn.inputs] == ["Textbox", "Dropdown"]]
|