Spaces:
Sleeping
Sleeping
File size: 7,574 Bytes
a13210a 42404e5 b036ed3 a13210a a3c9048 a13210a a3c9048 a13210a a3c9048 a13210a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 | """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("""
<header id="rs-header">
<span class="eyebrow">🛰️ REMOTE SENSING · REASONING SEGMENTATION</span>
<h1>RSReasonNet</h1>
<p>Describe what you are looking for. Explore how language guides
segmentation in satellite imagery.</p>
</header>
""")
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
|