import gradio as gr import requests import json import os import time from collections import defaultdict from PIL import Image import io BASE_URL = "https://api.jigsawstack.com/v1" headers = { "x-api-key": os.getenv("JIGSAWSTACK_API_KEY") } # Rate limiting configuration request_times = defaultdict(list) MAX_REQUESTS = 20 # Maximum requests per time window TIME_WINDOW = 3600 # Time window in seconds (1 hour) def get_real_ip(request: gr.Request): """Extract real IP address using x-forwarded-for header or fallback""" if not request: return "unknown" forwarded = request.headers.get("x-forwarded-for") if forwarded: ip = forwarded.split(",")[0].strip() # First IP in the list is the client's else: ip = request.client.host # fallback return ip def check_rate_limit(request: gr.Request): """Check if the current request exceeds rate limits""" if not request: return True, "Rate limit check failed - no request info" ip = get_real_ip(request) now = time.time() # Clean up old timestamps outside the time window request_times[ip] = [t for t in request_times[ip] if now - t < TIME_WINDOW] # Check if rate limit exceeded if len(request_times[ip]) >= MAX_REQUESTS: time_remaining = int(TIME_WINDOW - (now - request_times[ip][0])) time_remaining_minutes = round(time_remaining / 60, 1) time_window_minutes = round(TIME_WINDOW / 60, 1) return False, f"Rate limit exceeded. You can make {MAX_REQUESTS} requests per {time_window_minutes} minutes. Try again in {time_remaining_minutes} minutes." # Add current request timestamp request_times[ip].append(now) return True, "" def detect_objects(request: gr.Request, image_url=None, file_store_key=None, prompts=None, features=None): rate_limit_ok, rate_limit_msg = check_rate_limit(request) if not rate_limit_ok: return f"❌ {rate_limit_msg}", "", "", None if not image_url and not file_store_key: return "❌ Please provide either an image URL or file store key.", "", "", None if image_url and file_store_key: return "❌ Provide only one: image URL or file store key.", "", "", None try: payload = {} if image_url: payload["url"] = image_url if file_store_key: payload["file_store_key"] = file_store_key # Add optional parameters if prompts: payload["prompts"] = prompts if features: payload["features"] = features # Always return annotated image payload["annotated_image"] = True # Always use url as return_type payload["return_type"] = "url" 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}", "", "", None result = response.json() if not result.get("success"): return "❌ Detection failed.", "", "", None status = "✅ Detection successful!" objects = result.get("objects", []) annotated_image_url = result.get("annotated_image") # Create description with object details description = f"Image Size: {result.get('width', 'Unknown')} x {result.get('height', 'Unknown')}\n\n" description += f"Total Objects Detected: {len(objects)}\n\n" for i, obj in enumerate(objects): bounds = obj.get("bounds", {}) label = obj.get("label", "Unknown") bound_text = "" if bounds: width = bounds.get("width", "Unknown") height = bounds.get("height", "Unknown") top_left = bounds.get("top_left", {}) if top_left: x, y = top_left.get("x", "?"), top_left.get("y", "?") bound_text = f"Position: ({x}, {y}), Size: {width}x{height}" description += f"• {label}\n {bound_text}\n" raw_json = json.dumps(result, indent=2) return status, description.strip(), raw_json, annotated_image_url except Exception as e: return f"❌ Error: {str(e)}", "", "", None 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.