XiangpengYang commited on
Commit
df613f6
·
1 Parent(s): 380cc0c

feat: expose UR policy config in Gradio

Browse files
Files changed (2) hide show
  1. app.py +20 -4
  2. tests/test_app.py +18 -4
app.py CHANGED
@@ -21,7 +21,7 @@ except ImportError: # Local and dedicated-GPU environments omit this helper.
21
 
22
  from artifacts import resolve_checkpoint_path, resolve_model_id
23
  from inference import ACTION_LABELS, run_prediction
24
- from model_loader import MODEL_MANAGER
25
 
26
 
27
  def _gradio_integer(value, name: str) -> int:
@@ -39,6 +39,7 @@ def _gradio_integer(value, name: str) -> int:
39
  def predict_ui(
40
  model_id,
41
  checkpoint_path,
 
42
  fixed_image,
43
  wrist_image,
44
  instruction,
@@ -53,7 +54,7 @@ def predict_ui(
53
  ):
54
  try:
55
  trial = _gradio_integer(trial_index, "trial index")
56
- policy = MODEL_MANAGER.get(model_id, checkpoint_path)
57
  result = run_prediction(
58
  policy,
59
  fixed_image,
@@ -64,7 +65,12 @@ def predict_ui(
64
  model_id,
65
  checkpoint_path,
66
  )
67
- return result.actions, result.json_path, result.status
 
 
 
 
 
68
  except Exception as exc:
69
  gc.collect()
70
  try:
@@ -98,6 +104,11 @@ def build_demo():
98
  label="Checkpoint path",
99
  placeholder="checkpoints/30000",
100
  )
 
 
 
 
 
101
  with gr.Row():
102
  fixed_image = gr.Image(type="pil", label="Fixed camera")
103
  wrist_image = gr.Image(type="pil", label="Wrist camera")
@@ -106,7 +117,11 @@ def build_demo():
106
  placeholder="e.g. pick up the object and place it in the tray",
107
  lines=2,
108
  )
109
- gr.Markdown("### Current state — metres/radians, followed by gripper state")
 
 
 
 
110
  with gr.Row():
111
  tcp_x = gr.Number(value=0.0, label="TCP x")
112
  tcp_y = gr.Number(value=0.0, label="TCP y")
@@ -128,6 +143,7 @@ def build_demo():
128
  inputs=[
129
  model_id,
130
  checkpoint_path,
 
131
  fixed_image,
132
  wrist_image,
133
  instruction,
 
21
 
22
  from artifacts import resolve_checkpoint_path, resolve_model_id
23
  from inference import ACTION_LABELS, run_prediction
24
+ from model_loader import DEFAULT_POLICY_CONFIG, MODEL_MANAGER, POLICY_CONFIGS
25
 
26
 
27
  def _gradio_integer(value, name: str) -> int:
 
39
  def predict_ui(
40
  model_id,
41
  checkpoint_path,
42
+ config_name,
43
  fixed_image,
44
  wrist_image,
45
  instruction,
 
54
  ):
55
  try:
56
  trial = _gradio_integer(trial_index, "trial index")
57
+ policy = MODEL_MANAGER.get(model_id, checkpoint_path, config_name)
58
  result = run_prediction(
59
  policy,
60
  fixed_image,
 
65
  model_id,
66
  checkpoint_path,
67
  )
68
+ uses_discrete_state = config_name == "pi05_ur_demo_state"
69
+ status = (
70
+ f"{result.status} Config={config_name} "
71
+ f"(discrete_state_input={uses_discrete_state})."
72
+ )
73
+ return result.actions, result.json_path, status
74
  except Exception as exc:
75
  gc.collect()
76
  try:
 
104
  label="Checkpoint path",
105
  placeholder="checkpoints/30000",
106
  )
107
+ config_name = gr.Dropdown(
108
+ choices=list(POLICY_CONFIGS),
109
+ value=DEFAULT_POLICY_CONFIG,
110
+ label="Policy config",
111
+ )
112
  with gr.Row():
