zukhriddinai commited on
Commit
b2f8914
·
verified ·
1 Parent(s): ef93234

Deploy public authenticated AutoML gateway

Browse files
Files changed (1) hide show
  1. gateway_app.py +98 -92
gateway_app.py CHANGED
@@ -1875,38 +1875,46 @@ def _staged_visual_update(state: Any, index: int) -> Any:
1875
  updates = _staged_visual_updates(state)
1876
  update = updates[index]
1877
  if (
1878
- index > 0
1879
- and isinstance(update, Mapping)
1880
  and update.get("visible") is False
1881
- and update.get("value") is None
1882
  ):
1883
- # Gradio 6.17 can abort a chained event after applying an explicit
1884
- # hidden/empty Image update. Returning ``gr.skip()`` also terminates
1885
- # the remaining ``then`` callbacks in the live client, so preserve the
1886
- # chain with a visibility-only update. Model/package changes already
1887
- # run the canonical reset handler that clears prior visual values.
1888
  return gr.update(visible=False)
1889
  return update
1890
 
1891
 
1892
- def _staged_visual_warning(state: Any) -> Any:
1893
- return _staged_visual_update(state, 0)
1894
-
1895
-
1896
- def _staged_visual_source(state: Any) -> Any:
1897
- return _staged_visual_update(state, 1)
1898
-
1899
 
1900
- def _staged_visual_annotated(state: Any) -> Any:
1901
- return _staged_visual_update(state, 2)
1902
-
1903
-
1904
- def _staged_visual_mask(state: Any) -> Any:
1905
- return _staged_visual_update(state, 3)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1906
 
1907
 
1908
- def _staged_visual_overlay(state: Any) -> Any:
1909
- return _staged_visual_update(state, 4)
1910
 
1911
 
