sagarnildass commited on
Commit
f8e6c32
·
verified ·
1 Parent(s): 4013d2c

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +27 -4
app.py CHANGED
@@ -8,6 +8,12 @@ import gradio as gr
8
  import base64
9
  import time
10
  from collections import defaultdict
 
 
 
 
 
 
11
 
12
 
13
  load_dotenv(override=True)
@@ -163,10 +169,27 @@ in which they provide their email, then give a summary of the conversation so fa
163
  return system_prompt
164
 
165
  def chat(self, message, history):
166
- # Apply rate limiting with user's IP as the key
167
- # In a production app, you would get the real client IP
168
- user_id = "default_user" # Use request.client.host in a proper web framework
169
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
170
  if self.rate_limiter.is_rate_limited(user_id):
171
  return "You're sending messages too quickly. Please wait a moment before sending another message."
172
 
 
8
  import base64
9
  import time
10
  from collections import defaultdict
11
+ import fastapi
12
+ from gradio.context import Context
13
+ import logging
14
+
15
+ logger = logging.getLogger(__name__)
16
+ logger.setLevel(logging.DEBUG)
17
 
18
 
19
  load_dotenv(override=True)
 
169
  return system_prompt
170
 
171
  def chat(self, message, history):
172
+ # Get the client IP from Gradio's request context
173
+ try:
174
+ # Try to get the real client IP from request headers
175
+ request = Context.get_context().request
176
+ # Check for X-Forwarded-For header (common in reverse proxies like HF Spaces)
177
+ forwarded_for = request.headers.get("X-Forwarded-For")
178
+ # Check for Cf-Connecting-IP header (Cloudflare)
179
+ cloudflare_ip = request.headers.get("Cf-Connecting-IP")
180
+
181
+ if forwarded_for:
182
+ # X-Forwarded-For contains a comma-separated list of IPs, the first one is the client
183
+ user_id = forwarded_for.split(",")[0].strip()
184
+ elif cloudflare_ip:
185
+ user_id = cloudflare_ip
186
+ else:
187
+ # Fall back to direct client address
188
+ user_id = request.client.host
189
+ except (AttributeError, RuntimeError, fastapi.exceptions.FastAPIError):
190
+ # Fallback if we can't get context or if running outside of FastAPI
191
+ user_id = "default_user"
192
+ logger.debug(f"User ID: {user_id}")
193
  if self.rate_limiter.is_rate_limited(user_id):
194
  return "You're sending messages too quickly. Please wait a moment before sending another message."
195