Spaces:
Runtime error
Runtime error
Add SAM2.1 Masking Tool integration with new API Proxy tab and custom IFrame loader
Browse files
app.py
CHANGED
|
@@ -54,16 +54,22 @@ theme = gr.Theme.from_hub("LPX55/modal_ai")
|
|
| 54 |
with gr.Blocks(theme=theme, js=js_func, css=css_func) as demo:
|
| 55 |
show_space_tab = gr.State(False)
|
| 56 |
with gr.Sidebar(width="25vw"):
|
| 57 |
-
gr.Image("logo.png", width=80, show_label=False)
|
| 58 |
gr.Markdown("""
|
| 59 |
-
# 🤖
|
| 60 |
-
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
---
|
| 63 |
**Navigation**
|
| 64 |
- [Greeting](#greeting)
|
| 65 |
- [Calculator](#calculator)
|
| 66 |
- [Sepia Image](#sepia-image)
|
|
|
|
| 67 |
---
|
| 68 |
### [GitHub Repo](https://github.com/yourrepo) | [Docs](https://yourdocs)
|
| 69 |
---
|
|
@@ -84,6 +90,8 @@ print(client.predict("Alex", 5, False, api_name="/greet"))
|
|
| 84 |
load_space_btn = gr.Button("Load Extra Space", elem_id="load-space-btn")
|
| 85 |
load_sam_btn = gr.Button("Load SAM2.1 Masking Tool (gr.load)", elem_id="load-sam-btn")
|
| 86 |
load_sam_iframe_btn = gr.Button("Load SAM2.1 Masking Tool (iframe)", elem_id="load-sam-iframe-btn")
|
|
|
|
|
|
|
| 87 |
|
| 88 |
with gr.Tab("Greeting"):
|
| 89 |
gr.Markdown("## Greeting Generator")
|
|
@@ -143,6 +151,44 @@ print(client.predict("Alex", 5, False, api_name="/greet"))
|
|
| 143 |
gr.Markdown("## LPX55/SAM2_1-Image-Predictor-Masking-Tool-CPU (iframe)")
|
| 144 |
sam_iframe = gr.HTML(visible=True)
|
| 145 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
def show_tab():
|
| 147 |
return gr.Tab(visible=True)
|
| 148 |
|
|
@@ -159,6 +205,25 @@ print(client.predict("Alex", 5, False, api_name="/greet"))
|
|
| 159 |
|
| 160 |
load_sam_iframe_btn.click(fn=show_sam_iframe_tab, outputs=[sam_iframe_tab, sam_iframe])
|
| 161 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
gr.Markdown("""
|
| 163 |
---
|
| 164 |
## 📡 API Usage
|
|
|
|
| 54 |
with gr.Blocks(theme=theme, js=js_func, css=css_func) as demo:
|
| 55 |
show_space_tab = gr.State(False)
|
| 56 |
with gr.Sidebar(width="25vw"):
|
|
|
|
| 57 |
gr.Markdown("""
|
| 58 |
+
# 🤖 API + MCP Demo
|
| 59 |
+
_Dynamically load tabs 🤫_
|
| 60 |
|
| 61 |
+
---
|
| 62 |
+
**Ways to load the remote SAM2.1 Masking Tool:**
|
| 63 |
+
- **gr.load()**: Loads the remote interface at startup, just hidden until shown.
|
| 64 |
+
- **iframe**: Loads the remote UI only when you click the button (no API integration).
|
| 65 |
+
- **API Proxy**: Upload an image, and the backend will call the remote Space's API and show the result.
|
| 66 |
+
- **Open in New Tab**: Opens the remote Space in a new browser tab.
|
| 67 |
---
|
| 68 |
**Navigation**
|
| 69 |
- [Greeting](#greeting)
|
| 70 |
- [Calculator](#calculator)
|
| 71 |
- [Sepia Image](#sepia-image)
|
| 72 |
+
- [Custom IFrame Loader](#custom-iframe-loader)
|
| 73 |
---
|
| 74 |
### [GitHub Repo](https://github.com/yourrepo) | [Docs](https://yourdocs)
|
| 75 |
---
|
|
|
|
| 90 |
load_space_btn = gr.Button("Load Extra Space", elem_id="load-space-btn")
|
| 91 |
load_sam_btn = gr.Button("Load SAM2.1 Masking Tool (gr.load)", elem_id="load-sam-btn")
|
| 92 |
load_sam_iframe_btn = gr.Button("Load SAM2.1 Masking Tool (iframe)", elem_id="load-sam-iframe-btn")
|
| 93 |
+
load_sam_api_btn = gr.Button("Load SAM2.1 Masking Tool (API Proxy)", elem_id="load-sam-api-btn")
|
| 94 |
+
open_sam_tab_btn = gr.Button("Open SAM2.1 Masking Tool in New Tab", elem_id="open-sam-tab-btn")
|
| 95 |
|
| 96 |
with gr.Tab("Greeting"):
|
| 97 |
gr.Markdown("## Greeting Generator")
|
|
|
|
| 151 |
gr.Markdown("## LPX55/SAM2_1-Image-Predictor-Masking-Tool-CPU (iframe)")
|
| 152 |
sam_iframe = gr.HTML(visible=True)
|
| 153 |
|
| 154 |
+
with gr.Tab("SAM2.1 Masking Tool (API Proxy)", visible=False) as sam_api_tab:
|
| 155 |
+
gr.Markdown("## LPX55/SAM2_1-Image-Predictor-Masking-Tool-CPU (API Proxy)")
|
| 156 |
+
api_image = gr.Image(label="Input Image")
|
| 157 |
+
api_btn = gr.Button("Run Remote Masking")
|
| 158 |
+
api_output = gr.Textbox(label="API Response (raw)")
|
| 159 |
+
|
| 160 |
+
with gr.Tab("Custom IFrame Loader") as custom_iframe_tab:
|
| 161 |
+
gr.Markdown("## Load Any IFrame URL")
|
| 162 |
+
custom_url = gr.Textbox(label="IFrame URL", placeholder="https://example.com")
|
| 163 |
+
load_custom_iframe_btn = gr.Button("Load IFrame")
|
| 164 |
+
custom_iframe = gr.HTML(visible=True)
|
| 165 |
+
|
| 166 |
+
def call_sam_api(image):
|
| 167 |
+
import requests
|
| 168 |
+
import base64
|
| 169 |
+
import json
|
| 170 |
+
if image is None:
|
| 171 |
+
return "No image uploaded."
|
| 172 |
+
# Convert image to base64
|
| 173 |
+
with open(image, "rb") as f:
|
| 174 |
+
img_b64 = base64.b64encode(f.read()).decode()
|
| 175 |
+
# Prepare payload (this is a placeholder, actual API may differ)
|
| 176 |
+
payload = {"data": [img_b64]}
|
| 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 |
+
return f"Error: {resp.status_code} {resp.text}"
|
| 187 |
+
except Exception as e:
|
| 188 |
+
return f"Exception: {str(e)}"
|
| 189 |
+
|
| 190 |
+
api_btn.click(fn=call_sam_api, inputs=api_image, outputs=api_output)
|
| 191 |
+
|
| 192 |
def show_tab():
|
| 193 |
return gr.Tab(visible=True)
|
| 194 |
|
|
|
|
| 205 |
|
| 206 |
load_sam_iframe_btn.click(fn=show_sam_iframe_tab, outputs=[sam_iframe_tab, sam_iframe])
|
| 207 |
|
| 208 |
+
def show_sam_api_tab():
|
| 209 |
+
return gr.Tab(visible=True)
|
| 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
|
| 224 |
+
|
| 225 |
+
open_sam_tab_btn.click(fn=open_in_new_tab, inputs=None, outputs=None, js="window.open('https://lpx55-sam2-1-image-predictor-masking-tool-cpu.hf.space', '_blank')")
|
| 226 |
+
|
| 227 |
gr.Markdown("""
|
| 228 |
---
|
| 229 |
## 📡 API Usage
|