Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -2,13 +2,61 @@ import gradio as gr
|
|
2 |
import requests
|
3 |
import json
|
4 |
import os
|
|
|
|
|
|
|
|
|
5 |
|
6 |
BASE_URL = "https://api.jigsawstack.com/v1"
|
7 |
headers = {
|
8 |
"x-api-key": os.getenv("JIGSAWSTACK_API_KEY")
|
9 |
}
|
10 |
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
if not image_url and not file_store_key:
|
13 |
return "❌ Please provide either an image URL or file store key.", "", "", None
|
14 |
|
@@ -112,7 +160,7 @@ with gr.Blocks() as demo:
|
|
112 |
|
113 |
input_type.change(fn=toggle_inputs, inputs=input_type, outputs=[image_url, file_store_key])
|
114 |
|
115 |
-
def on_detect(input_mode, url, key, prompts_text, features_list):
|
116 |
# Parse prompts
|
117 |
prompts_list = None
|
118 |
if prompts_text.strip():
|
@@ -120,12 +168,14 @@ with gr.Blocks() as demo:
|
|
120 |
|
121 |
if input_mode == "Image URL":
|
122 |
return detect_objects(
|
|
|
123 |
image_url=url.strip(),
|
124 |
prompts=prompts_list,
|
125 |
features=features_list
|
126 |
)
|
127 |
else:
|
128 |
return detect_objects(
|
|
|
129 |
file_store_key=key.strip(),
|
130 |
prompts=prompts_list,
|
131 |
features=features_list
|
@@ -144,3 +194,4 @@ with gr.Blocks() as demo:
|
|
144 |
])
|
145 |
|
146 |
demo.launch()
|
|
|
|
2 |
import requests
|
3 |
import json
|
4 |
import os
|
5 |
+
import time
|
6 |
+
from collections import defaultdict
|
7 |
+
from PIL import Image
|
8 |
+
import io
|
9 |
|
10 |
BASE_URL = "https://api.jigsawstack.com/v1"
|
11 |
headers = {
|
12 |
"x-api-key": os.getenv("JIGSAWSTACK_API_KEY")
|
13 |
}
|
14 |
|
15 |
+
# Rate limiting configuration
|
16 |
+
request_times = defaultdict(list)
|
17 |
+
MAX_REQUESTS = 1 # Maximum requests per time window
|
18 |
+
TIME_WINDOW = 3600 # Time window in seconds (1 hour)
|
19 |
+
|
20 |
+
def get_real_ip(request: gr.Request):
|
21 |
+
"""Extract real IP address using x-forwarded-for header or fallback"""
|
22 |
+
if not request:
|
23 |
+
return "unknown"
|
24 |
+
|
25 |
+
forwarded = request.headers.get("x-forwarded-for")
|
26 |
+
if forwarded:
|
27 |
+
ip = forwarded.split(",")[0].strip() # First IP in the list is the client's
|
28 |
+
else:
|
29 |
+
ip = request.client.host # fallback
|
30 |
+
return ip
|
31 |
+
|
32 |
+
def check_rate_limit(request: gr.Request):
|
33 |
+
"""Check if the current request exceeds rate limits"""
|
34 |
+
if not request:
|
35 |
+
return True, "Rate limit check failed - no request info"
|
36 |
+
|
37 |
+
ip = get_real_ip(request)
|
38 |
+
now = time.time()
|
39 |
+
|
40 |
+
# Clean up old timestamps outside the time window
|
41 |
+
request_times[ip] = [t for t in request_times[ip] if now - t < TIME_WINDOW]
|
42 |
+
|
43 |
+
# Check if rate limit exceeded
|
44 |
+
if len(request_times[ip]) >= MAX_REQUESTS:
|
45 |
+
time_remaining = int(TIME_WINDOW - (now - request_times[ip][0]))
|
46 |
+
time_remaining_minutes = round(time_remaining / 60, 1)
|
47 |
+
time_window_minutes = round(TIME_WINDOW / 60, 1)
|
48 |
+
|
49 |
+
return False, f"Rate limit exceeded. You can make {MAX_REQUESTS} requests per {time_window_minutes} minutes. Try again in {time_remaining_minutes} minutes."
|
50 |
+
|
51 |
+
# Add current request timestamp
|
52 |
+
request_times[ip].append(now)
|
53 |
+
return True, ""
|
54 |
+
|
55 |
+
def detect_objects(request: gr.Request, image_url=None, file_store_key=None, prompts=None, features=None):
|
56 |
+
rate_limit_ok, rate_limit_msg = check_rate_limit(request)
|
57 |
+
if not rate_limit_ok:
|
58 |
+
return f"❌ {rate_limit_msg}", "", "", None
|
59 |
+
|
60 |
if not image_url and not file_store_key:
|
61 |
return "❌ Please provide either an image URL or file store key.", "", "", None
|
62 |
|
|
|
160 |
|
161 |
input_type.change(fn=toggle_inputs, inputs=input_type, outputs=[image_url, file_store_key])
|
162 |
|
163 |
+
def on_detect(input_mode, url, key, prompts_text, features_list, request: gr.Request):
|
164 |
# Parse prompts
|
165 |
prompts_list = None
|
166 |
if prompts_text.strip():
|
|
|
168 |
|
169 |
if input_mode == "Image URL":
|
170 |
return detect_objects(
|
171 |
+
request=request,
|
172 |
image_url=url.strip(),
|
173 |
prompts=prompts_list,
|
174 |
features=features_list
|
175 |
)
|
176 |
else:
|
177 |
return detect_objects(
|
178 |
+
request=request,
|
179 |
file_store_key=key.strip(),
|
180 |
prompts=prompts_list,
|
181 |
features=features_list
|
|
|
194 |
])
|
195 |
|
196 |
demo.launch()
|
197 |
+
|