Spaces:
Sleeping
Sleeping
File size: 6,141 Bytes
e06a21d |
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 |
#!/usr/bin/env python3
"""
Health check and diagnostic script for CVE Fact Checker.
Run this to diagnose common issues and verify system health.
"""
import os
import sys
import tempfile
import requests
import time
from pathlib import Path
def check_environment():
"""Check environment variables and directories."""
print("π Environment Check")
print("=" * 50)
# Check Python version
print(f"Python version: {sys.version}")
# Check key environment variables
env_vars = [
'PORT', 'VECTOR_PERSIST_DIR', 'SENTENCE_TRANSFORMERS_HOME',
'FIREBASE_API_KEY', 'OPENROUTER_API_KEY', 'AUTO_INGEST', 'LANGUAGE_FILTER'
]
for var in env_vars:
value = os.environ.get(var, 'Not set')
if 'API_KEY' in var and value != 'Not set':
value = f"{value[:8]}..." if len(value) > 8 else "***"
print(f" {var}: {value}")
print()
def check_directories():
"""Check directory permissions and setup."""
print("π Directory Permissions Check")
print("=" * 50)
dirs_to_check = [
('/tmp', 'System temp directory'),
('/tmp/vector_db', 'Vector DB storage'),
('/tmp/sentence_transformers', 'Model cache'),
('/data', 'Persistent data (if available)'),
('.', 'Current directory'),
]
for dir_path, description in dirs_to_check:
try:
path = Path(dir_path)
# Check if exists
exists = path.exists()
if not exists and dir_path != '/data': # /data might not exist
path.mkdir(parents=True, exist_ok=True)
exists = True
# Test write permission if exists
writable = False
if exists:
try:
test_file = path / "health_check_test"
test_file.write_text("test")
test_file.unlink()
writable = True
except:
pass
status = "β
" if (exists and writable) else "β"
print(f" {status} {dir_path} - {description}")
if exists and not writable:
print(f" β οΈ Exists but not writable")
elif not exists:
print(f" β οΈ Does not exist")
except Exception as e:
print(f" β {dir_path} - Error: {e}")
print()
def check_packages():
"""Check if required packages are importable."""
print("π¦ Package Import Check")
print("=" * 50)
packages = [
('flask', 'Flask web framework'),
('requests', 'HTTP requests'),
('langchain', 'LangChain framework'),
('chromadb', 'Vector database'),
('sentence_transformers', 'Embedding models'),
('firebase_admin', 'Firebase SDK'),
]
for package, description in packages:
try:
__import__(package)
print(f" β
{package} - {description}")
except ImportError as e:
print(f" β {package} - {description} (Error: {e})")
except Exception as e:
print(f" β οΈ {package} - {description} (Warning: {e})")
print()
def check_app_health():
"""Check if the Flask app can be imported and basic functionality."""
print("π₯ Application Health Check")
print("=" * 50)
try:
# Test basic imports
from cve_factchecker.config import load_openrouter_config
print(" β
Config module imports")
from cve_factchecker.embeddings import build_embeddings
print(" β
Embeddings module imports")
from cve_factchecker.models import NewsArticle
print(" β
Models module imports")
# Test embeddings
embeddings = build_embeddings()
test_embedding = embeddings.embed_query("test")
if len(test_embedding) > 0:
print(" β
Embeddings functional")
else:
print(" β Embeddings not working")
# Test Flask app import
from cve_factchecker.app import app
print(" β
Flask app imports")
# Test basic app functionality
with app.test_client() as client:
response = client.get('/health')
if response.status_code == 200:
print(" β
Health endpoint responds")
else:
print(f" β Health endpoint error: {response.status_code}")
except Exception as e:
print(f" β Application error: {e}")
import traceback
traceback.print_exc()
print()
def check_firebase_connectivity():
"""Check Firebase connectivity."""
print("π₯ Firebase Connectivity Check")
print("=" * 50)
try:
from cve_factchecker.firebase_loader import FirebaseNewsLoader
loader = FirebaseNewsLoader()
print(f" β
Firebase loader initialized")
print(f" π Project ID: {loader.project_id}")
# Test with very small limit to avoid rate limiting, filter for English
print(" π Testing connectivity with 1 English article limit...")
articles = loader.fetch_articles(limit=1, language="English")
if articles:
print(f" β
Successfully fetched {len(articles)} article(s)")
print(f" π Sample title: {articles[0].title[:50]}...")
else:
print(" β οΈ No articles fetched (might be rate limited or empty)")
except Exception as e:
print(f" β Firebase error: {e}")
print()
def main():
"""Run all health checks."""
print("π₯ CVE Fact Checker Health Check")
print("=" * 60)
print(f"Timestamp: {time.strftime('%Y-%m-%d %H:%M:%S')}")
print()
check_environment()
check_directories()
check_packages()
check_app_health()
check_firebase_connectivity()
print("π Health check complete!")
print("=" * 60)
if __name__ == "__main__":
main()
|