Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -183,20 +183,53 @@ def get_context(message):
|
|
183 |
|
184 |
def get_ip_info(ip_address):
|
185 |
"""Get geolocation info for an IP address"""
|
186 |
-
if ip_address
|
187 |
-
return {"country": "
|
|
|
|
|
|
|
|
|
|
|
188 |
try:
|
189 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
190 |
if response.status_code == 200:
|
191 |
data = response.json()
|
|
|
|
|
|
|
|
|
|
|
|
|
192 |
return {
|
193 |
"country": data.get("country_name", "Unknown"),
|
194 |
"city": data.get("city", "Unknown"),
|
195 |
-
"region": data.get("region", "Unknown")
|
|
|
|
|
|
|
|
|
196 |
}
|
197 |
-
|
|
|
|
|
|
|
|
|
|
|
198 |
print(f"Error getting IP info: {str(e)}")
|
199 |
-
|
|
|
|
|
|
|
200 |
|
201 |
def log_conversation(timestamp, user_message, assistant_response, model_name, context, error=None, client_ip=None):
|
202 |
"""Log conversation details to JSON file - local directory or HuggingFace Dataset repository"""
|
@@ -306,8 +339,18 @@ def log_conversation(timestamp, user_message, assistant_response, model_name, co
|
|
306 |
def chat_response(message, history, model_name, request: gr.Request):
|
307 |
"""Chat response function for Gradio interface"""
|
308 |
try:
|
309 |
-
# Get client IP address
|
310 |
-
client_ip =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
311 |
|
312 |
# Append 'at BAS' to the user's message
|
313 |
message += " at BAS"
|
|
|
183 |
|
184 |
def get_ip_info(ip_address):
|
185 |
"""Get geolocation info for an IP address"""
|
186 |
+
if not ip_address:
|
187 |
+
return {"country": "Unknown", "city": "Unknown", "region": "Unknown"}
|
188 |
+
|
189 |
+
# Handle local/private IPs
|
190 |
+
if ip_address in ['127.0.0.1', 'localhost', '0.0.0.0'] or ip_address.startswith(('10.', '172.', '192.168.')):
|
191 |
+
return {"country": "Local Network", "city": "Local", "region": "Local"}
|
192 |
+
|
193 |
try:
|
194 |
+
# Add user-agent to be a good API citizen
|
195 |
+
headers = {
|
196 |
+
'User-Agent': 'BAS-Website-Chat/1.0'
|
197 |
+
}
|
198 |
+
|
199 |
+
response = requests.get(
|
200 |
+
f'https://ipapi.co/{ip_address}/json/',
|
201 |
+
headers=headers,
|
202 |
+
timeout=5 # 5 second timeout
|
203 |
+
)
|
204 |
+
|
205 |
if response.status_code == 200:
|
206 |
data = response.json()
|
207 |
+
|
208 |
+
# Check for error responses
|
209 |
+
if 'error' in data:
|
210 |
+
print(f"IP API error: {data.get('reason', 'Unknown error')}")
|
211 |
+
return {"country": "Unknown", "city": "Unknown", "region": "Unknown"}
|
212 |
+
|
213 |
return {
|
214 |
"country": data.get("country_name", "Unknown"),
|
215 |
"city": data.get("city", "Unknown"),
|
216 |
+
"region": data.get("region", "Unknown"),
|
217 |
+
"latitude": data.get("latitude"),
|
218 |
+
"longitude": data.get("longitude"),
|
219 |
+
"timezone": data.get("timezone"),
|
220 |
+
"org": data.get("org")
|
221 |
}
|
222 |
+
else:
|
223 |
+
print(f"IP API returned status code: {response.status_code}")
|
224 |
+
|
225 |
+
except requests.exceptions.Timeout:
|
226 |
+
print(f"Timeout getting IP info for {ip_address}")
|
227 |
+
except requests.exceptions.RequestException as e:
|
228 |
print(f"Error getting IP info: {str(e)}")
|
229 |
+
except Exception as e:
|
230 |
+
print(f"Unexpected error getting IP info: {str(e)}")
|
231 |
+
|
232 |
+
return {"country": "Unknown", "city": "Unknown", "region": "Unknown"}
|
233 |
|
234 |
def log_conversation(timestamp, user_message, assistant_response, model_name, context, error=None, client_ip=None):
|
235 |
"""Log conversation details to JSON file - local directory or HuggingFace Dataset repository"""
|
|
|
339 |
def chat_response(message, history, model_name, request: gr.Request):
|
340 |
"""Chat response function for Gradio interface"""
|
341 |
try:
|
342 |
+
# Get client IP address with better proxy handling
|
343 |
+
client_ip = None
|
344 |
+
if request:
|
345 |
+
# Try to get real IP from headers in order of reliability
|
346 |
+
client_ip = (
|
347 |
+
request.headers.get('X-Forwarded-For', '').split(',')[0].strip() or
|
348 |
+
request.headers.get('X-Real-IP') or
|
349 |
+
request.headers.get('CF-Connecting-IP') or # Cloudflare
|
350 |
+
request.client.host
|
351 |
+
)
|
352 |
+
print(f"\nClient IP detected: {client_ip}")
|
353 |
+
print(f"Request headers: {request.headers}")
|
354 |
|
355 |
# Append 'at BAS' to the user's message
|
356 |
message += " at BAS"
|