1912
  def _existing_payload(
@@ -2290,6 +2298,19 @@ def _guided_visual_outputs(outputs: Any) -> tuple[Any, ...]:
2290
  return _visual_ui_updates(value, warning=warning)
2291
 
2292
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2293
  def guided_run_ui_handler(
2294
  state_data,
2295
  data_source,
@@ -2580,7 +2601,7 @@ def guided_parallel_plan_ui_handler(
2580
  for value in (
2581
  *outputs,
2582
  *_guided_snapshot_outputs(variant_state, {}),
2583
- *_guided_visual_outputs(outputs),
2584
  )
2585
  )
2586
 
@@ -2597,7 +2618,7 @@ def guided_parallel_plan_ui_handler(
2597
  for value in (
2598
  *outputs,
2599
  *_guided_snapshot_outputs(variant_state, outputs),
2600
- *_guided_visual_outputs(outputs),
2601
  )
2602
  )
2603
 
@@ -3244,34 +3265,34 @@ def _add_results_section(label: str = "Latest run"):
3244
  visible=False,
3245
  elem_classes=["visual-prediction-warning"],
3246
  )
3247
- with gr.Row(equal_height=True):
3248
- visual_source = gr.Image(
3249
- label="Original image",
3250
- interactive=False,
3251
- visible=False,
3252
- type="filepath",
3253
- elem_classes=["visual-prediction-image"],
3254
- )
 
 
 
 
 
 
3255
  visual_annotated = gr.Image(
3256
- label="Annotated prediction",
3257
  interactive=False,
3258
  visible=False,
3259
  type="filepath",
3260
- elem_classes=["visual-prediction-image"],
3261
  )
3262
  visual_mask = gr.Image(
3263
- label="Predicted binary mask",
3264
  interactive=False,
3265
  visible=False,
3266
  type="filepath",
3267
- elem_classes=["visual-prediction-image"],
3268
  )
3269
  visual_overlay = gr.Image(
3270
- label="Mask overlay",
3271
  interactive=False,
3272
  visible=False,
3273
  type="filepath",
3274
- elem_classes=["visual-prediction-image"],
3275
  )
3276
  predictions = gr.DataFrame(
3277
  label="Held-out prediction sample",
@@ -4152,35 +4173,34 @@ def create_interface() -> gr.Blocks:
4152
  predict_visual_staging_state = gr.State(
4153
  _staged_visual_updates(())
4154
  )
4155
- with gr.Row(equal_height=True):
4156
- predict_visual_source = gr.Image(
4157
- label="Original image",
4158
- interactive=False,
4159
- visible=False,
4160
- type="filepath",
4161
- elem_classes=["visual-prediction-image"],
4162
- )
 
 
 
 
 
 
4163
  predict_visual_annotated = gr.Image(
4164
- label="Annotated prediction",
4165
  interactive=False,
4166
  visible=False,
4167
  type="filepath",
4168
- elem_classes=["visual-prediction-image"],
4169
  )
4170
- with gr.Row(equal_height=True):
4171
  predict_visual_mask = gr.Image(
4172
- label="Predicted binary mask",
4173
  interactive=False,
4174
  visible=False,
4175
  type="filepath",
4176
- elem_classes=["visual-prediction-image"],
4177
  )
4178
  predict_visual_overlay = gr.Image(
4179
- label="Mask overlay",
4180
  interactive=False,
4181
  visible=False,
4182
  type="filepath",
4183
- elem_classes=["visual-prediction-image"],
4184
  )
4185
  predict_frame = gr.DataFrame(
4186
  label="Prediction",
@@ -4822,22 +4842,18 @@ def create_interface() -> gr.Blocks:
4822
  [*latest_outputs[:16], latest_visual_staging_state],
4823
  api_name=False,
4824
  )
4825
- for visual_fn, visual_output in zip(
4826
- (
4827
- _staged_visual_warning,
4828
- _staged_visual_source,
4829
- _staged_visual_annotated,
4830
- _staged_visual_mask,
4831
- _staged_visual_overlay,
4832
- ),
4833
- latest_outputs[16:21],
4834
- ):
4835
- primary_run_event = primary_run_event.then(
4836
- visual_fn,
4837
- latest_visual_staging_state,
4838
- visual_output,
4839
- api_name=False,
4840
- )
4841
  primary_run_event.then(
4842
  _guided_run_button_ready,
4843
  inputs=[],
@@ -4891,28 +4907,18 @@ def create_interface() -> gr.Blocks:
4891
  ],
4892
  api_name=False,
4893
  )
4894
- for visual_fn, visual_output in zip(
4895
- (
4896
- _staged_visual_warning,
4897
- _staged_visual_source,
4898
- _staged_visual_annotated,
4899
- _staged_visual_mask,
4900
- _staged_visual_overlay,
4901
- ),
4902
- (
4903
- predict_visual_warning,
4904
- predict_visual_source,
4905
- predict_visual_annotated,
4906
- predict_visual_mask,
4907
- predict_visual_overlay,
4908
- ),
4909
- ):
4910
- predict_event = predict_event.then(
4911
- visual_fn,
4912
- predict_visual_staging_state,
4913
- visual_output,
4914
- api_name=False,
4915
- )
4916
  return demo
4917
 
4918
 
 
1875
  updates = _staged_visual_updates(state)
1876
  update = updates[index]
1877
  if (
1878
+ isinstance(update, Mapping)
 
1879
  and update.get("visible") is False
1880
+ and update.get("value") in (None, "")
1881
  ):
1882
+ # Keep an empty warning as a visibility-only change. Gradio 6.17 can
1883
+ # abort a chained event when it receives an explicit hidden/empty
1884
+ # value, while ``gr.skip()`` terminates the remaining callbacks.
 
 
1885
  return gr.update(visible=False)
1886
  return update
1887
 
1888
 
1889
+ def _staged_visual_gallery(state: Any) -> Any:
1890
+ """Present task-relevant visual artifacts in one stable, captioned gallery."""
 
 
 
 
 
1891
 
1892
+ updates = _staged_visual_updates(state)
1893
+ visible_paths: dict[int, str] = {}
1894
+ for index in range(1, 5):
1895
+ update = updates[index]
1896
+ if not isinstance(update, Mapping) or update.get("visible") is not True:
1897
+ continue
1898
+ value = str(update.get("value") or "").strip()
1899
+ if value:
1900
+ visible_paths[index] = value
1901
+
1902
+ captions = {
1903
+ 1: "Original image",
1904
+ 2: "Annotated prediction",
1905
+ 3: "Predicted binary mask",
1906
+ 4: (
1907
+ "Image + mask overlay"
1908
+ if 3 in visible_paths
1909
+ else "Instance-mask overlay"
1910
+ ),
1911
+ }
1912
+ items = [(path, captions[index]) for index, path in visible_paths.items()]
1913
+ return gr.update(value=items or None, visible=bool(items))
1914
 
1915
 
1916
+ def _staged_visual_warning(state: Any) -> Any:
1917
+ return _staged_visual_update(state, 0)
1918
 
1919
 
1920
  def _existing_payload(
 
2298
  return _visual_ui_updates(value, warning=warning)
2299
 
2300
 
2301
+ def _guided_gallery_outputs(outputs: Any) -> tuple[Any, ...]:
2302
+ """Adapt parallel-run visual sidecars to the stable gallery presentation."""
2303
+
2304
+ updates = _guided_visual_outputs(outputs)
2305
+ return (
2306
+ _staged_visual_update(updates, 0),
2307
+ _staged_visual_gallery(updates),
2308
+ gr.update(value=None, visible=False),
2309
+ gr.update(value=None, visible=False),
2310
+ gr.update(value=None, visible=False),
2311
+ )
2312
+
2313
+
2314
  def guided_run_ui_handler(
2315
  state_data,
2316
  data_source,
 
2601
  for value in (
2602
  *outputs,
2603
  *_guided_snapshot_outputs(variant_state, {}),
2604
+ *_guided_gallery_outputs(outputs),
2605
  )
2606
  )
2607
 
 
2618
  for value in (
2619
  *outputs,
2620
  *_guided_snapshot_outputs(variant_state, outputs),
2621
+ *_guided_gallery_outputs(outputs),
2622
  )
2623
  )
2624
 
 
3265
  visible=False,
3266
  elem_classes=["visual-prediction-warning"],
3267
  )
