andito HF Staff commited on
Commit
0f26eea
·
verified ·
1 Parent(s): 62b5801

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -18
app.py CHANGED
@@ -6,7 +6,6 @@ import threading
6
  import requests
7
  from datetime import datetime, timezone
8
  from typing import Dict, Optional, Tuple
9
- from collections import defaultdict
10
 
11
  import gradio as gr
12
  from huggingface_hub import HfApi
@@ -38,9 +37,13 @@ _cache = {
38
  }
39
  CACHE_TTL_SEC = 10.0
40
 
41
- # Request tracking to identify suspicious patterns
42
- _request_tracker = defaultdict(list) # {ip: [timestamps]}
43
  _request_log_buffer = [] # Buffer logs before writing to avoid too many uploads
 
 
 
 
 
44
 
45
  # -------------------------
46
  # Helpers
@@ -180,37 +183,73 @@ def _flush_request_log():
180
  )
181
 
182
  _request_log_buffer.clear()
 
 
 
183
  except Exception as e:
184
  print(f"Warning: Failed to flush request log: {e}")
185
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
186
  def _check_suspicious_activity(ip_address: str, order_number: str) -> list:
187
  """Check for suspicious patterns and return list of flags"""
188
  flags = []
189
- now = time.time()
190
-
191
- # Track requests from this IP
192
- _request_tracker[ip_address].append(now)
193
 
194
- # Clean old entries (older than 1 hour)
195
- _request_tracker[ip_address] = [
196
- ts for ts in _request_tracker[ip_address]
197
- if now - ts < 3600
198
- ]
199
 
200
- recent_requests = _request_tracker[ip_address]
 
201
 
202
  # Flag: More than 20 requests in the last hour
203
- if len(recent_requests) > 20:
204
  flags.append("HIGH_FREQUENCY")
205
 
206
  # Flag: More than 5 requests in the last minute
207
- last_minute = [ts for ts in recent_requests if now - ts < 60]
208
- if len(last_minute) > 5:
209
  flags.append("RAPID_REQUESTS")
210
 
211
  # Flag: More than 10 requests in the last 5 minutes
212
- last_5_min = [ts for ts in recent_requests if now - ts < 300]
213
- if len(last_5_min) > 10:
214
  flags.append("BURST_PATTERN")
215
 
216
  return flags
@@ -364,3 +403,4 @@ with gr.Blocks(title="API") as demo:
364
 
365
  demo.queue()
366
  demo.launch()
 
 
6
  import requests
7
  from datetime import datetime, timezone
8
  from typing import Dict, Optional, Tuple
 
9
 
10
  import gradio as gr
11
  from huggingface_hub import HfApi
 
37
  }
38
  CACHE_TTL_SEC = 10.0
39
 
40
+ # Request log buffer and cache
 
41
  _request_log_buffer = [] # Buffer logs before writing to avoid too many uploads
42
+ _request_log_cache = {
43
+ "ts": 0.0,
44
+ "data": None, # Cached parsed log data
45
+ }
46
+ REQUEST_LOG_CACHE_TTL_SEC = 30.0 # Cache log for 30 seconds
47
 
48
  # -------------------------
49
  # Helpers
 
183
  )
184
 
185
  _request_log_buffer.clear()
186
+
187
+ # Invalidate cache so next check gets fresh data
188
+ _request_log_cache["ts"] = 0.0
189
  except Exception as e:
190
  print(f"Warning: Failed to flush request log: {e}")
191
 
192
+ def _get_recent_requests_from_log(ip_address: str) -> list:
193
+ """Get recent requests for an IP from the log file (with caching)"""
194
+ now_time = time.time()
195
+ now_dt = datetime.now(timezone.utc)
196
+
197
+ # Check cache
198
+ if (_request_log_cache["data"] is not None and
199
+ (now_time - _request_log_cache["ts"] < REQUEST_LOG_CACHE_TTL_SEC)):
200
+ log_data = _request_log_cache["data"]
201
+ else:
202
+ # Download and parse log
203
+ try:
204
+ log_csv = _download_csv(DATASET_REPO_ID, REQUEST_LOG_PATH)
205
+ reader = csv.DictReader(io.StringIO(log_csv))
206
+ log_data = list(reader)
207
+
208
+ # Update cache
209
+ _request_log_cache["data"] = log_data
210
+ _request_log_cache["ts"] = now_time
211
+ except Exception:
212
+ # If log doesn't exist or there's an error, return empty list
213
+ return []
214
+
215
+ # Filter for this IP and recent requests
216
+ recent_timestamps = []
217
+ for row in log_data:
218
+ if row.get('ip_address') == ip_address:
219
+ try:
220
+ timestamp = datetime.fromisoformat(row['timestamp'])
221
+ age_seconds = (now_dt - timestamp).total_seconds()
222
+
223
+ # Only consider requests from the last hour
224
+ if age_seconds < 3600:
225
+ recent_timestamps.append(age_seconds)
226
+ except (ValueError, KeyError):
227
+ continue
228
+
229
+ return recent_timestamps
230
+
231
  def _check_suspicious_activity(ip_address: str, order_number: str) -> list:
232
  """Check for suspicious patterns and return list of flags"""
233
  flags = []
 
 
 
 
234
 
235
+ # Get recent requests from the persistent log
236
+ recent_request_ages = _get_recent_requests_from_log(ip_address)
 
 
 
237
 
238
+ # Count current request
239
+ total_requests = len(recent_request_ages) + 1
240
 
241
  # Flag: More than 20 requests in the last hour
242
+ if total_requests > 20:
243
  flags.append("HIGH_FREQUENCY")
244
 
245
  # Flag: More than 5 requests in the last minute
246
+ last_minute = sum(1 for age in recent_request_ages if age < 60) + 1
247
+ if last_minute > 5:
248
  flags.append("RAPID_REQUESTS")
249
 
250
  # Flag: More than 10 requests in the last 5 minutes
251
+ last_5_min = sum(1 for age in recent_request_ages if age < 300) + 1
252
+ if last_5_min > 10:
253
  flags.append("BURST_PATTERN")
254
 
255
  return flags
 
403
 
404
  demo.queue()
405
  demo.launch()
406
+