theoracle commited on
Commit
5bd13f7
Β·
1 Parent(s): 4ecc4dd

Fix step error handling and wire up error_box

Browse files
Files changed (1) hide show
  1. app.py +12 -13
app.py CHANGED
@@ -27,7 +27,6 @@ def safe_run_background(*args, **kwargs):
27
  return run_background_removal_and_inpaint(*args, **kwargs)
28
  except Exception as e:
29
  _print_trace()
30
- # We still raise gr.Error so the wrapper will catch & log
31
  raise gr.Error(f"[Step 2] Background replacement failed: {type(e).__name__}: {e}")
32
 
33
  # ───────────────────── UI ─────────────────────
@@ -62,7 +61,6 @@ with gr.Blocks() as demo:
62
  def _save_to_state(img):
63
  return {"step1": img} if img is not None else gr.skip()
64
 
65
- # Build the click-chain and store it in `event`
66
  event = (
67
  run_btn.click(
68
  fn=safe_generate_with_lora,
@@ -75,8 +73,8 @@ with gr.Blocks() as demo:
75
 
76
  # ─────────── STEP 2: Background Replacement ───────────
77
  with gr.Tab("Step 2: Replace Background"):
78
- # This textbox will show any error messages from Step 2
79
- error_box = gr.Textbox(label="Error", interactive=False, lines=2)
80
 
81
  with gr.Row():
82
  inpaint_prompt = gr.Textbox(
@@ -95,26 +93,27 @@ with gr.Blocks() as demo:
95
  inpaint_btn = gr.Button("Remove Background & Inpaint", interactive=False)
96
 
97
  def guarded_inpaint(img, prompt_bg, neg_bg):
98
- # If no image is in state, return an error immediately
99
  if img is None:
100
- return None, "Error: No headshot foundβ€”please run Step 1 first."
 
101
  try:
102
  print("[DEBUG] Starting background removal and inpainting…", flush=True)
103
  result = safe_run_background(img, prompt_bg, neg_bg)
104
- return result, "" # success: show result, clear error
 
 
 
105
  except Exception as e:
106
- # Log to console/Space logs
107
- print(f"[Step 2 ERROR] {type(e).__name__}: {e}", flush=True)
108
- return None, f"{type(e).__name__}: {e}"
109
 
110
  inpaint_btn.click(
111
  fn=guarded_inpaint,
112
  inputs=[shared_output, inpaint_prompt, inpaint_negative],
113
- outputs=[inpaint_result, error_box], # wire both outputs!
114
  )
115
 
116
- # Enable the Step 2 button once Step 1’s chain finishes
117
  event.then(lambda: gr.update(interactive=True), None, inpaint_btn)
118
 
119
- # Launch in debug mode so exceptions & prints go to your terminal/logs
120
  demo.launch(debug=True)
 
27
  return run_background_removal_and_inpaint(*args, **kwargs)
28
  except Exception as e:
29
  _print_trace()
 
30
  raise gr.Error(f"[Step 2] Background replacement failed: {type(e).__name__}: {e}")
31
 
32
  # ───────────────────── UI ─────────────────────
 
61
  def _save_to_state(img):
62
  return {"step1": img} if img is not None else gr.skip()
63
 
 
64
  event = (
65
  run_btn.click(
66
  fn=safe_generate_with_lora,
 
73
 
74
  # ─────────── STEP 2: Background Replacement ───────────
75
  with gr.Tab("Step 2: Replace Background"):
76
+ # Show formatted error messages
77
+ error_box = gr.Markdown(value="", visible=True)
78
 
79
  with gr.Row():
80
  inpaint_prompt = gr.Textbox(
 
93
  inpaint_btn = gr.Button("Remove Background & Inpaint", interactive=False)
94
 
95
  def guarded_inpaint(img, prompt_bg, neg_bg):
 
96
  if img is None:
97
+ return None, "**πŸ›‘ Error:** No headshot found β€” please run Step 1 first."
98
+
99
  try:
100
  print("[DEBUG] Starting background removal and inpainting…", flush=True)
101
  result = safe_run_background(img, prompt_bg, neg_bg)
102
+ return result, "" # Clear error on success
103
+ except gr.Error as e:
104
+ print(f"[Step 2 gr.Error] {e}", flush=True)
105
+ return None, f"**πŸ›‘ Step 2 Failed:** {str(e)}"
106
  except Exception as e:
107
+ print(f"[Step 2 UNEXPECTED ERROR] {type(e).__name__}: {e}", flush=True)
108
+ return None, f"**❌ Unexpected Error:** {type(e).__name__}: {e}"
 
109
 
110
  inpaint_btn.click(
111
  fn=guarded_inpaint,
112
  inputs=[shared_output, inpaint_prompt, inpaint_negative],
113
+ outputs=[inpaint_result, error_box],
114
  )
115
 
116
+ # Enable Step 2 after Step 1 completes
117
  event.then(lambda: gr.update(interactive=True), None, inpaint_btn)
118
 
 
119
  demo.launch(debug=True)