Daryl Lim Claude Opus 4.6 commited on
Commit
3872c0b
·
1 Parent(s): 4b929eb

feat: rebuild UI with sidebar and tabbed Single/Batch layout

Browse files

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

Files changed (2) hide show
  1. app.py +59 -35
  2. tests/test_app.py +33 -0
app.py CHANGED
@@ -170,42 +170,66 @@ def _build_demo() -> gr.Blocks:
170
  "[Paper](https://arxiv.org/pdf/2309.04662)"
171
  )
172
 
173
- target_language = gr.Dropdown(
174
- choices=language_names,
175
- value="French",
176
- label="Target language",
177
- )
178
-
179
  with gr.Row():
180
- input_text = gr.Textbox(
181
- label="English",
182
- placeholder="Enter English text here",
183
- lines=4,
184
- )
185
- output_text = gr.Textbox(
186
- label="Translation",
187
- lines=4,
188
- buttons=["copy"],
189
- interactive=False,
190
- )
191
-
192
- translate_btn = gr.Button("Translate", variant="primary")
193
-
194
- translate_btn.click(
195
- fn=translate,
196
- inputs=[input_text, target_language],
197
- outputs=output_text,
198
- )
199
-
200
- gr.Examples(
201
- examples=[
202
- ["Hello, how are you today?", "French"],
203
- ["The weather is beautiful.", "Spanish"],
204
- ["Thank you very much.", "German"],
205
- ["Where is the train station?", "Portuguese"],
206
- ],
207
- inputs=[input_text, target_language],
208
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
209
 
210
  return demo
211
 
 
170
  "[Paper](https://arxiv.org/pdf/2309.04662)"
171
  )
172
 
 
 
 
 
 
 
173
  with gr.Row():
174
+ # --- Sidebar ---
175
+ with gr.Column(scale=1, min_width=250):
176
+ gr.Markdown("### Settings")
177
+
178
+ target_language = gr.Dropdown(
179
+ choices=language_names,
180
+ value="French",
181
+ label="Target language",
182
+ )
183
+ max_new_tokens = gr.Slider(minimum=1, maximum=1024, value=512, step=1, label="Max new tokens")
184
+ num_beams = gr.Slider(minimum=1, maximum=10, value=1, step=1, label="Num beams")
185
+ temperature = gr.Slider(minimum=0.1, maximum=2.0, value=1.0, step=0.1, label="Temperature")
186
+
187
+ # --- Main area ---
188
+ with gr.Column(scale=3):
189
+ with gr.Tab("Single"):
190
+ with gr.Row():
191
+ input_text = gr.Textbox(
192
+ label="English",
193
+ placeholder="Enter English text here",
194
+ lines=4,
195
+ )
196
+ output_text = gr.Textbox(
197
+ label="Translation",
198
+ lines=4,
199
+ buttons=["copy"],
200
+ interactive=False,
201
+ )
202
+
203
+ single_btn = gr.Button("Translate", variant="primary")
204
+
205
+ single_btn.click(
206
+ fn=translate,
207
+ inputs=[input_text, target_language, max_new_tokens, num_beams, temperature],
208
+ outputs=output_text,
209
+ )
210
+
211
+ gr.Examples(
212
+ examples=[
213
+ ["Hello, how are you today?", "French"],
214
+ ["The weather is beautiful.", "Spanish"],
215
+ ["Thank you very much.", "German"],
216
+ ["Where is the train station?", "Portuguese"],
217
+ ],
218
+ inputs=[input_text, target_language],
219
+ )
220
+
221
+ with gr.Tab("Batch"):
222
+ gr.Markdown("Upload a `.txt` file (one sentence per line) or a `.csv` file with a `text` column.")
223
+ batch_input = gr.File(label="Upload file", file_types=[".txt", ".csv"])
224
+ batch_output = gr.File(label="Download translations", interactive=False)
225
+
226
+ batch_btn = gr.Button("Translate", variant="primary")
227
+
228
+ batch_btn.click(
229
+ fn=translate_batch,
230
+ inputs=[batch_input, target_language, max_new_tokens, num_beams, temperature],
231
+ outputs=batch_output,
232
+ )
233
 
234
  return demo
235
 
tests/test_app.py CHANGED
@@ -122,6 +122,39 @@ def test_translate_batch_exists():
122
  assert callable(app.translate_batch)
123
 
124
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
125
  # --- Slow tests (require CUDA + model download) ---
126
 
127
  gpu_available = torch.cuda.is_available()
 
122
  assert callable(app.translate_batch)
123
 
124
 
125
+ def test_demo_has_sidebar_and_tabs():
126
+ """Verify the demo contains the expected layout components."""
127
+ import app
128
+
129
+ demo = app._build_demo()
130
+ # Flatten all blocks in the demo
131
+ blocks = demo.blocks
132
+
133
+ # Check for key component types by looking at block values
134
+ component_types = {type(b).__name__ for b in blocks.values()}
135
+ assert "Tab" in component_types, f"Expected Tab component, found: {component_types}"
136
+ assert "Slider" in component_types, f"Expected Slider component, found: {component_types}"
137
+ assert "Dropdown" in component_types, f"Expected Dropdown component, found: {component_types}"
138
+
139
+
140
+ def test_demo_has_three_sliders():
141
+ """Three sliders: max_new_tokens, num_beams, temperature."""
142
+ import app
143
+
144
+ demo = app._build_demo()
145
+ sliders = [b for b in demo.blocks.values() if type(b).__name__ == "Slider"]
146
+ assert len(sliders) == 3, f"Expected 3 sliders, found {len(sliders)}"
147
+
148
+
149
+ def test_demo_has_two_tabs():
150
+ """Two tabs: Single and Batch."""
151
+ import app
152
+
153
+ demo = app._build_demo()
154
+ tabs = [b for b in demo.blocks.values() if type(b).__name__ == "Tab"]
155
+ assert len(tabs) == 2, f"Expected 2 tabs, found {len(tabs)}"
156
+
157
+
158
  # --- Slow tests (require CUDA + model download) ---
159
 
160
  gpu_available = torch.cuda.is_available()