CatoG commited on
Commit ·
a665704
1
Parent(s): 36c40b0
fix user location - not using datacenter location
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ import uuid
|
|
| 4 |
import random
|
| 5 |
import warnings
|
| 6 |
import traceback
|
|
|
|
| 7 |
from datetime import datetime, timezone
|
| 8 |
from typing import Dict, List, Optional, Tuple
|
| 9 |
|
|
@@ -104,6 +105,9 @@ LLM_CACHE: Dict[str, object] = {}
|
|
| 104 |
AGENT_CACHE: Dict[Tuple[str, Tuple[str, ...]], object] = {}
|
| 105 |
RUNTIME_HEALTH: Dict[str, str] = {}
|
| 106 |
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
# ============================================================
|
| 109 |
# Shared wrappers
|
|
@@ -404,8 +408,10 @@ def generate_uuid(_: str = "") -> str:
|
|
| 404 |
@tool
|
| 405 |
def get_user_location(_: str = "") -> str:
|
| 406 |
"""Determine the user's approximate physical location based on their public IP address."""
|
|
|
|
|
|
|
| 407 |
try:
|
| 408 |
-
response = requests.get(
|
| 409 |
response.raise_for_status()
|
| 410 |
data = response.json()
|
| 411 |
if data.get("status") != "success":
|
|
@@ -578,9 +584,23 @@ def build_debug_report(
|
|
| 578 |
# Run agent
|
| 579 |
# ============================================================
|
| 580 |
|
| 581 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 582 |
history = history or []
|
| 583 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 584 |
if not message or not str(message).strip():
|
| 585 |
return history, "No input provided.", "", None, model_status_text(model_id), "No input provided."
|
| 586 |
|
|
|
|
| 4 |
import random
|
| 5 |
import warnings
|
| 6 |
import traceback
|
| 7 |
+
import threading
|
| 8 |
from datetime import datetime, timezone
|
| 9 |
from typing import Dict, List, Optional, Tuple
|
| 10 |
|
|
|
|
| 105 |
AGENT_CACHE: Dict[Tuple[str, Tuple[str, ...]], object] = {}
|
| 106 |
RUNTIME_HEALTH: Dict[str, str] = {}
|
| 107 |
|
| 108 |
+
# Thread-local storage so each Gradio request can carry the real client IP
|
| 109 |
+
_request_context = threading.local()
|
| 110 |
+
|
| 111 |
|
| 112 |
# ============================================================
|
| 113 |
# Shared wrappers
|
|
|
|
| 408 |
@tool
|
| 409 |
def get_user_location(_: str = "") -> str:
|
| 410 |
"""Determine the user's approximate physical location based on their public IP address."""
|
| 411 |
+
client_ip = getattr(_request_context, "client_ip", "") or ""
|
| 412 |
+
url = f"http://ip-api.com/json/{client_ip}" if client_ip else "http://ip-api.com/json/"
|
| 413 |
try:
|
| 414 |
+
response = requests.get(url, timeout=5)
|
| 415 |
response.raise_for_status()
|
| 416 |
data = response.json()
|
| 417 |
if data.get("status") != "success":
|
|
|
|
| 584 |
# Run agent
|
| 585 |
# ============================================================
|
| 586 |
|
| 587 |
+
def _extract_client_ip(request: gr.Request) -> str:
|
| 588 |
+
"""Return the real client IP, respecting reverse-proxy headers."""
|
| 589 |
+
headers = dict(request.headers or {})
|
| 590 |
+
for header in ("x-forwarded-for", "x-real-ip", "cf-connecting-ip"):
|
| 591 |
+
val = headers.get(header, "")
|
| 592 |
+
if val:
|
| 593 |
+
return val.split(",")[0].strip()
|
| 594 |
+
return getattr(request.client, "host", "") or ""
|
| 595 |
+
|
| 596 |
+
|
| 597 |
+
def run_agent(message, history, selected_tools, model_id, request: gr.Request = None):
|
| 598 |
history = history or []
|
| 599 |
|
| 600 |
+
# Capture the real client IP for get_user_location
|
| 601 |
+
if request is not None:
|
| 602 |
+
_request_context.client_ip = _extract_client_ip(request)
|
| 603 |
+
|
| 604 |
if not message or not str(message).strip():
|
| 605 |
return history, "No input provided.", "", None, model_status_text(model_id), "No input provided."
|
| 606 |
|