Update app.py
Browse files
app.py
CHANGED
|
@@ -204,15 +204,37 @@ document_inspection_tool = None
|
|
| 204 |
def get_user_session(request: gr.Request) -> str:
|
| 205 |
"""Get or create a unique session ID for the user."""
|
| 206 |
if not request:
|
|
|
|
| 207 |
return str(uuid.uuid4())
|
| 208 |
|
| 209 |
# Try to get session from headers or create new one
|
| 210 |
session_id = request.headers.get("x-session-id")
|
| 211 |
if not session_id:
|
| 212 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
|
| 214 |
return session_id
|
| 215 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 216 |
def get_session_data(session_id: str) -> dict:
|
| 217 |
"""Get session data for a specific user."""
|
| 218 |
with session_lock:
|
|
@@ -296,10 +318,12 @@ class GradioUI:
|
|
| 296 |
def interact_with_agent(self, prompt, messages, request: gr.Request):
|
| 297 |
"""Handle agent interaction with proper session management."""
|
| 298 |
# Get unique session ID for this user
|
| 299 |
-
session_id =
|
| 300 |
session_data = get_session_data(session_id)
|
| 301 |
|
| 302 |
-
logger.info(f"Processing request for session {session_id
|
|
|
|
|
|
|
| 303 |
|
| 304 |
# Check if we have a valid agent for this session
|
| 305 |
if not session_data.get("agent"):
|
|
@@ -443,10 +467,12 @@ User Query: """
|
|
| 443 |
def setup_api_key(self, api_key: str, max_steps: int, request: gr.Request) -> str:
|
| 444 |
"""Setup API key for the user's session."""
|
| 445 |
# Get unique session ID for this user
|
| 446 |
-
session_id =
|
| 447 |
session_data = get_session_data(session_id)
|
| 448 |
|
| 449 |
-
logger.info(f"Setting up API key for session {session_id
|
|
|
|
|
|
|
| 450 |
|
| 451 |
# Check if API key is provided from interface
|
| 452 |
if api_key and api_key.strip():
|
|
|
|
| 204 |
def get_user_session(request: gr.Request) -> str:
|
| 205 |
"""Get or create a unique session ID for the user."""
|
| 206 |
if not request:
|
| 207 |
+
logger.warning("No request object, using random session ID")
|
| 208 |
return str(uuid.uuid4())
|
| 209 |
|
| 210 |
# Try to get session from headers or create new one
|
| 211 |
session_id = request.headers.get("x-session-id")
|
| 212 |
if not session_id:
|
| 213 |
+
# Use client IP and user agent as a more stable identifier
|
| 214 |
+
client_ip = request.client.host if hasattr(request, 'client') and request.client else "unknown"
|
| 215 |
+
user_agent = request.headers.get("user-agent", "unknown")
|
| 216 |
+
# Create a hash-based session ID for more stability
|
| 217 |
+
import hashlib
|
| 218 |
+
session_hash = hashlib.md5(f"{client_ip}:{user_agent}".encode()).hexdigest()
|
| 219 |
+
session_id = f"session_{session_hash[:8]}"
|
| 220 |
+
logger.info(f"Created stable session ID {session_id} for client {client_ip}")
|
| 221 |
|
| 222 |
return session_id
|
| 223 |
|
| 224 |
+
def get_stable_session_id(request: gr.Request) -> str:
|
| 225 |
+
"""Get a stable session ID that persists across requests."""
|
| 226 |
+
if not request:
|
| 227 |
+
return "default_session"
|
| 228 |
+
|
| 229 |
+
# Use a combination of client info for more stable sessions
|
| 230 |
+
client_ip = getattr(request.client, 'host', 'unknown') if request.client else 'unknown'
|
| 231 |
+
user_agent = request.headers.get("user-agent", "unknown")
|
| 232 |
+
|
| 233 |
+
# Create a stable session ID
|
| 234 |
+
import hashlib
|
| 235 |
+
session_hash = hashlib.md5(f"{client_ip}:{user_agent}".encode()).hexdigest()
|
| 236 |
+
return f"user_{session_hash[:12]}"
|
| 237 |
+
|
| 238 |
def get_session_data(session_id: str) -> dict:
|
| 239 |
"""Get session data for a specific user."""
|
| 240 |
with session_lock:
|
|
|
|
| 318 |
def interact_with_agent(self, prompt, messages, request: gr.Request):
|
| 319 |
"""Handle agent interaction with proper session management."""
|
| 320 |
# Get unique session ID for this user
|
| 321 |
+
session_id = get_stable_session_id(request)
|
| 322 |
session_data = get_session_data(session_id)
|
| 323 |
|
| 324 |
+
logger.info(f"Processing request for session {session_id}...")
|
| 325 |
+
logger.info(f"Request client: {request.client.host if request and request.client else 'unknown'}")
|
| 326 |
+
logger.info(f"Request user-agent: {request.headers.get('user-agent', 'unknown')[:50] if request else 'unknown'}")
|
| 327 |
|
| 328 |
# Check if we have a valid agent for this session
|
| 329 |
if not session_data.get("agent"):
|
|
|
|
| 467 |
def setup_api_key(self, api_key: str, max_steps: int, request: gr.Request) -> str:
|
| 468 |
"""Setup API key for the user's session."""
|
| 469 |
# Get unique session ID for this user
|
| 470 |
+
session_id = get_stable_session_id(request)
|
| 471 |
session_data = get_session_data(session_id)
|
| 472 |
|
| 473 |
+
logger.info(f"Setting up API key for session {session_id}...")
|
| 474 |
+
logger.info(f"Setup request client: {request.client.host if request and request.client else 'unknown'}")
|
| 475 |
+
logger.info(f"Setup request user-agent: {request.headers.get('user-agent', 'unknown')[:50] if request else 'unknown'}")
|
| 476 |
|
| 477 |
# Check if API key is provided from interface
|
| 478 |
if api_key and api_key.strip():
|