Frankie-walsh4 commited on
Commit
efa9970
·
1 Parent(s): e21cbb5

IP limit testing (set 1 per day)

Browse files
Files changed (1) hide show
  1. app.py +131 -1
app.py CHANGED
@@ -4,12 +4,22 @@ import time
4
  import html
5
  import re
6
  import traceback
 
 
 
 
7
 
8
  """
9
  For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
10
  """
11
  client = InferenceClient("Trinoid/Data_Management")
12
 
 
 
 
 
 
 
13
  # Comprehensive patterns to filter out thinking and meta-commentary
14
  THINKING_PATTERNS = [
15
  r"Okay, so I('m| am) (trying to|going to|attempting to)",
@@ -32,6 +42,39 @@ THINKING_PATTERNS = [
32
  r"I'll approach this by",
33
  ]
34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  def process_final_response(response_text):
36
  """Comprehensive processing of the final response to ensure quality"""
37
 
@@ -107,6 +150,23 @@ def respond(
107
  temperature,
108
  top_p,
109
  ):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110
  # Create a more effective system prompt
111
  enhanced_system_message = f"""You are an expert in Microsoft 365 services including SharePoint, OneDrive, Teams, and the Microsoft 365 compliance ecosystem.
112
 
@@ -202,6 +262,30 @@ If comparing services:
202
  error_msg = f"I apologize, but I encountered an error while generating a response. Error details: {str(e)}"
203
  yield error_msg
204
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
205
 
206
  # Custom CSS for Plant Wisdom.AI styling
207
  custom_css = """
@@ -309,12 +393,25 @@ summary:hover {
309
  .thinking-step:last-child {
310
  border-bottom: none;
311
  }
 
 
 
 
 
 
 
 
 
 
 
 
312
  """
313
 
314
  """
315
  For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
316
  """
317
- demo = gr.ChatInterface(
 
318
  respond,
319
  title="AI Data Management Expert",
320
  description="Hello! I am your Data Management Expert, specialized in Microsoft 365. I'm here to help you with guidance on Data Management procedures. How can I assist you today?",
@@ -412,6 +509,39 @@ IMPORTANT: If a question has been asked before in the conversation, acknowledge
412
  ],
413
  )
414
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
415
 
416
  if __name__ == "__main__":
417
  demo.launch(server_name="0.0.0.0")
 
4
  import html
5
  import re
6
  import traceback
7
+ import datetime
8
+ import threading
9
+ from collections import defaultdict
10
+ from flask import request
11
 
12
  """
13
  For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
14
  """
15
  client = InferenceClient("Trinoid/Data_Management")
16
 
17
+ # Rate limiting settings
18
+ MAX_REQUESTS_PER_DAY = 1 # Maximum number of requests per IP per day (set to 1 for testing)
19
+ ip_request_counters = defaultdict(int) # Tracks request count per IP
20
+ ip_last_reset = {} # Tracks when counters were last reset for each IP
21
+ rate_limit_lock = threading.Lock() # Lock for thread-safe counter access
22
+
23
  # Comprehensive patterns to filter out thinking and meta-commentary
24
  THINKING_PATTERNS = [
25
  r"Okay, so I('m| am) (trying to|going to|attempting to)",
 
42
  r"I'll approach this by",
43
  ]
44
 
45
+ def get_client_ip():
46
+ """Get the client's IP address from the request"""
47
+ try:
48
+ # Try to get IP from request context in a Flask environment
49
+ if request:
50
+ return request.remote_addr
51
+ except:
52
+ pass
53
+
54
+ # Fallback if we can't get a real IP
55
+ return "127.0.0.1"
56
+
57
+ def check_rate_limit():
58
+ """Check if the current IP has exceeded its daily limit"""
59
+ current_ip = get_client_ip()
60
+ current_date = datetime.datetime.now().date()
61
+
62
+ with rate_limit_lock:
63
+ # Reset counter if it's a new day
64
+ if current_ip in ip_last_reset and ip_last_reset[current_ip] != current_date:
65
+ ip_request_counters[current_ip] = 0
66
+
67
+ # Update last reset date
68
+ ip_last_reset[current_ip] = current_date
69
+
70
+ # Check if limit is exceeded
71
+ if ip_request_counters[current_ip] >= MAX_REQUESTS_PER_DAY:
72
+ return False
73
+
74
+ # Increment counter
75
+ ip_request_counters[current_ip] += 1
76
+ return True
77
+
78
  def process_final_response(response_text):
79
  """Comprehensive processing of the final response to ensure quality"""
80
 
 
150
  temperature,
151
  top_p,
152
  ):
153
+ # Check rate limit before processing the request
154
+ if not check_rate_limit():
155
+ current_ip = get_client_ip()
156
+ next_reset = (datetime.datetime.now() + datetime.timedelta(days=1)).replace(hour=0, minute=0, second=0)
157
+ hours_until_reset = int((next_reset - datetime.datetime.now()).total_seconds() / 3600)
158
+
159
+ limit_message = f"""
160
+ <div class="rate-limit-warning">
161
+ <h3>Daily Request Limit Reached</h3>
162
+ <p>You've reached the maximum of {MAX_REQUESTS_PER_DAY} requests allowed per day.</p>
163
+ <p>Your limit will reset in approximately {hours_until_reset} hours (at midnight your local time).</p>
164
+ <p>Please try again tomorrow. Thank you for your understanding!</p>
165
+ </div>
166
+ """
167
+ yield limit_message
168
+ return
169
+
170
  # Create a more effective system prompt
171
  enhanced_system_message = f"""You are an expert in Microsoft 365 services including SharePoint, OneDrive, Teams, and the Microsoft 365 compliance ecosystem.
172
 
 
262
  error_msg = f"I apologize, but I encountered an error while generating a response. Error details: {str(e)}"
263
  yield error_msg
264
 
265
+ # Function to get admin stats about rate limiting
266
+ def get_rate_limit_stats():
267
+ stats = {
268
+ "max_requests_per_day": MAX_REQUESTS_PER_DAY,
269
+ "active_ips": len(ip_request_counters),
270
+ "ip_usage": dict(ip_request_counters),
271
+ "last_reset_dates": {ip: date.strftime("%Y-%m-%d") for ip, date in ip_last_reset.items()}
272
+ }
273
+ return stats
274
+
275
+ # Admin interface to adjust rate limit settings
276
+ def update_rate_limit(new_limit):
277
+ global MAX_REQUESTS_PER_DAY
278
+
279
+ if new_limit < 1:
280
+ return {"success": False, "message": "Limit must be at least 1"}
281
+
282
+ old_limit = MAX_REQUESTS_PER_DAY
283
+ MAX_REQUESTS_PER_DAY = new_limit
284
+
285
+ return {
286
+ "success": True,
287
+ "message": f"Rate limit updated from {old_limit} to {new_limit} requests per IP per day"
288
+ }
289
 
290
  # Custom CSS for Plant Wisdom.AI styling
291
  custom_css = """
 
393
  .thinking-step:last-child {
394
  border-bottom: none;
395
  }
396
+
397
+ /* Rate limit warning styling */
398
+ .rate-limit-warning {
399
+ background-color: #fff3cd;
400
+ color: #856404;
401
+ border: 1px solid #ffeeba;
402
+ border-radius: 8px;
403
+ padding: 16px;
404
+ margin: 16px 0;
405
+ text-align: center;
406
+ font-weight: 500;
407
+ }
408
  """
409
 
410
  """
411
  For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
412
  """
413
+ # Main chat interface
414
+ chat_interface = gr.ChatInterface(
415
  respond,
416
  title="AI Data Management Expert",
417
  description="Hello! I am your Data Management Expert, specialized in Microsoft 365. I'm here to help you with guidance on Data Management procedures. How can I assist you today?",
 
509
  ],
510
  )
511
 
512
+ # Create Gradio Blocks app with both interfaces
513
+ with gr.Blocks(theme=gr.themes.Base()) as demo:
514
+ # Main chat interface
515
+ chat_interface.render()
516
+
517
+ # Hidden admin panel (accessible at /admin)
518
+ with gr.Tab("Admin", visible=False):
519
+ gr.Markdown("## Rate Limiting Settings")
520
+
521
+ with gr.Row():
522
+ current_limit = gr.Number(label="Current Request Limit Per IP Per Day", value=MAX_REQUESTS_PER_DAY, interactive=False)
523
+ new_limit = gr.Number(label="New Request Limit", value=MAX_REQUESTS_PER_DAY, minimum=1, step=1)
524
+ update_btn = gr.Button("Update Limit")
525
+
526
+ result_text = gr.Textbox(label="Result", interactive=False)
527
+
528
+ # Stats display
529
+ with gr.Accordion("Usage Statistics", open=False):
530
+ refresh_btn = gr.Button("Refresh Stats")
531
+ stats_json = gr.JSON(label="Current Usage Stats")
532
+
533
+ # Connect buttons to functions
534
+ update_btn.click(
535
+ fn=update_rate_limit,
536
+ inputs=[new_limit],
537
+ outputs=[result_text, current_limit]
538
+ )
539
+
540
+ refresh_btn.click(
541
+ fn=get_rate_limit_stats,
542
+ inputs=[],
543
+ outputs=[stats_json]
544
+ )
545
 
546
  if __name__ == "__main__":
547
  demo.launch(server_name="0.0.0.0")