Spaces:
Running
Running
Update app.py
Browse files226 unmodified lines2272282292302312322332342352362372382392402412422432442458 unmodified lines2542552562572582592602612622632642652662673 unmodified lines2712722732742752762776 unmodified lines2842852862872882892902 unmodified lines2932942952962972983 unmodified lines302303304305306307308309310311312313314315316317318319320321226 unmodified linesreturn marked_ref, heatmap_pil, overlay_pil, overlay_boxes_pil# ----------------------------# Gradio UI (+ Start button, + Model dropdown)# ----------------------------with gr.Blocks(theme=gr.themes.Soft(), title="DINOv3 Single-Image Patch Similarity") as demo:gr.Markdown("# 🦖 DINOv3 Single-Image Patch Similarity")gr.Markdown("## Running on CPU-only Space, feature extraction after uploading an image can take a moment")gr.Markdown("Upload one image, then **click anywhere** to highlight the most similar regions in the *same* image.")app_state = gr.State()with gr.Row():with gr.Column(scale=1):input_image = gr.Image(label="Image (click anywhere)",type="pil",value="https://images.squarespace-cdn.com/content/v1/607f89e638219e13eee71b1e/1684821560422-SD5V37BAG28BURTLIXUQ/michael-sum-LEpfefQf4rU-unsplash.jpg")8 unmodified lines["viridis", "magma", "plasma", "inferno", "turbo", "cividis"],value="viridis", label="Colormap",)# NEW: Backbone selector (default = smaller/faster ViT-S/16+)model_choice = gr.Dropdown(choices=AVAILABLE_MODELS,value=DEFAULT_MODEL_ID,label="Backbone (DINOv3)",info="ViT-S/16+ is smaller & faster; ViT-H/16+ is larger.",)# Start processing buttonwith gr.Row():start_btn = gr.Button("▶️ Start processing", variant="primary")3 unmodified linesbox_radius = gr.Slider(0, 10, value=1, step=1, label="Box radius (patches)")with gr.Row():marked_image = gr.Image(label="Click marker", interactive=False)heatmap_output = gr.Image(label="Similarity heatmap", interactive=False)with gr.Row():overlay_output = gr.Image(label="Overlay (image ⊕ heatmap)", interactive=False)6 unmodified linesmodel = get_model(model_id)_current_model_id = model_iddef _on_upload_or_slider_change(img: Image.Image, long_side: int, model_id: str, progress=gr.Progress(track_tqdm=True)):if img is None:return None, None_ensure_model(model_id)2 unmodified linesprogress(1, desc="Done!")return st["img"], stdef _on_click(st, a: float, m: str, excl: int, k: int, box_rad: int, evt: gr.SelectData):if not st or evt is None:return None, None, None, None3 unmodified linesbox_radius_patches=int(box_rad),)# Wire eventsinputs_for_update = [input_image, target_long_side, model_choice]outputs_for_update = [marked_image, app_state]# Auto triggers (kept)input_image.upload(_on_upload_or_slider_change, inputs=inputs_for_update, outputs=outputs_for_update)target_long_side.change(_on_upload_or_slider_change, inputs=inputs_for_update, outputs=outputs_for_update)model_choice.change(_on_upload_or_slider_change, inputs=inputs_for_update, outputs=outputs_for_update)demo.load(_on_upload_or_slider_change, inputs=inputs_for_update, outputs=outputs_for_update) # Process default image on load# Manual trigger via button (kept)start_btn.click(_on_upload_or_slider_change, inputs=inputs_for_update, outputs=outputs_for_update)# Click to compute similaritiesmarked_image.select(_on_click,inputs=[app_state, alpha, cmap, exclude_r, topk, box_radius],226 unmodified lines2272282292302312322332342352362372382392402412422432448 unmodified lines2532542552562572582592602612622632642652663 unmodified lines2702712722732742752766 unmodified lines2832842852862872882892902 unmodified lines2932942952962972982993 unmodified lines303304305306307308309310311312313314315316317318319320321322323324325326327328329330331226 unmodified linesreturn marked_ref, heatmap_pil, overlay_pil, overlay_boxes_pil# ----------------------------# Gradio UI (Manual-only processing)# ----------------------------with gr.Blocks(theme=gr.themes.Soft(), title="DINOv3 Single-Image Patch Similarity") as demo:gr.Markdown("# 🦖 DINOv3 Single-Image Patch Similarity")gr.Markdown("Upload one image, adjust settings, then press **▶️ Start processing**. Click on the processed image to find similar regions.")app_state = gr.State()with gr.Row():with gr.Column(scale=1):input_image = gr.Image(label="Image (click anywhere after processing)",type="pil",value="https://images.squarespace-cdn.com/content/v1/607f89e638219e13eee71b1e/1684821560422-SD5V37BAG28BURTLIXUQ/michael-sum-LEpfefQf4rU-unsplash.jpg")8 unmodified lines["viridis", "magma", "plasma", "inferno", "turbo", "cividis"],value="viridis", label="Colormap",)# Backbone selector (default = smaller/faster ViT-S/16+)model_choice = gr.Dropdown(choices=AVAILABLE_MODELS,value=DEFAULT_MODEL_ID,label="Backbone (DINOv3)",info="ViT-S/16+ is smaller & faster; ViT-H/16+ is larger.",)# Start processing button (manual trigger)with gr.Row():start_btn = gr.Button("▶️ Start processing", variant="primary")3 unmodified linesbox_radius = gr.Slider(0, 10, value=1, step=1, label="Box radius (patches)")with gr.Row():marked_image = gr.Image(label="Click marker / Preview", interactive=False)heatmap_output = gr.Image(label="Similarity heatmap", interactive=False)with gr.Row():overlay_output = gr.Image(label="Overlay (image ⊕ heatmap)", interactive=False)6 unmodified linesmodel = get_model(model_id)_current_model_id = model_id# Manual feature extraction (only runs on Start button)def _run_extraction(img: Image.Image, long_side: int, model_id: str, progress=gr.Progress(track_tqdm=True)):if img is None:return None, None_ensure_model(model_id)2 unmodified linesprogress(1, desc="Done!")return st["img"], st# Clicking on processed image to compute similaritiesdef _on_click(st, a: float, m: str, excl: int, k: int, box_rad: int, evt: gr.SelectData):if not st or evt is None:return None, None, None, None3 unmodified linesbox_radius_patches=int(box_rad),)# On image change: just preview and clear outputs/state (NO extraction)def _on_image_changed(img: Image.Image):if img is None:return None, None, None, None, Nonereturn img, None, None, None, None# ---------- Wiring (Manual mode) ----------# Do NOT auto-run on upload/slider/model change or on app load.# Only the Start button triggers extraction.start_btn.click(_run_extraction,inputs=[input_image, target_long_side, model_choice],outputs=[marked_image, app_state],)# When a new image is picked, show it as preview and clear old results.input_image.change(_on_image_changed,inputs=[input_image],outputs=[marked_image, app_state, heatmap_output, overlay_output, overlay_boxes_output],)# Keep click handler the same.marked_image.select(_on_click,inputs=[app_state, alpha, cmap, exclude_r, topk, box_radius],
app.py+30-20