vineet124jig commited on
Commit
a471a75
·
verified ·
1 Parent(s): 276f893

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -4
app.py CHANGED
@@ -1,15 +1,64 @@
1
  import gradio as gr
2
  import requests
3
- from PIL import Image
4
  import os
 
 
 
5
  import io
6
 
7
  BASE_URL = "https://api.jigsawstack.com/v1"
8
- headers = {"x-api-key": os.getenv("JIGSAWSTACK_API_KEY")}
 
 
 
 
 
 
 
 
9
 
10
- # ----------------- JigsawStack API Wrappers ------------------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
- def generate_image(prompt, aspect_ratio, width, height, steps, negative_prompt, guidance, seed):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  # Validate required inputs
14
  if not prompt or not prompt.strip():
15
  return None, {"error": "Prompt is required"}
 
1
  import gradio as gr
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
+
16
+ # Rate limiting configuration
17
+ request_times = defaultdict(list)
18
+ MAX_REQUESTS = 1 # Maximum requests per time window
19
+ TIME_WINDOW = 3600 # Time window in seconds (1 hour)
20
 
21
+ def get_real_ip(request: gr.Request):
22
+ """Extract real IP address using x-forwarded-for header or fallback"""
23
+ if not request:
24
+ return "unknown"
25
+
26
+ forwarded = request.headers.get("x-forwarded-for")
27
+ if forwarded:
28
+ ip = forwarded.split(",")[0].strip() # First IP in the list is the client's
29
+ else:
30
+ ip = request.client.host # fallback
31
+ return ip
32
+
33
+ def check_rate_limit(request: gr.Request):
34
+ """Check if the current request exceeds rate limits"""
35
+ if not request:
36
+ return True, "Rate limit check failed - no request info"
37
+
38
+ ip = get_real_ip(request)
39
+ now = time.time()
40
 
41
+ # Clean up old timestamps outside the time window
42
+ request_times[ip] = [t for t in request_times[ip] if now - t < TIME_WINDOW]
43
+
44
+
45
+ # Check if rate limit exceeded
46
+ if len(request_times[ip]) >= MAX_REQUESTS:
47
+ time_remaining = int(TIME_WINDOW - (now - request_times[ip][0]))
48
+ time_remaining_minutes = round(time_remaining / 60, 1)
49
+ time_window_minutes = round(TIME_WINDOW / 60, 1)
50
+
51
+ return False, f"Rate limit exceeded. You can make {MAX_REQUESTS} requests per {time_window_minutes} minutes. Try again in {time_remaining_minutes} minutes."
52
+
53
+ # Add current request timestamp
54
+ request_times[ip].append(now)
55
+ return True, ""
56
+
57
+ def generate_image(prompt, aspect_ratio, width, height, steps, negative_prompt, guidance, seed, request: gr.Request):
58
+ rate_limit_ok, rate_limit_msg = check_rate_limit(request)
59
+ if not rate_limit_ok:
60
+ return None, {"error": "Rate limit exceeded", "message": rate_limit_msg}
61
+
62
  # Validate required inputs
63
  if not prompt or not prompt.strip():
64
  return None, {"error": "Prompt is required"}