Lasdw commited on
Commit
4633c30
·
1 Parent(s): 3857b0c

updated rate limting for ips 2

Browse files
Files changed (1) hide show
  1. app.py +7 -6
app.py CHANGED
@@ -12,7 +12,7 @@ from flask import request
12
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
13
  ALLOWED_FILE_EXTENSIONS = [".mp3", ".xlsx", ".py", ".png", ".jpg", ".jpeg", ".gif", ".txt", ".md", ".json", ".csv", ".yml", ".yaml", ".html", ".css", ".js"]
14
 
15
- # Initialize rate limiter (10 queries per hour)
16
  query_limiter = QueryRateLimiter(max_queries_per_hour=5)
17
 
18
  # --- Basic Agent Definition ---
@@ -28,7 +28,7 @@ class BasicAgent:
28
  return answer
29
 
30
  # --- Chat Interface Functions ---
31
- def chat_with_agent(question: str, file_uploads, history: list) -> tuple:
32
  """
33
  Handle chat interaction with TurboNerd agent, now with file upload support.
34
  """
@@ -36,8 +36,9 @@ def chat_with_agent(question: str, file_uploads, history: list) -> tuple:
36
  return history, "", "Remaining queries this hour: 5/5"
37
 
38
  try:
39
- # Get client IP for rate limiting
40
- ip_address = request.remote_addr if request else "127.0.0.1"
 
41
 
42
  # Check rate limit
43
  if not query_limiter.is_allowed(ip_address):
@@ -324,13 +325,13 @@ with gr.Blocks(title="TurboNerd Agent🤓") as demo:
324
  # Chat interface event handlers
325
  submit_btn.click(
326
  fn=chat_with_agent,
327
- inputs=[question_input, file_upload, chatbot],
328
  outputs=[chatbot, question_input, remaining_queries]
329
  )
330
 
331
  question_input.submit(
332
  fn=chat_with_agent,
333
- inputs=[question_input, file_upload, chatbot],
334
  outputs=[chatbot, question_input, remaining_queries]
335
  )
336
 
 
12
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
13
  ALLOWED_FILE_EXTENSIONS = [".mp3", ".xlsx", ".py", ".png", ".jpg", ".jpeg", ".gif", ".txt", ".md", ".json", ".csv", ".yml", ".yaml", ".html", ".css", ".js"]
14
 
15
+ # Initialize rate limiter (5 queries per hour)
16
  query_limiter = QueryRateLimiter(max_queries_per_hour=5)
17
 
18
  # --- Basic Agent Definition ---
 
28
  return answer
29
 
30
  # --- Chat Interface Functions ---
31
+ def chat_with_agent(question: str, file_uploads, history: list, request: gr.Request) -> tuple:
32
  """
33
  Handle chat interaction with TurboNerd agent, now with file upload support.
34
  """
 
36
  return history, "", "Remaining queries this hour: 5/5"
37
 
38
  try:
39
+ # Get client IP for rate limiting using Gradio's request object
40
+ ip_address = request.client_ip if request else "127.0.0.1"
41
+ print(f"Request from IP: {ip_address}")
42
 
43
  # Check rate limit
44
  if not query_limiter.is_allowed(ip_address):
 
325
  # Chat interface event handlers
326
  submit_btn.click(
327
  fn=chat_with_agent,
328
+ inputs=[question_input, file_upload, chatbot, gr.Request()],
329
  outputs=[chatbot, question_input, remaining_queries]
330
  )
331
 
332
  question_input.submit(
333
  fn=chat_with_agent,
334
+ inputs=[question_input, file_upload, chatbot, gr.Request()],
335
  outputs=[chatbot, question_input, remaining_queries]
336
  )
337