| |
| """ |
| Quick test to diagnose the current HF Spaces chat issue. |
| Run this after uploading a document to test the session state. |
| """ |
| import requests |
| import json |
|
|
| |
| BASE_URL = "https://hyperxd-0-cognichat.hf.space" |
|
|
| def test_endpoints(): |
| """Test the debug and session endpoints to understand the issue.""" |
| |
| print("=== CogniChat HF Spaces Diagnostic ===\n") |
| |
| |
| print("1. Testing /debug endpoint...") |
| try: |
| response = requests.get(f"{BASE_URL}/debug") |
| if response.status_code == 200: |
| data = response.json() |
| print("β Debug endpoint working") |
| print(f" Environment: {data.get('environment')}") |
| print(f" GROQ API Key: {'Set' if data.get('groq_api_key_set') else 'NOT SET'}") |
| print(f" Sessions count: {data.get('sessions_count')}") |
| print(f" Upload folder: {data.get('upload_folder')}") |
| print(f" Upload folder writable: {data.get('upload_folder_writable')}") |
| print(f" Flask session ID: {data.get('flask_session_id')}") |
| print(f" Session keys: {data.get('flask_session_keys')}") |
| else: |
| print(f"β Debug endpoint failed: {response.status_code}") |
| except Exception as e: |
| print(f"β Error accessing debug endpoint: {e}") |
| |
| print() |
| |
| |
| print("2. Testing /test-session endpoint...") |
| try: |
| |
| session = requests.Session() |
| |
| |
| response = session.post(f"{BASE_URL}/test-session") |
| if response.status_code == 200: |
| data = response.json() |
| print("β Session write working") |
| print(f" Test key: {data.get('test_key')}") |
| print(f" Session keys: {data.get('session_keys')}") |
| else: |
| print(f"β Session write failed: {response.status_code}") |
| |
| |
| response = session.get(f"{BASE_URL}/test-session") |
| if response.status_code == 200: |
| data = response.json() |
| print("β Session read working") |
| print(f" Test key persisted: {data.get('test_key')}") |
| print(f" Has session data: {data.get('has_session_data')}") |
| |
| if not data.get('test_key'): |
| print("β οΈ WARNING: Sessions are not persisting between requests!") |
| print(" This is likely the cause of the 400 chat error.") |
| else: |
| print(f"β Session read failed: {response.status_code}") |
| |
| except Exception as e: |
| print(f"β Error testing sessions: {e}") |
| |
| print() |
| |
| |
| print("3. Checking for existing RAG sessions...") |
| try: |
| response = requests.get(f"{BASE_URL}/debug") |
| if response.status_code == 200: |
| data = response.json() |
| session_ids = data.get('session_ids', []) |
| if session_ids: |
| print(f"β Found {len(session_ids)} existing RAG sessions") |
| print(f" Session IDs: {session_ids[:3]}{'...' if len(session_ids) > 3 else ''}") |
| else: |
| print("βΉοΈ No RAG sessions found (normal if no documents were uploaded)") |
| |
| except Exception as e: |
| print(f"β Error checking RAG sessions: {e}") |
| |
| print() |
| print("=== Diagnosis Complete ===") |
| print() |
| print("LIKELY ISSUE:") |
| print("If sessions are not persisting, this is a common issue in HF Spaces") |
| print("where Flask sessions don't work properly across requests.") |
| print() |
| print("SOLUTION:") |
| print("We need to modify the app to use a different session storage method") |
| print("or pass session ID through request body instead of Flask sessions.") |
|
|
| if __name__ == "__main__": |
| print("Before running this script:") |
| print("1. Update BASE_URL with your actual HF Spaces URL") |
| print("2. Make sure your Space is running") |
| print("3. Optionally upload a document first") |
| print() |
| |
| |
| |
| |
| print("Update the BASE_URL variable above and uncomment the test_endpoints() call") |