File size: 8,341 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
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
#!/usr/bin/env python3
"""
Verification script to check if CogniChat is ready for HuggingFace Spaces deployment.
Run this before deploying to catch any configuration issues.
"""

import os
import sys
from pathlib import Path

def print_header(text):
    """Print a formatted header."""
    print(f"\n{'='*60}")
    print(f"  {text}")
    print(f"{'='*60}")

def print_check(condition, message):
    """Print a check result."""
    status = "PASS" if condition else "FAIL"
    print(f"{status}: {message}")
    return condition

def verify_files_exist():
    """Verify all required files exist."""
    print_header("1. Checking Required Files")
    
    required_files = [
        'app.py',
        'rag_processor.py',
        'Dockerfile',
        'requirements.txt',
        'templates/index.html'
    ]
    
    all_exist = True
    for file in required_files:
        exists = Path(file).exists()
        all_exist = all_exist and print_check(exists, f"File exists: {file}")
    
    return all_exist

def verify_upload_folder_config():
    """Verify upload folder configuration in app.py."""
    print_header("2. Checking Upload Folder Configuration")
    
    with open('app.py', 'r') as f:
        app_content = f.read()
    
    checks = [
        ('SPACE_ID' in app_content, "Detects SPACE_ID environment variable"),
        ("'/tmp/uploads'" in app_content, "Uses /tmp/uploads for HF Spaces"),
        ('is_hf_spaces' in app_content, "Has HF Spaces detection logic"),
        ('os.makedirs(app.config' in app_content, "Creates upload directory"),
    ]
    
    all_pass = True
    for condition, message in checks:
        all_pass = all_pass and print_check(condition, message)
    
    return all_pass

def verify_session_management():
    """Verify session management implementation."""
    print_header("3. Checking Session Management")
    
    # Check backend
    with open('app.py', 'r') as f:
        app_content = f.read()
    
    backend_checks = [
        ("session['session_id']" in app_content, "Stores session_id in Flask session"),
        ("'session_id': session_id" in app_content, "Returns session_id in upload response"),
        ("data.get('session_id')" in app_content, "Accepts session_id from request body"),
    ]
    
    # Check frontend
    with open('templates/index.html', 'r') as f:
        html_content = f.read()
    
    frontend_checks = [
        ('sessionStorage.setItem' in html_content, "Frontend stores session_id"),
        ('sessionStorage.getItem' in html_content, "Frontend retrieves session_id"),
        ('requestBody.session_id' in html_content, "Frontend sends session_id in chat"),
    ]
    
    all_pass = True
    print("  Backend Implementation:")
    for condition, message in backend_checks:
        all_pass = all_pass and print_check(condition, f"  {message}")
    
    print("\n  Frontend Implementation:")
    for condition, message in frontend_checks:
        all_pass = all_pass and print_check(condition, f"  {message}")
    
    return all_pass

def verify_error_handling():
    """Verify robust error handling."""
    print_header("4. Checking Error Handling")
    
    with open('app.py', 'r') as f:
        app_content = f.read()
    
    checks = [
        ('try:' in app_content and 'except' in app_content, "Has try/except blocks"),
        ('load_pdf_with_fallback' in app_content, "Has PDF fallback loading"),
        ('failed_files' in app_content, "Tracks failed file uploads"),
        ('fallback_dir' in app_content, "Has cache directory fallbacks"),
    ]
    
    all_pass = True
    for condition, message in checks:
        all_pass = all_pass and print_check(condition, message)
    
    return all_pass

def verify_environment_variables():
    """Check for environment variable handling."""
    print_header("5. Checking Environment Variables")
    
    with open('app.py', 'r') as f:
        app_content = f.read()
    
    with open('rag_processor.py', 'r') as f:
        rag_content = f.read()
    
    checks = [
        ('GROQ_API_KEY' in rag_content, "RAG processor checks GROQ_API_KEY"),
        ('SPACE_ID' in app_content, "App detects HF Spaces environment"),
        ('HF_HOME' in app_content, "Sets HuggingFace cache paths"),
        ('load_dotenv()' in rag_content, "Loads .env file for local dev"),
    ]
    
    all_pass = True
    for condition, message in checks:
        all_pass = all_pass and print_check(condition, message)
    
    # Check if .env.example exists
    all_pass = all_pass and print_check(
        Path('.env.example').exists() or Path('README.md').exists(),
        "Has documentation for environment variables"
    )
    
    return all_pass

