Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -15,12 +15,24 @@ request_times = defaultdict(list)
|
|
15 |
MAX_REQUESTS = 10 # Maximum requests per time window
|
16 |
TIME_WINDOW = 60 # Time window in seconds
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
def check_rate_limit(request: gr.Request):
|
19 |
"""Check if the current request exceeds rate limits"""
|
20 |
if not request:
|
21 |
return True, "Rate limit check failed - no request info"
|
22 |
|
23 |
-
ip = request
|
24 |
now = time.time()
|
25 |
|
26 |
print(f"Ip: {ip}")
|
|
|
15 |
MAX_REQUESTS = 10 # Maximum requests per time window
|
16 |
TIME_WINDOW = 60 # Time window in seconds
|
17 |
|
18 |
+
def get_real_ip(request: gr.Request):
|
19 |
+
"""Extract real IP address using x-forwarded-for header or fallback"""
|
20 |
+
if not request:
|
21 |
+
return "unknown"
|
22 |
+
|
23 |
+
forwarded = request.headers.get("x-forwarded-for")
|
24 |
+
if forwarded:
|
25 |
+
ip = forwarded.split(",")[0].strip() # First IP in the list is the client's
|
26 |
+
else:
|
27 |
+
ip = request.client.host # fallback
|
28 |
+
return ip
|
29 |
+
|
30 |
def check_rate_limit(request: gr.Request):
|
31 |
"""Check if the current request exceeds rate limits"""
|
32 |
if not request:
|
33 |
return True, "Rate limit check failed - no request info"
|
34 |
|
35 |
+
ip = get_real_ip(request)
|
36 |
now = time.time()
|
37 |
|
38 |
print(f"Ip: {ip}")
|