Spaces:
Running
Running
import gradio as gr | |
import requests | |
import json | |
import os | |
BASE_URL = "https://api.jigsawstack.com/v1" | |
headers = { | |
"x-api-key": os.getenv("JIGSAWSTACK_API_KEY") | |
} | |
def detect_objects(image_url=None, file_store_key=None): | |
if not image_url and not file_store_key: | |
return "β Please provide either an image URL or file store key.", [], "", "" | |
if image_url and file_store_key: | |
return "β Provide only one: image URL or file store key.", [], "", "" | |
try: | |
payload = {} | |
if image_url: | |
payload["url"] = image_url | |
if file_store_key: | |
payload["file_store_key"] = file_store_key | |
response = requests.post(f"{BASE_URL}/ai/object_detection", headers=headers, json=payload) | |
if response.status_code != 200: | |
return f"β Error: {response.status_code} - {response.text}", [], "", "" | |
result = response.json() | |
if not result.get("success"): | |
return "β Detection failed.", [], "", "" | |
status = "β Detection successful!" | |
tags = result.get("tags", []) | |
objects = result.get("objects", []) | |
description = f"Image Size: {result.get('width')} x {result.get('height')}\n\n" | |
for obj in objects: | |
bounds = obj.get("bounds", {}) | |
bound_text = "" | |
if bounds.get("top_left") and bounds.get("top_right"): | |
tl = bounds["top_left"] | |
tr = bounds["top_right"] | |
bound_text = f"Bounds: ({tl['x']}, {tl['y']}) to ({tr['x']}, {tr['y']})" | |
description += f"β’ {obj['name']} (Confidence: {obj['confidence']:.2f})\n {bound_text}\n" | |
raw_json = json.dumps(result, indent=2) | |
return status, tags, description.strip(), raw_json | |
except Exception as e: | |
return f"β Error: {str(e)}", [], "", "" | |
with gr.Blocks() as demo: | |
gr.Markdown(""" | |
<div style='text-align: center; margin-bottom: 24px;'> | |
<h1 style='font-size:2.2em; margin-bottom: 0.2em;'>π§© Object Detection</h1> | |
<p style='font-size:1.2em; margin-top: 0;'>Detect objects within images with great accuracy using AI models.</p> | |
<p style='font-size:1em; margin-top: 0.5em;'>For more details and API usage, see the <a href='https://jigsawstack.com/docs/api-reference/ai/object-detection' target='_blank'>documentation</a>.</p> | |
</div> | |
""") | |
with gr.Row(): | |
with gr.Column(): | |
input_type = gr.Radio(choices=["Image URL", "File Store Key"], value="Image URL", label="Input Type") | |
image_url = gr.Textbox(label="Image URL", placeholder="https://example.com/image.jpg", visible=True) | |
file_store_key = gr.Textbox(label="File Store Key", placeholder="my-image.jpg", visible=False) | |
detect_btn = gr.Button("π Detect Objects") | |
clear_btn = gr.Button("Clear") | |
with gr.Column(): | |
status_box = gr.Textbox(label="Status", interactive=False) | |
tag_display = gr.Label(label="Detected Tags") | |
desc_display = gr.Textbox(label="Object Details", lines=10, interactive=False) | |
json_box = gr.Accordion("Raw JSON Response", open=False) | |
with json_box: | |
json_output = gr.Textbox(show_label=False, lines=20, interactive=False) | |
def toggle_inputs(choice): | |
return ( | |
gr.update(visible=(choice == "Image URL")), | |
gr.update(visible=(choice == "File Store Key")) | |
) | |
input_type.change(fn=toggle_inputs, inputs=input_type, outputs=[image_url, file_store_key]) | |
def on_detect(input_mode, url, key): | |
if input_mode == "Image URL": | |
return detect_objects(image_url=url.strip()) | |
else: | |
return detect_objects(file_store_key=key.strip()) | |
detect_btn.click(fn=on_detect, inputs=[input_type, image_url, file_store_key], | |
outputs=[status_box, tag_display, desc_display, json_output]) | |
def clear_all(): | |
return "Image URL", "", "", "", "", "" | |
clear_btn.click(fn=clear_all, inputs=[], outputs=[ | |
input_type, image_url, file_store_key, status_box, desc_display, json_output | |
]) | |
demo.launch() | |