113
  fixed_image = gr.Image(type="pil", label="Fixed camera")
114
  wrist_image = gr.Image(type="pil", label="Wrist camera")
 
117
  placeholder="e.g. pick up the object and place it in the tray",
118
  lines=2,
119
  )
120
+ gr.Markdown(
121
+ "### Current state — metres/radians, followed by gripper state\n"
122
+ "These values are discrete state conditioning only when "
123
+ "`pi05_ur_demo_state` is selected."
124
+ )
125
  with gr.Row():
126
  tcp_x = gr.Number(value=0.0, label="TCP x")
127
  tcp_y = gr.Number(value=0.0, label="TCP y")
 
143
  inputs=[
144
  model_id,
145
  checkpoint_path,
146
+ config_name,
147
  fixed_image,
148
  wrist_image,
149
  instruction,
tests/test_app.py CHANGED
@@ -11,21 +11,33 @@ class AppTests(unittest.TestCase):
11
  "Result", (),
12
  {"actions": "table", "json_path": "/tmp/result.json", "status": "done"},
13
  )()
14
- with mock.patch.object(app.MODEL_MANAGER, "get", return_value=object()), mock.patch.object(
15
  app, "run_prediction", return_value=result
16
  ):
17
  actual = app.predict_ui(
18
- "owner/model", "checkpoint", object(), object(), "task",
 
19
  1, 2, 3, 4, 5, 6, 0, 0,
20
  )
21
- self.assertEqual(actual, ("table", "/tmp/result.json", "done"))
 
 
 
 
 
 
 
 
 
 
22
 
23
  def test_predict_ui_turns_exceptions_into_status(self):
24
  import app
25
 
26
  with mock.patch.object(app.MODEL_MANAGER, "get", side_effect=RuntimeError("load failed")):
27
  table, output_file, status = app.predict_ui(
28
- "model", "checkpoint", object(), object(), "task",
 
29
  0, 0, 0, 0, 0, 0, 0, 0,
30
  )
31
  self.assertIsNone(table)
@@ -40,6 +52,8 @@ class AppTests(unittest.TestCase):
40
  ):
41
  self.assertIn(label, source)
42
  self.assertIn("default_concurrency_limit=1", source)
 
 
43
 
44
 
45
  if __name__ == "__main__":
 
11
  "Result", (),
12
  {"actions": "table", "json_path": "/tmp/result.json", "status": "done"},
13
  )()
14
+ with mock.patch.object(app.MODEL_MANAGER, "get", return_value=object()) as get_model, mock.patch.object(
15
  app, "run_prediction", return_value=result
16
  ):
17
  actual = app.predict_ui(
18
+ "owner/model", "checkpoint", "pi05_ur_demo_no_state",
19
+ object(), object(), "task",
20
  1, 2, 3, 4, 5, 6, 0, 0,
21
  )
22
+ self.assertEqual(
23
+ actual,
24
+ (
25
+ "table",
26
+ "/tmp/result.json",
27
+ "done Config=pi05_ur_demo_no_state (discrete_state_input=False).",
28
+ ),
29
+ )
30
+ get_model.assert_called_once_with(
31
+ "owner/model", "checkpoint", "pi05_ur_demo_no_state"
32
+ )
33
 
34
  def test_predict_ui_turns_exceptions_into_status(self):
35
  import app
36
 
37
  with mock.patch.object(app.MODEL_MANAGER, "get", side_effect=RuntimeError("load failed")):
38
  table, output_file, status = app.predict_ui(
39
+ "model", "checkpoint", "pi05_ur_demo_state",
40
+ object(), object(), "task",
41
  0, 0, 0, 0, 0, 0, 0, 0,
42
  )
43
  self.assertIsNone(table)
 
52
  ):
53
  self.assertIn(label, source)
54
  self.assertIn("default_concurrency_limit=1", source)
55
+ self.assertIn('label="Policy config"', source)
56
+ self.assertIn("value=DEFAULT_POLICY_CONFIG", source)
57
 
58
 
59
  if __name__ == "__main__":