Spaces:
Running
Running
| #!/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() | |