File size: 7,350 Bytes
e207dc8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#!/usr/bin/env python3
"""
Test script for Textilindo AI Assistant deployment
Run this to verify your setup before deploying to Hugging Face Spaces
"""

import os
import sys
import json
import requests
from pathlib import Path

def test_file_structure():
    """Test if all required files exist"""
    print("πŸ” Testing file structure...")
    
    required_files = [
        "app.py",
        "Dockerfile", 
        "requirements.txt",
        "configs/system_prompt.md",
        "configs/training_config.yaml",
        "templates/chat.html"
    ]
    
    required_dirs = [
        "data",
        "configs",
        "templates"
    ]
    
    missing_files = []
    missing_dirs = []
    
    for file in required_files:
        if not Path(file).exists():
            missing_files.append(file)
    
    for dir in required_dirs:
        if not Path(dir).exists():
            missing_dirs.append(dir)
    
    if missing_files:
        print(f"❌ Missing files: {missing_files}")
        return False
    
    if missing_dirs:
        print(f"❌ Missing directories: {missing_dirs}")
        return False
    
    print("βœ… All required files and directories exist")
    return True

def test_data_files():
    """Test if data files exist and are valid"""
    print("πŸ” Testing data files...")
    
    data_dir = Path("data")
    if not data_dir.exists():
        print("❌ Data directory not found")
        return False
    
    jsonl_files = list(data_dir.glob("*.jsonl"))
    if not jsonl_files:
        print("❌ No JSONL files found in data directory")
        return False
    
    print(f"βœ… Found {len(jsonl_files)} JSONL files:")
    for file in jsonl_files:
        print(f"  - {file.name}")
    
    # Test one JSONL file
    test_file = jsonl_files[0]
    try:
        with open(test_file, 'r', encoding='utf-8') as f:
            lines = f.readlines()
        
        if not lines:
            print(f"❌ {test_file.name} is empty")
            return False
        
        # Test first line
        first_line = lines[0].strip()
        if first_line:
            json.loads(first_line)
            print(f"βœ… {test_file.name} contains valid JSON")
        
        print(f"βœ… {test_file.name} has {len(lines)} lines")
        return True
        
    except json.JSONDecodeError as e:
        print(f"❌ {test_file.name} contains invalid JSON: {e}")
        return False
    except Exception as e:
        print(f"❌ Error reading {test_file.name}: {e}")
        return False

def test_config_files():
    """Test if configuration files are valid"""
    print("πŸ” Testing configuration files...")
    
    # Test system prompt
    prompt_file = Path("configs/system_prompt.md")
    if not prompt_file.exists():
        print("❌ System prompt file not found")
        return False
    
    try:
        with open(prompt_file, 'r', encoding='utf-8') as f:
            content = f.read()
        
        if 'SYSTEM_PROMPT' not in content:
            print("⚠️  SYSTEM_PROMPT not found in system_prompt.md")
        else:
            print("βœ… System prompt file is valid")
        
    except Exception as e:
        print(f"❌ Error reading system prompt: {e}")
        return False
    
    # Test training config
    config_file = Path("configs/training_config.yaml")
    if not config_file.exists():
        print("❌ Training config file not found")
        return False
    
    try:
        import yaml
        with open(config_file, 'r') as f:
            config = yaml.safe_load(f)
        
        required_fields = ['model_name', 'model_path', 'dataset_path']
        for field in required_fields:
            if field not in config:
                print(f"❌ Missing field in config: {field}")
                return False
        
        print("βœ… Training configuration is valid")
        return True
        
    except Exception as e:
        print(f"❌ Error reading training config: {e}")
        return False

def test_app_import():
    """Test if the app can be imported"""
    print("πŸ” Testing app import...")
    
    try:
        # Add current directory to path
        sys.path.insert(0, '.')
        
        # Try to import the app
        import app
        print("βœ… App module imported successfully")
        
        # Test if FastAPI app exists
        if hasattr(app, 'app'):
            print("βœ… FastAPI app found")
        else:
            print("❌ FastAPI app not found")
            return False
        
        return True
        
    except ImportError as e:
        print(f"❌ Error importing app: {e}")
        return False
    except Exception as e:
        print(f"❌ Unexpected error: {e}")
        return False

def test_environment():
    """Test environment variables"""
    print("πŸ” Testing environment...")
    
    # Check if HUGGINGFACE_API_KEY is set
    api_key = os.getenv('HUGGINGFACE_API_KEY')
    if api_key:
        print("βœ… HUGGINGFACE_API_KEY is set")
    else:
        print("⚠️  HUGGINGFACE_API_KEY not set (will use mock responses)")
    
    # Check Python version
    python_version = sys.version_info
    if python_version >= (3, 8):
        print(f"βœ… Python version {python_version.major}.{python_version.minor} is compatible")
    else:
        print(f"❌ Python version {python_version.major}.{python_version.minor} is too old (need 3.8+)")
        return False
    
    return True

def test_dependencies():
    """Test if required dependencies can be imported"""
    print("πŸ” Testing dependencies...")
    
    required_modules = [
        'fastapi',
        'uvicorn',
        'pydantic',
        'requests',
        'huggingface_hub'
    ]
    
    missing_modules = []
    
    for module in required_modules:
        try:
            __import__(module)
            print(f"βœ… {module}")
        except ImportError:
            missing_modules.append(module)
            print(f"❌ {module}")
    
    if missing_modules:
        print(f"❌ Missing modules: {missing_modules}")
        print("Install with: pip install " + " ".join(missing_modules))
        return False
    
    return True

def main():
    """Run all tests"""
    print("πŸ§ͺ Textilindo AI Assistant - Deployment Test")
    print("=" * 50)
    
    tests = [
        test_file_structure,
        test_data_files,
        test_config_files,
        test_environment,
        test_dependencies,
        test_app_import
    ]
    
    passed = 0
    total = len(tests)
    
    for test in tests:
        try:
            if test():
                passed += 1
            print()
        except Exception as e:
            print(f"❌ Test failed with error: {e}")
            print()
    
    print("=" * 50)
    print(f"πŸ“Š Test Results: {passed}/{total} tests passed")
    
    if passed == total:
        print("πŸŽ‰ All tests passed! Your setup is ready for deployment.")
        print("\nπŸ“‹ Next steps:")
        print("1. Run: ./deploy_to_hf_space.sh")
        print("2. Or manually deploy to Hugging Face Spaces")
        print("3. Set environment variables in your space settings")
        print("4. Test your deployed application")
    else:
        print("❌ Some tests failed. Please fix the issues above before deploying.")
        return 1
    
    return 0

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