"""Gradio layout and bundled examples for the RSReasonNet demo.""" import json from pathlib import Path import gradio as gr EXAMPLE_DIR = Path(__file__).resolve().parent / "example" EXAMPLE_TITLES = { "03261.jpg": "Ship · Maritime logistics", "04077.jpg": "Storage tank · Industrial infrastructure", "02026.jpg": "Tennis court · Everyday language", } def load_examples(): """Keep each image paired with its original annotated instruction.""" with (EXAMPLE_DIR / "annotations.json").open(encoding="utf-8") as handle: annotations = json.load(handle) examples = [] for annotation in annotations: image_path = EXAMPLE_DIR / annotation["id"] if not image_path.is_file(): raise FileNotFoundError(f"Example image is missing: {image_path.name}") examples.append((str(image_path), annotation["sentence"])) return examples CSS = """ .gradio-container { max-width: 1120px !important; margin: auto; } #rs-header { text-align: center; padding: 22px 16px 18px; } #rs-header .eyebrow { display: inline-block; padding: 5px 12px; border-radius: 999px; border: 1px solid var(--border-color-primary); color: var(--body-text-color-subdued); font-size: 12px; letter-spacing: .05em; } #rs-header h1 { margin: 12px 0 8px; font-size: clamp(30px, 5vw, 42px); font-weight: 700; letter-spacing: -.04em; color: var(--body-text-color); } #rs-header p { margin: 0 auto; max-width: 640px; color: var(--body-text-color-subdued); font-size: 15px; line-height: 1.6; } .rs-panel { padding: 18px !important; border-radius: 16px !important; } .rs-section h2 { font-size: 18px !important; margin: 0 0 4px !important; } .rs-section p { color: var(--body-text-color-subdued); font-size: 13px; } #rs-run { min-height: 44px; font-weight: 600; } #rs-examples-heading { margin-top: 12px; } #rs-examples { border-radius: 14px; } #rs-examples .grid-wrap { height: auto; min-height: 0; max-height: none; } #rs-examples .grid-container { grid-template-columns: repeat(3, minmax(0, 1fr)); grid-template-rows: none; } #rs-examples .thumbnail-item { cursor: pointer; aspect-ratio: auto; height: 210px; } #rs-examples .thumbnail-item:hover { border-color: var(--color-accent); } #rs-examples .caption-label { font-size: 13px; } #rs-note { color: var(--body-text-color-subdued); font-size: 12px; } @media (max-width: 640px) { #rs-header { padding: 14px 4px; } .rs-panel { padding: 12px !important; } #rs-examples .grid-container { grid-template-columns: minmax(0, 1fr); } } """ def build_demo(predict): examples = load_examples() theme = gr.themes.Soft( primary_hue="blue", secondary_hue="slate", neutral_hue="slate", font=["system-ui", "sans-serif"], ) with gr.Blocks( title="RSReasonNet · Reasoning Segmentation", theme=theme, css=CSS, analytics_enabled=False, ) as demo: gr.HTML("""
🛰️ REMOTE SENSING · REASONING SEGMENTATION

RSReasonNet

Describe what you are looking for. Explore how language guides segmentation in satellite imagery.

""") with gr.Row(equal_height=True): with gr.Column(min_width=300, variant="panel", elem_classes="rs-panel"): gr.Markdown( "## 1. Image & instruction\nUpload an image or choose an example below.", elem_classes="rs-section", ) input_image = gr.Image( value=examples[0][0], type="filepath", sources=["upload"], label="Satellite image", height=320, elem_id="rs-input-image", show_share_button=False, ) instruction = gr.Textbox( value=examples[0][1], label="What should the model find?", placeholder="Describe an object, its purpose, or a clue in English…", lines=3, max_lines=5, info="Use an English instruction (up to 1,024 characters).", ) with gr.Row(): run_button = gr.Button( "Run segmentation", variant="primary", scale=3, elem_id="rs-run", ) clear_button = gr.ClearButton(value="Clear", scale=1) with gr.Column(min_width=300, variant="panel", elem_classes="rs-panel"): gr.Markdown( "## 2. Segmentation & response\nRun the model to see the region it identifies.", elem_classes="rs-section", ) output_image = gr.Image( type="numpy", label="Segmentation overlay", height=320, interactive=False, show_share_button=False, elem_id="rs-output-image", ) text_output = gr.Textbox( label="Model response", lines=3, max_lines=6, interactive=False, show_copy_button=True, placeholder="The model's text response will appear here.", ) gr.Markdown( "🔴 **Red overlay** marks the predicted region. " "Some instructions may return text without a mask.", elem_id="rs-note", ) gr.Markdown( "## Try an example\nClick a thumbnail to load its image and original instruction, " "then select **Run segmentation**.", elem_id="rs-examples-heading", elem_classes="rs-section", ) gallery = gr.Gallery( value=[(path, EXAMPLE_TITLES.get(Path(path).name, Path(path).stem)) for path, _ in examples], label="Example images", show_label=False, columns=3, rows=1, object_fit="contain", allow_preview=False, interactive=False, show_share_button=False, show_download_button=False, show_fullscreen_button=False, elem_id="rs-examples", ) # Keep the existing API argument order: instruction first, image second. run_event = gr.on( triggers=[run_button.click, instruction.submit], fn=predict, inputs=[instruction, input_image], outputs=[output_image, text_output], api_name="predict", concurrency_limit=1, concurrency_id="segmentation", show_progress="full", ) def select_example(event: gr.SelectData): image_path, prompt = examples[event.index] # Allow the same card to be selected again after clearing or editing. return image_path, prompt, None, "", gr.Gallery(selected_index=None) gallery.select( select_example, inputs=None, outputs=[input_image, instruction, output_image, text_output, gallery], queue=False, api_name=False, cancels=[run_event], ) clear_button.add([input_image, instruction, output_image, text_output]) clear_button.click(fn=None, cancels=[run_event], queue=False, api_name=False) # A result belongs to its input pair; remove it when that pair is edited. gr.on( triggers=[input_image.input, instruction.input], fn=lambda: (None, ""), inputs=None, outputs=[output_image, text_output], queue=False, api_name=False, cancels=[run_event], ) return demo