Spaces:
Runtime error
Runtime error
Add dynamic tab rendering for Extra Space, SAM2.1 Masking Tool, and Custom IFrame Loader with open/close functionality
Browse files
app.py
CHANGED
|
@@ -139,55 +139,128 @@ print(client.predict("Alex", 5, False, api_name="/greet"))
|
|
| 139 |
image_output = gr.Image(label="Sepia Image")
|
| 140 |
sepia_btn.click(sepia, image_input, image_output)
|
| 141 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
with gr.Tab("Extra Space", visible=False) as extra_space_tab:
|
| 143 |
-
gr.
|
| 144 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
|
| 146 |
with gr.Tab("SAM2.1 Masking Tool (gr.load)", visible=False) as sam_tab:
|
| 147 |
-
gr.
|
| 148 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
|
| 150 |
with gr.Tab("SAM2.1 Masking Tool (iframe)", visible=False) as sam_iframe_tab:
|
| 151 |
-
gr.
|
| 152 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
with gr.Tab("SAM2.1 Masking Tool (API Proxy)", visible=False) as sam_api_tab:
|
| 155 |
-
gr.
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
|
| 160 |
with gr.Tab("Custom IFrame Loader") as custom_iframe_tab:
|
| 161 |
-
gr.
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
try:
|
| 178 |
-
resp = requests.post(
|
| 179 |
-
"https://lpx55-sam2-1-image-predictor-masking-tool-cpu.hf.space/run/predict",
|
| 180 |
-
json=payload,
|
| 181 |
-
timeout=60
|
| 182 |
-
)
|
| 183 |
-
if resp.status_code == 200:
|
| 184 |
-
return json.dumps(resp.json(), indent=2)
|
| 185 |
else:
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
|
| 192 |
def show_tab():
|
| 193 |
return gr.Tab(visible=True)
|
|
@@ -210,14 +283,6 @@ print(client.predict("Alex", 5, False, api_name="/greet"))
|
|
| 210 |
|
| 211 |
load_sam_api_btn.click(fn=show_sam_api_tab, outputs=[sam_api_tab])
|
| 212 |
|
| 213 |
-
def load_custom_iframe(url):
|
| 214 |
-
if not url:
|
| 215 |
-
return "<div style='color:red'>Please enter a URL.</div>"
|
| 216 |
-
return f'<iframe src="{url}" width="100%" height="800" style="border:none;"></iframe>'
|
| 217 |
-
|
| 218 |
-
load_custom_iframe_btn.click(fn=load_custom_iframe, inputs=custom_url, outputs=custom_iframe)
|
| 219 |
-
|
| 220 |
-
|
| 221 |
def open_in_new_tab():
|
| 222 |
# This function does nothing server-side, but the button will have a link
|
| 223 |
pass
|
|
|
|
| 139 |
image_output = gr.Image(label="Sepia Image")
|
| 140 |
sepia_btn.click(sepia, image_input, image_output)
|
| 141 |
|
| 142 |
+
# Add state variables for each dynamic tab
|
| 143 |
+
extra_space_open = gr.State(True)
|
| 144 |
+
sam_tab_open = gr.State(True)
|
| 145 |
+
sam_iframe_tab_open = gr.State(True)
|
| 146 |
+
sam_api_tab_open = gr.State(True)
|
| 147 |
+
custom_iframe_tab_open = gr.State(True)
|
| 148 |
+
|
| 149 |
with gr.Tab("Extra Space", visible=False) as extra_space_tab:
|
| 150 |
+
@gr.render(inputs=extra_space_open)
|
| 151 |
+
def render_extra_space(is_open):
|
| 152 |
+
if is_open:
|
| 153 |
+
gr.Markdown("## External Gradio Space")
|
| 154 |
+
gr.HTML('<iframe src="https://huggingface.co/spaces/gradio/calculator" width="100%" height="600" style="border:none;"></iframe>')
|
| 155 |
+
close_btn = gr.Button("Close Tab")
|
| 156 |
+
def close_tab():
|
| 157 |
+
return False
|
| 158 |
+
close_btn.click(fn=close_tab, outputs=extra_space_open)
|
| 159 |
+
else:
|
| 160 |
+
gr.Markdown("Tab closed. Click below to reopen.")
|
| 161 |
+
reopen_btn = gr.Button("Reopen Tab")
|
| 162 |
+
def reopen_tab():
|
| 163 |
+
return True
|
| 164 |
+
reopen_btn.click(fn=reopen_tab, outputs=extra_space_open)
|
| 165 |
|
| 166 |
with gr.Tab("SAM2.1 Masking Tool (gr.load)", visible=False) as sam_tab:
|
| 167 |
+
@gr.render(inputs=sam_tab_open)
|
| 168 |
+
def render_sam_tab(is_open):
|
| 169 |
+
if is_open:
|
| 170 |
+
gr.Markdown("## LPX55/SAM2_1-Image-Predictor-Masking-Tool-CPU (gr.load)")
|
| 171 |
+
sam_interface = gr.load("LPX55/SAM2_1-Image-Predictor-Masking-Tool-CPU", src="spaces")
|
| 172 |
+
close_btn = gr.Button("Close Tab")
|
| 173 |
+
def close_tab():
|
| 174 |
+
return False
|
| 175 |
+
close_btn.click(fn=close_tab, outputs=sam_tab_open)
|
| 176 |
+
else:
|
| 177 |
+
gr.Markdown("Tab closed. Click below to reopen.")
|
| 178 |
+
reopen_btn = gr.Button("Reopen Tab")
|
| 179 |
+
def reopen_tab():
|
| 180 |
+
return True
|
| 181 |
+
reopen_btn.click(fn=reopen_tab, outputs=sam_tab_open)
|
| 182 |
|
| 183 |
with gr.Tab("SAM2.1 Masking Tool (iframe)", visible=False) as sam_iframe_tab:
|
| 184 |
+
@gr.render(inputs=sam_iframe_tab_open)
|
| 185 |
+
def render_sam_iframe_tab(is_open):
|
| 186 |
+
if is_open:
|
| 187 |
+
gr.Markdown("## LPX55/SAM2_1-Image-Predictor-Masking-Tool-CPU (iframe)")
|
| 188 |
+
gr.HTML('<iframe src="https://lpx55-sam2-1-image-predictor-masking-tool-cpu.hf.space" width="100%" height="800" style="border:none;"></iframe>')
|
| 189 |
+
close_btn = gr.Button("Close Tab")
|
| 190 |
+
def close_tab():
|
| 191 |
+
return False
|
| 192 |
+
close_btn.click(fn=close_tab, outputs=sam_iframe_tab_open)
|
| 193 |
+
else:
|
| 194 |
+
gr.Markdown("Tab closed. Click below to reopen.")
|
| 195 |
+
reopen_btn = gr.Button("Reopen Tab")
|
| 196 |
+
def reopen_tab():
|
| 197 |
+
return True
|
| 198 |
+
reopen_btn.click(fn=reopen_tab, outputs=sam_iframe_tab_open)
|
| 199 |
|
| 200 |
with gr.Tab("SAM2.1 Masking Tool (API Proxy)", visible=False) as sam_api_tab:
|
| 201 |
+
@gr.render(inputs=sam_api_tab_open)
|
| 202 |
+
def render_sam_api_tab(is_open):
|
| 203 |
+
if is_open:
|
| 204 |
+
gr.Markdown("## LPX55/SAM2_1-Image-Predictor-Masking-Tool-CPU (API Proxy)")
|
| 205 |
+
api_image = gr.Image(label="Input Image")
|
| 206 |
+
api_btn = gr.Button("Run Remote Masking")
|
| 207 |
+
api_output = gr.Textbox(label="API Response (raw)")
|
| 208 |
+
def call_sam_api(image):
|
| 209 |
+
import requests
|
| 210 |
+
import base64
|
| 211 |
+
import json
|
| 212 |
+
if image is None:
|
| 213 |
+
return "No image uploaded."
|
| 214 |
+
with open(image, "rb") as f:
|
| 215 |
+
img_b64 = base64.b64encode(f.read()).decode()
|
| 216 |
+
payload = {"data": [img_b64]}
|
| 217 |
+
try:
|
| 218 |
+
resp = requests.post(
|
| 219 |
+
"https://lpx55-sam2-1-image-predictor-masking-tool-cpu.hf.space/run/predict",
|
| 220 |
+
json=payload,
|
| 221 |
+
timeout=60
|
| 222 |
+
)
|
| 223 |
+
if resp.status_code == 200:
|
| 224 |
+
return json.dumps(resp.json(), indent=2)
|
| 225 |
+
else:
|
| 226 |
+
return f"Error: {resp.status_code} {resp.text}"
|
| 227 |
+
except Exception as e:
|
| 228 |
+
return f"Exception: {str(e)}"
|
| 229 |
+
api_btn.click(fn=call_sam_api, inputs=api_image, outputs=api_output)
|
| 230 |
+
close_btn = gr.Button("Close Tab")
|
| 231 |
+
def close_tab():
|
| 232 |
+
return False
|
| 233 |
+
close_btn.click(fn=close_tab, outputs=sam_api_tab_open)
|
| 234 |
+
else:
|
| 235 |
+
gr.Markdown("Tab closed. Click below to reopen.")
|
| 236 |
+
reopen_btn = gr.Button("Reopen Tab")
|
| 237 |
+
def reopen_tab():
|
| 238 |
+
return True
|
| 239 |
+
reopen_btn.click(fn=reopen_tab, outputs=sam_api_tab_open)
|
| 240 |
|
| 241 |
with gr.Tab("Custom IFrame Loader") as custom_iframe_tab:
|
| 242 |
+
@gr.render(inputs=custom_iframe_tab_open)
|
| 243 |
+
def render_custom_iframe_tab(is_open):
|
| 244 |
+
if is_open:
|
| 245 |
+
gr.Markdown("## Load Any IFrame URL")
|
| 246 |
+
custom_url = gr.Textbox(label="IFrame URL", placeholder="https://example.com")
|
| 247 |
+
load_custom_iframe_btn = gr.Button("Load IFrame")
|
| 248 |
+
custom_iframe = gr.HTML(visible=True)
|
| 249 |
+
def load_custom_iframe(url):
|
| 250 |
+
if not url:
|
| 251 |
+
return "<div style='color:red'>Please enter a URL.</div>"
|
| 252 |
+
return f'<iframe src="{url}" width="100%" height="800" style="border:none;"></iframe>'
|
| 253 |
+
load_custom_iframe_btn.click(fn=load_custom_iframe, inputs=custom_url, outputs=custom_iframe)
|
| 254 |
+
close_btn = gr.Button("Close Tab")
|
| 255 |
+
def close_tab():
|
| 256 |
+
return False
|
| 257 |
+
close_btn.click(fn=close_tab, outputs=custom_iframe_tab_open)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 258 |
else:
|
| 259 |
+
gr.Markdown("Tab closed. Click below to reopen.")
|
| 260 |
+
reopen_btn = gr.Button("Reopen Tab")
|
| 261 |
+
def reopen_tab():
|
| 262 |
+
return True
|
| 263 |
+
reopen_btn.click(fn=reopen_tab, outputs=custom_iframe_tab_open)
|
| 264 |
|
| 265 |
def show_tab():
|
| 266 |
return gr.Tab(visible=True)
|
|
|
|
| 283 |
|
| 284 |
load_sam_api_btn.click(fn=show_sam_api_tab, outputs=[sam_api_tab])
|
| 285 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 286 |
def open_in_new_tab():
|
| 287 |
# This function does nothing server-side, but the button will have a link
|
| 288 |
pass
|