3268
+ visual_source = gr.Gallery(
3269
+ label="Visual prediction",
3270
+ interactive=False,
3271
+ visible=False,
3272
+ type="filepath",
3273
+ columns=3,
3274
+ rows=1,
3275
+ height="auto",
3276
+ object_fit="contain",
3277
+ allow_preview=True,
3278
+ fit_columns=True,
3279
+ elem_classes=["visual-prediction-gallery"],
3280
+ )
3281
+ with gr.Row(visible=False):
3282
  visual_annotated = gr.Image(
 
3283
  interactive=False,
3284
  visible=False,
3285
  type="filepath",
 
3286
  )
3287
  visual_mask = gr.Image(
 
3288
  interactive=False,
3289
  visible=False,
3290
  type="filepath",
 
3291
  )
3292
  visual_overlay = gr.Image(
 
3293
  interactive=False,
3294
  visible=False,
3295
  type="filepath",
 
3296
  )
3297
  predictions = gr.DataFrame(
3298
  label="Held-out prediction sample",
 
4173
  predict_visual_staging_state = gr.State(
4174
  _staged_visual_updates(())
4175
  )
4176
+ predict_visual_source = gr.Gallery(
4177
+ label="Visual prediction",
4178
+ interactive=False,
4179
+ visible=False,
4180
+ type="filepath",
4181
+ columns=3,
4182
+ rows=1,
4183
+ height="auto",
4184
+ object_fit="contain",
4185
+ allow_preview=True,
4186
+ fit_columns=True,
4187
+ elem_classes=["visual-prediction-gallery"],
4188
+ )
4189
+ with gr.Row(visible=False):
4190
  predict_visual_annotated = gr.Image(
 
4191
  interactive=False,
4192
  visible=False,
4193
  type="filepath",
 
4194
  )
 
4195
  predict_visual_mask = gr.Image(
 
4196
  interactive=False,
4197
  visible=False,
4198
  type="filepath",
 
4199
  )
4200
  predict_visual_overlay = gr.Image(
 
4201
  interactive=False,
4202
  visible=False,
4203
  type="filepath",
 
4204
  )
4205
  predict_frame = gr.DataFrame(
4206
  label="Prediction",
 
4842
  [*latest_outputs[:16], latest_visual_staging_state],
4843
  api_name=False,
4844
  )
4845
+ primary_run_event = primary_run_event.then(
4846
+ _staged_visual_gallery,
4847
+ latest_visual_staging_state,
4848
+ latest_outputs[17],
4849
+ api_name=False,
4850
+ )
4851
+ primary_run_event = primary_run_event.then(
4852
+ _staged_visual_warning,
4853
+ latest_visual_staging_state,
4854
+ latest_outputs[16],
4855
+ api_name=False,
4856
+ )
 
 
 
 
4857
  primary_run_event.then(
4858
  _guided_run_button_ready,
4859
  inputs=[],
 
4907
  ],
4908
  api_name=False,
4909
  )
4910
+ predict_event = predict_event.then(
4911
+ _staged_visual_gallery,
4912
+ predict_visual_staging_state,
4913
+ predict_visual_source,
4914
+ api_name=False,
4915
+ )
4916
+ predict_event.then(
4917
+ _staged_visual_warning,
4918
+ predict_visual_staging_state,
4919
+ predict_visual_warning,
4920
+ api_name=False,
4921
+ )
 
 
 
 
 
 
 
 
 
 
4922
  return demo
4923
 
4924