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("""
Detect objects within images with great accuracy using AI models.
For more details and API usage, see the documentation.