File size: 6,638 Bytes
2c5e855
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/usr/bin/env python3
"""
Test script for PDF Analysis & Orchestrator deployment
Run this to verify all components are working correctly
"""

import os
import sys
import asyncio
from pathlib import Path

def test_imports():
    """Test that all required modules can be imported"""
    print("πŸ” Testing imports...")
    
    try:
        import gradio as gr
        print("βœ… Gradio imported successfully")
    except ImportError as e:
        print(f"❌ Gradio import failed: {e}")
        return False
    
    try:
        import openai
        print("βœ… OpenAI imported successfully")
    except ImportError as e:
        print(f"❌ OpenAI import failed: {e}")
        return False
    
    try:
        import pdfplumber
        print("βœ… pdfplumber imported successfully")
    except ImportError as e:
        print(f"❌ pdfplumber import failed: {e}")
        return False
    
    try:
        import numpy as np
        print("βœ… NumPy imported successfully")
    except ImportError as e:
        print(f"❌ NumPy import failed: {e}")
        return False
    
    try:
        from reportlab.lib.pagesizes import letter
        print("βœ… ReportLab imported successfully")
    except ImportError as e:
        print(f"❌ ReportLab import failed: {e}")
        return False
    
    return True

def test_config():
    """Test configuration loading"""
    print("\nπŸ”§ Testing configuration...")
    
    try:
        from config import Config
        print("βœ… Config module imported successfully")
        
        # Test configuration values
        print(f"   - OpenAI Model: {Config.OPENAI_MODEL}")
        print(f"   - Chunk Size: {Config.CHUNK_SIZE}")
        print(f"   - Cache Enabled: {Config.CACHE_ENABLED}")
        print(f"   - Max Upload MB: {Config.MAX_FILE_SIZE_MB}")
        
        return True
    except Exception as e:
        print(f"❌ Config test failed: {e}")
        return False

def test_utils():
    """Test utility functions"""
    print("\nπŸ› οΈ Testing utilities...")
    
    try:
        from utils import chunk_text, get_file_hash, load_pdf_text_cached
        print("βœ… Core utilities imported successfully")
        
        # Test chunking
        test_text = "This is a test document. " * 1000  # Create long text
        chunks = chunk_text(test_text, 100)
        print(f"   - Chunking test: {len(chunks)} chunks created")
        
        # Test file hash
        test_file = Path("test.txt")
        test_file.write_text("test content")
        file_hash = get_file_hash(str(test_file))
        print(f"   - File hash test: {file_hash[:8]}...")
        test_file.unlink()  # Clean up
        
        return True
    except Exception as e:
        print(f"❌ Utils test failed: {e}")
        return False

def test_agents():
    """Test agent initialization"""
    print("\nπŸ€– Testing agents...")
    
    try:
        from agents import AnalysisAgent, CollaborationAgent, ConversationAgent, MasterOrchestrator
        print("βœ… Agent classes imported successfully")
        
        # Test agent creation
        analysis_agent = AnalysisAgent("TestAgent", "gpt-4", 0)
        print("   - AnalysisAgent created successfully")
        
        # Test orchestrator
        agents = {
            "analysis": analysis_agent,
            "collab": CollaborationAgent("TestCollab", "gpt-4", 0),
            "conversation": ConversationAgent("TestConv", "gpt-4", 0)
        }
        orchestrator = MasterOrchestrator(agents)
        print("   - MasterOrchestrator created successfully")
        
        return True
    except Exception as e:
        print(f"❌ Agents test failed: {e}")
        return False

def test_managers():
    """Test manager classes"""
    print("\nπŸ“‹ Testing managers...")
    
    try:
        from utils.prompts import PromptManager
        from utils.export import ExportManager
        print("βœ… Manager classes imported successfully")
        
        # Test prompt manager
        prompt_manager = PromptManager()
        prompts = prompt_manager.get_all_prompts()
        print(f"   - PromptManager: {len(prompts)} default prompts loaded")
        
        # Test export manager
        export_manager = ExportManager()
        print("   - ExportManager created successfully")
        
        return True
    except Exception as e:
        print(f"❌ Managers test failed: {e}")
        return False

def test_environment():
    """Test environment variables"""
    print("\n🌍 Testing environment...")
    
    openai_key = os.environ.get("OPENAI_API_KEY")
    if openai_key:
        print("βœ… OPENAI_API_KEY is set")
        print(f"   - Key starts with: {openai_key[:8]}...")
    else:
        print("⚠️ OPENAI_API_KEY not set (required for full functionality)")
    
    # Check other important environment variables
    env_vars = [
        "OPENAI_MODEL",
        "CHUNK_SIZE", 
        "CACHE_ENABLED",
        "ANALYSIS_MAX_UPLOAD_MB"
    ]
    
    for var in env_vars:
        value = os.environ.get(var)
        if value:
            print(f"   - {var}: {value}")
        else:
            print(f"   - {var}: using default")
    
    return True

def test_gradio_interface():
    """Test Gradio interface creation"""
    print("\n🎨 Testing Gradio interface...")
    
    try:
        # Import the main app components
        from app import demo
        print("βœ… Gradio interface created successfully")
        
        # Test if the interface has the expected components
        if hasattr(demo, 'blocks'):
            print("   - Interface has blocks structure")
        
        return True
    except Exception as e:
        print(f"❌ Gradio interface test failed: {e}")
        return False

def main():
    """Run all tests"""
    print("πŸš€ PDF Analysis & Orchestrator - Deployment Test")
    print("=" * 50)
    
    tests = [
        test_imports,
        test_config,
        test_utils,
        test_agents,
        test_managers,
        test_environment,
        test_gradio_interface
    ]
    
    passed = 0
    total = len(tests)
    
    for test in tests:
        try:
            if test():
                passed += 1
        except Exception as e:
            print(f"❌ Test {test.__name__} failed with exception: {e}")
    
    print("\n" + "=" * 50)
    print(f"πŸ“Š Test Results: {passed}/{total} tests passed")
    
    if passed == total:
        print("πŸŽ‰ All tests passed! Your deployment is ready.")
        return 0
    else:
        print("⚠️ Some tests failed. Please check the errors above.")
        return 1

if __name__ == "__main__":
    sys.exit(main())