HongzeFu commited on
Commit
cf030f7
·
1 Parent(s): 7f121df

选项大写

Browse files
gradio-web/gradio_callbacks.py CHANGED
@@ -271,6 +271,23 @@ def capitalize_first_letter(text: str) -> str:
271
  return text[0].upper() + text[1:]
272
 
273
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
274
 
275
 
276
  def _ui_option_label(session, opt_label: str, opt_idx: int) -> str:
@@ -282,19 +299,19 @@ def _ui_option_label(session, opt_label: str, opt_idx: int) -> str:
282
  try:
283
  option_index = int(opt_idx)
284
  except (TypeError, ValueError):
285
- return opt_label
286
 
287
  raw_solve_options = getattr(session, "raw_solve_options", None)
288
  if not isinstance(raw_solve_options, list):
289
- return opt_label
290
  if not (0 <= option_index < len(raw_solve_options)):
291
- return opt_label
292
 
293
  raw_option = raw_solve_options[option_index]
294
  if not isinstance(raw_option, dict):
295
- return opt_label
296
 
297
- raw_label = str(raw_option.get("label", "")).strip()
298
  raw_action = str(raw_option.get("action", "")).strip()
299
  mapped_action = get_ui_action_text(getattr(session, "env_id", None), raw_action)
300
 
 
271
  return text[0].upper() + text[1:]
272
 
273
 
274
+ def _format_choice_prefix(text: str) -> str:
275
+ """Display multi-choice labels like a/b/c/d as uppercase without changing action text."""
276
+ if not isinstance(text, str):
277
+ return text
278
+
279
+ stripped = text.strip()
280
+ if not stripped:
281
+ return stripped
282
+
283
+ prefix, dot, rest = stripped.partition(".")
284
+ if dot and prefix.isalpha() and len(prefix) <= 4:
285
+ return f"{prefix.upper()}.{rest}"
286
+ if stripped.isalpha() and len(stripped) <= 4:
287
+ return stripped.upper()
288
+ return stripped
289
+
290
+
291
 
292
 
293
  def _ui_option_label(session, opt_label: str, opt_idx: int) -> str:
 
299
  try:
300
  option_index = int(opt_idx)
301
  except (TypeError, ValueError):
302
+ return _format_choice_prefix(opt_label)
303
 
304
  raw_solve_options = getattr(session, "raw_solve_options", None)
305
  if not isinstance(raw_solve_options, list):
306
+ return _format_choice_prefix(opt_label)
307
  if not (0 <= option_index < len(raw_solve_options)):
308
+ return _format_choice_prefix(opt_label)
309
 
310
  raw_option = raw_solve_options[option_index]
311
  if not isinstance(raw_option, dict):
312
+ return _format_choice_prefix(opt_label)
313
 
314
+ raw_label = _format_choice_prefix(str(raw_option.get("label", "")).strip())
315
  raw_action = str(raw_option.get("action", "")).strip()
316
  mapped_action = get_ui_action_text(getattr(session, "env_id", None), raw_action)
317
 
gradio-web/test/test_ui_text_config.py CHANGED
@@ -226,7 +226,7 @@ def test_ui_option_label_uses_patternlock_configured_action_text(reload_module):
226
  raw_solve_options=[{"label": "a", "action": "move forward", "available": False}],
227
  )
228
 
229
- assert callbacks._ui_option_label(session, "fallback", 0) == "a. move forward↓"
230
 
231
 
232
  def test_load_status_task_appends_configured_point_suffix_after_mapped_label(monkeypatch, reload_module):
@@ -253,7 +253,7 @@ def test_load_status_task_appends_configured_point_suffix_after_mapped_label(mon
253
 
254
  assert result[4]["choices"] == [
255
  (
256
- f"a. move forward↓{config.UI_TEXT['actions']['point_required_suffix']}",
257
  0,
258
  )
259
  ]
 
226
  raw_solve_options=[{"label": "a", "action": "move forward", "available": False}],
227
  )
228
 
229
+ assert callbacks._ui_option_label(session, "fallback", 0) == "A. move forward↓"
230
 
231
 
232
  def test_load_status_task_appends_configured_point_suffix_after_mapped_label(monkeypatch, reload_module):
 
253
 
254
  assert result[4]["choices"] == [
255
  (
256
+ f"A. move forward↓{config.UI_TEXT['actions']['point_required_suffix']}",
257
  0,
258
  )
259
  ]