cdancette commited on
Commit
1b8bf68
·
1 Parent(s): 882bd8f

better display

Browse files
Files changed (1) hide show
  1. app.py +10 -13
app.py CHANGED
@@ -32,7 +32,6 @@ from inference import (
32
  infer_image,
33
  )
34
 
35
-
36
  # ---------------------------------------------------------------------------
37
  # Configuration
38
  # ---------------------------------------------------------------------------
@@ -404,12 +403,12 @@ def load_dataset_sample(
404
 
405
  target = meta.get("target")
406
  # Generate ground truth display
407
- ground_truth_update = gr.update(visible=False)
408
  if target is not None:
409
  # Use id_to_labels.json mapping
410
  id2label = load_id_to_labels().get(head, {})
411
  label_name = id2label.get(target, str(target))
412
- ground_truth_update = gr.update(value=f"**Ground Truth:** {label_name} (class {target})", visible=True)
413
 
414
  return (
415
  display,
@@ -448,7 +447,7 @@ def run_inference(
448
  output = infer_image(image, head, image_state.get("mask"), return_probs=head not in REGRESSION_HEADS)
449
 
450
  if head in REGRESSION_HEADS:
451
- return f"**Prediction:** {output:.3f}", gr.update(visible=False)
452
 
453
  # Use id_to_labels.json mapping, fall back to model config if not available
454
  id2label = load_id_to_labels().get(head, {})
@@ -456,13 +455,12 @@ def run_inference(
456
  df = format_probabilities(output, id2label)
457
  top_row = df.iloc[0]
458
  prediction = f"{top_row['label']} (p={top_row['probability']:.3f})"
459
- result_text = f"**Prediction:** {prediction}"
460
  return result_text, gr.update(visible=True, value=df)
461
  except Exception as exc: # pragma: no cover - surfaced in UI
462
  traceback.print_exc()
463
  return f"Failed to run inference: {exc}", gr.update(visible=False)
464
 
465
-
466
  def handle_upload_preview(
467
  image: np.ndarray | Image.Image | None,
468
  head: str,
@@ -485,11 +483,11 @@ def handle_upload_preview(
485
  "Image uploaded. Click 'Run inference' to compute predictions.",
486
  "", # Reset prediction text
487
  pd.DataFrame(),
488
- gr.update(visible=False),
489
  {"image": np_image, "mask": None}, # Store raw image for inference
490
  )
491
  except Exception as exc: # pragma: no cover - surfaced in UI
492
- return None, f"Failed to load image: {exc}", "", pd.DataFrame(), gr.update(visible=False), None
493
 
494
 
495
  # ---------------------------------------------------------------------------
@@ -518,11 +516,11 @@ def build_demo() -> gr.Blocks:
518
  value=default_head,
519
  )
520
 
521
- gr.Markdown("---")
522
 
523
  with gr.Row():
524
  with gr.Column():
525
- gr.Markdown("### Load dataset sample")
526
  dataset_display = gr.Markdown(f"**Dataset:** {DATASET_OPTIONS.get(DEFAULT_DATASET_FOR_HEAD.get(default_head, ''), 'Unknown')}")
527
  dataset_status = gr.Markdown("Select a model head to load class metadata.")
528
  class_dropdown = gr.Dropdown(label="Target class filter", choices=["Random"], value="Random")
@@ -556,9 +554,8 @@ def build_demo() -> gr.Blocks:
556
  image_display = gr.Image(label="Image", interactive=False, type="numpy")
557
 
558
  with gr.Column():
559
- ground_truth_display = gr.Markdown(visible=False)
560
- gr.Markdown("### Predictions")
561
- main_prediction = gr.Markdown()
562
  prediction_probs = gr.Dataframe(headers=["class_id", "label", "probability"], visible=False)
563
 
564
  image_state = gr.State()
 
32
  infer_image,
33
  )
34
 
 
35
  # ---------------------------------------------------------------------------
36
  # Configuration
37
  # ---------------------------------------------------------------------------
 
403
 
404
  target = meta.get("target")
405
  # Generate ground truth display
406
+ ground_truth_update = gr.update(value="")
407
  if target is not None:
408
  # Use id_to_labels.json mapping
409
  id2label = load_id_to_labels().get(head, {})
410
  label_name = id2label.get(target, str(target))
411
+ ground_truth_update = gr.update(value=f"{label_name} (class {target})", visible=True)
412
 
413
  return (
414
  display,
 
447
  output = infer_image(image, head, image_state.get("mask"), return_probs=head not in REGRESSION_HEADS)
448
 
449
  if head in REGRESSION_HEADS:
450
+ return f"{output:.3f}", gr.update(visible=False)
451
 
452
  # Use id_to_labels.json mapping, fall back to model config if not available
453
  id2label = load_id_to_labels().get(head, {})
 
455
  df = format_probabilities(output, id2label)
456
  top_row = df.iloc[0]
457
  prediction = f"{top_row['label']} (p={top_row['probability']:.3f})"
458
+ result_text = prediction
459
  return result_text, gr.update(visible=True, value=df)
460
  except Exception as exc: # pragma: no cover - surfaced in UI
461
  traceback.print_exc()
462
  return f"Failed to run inference: {exc}", gr.update(visible=False)
463
 
 
464
  def handle_upload_preview(
465
  image: np.ndarray | Image.Image | None,
466
  head: str,
 
483
  "Image uploaded. Click 'Run inference' to compute predictions.",
484
  "", # Reset prediction text
485
  pd.DataFrame(),
486
+ gr.update(value=""),
487
  {"image": np_image, "mask": None}, # Store raw image for inference
488
  )
489
  except Exception as exc: # pragma: no cover - surfaced in UI
490
+ return None, f"Failed to load image: {exc}", "", pd.DataFrame(), gr.update(value=""), None
491
 
492
 
493
  # ---------------------------------------------------------------------------
 
516
  value=default_head,
517
  )
518
 
519
+ # gr.Markdown("---")
520
 
521
  with gr.Row():
522
  with gr.Column():
523
+ # gr.Markdown("### Load dataset sample")
524
  dataset_display = gr.Markdown(f"**Dataset:** {DATASET_OPTIONS.get(DEFAULT_DATASET_FOR_HEAD.get(default_head, ''), 'Unknown')}")
525
  dataset_status = gr.Markdown("Select a model head to load class metadata.")
526
  class_dropdown = gr.Dropdown(label="Target class filter", choices=["Random"], value="Random")
 
554
  image_display = gr.Image(label="Image", interactive=False, type="numpy")
555
 
556
  with gr.Column():
557
+ ground_truth_display = gr.Textbox(label="Ground Truth", interactive=False)
558
+ main_prediction = gr.Textbox(label="Prediction", value="", interactive=False)
 
559
  prediction_probs = gr.Dataframe(headers=["class_id", "label", "probability"], visible=False)
560
 
561
  image_state = gr.State()