Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -12,8 +12,8 @@ headers = {
|
|
12 |
|
13 |
# Rate limiting configuration
|
14 |
request_times = defaultdict(list)
|
15 |
-
MAX_REQUESTS =
|
16 |
-
TIME_WINDOW =
|
17 |
|
18 |
def get_real_ip(request: gr.Request):
|
19 |
"""Extract real IP address using x-forwarded-for header or fallback"""
|
@@ -41,7 +41,10 @@ def check_rate_limit(request: gr.Request):
|
|
41 |
# Check if rate limit exceeded
|
42 |
if len(request_times[ip]) >= MAX_REQUESTS:
|
43 |
time_remaining = int(TIME_WINDOW - (now - request_times[ip][0]))
|
44 |
-
|
|
|
|
|
|
|
45 |
|
46 |
# Add current request timestamp
|
47 |
request_times[ip].append(now)
|
|
|
12 |
|
13 |
# Rate limiting configuration
|
14 |
request_times = defaultdict(list)
|
15 |
+
MAX_REQUESTS = 20 # Maximum requests per time window
|
16 |
+
TIME_WINDOW = 3600 # Time window in seconds (1 hour)
|
17 |
|
18 |
def get_real_ip(request: gr.Request):
|
19 |
"""Extract real IP address using x-forwarded-for header or fallback"""
|
|
|
41 |
# Check if rate limit exceeded
|
42 |
if len(request_times[ip]) >= MAX_REQUESTS:
|
43 |
time_remaining = int(TIME_WINDOW - (now - request_times[ip][0]))
|
44 |
+
time_remaining_minutes = time_remaining / 60
|
45 |
+
time_window_minutes = TIME_WINDOW / 60
|
46 |
+
return False, f"Rate limit exceeded. You can make {MAX_REQUESTS} requests per {time_window_minutes} minutes. Try again in {time_remaining_minutes} minutes."
|
47 |
+
|
48 |
|
49 |
# Add current request timestamp
|
50 |
request_times[ip].append(now)
|