def verify_dockerfile():
    """Verify Dockerfile configuration."""
    print_header("6. Checking Dockerfile")
    
    if not Path('Dockerfile').exists():
        print_check(False, "Dockerfile exists")
        return False
    
    with open('Dockerfile', 'r') as f:
        dockerfile_content = f.read()
    
    checks = [
        ('FROM python' in dockerfile_content, "Uses Python base image"),
        ('WORKDIR /app' in dockerfile_content, "Sets working directory"),
        ('EXPOSE 7860' in dockerfile_content, "Exposes port 7860 (HF requirement)"),
        ('appuser' in dockerfile_content, "Runs as non-root user"),
        ('CMD' in dockerfile_content or 'ENTRYPOINT' in dockerfile_content, "Has startup command"),
    ]
    
    all_pass = True
    for condition, message in checks:
        all_pass = all_pass and print_check(condition, message)
    
    return all_pass

def verify_requirements():
    """Verify requirements.txt has all dependencies."""
    print_header("7. Checking Dependencies")
    
    with open('requirements.txt', 'r') as f:
        requirements = f.read().lower()
    
    critical_deps = [
        'flask',
        'langchain',
        'groq',
        'faiss',
        'sentence-transformers',
        'pypdf',
        'gtts',
        'rank-bm25'
    ]
    
    all_pass = True
    for dep in critical_deps:
        found = dep in requirements
        all_pass = all_pass and print_check(found, f"Has dependency: {dep}")
    
    return all_pass

def verify_no_duplicates():
    """Check for duplicate content in index.html."""
    print_header("8. Checking for Duplicates")
    
    with open('templates/index.html', 'r') as f:
        html_content = f.read()
    
    # Count DOCTYPE declarations
    doctype_count = html_content.count('<!DOCTYPE html>')
    checks = [
        (doctype_count == 1, f"Single HTML document (found {doctype_count} DOCTYPE declarations)"),
        (html_content.count('</html>') == 1, "Single closing </html> tag"),
        (html_content.count('uploadAndProcessFiles') <= 2, "No duplicate JavaScript functions"),
    ]
    
    all_pass = True
    for condition, message in checks:
        all_pass = all_pass and print_check(condition, message)
    
    return all_pass

def main():
    """Run all verification checks."""
    print("\n" + "="*60)
    print("  HuggingFace Spaces Readiness Check for CogniChat")
    print("="*60)
    
    checks = [
        verify_files_exist(),
        verify_upload_folder_config(),
        verify_session_management(),
        verify_error_handling(),
        verify_environment_variables(),
        verify_dockerfile(),
        verify_requirements(),
        verify_no_duplicates(),
    ]
    
    print_header("Summary")
    
    passed = sum(checks)
    total = len(checks)
    
    if all(checks):
        print(f"\n✅ ALL CHECKS PASSED ({passed}/{total})")
        print("\n🚀 Your application is ready for HuggingFace Spaces deployment!")
        print("\nNext steps:")
        print("1. Go to https://huggingface.co/new-space")
        print("2. Select 'Docker' as SDK")
        print("3. Upload all project files")
        print("4. Set GROQ_API_KEY in Space secrets")
        print("5. Wait for build to complete")
        return 0
    else:
        print(f"\n SOME CHECKS FAILED ({total - passed}/{total} issues)")
        print("\n Please fix the issues above before deploying.")
        print("\nFor detailed guidance, see:")
        print("- HF_SPACES_FILE_STORAGE_GUIDE.md")
        print("- DEPLOYMENT.md")
        print("- HF_SPACES_CHECKLIST.md")
        return 1

if __name__ == '__main__':
    exit_code = main()
    sys.exit(exit_code)