ShashwatGupta23 Cursor commited on
Commit
ad3fc61
·
1 Parent(s): 5673379

Fix HF UI: restore gr.Interface layout instead of bare Blocks

Browse files

The Blocks + bottom Examples gallery broke badly in the HF iframe.
Use Interface with Soft theme and 4 curated examples for consistent
local and Space appearance.

Co-authored-by: Cursor <cursoragent@cursor.com>

Files changed (1) hide show
  1. app.py +36 -21
app.py CHANGED
@@ -65,11 +65,27 @@ def run_demo(defect_path: str | None, reference_path: str | None):
65
 
66
 
67
  def _example_pairs() -> list[list[str]]:
 
68
  pairs: list[list[str]] = []
69
  ex_dir = _ROOT / "examples"
70
  if not ex_dir.is_dir():
71
  return pairs
 
 
 
 
 
 
 
 
 
 
 
 
 
72
  for inp in sorted(ex_dir.glob("*_input.jpg")):
 
 
73
  ref = inp.with_name(inp.name.replace("_input.", "_reference."))
74
  if ref.is_file():
75
  pairs.append([str(inp), str(ref)])
@@ -84,27 +100,26 @@ def _preload_model() -> None:
84
  print(f"[RefDiffNet] WARN: {e}")
85
 
86
 
87
- def build_demo() -> gr.Blocks:
88
- with gr.Blocks(title=TITLE) as demo:
89
- gr.Markdown(DESCRIPTION.strip())
90
- with gr.Row():
91
- defect_in = gr.Image(type="filepath", label="Defect image (input)")
92
- ref_in = gr.Image(type="filepath", label="Golden reference")
93
- run_btn = gr.Button("Run enrichment", variant="primary")
94
- with gr.Row():
95
- out_enriched = gr.Image(type="pil", label="Enriched output")
96
- out_row = gr.Image(type="pil", label="Defect | Reference | Enriched")
97
- run_btn.click(run_demo, inputs=[defect_in, ref_in], outputs=[out_enriched, out_row])
98
-
99
- examples = _example_pairs()
100
- if examples:
101
- gr.Examples(
102
- examples=examples,
103
- inputs=[defect_in, ref_in],
104
- examples_per_page=3,
105
- label="Example pairs (pre-aligned HR-IPCB samples)",
106
- )
107
- return demo
108
 
109
 
110
  demo = build_demo()
 
65
 
66
 
67
  def _example_pairs() -> list[list[str]]:
68
+ """Curated examples (Interface layout; keep short for HF iframe width)."""
69
  pairs: list[list[str]] = []
70
  ex_dir = _ROOT / "examples"
71
  if not ex_dir.is_dir():
72
  return pairs
73
+ preferred = [
74
+ "Short_06_short_11_rot_idx010_input.jpg",
75
+ "Open_circuit_04_open_circuit_14_idx002_input.jpg",
76
+ "Mouse_bite_05_mouse_bite_02_idx007_input.jpg",
77
+ "Missing_hole_07_missing_hole_03_rot_idx004_input.jpg",
78
+ ]
79
+ seen: set[str] = set()
80
+ for name in preferred:
81
+ inp = ex_dir / name
82
+ ref = inp.with_name(name.replace("_input.", "_reference."))
83
+ if inp.is_file() and ref.is_file():
84
+ pairs.append([str(inp), str(ref)])
85
+ seen.add(name)
86
  for inp in sorted(ex_dir.glob("*_input.jpg")):
87
+ if inp.name in seen or len(pairs) >= 4:
88
+ break
89
  ref = inp.with_name(inp.name.replace("_input.", "_reference."))
90
  if ref.is_file():
91
  pairs.append([str(inp), str(ref)])
 
100
  print(f"[RefDiffNet] WARN: {e}")
101
 
102
 
103
+ def build_demo() -> gr.Interface:
104
+ """gr.Interface gives the same polished card layout locally and on HF Spaces."""
105
+ return gr.Interface(
106
+ fn=run_demo,
107
+ inputs=[
108
+ gr.Image(type="filepath", label="Defect image (input)", height=320),
109
+ gr.Image(type="filepath", label="Golden reference", height=320),
110
+ ],
111
+ outputs=[
112
+ gr.Image(type="pil", label="Enriched output", height=360),
113
+ gr.Image(type="pil", label="Defect | Reference | Enriched", height=360),
114
+ ],
115
+ title=TITLE,
116
+ description=DESCRIPTION.strip(),
117
+ examples=_example_pairs() or None,
118
+ examples_per_page=2,
119
+ cache_examples=False,
120
+ flagging_mode="never",
121
+ theme=gr.themes.Soft(),
122
+ )
 
123
 
124
 
125
  demo = build_demo()