File size: 3,889 Bytes
becc8f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import requests
import json
BASE_URL = "https://huggingface.co/spaces/Zeri00/Cogni-chat-document-reader"

def test_endpoints():
    """Test the debug and session endpoints to understand the issue."""
    
    print("CogniChat HF Spaces Diagnostic\n")
    
    # Test 1: Check debug endpoint
    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()
    
    # Test 3: Check if we can find any existing sessions
    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")