Spaces:
Sleeping
Sleeping
File size: 21,369 Bytes
aa69d4c |
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 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 |
#!/usr/bin/env python3
"""
Comprehensive diagnostic tool to trace all components of the CVE Fact Checker system.
This will identify issues in data fetching, chunking, embeddings, vector store, and retrieval.
"""
import os
import sys
import time
import json
from datetime import datetime
from typing import Dict, Any, List, Optional
# Add the parent directory to Python path
current_dir = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, current_dir)
class CVEFactCheckerDiagnostic:
def __init__(self):
self.results = {
"timestamp": datetime.now().isoformat(),
"environment": self._get_environment_info(),
"components": {}
}
def _get_environment_info(self) -> Dict[str, Any]:
"""Get environment information."""
return {
"python_version": sys.version,
"working_directory": os.getcwd(),
"environment_vars": {
"AUTO_INGEST": os.environ.get("AUTO_INGEST", "not_set"),
"LANGUAGE_FILTER": os.environ.get("LANGUAGE_FILTER", "not_set"),
"FIREBASE_API_KEY": "set" if os.environ.get("FIREBASE_API_KEY") else "not_set",
"HF_HOME": os.environ.get("HF_HOME", "not_set"),
"TRANSFORMERS_CACHE": os.environ.get("TRANSFORMERS_CACHE", "not_set"),
},
"file_system": {
"/tmp": os.path.exists("/tmp"),
"/data": os.path.exists("/data"),
"/app": os.path.exists("/app"),
}
}
def diagnose_firebase_connection(self) -> Dict[str, Any]:
"""Diagnose Firebase connection and data fetching."""
print("π Diagnosing Firebase Connection...")
result = {"status": "unknown", "errors": [], "data": {}}
try:
from cve_factchecker.firebase_loader import FirebaseNewsLoader, FirebaseConfig
# Test Firebase configuration
loader = FirebaseNewsLoader()
result["data"]["project_id"] = loader.project_id
result["data"]["api_key_length"] = len(loader.api_key) if loader.api_key else 0
# Test basic connectivity
print(" Testing basic Firebase connectivity...")
try:
# Test with minimal fetch
articles = loader.fetch_english_articles(limit=1)
result["data"]["connectivity"] = "success"
result["data"]["test_fetch_count"] = len(articles)
if articles:
sample = articles[0]
result["data"]["sample_article"] = {
"title": sample.title[:50] + "..." if len(sample.title) > 50 else sample.title,
"content_length": len(sample.content),
"has_url": bool(sample.url),
"language": getattr(sample, 'language', 'unknown')
}
except Exception as e:
result["errors"].append(f"Firebase connectivity failed: {e}")
result["data"]["connectivity"] = "failed"
# Test larger fetch
print(" Testing larger data fetch...")
try:
start_time = time.time()
articles = loader.fetch_english_articles(limit=10)
fetch_time = time.time() - start_time
result["data"]["larger_fetch"] = {
"count": len(articles),
"time_seconds": round(fetch_time, 2),
"avg_time_per_article": round(fetch_time / max(len(articles), 1), 3)
}
except Exception as e:
result["errors"].append(f"Larger fetch failed: {e}")
# Test collection accessibility
print(" Testing collection configurations...")
config = FirebaseConfig(
api_key="test", auth_domain="test", project_id="test",
storage_bucket="test", messaging_sender_id="test", app_id="test"
)
result["data"]["collections"] = {
"articles_collection": config.ARTICLES_COLLECTION,
"english_articles_collection": config.ENGLISH_ARTICLES_COLLECTION
}
result["status"] = "success" if not result["errors"] else "partial"
except ImportError as e:
result["errors"].append(f"Import error: {e}")
result["status"] = "failed"
except Exception as e:
result["errors"].append(f"Unexpected error: {e}")
result["status"] = "failed"
return result
def diagnose_chunking_and_embeddings(self) -> Dict[str, Any]:
"""Diagnose chunking strategy and embeddings generation."""
print("π Diagnosing Chunking and Embeddings...")
result = {"status": "unknown", "errors": [], "data": {}}
try:
from cve_factchecker.retriever import VectorNewsRetriever
from cve_factchecker.embeddings import build_embeddings
from langchain.text_splitter import RecursiveCharacterTextSplitter
from cve_factchecker.firebase_loader import FirebaseNewsLoader
# Test embeddings
print(" Testing embeddings generation...")
try:
embeddings = build_embeddings()
test_text = "This is a test sentence for embedding generation."
start_time = time.time()
test_embedding = embeddings.embed_query(test_text)
embedding_time = time.time() - start_time
result["data"]["embeddings"] = {
"model_loaded": True,
"embedding_dimension": len(test_embedding),
"generation_time_seconds": round(embedding_time, 3),
"sample_embedding_preview": test_embedding[:5] # First 5 values
}
except Exception as e:
result["errors"].append(f"Embeddings failed: {e}")
result["data"]["embeddings"] = {"model_loaded": False}
# Test chunking strategy
print(" Testing chunking strategy...")
try:
splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
# Get test content
loader = FirebaseNewsLoader()
articles = loader.fetch_english_articles(limit=1)
if articles:
test_article = articles[0]
chunks = splitter.split_text(test_article.content)
result["data"]["chunking"] = {
"strategy": "RecursiveCharacterTextSplitter",
"chunk_size": 1000,
"chunk_overlap": 200,
"test_article_length": len(test_article.content),
"chunks_created": len(chunks),
"chunk_lengths": [len(chunk) for chunk in chunks],
"avg_chunk_length": sum(len(chunk) for chunk in chunks) / len(chunks) if chunks else 0
}
else:
result["errors"].append("No test articles available for chunking test")
except Exception as e:
result["errors"].append(f"Chunking test failed: {e}")
result["status"] = "success" if not result["errors"] else "partial"
except ImportError as e:
result["errors"].append(f"Import error: {e}")
result["status"] = "failed"
except Exception as e:
result["errors"].append(f"Unexpected error: {e}")
result["status"] = "failed"
return result
def diagnose_vector_store(self) -> Dict[str, Any]:
"""Diagnose vector store operations and persistence."""
print("π Diagnosing Vector Store...")
result = {"status": "unknown", "errors": [], "data": {}}
try:
from cve_factchecker.retriever import VectorNewsRetriever
from cve_factchecker.firebase_loader import FirebaseNewsLoader
# Test vector store initialization
print(" Testing vector store initialization...")
try:
retriever = VectorNewsRetriever()
result["data"]["vector_store"] = {
"persist_directory": retriever.persist_directory,
"initialization": "success"
}
# Check current document count
try:
# Try to get document count
search_results = retriever.semantic_search("test", k=1)
result["data"]["current_documents"] = len(search_results) if search_results else 0
except:
result["data"]["current_documents"] = "unknown"
except Exception as e:
result["errors"].append(f"Vector store initialization failed: {e}")
result["data"]["vector_store"] = {"initialization": "failed"}
# Test complete ingestion process
print(" Testing complete ingestion process...")
try:
loader = FirebaseNewsLoader()
articles = loader.fetch_english_articles(limit=3)
if articles:
start_time = time.time()
retriever.store_articles_in_vector_db(articles, clear_first=True)
ingestion_time = time.time() - start_time
# Test search after ingestion
search_results = retriever.semantic_search("test", k=2)
result["data"]["ingestion_test"] = {
"articles_processed": len(articles),
"ingestion_time_seconds": round(ingestion_time, 3),
"searchable_chunks": len(search_results),
"ingestion_success": len(search_results) > 0
}
if search_results:
sample_result = search_results[0]
result["data"]["sample_search_result"] = {
"title": sample_result.get("title", "")[:50] + "...",
"content_length": len(sample_result.get("content", "")),
"has_url": bool(sample_result.get("url")),
"metadata_keys": list(sample_result.get("metadata", {}).keys())
}
else:
result["errors"].append("No articles available for ingestion test")
except Exception as e:
result["errors"].append(f"Ingestion test failed: {e}")
# Test persistence
print(" Testing vector store persistence...")
try:
persist_dir = result["data"]["vector_store"]["persist_directory"]
if persist_dir and os.path.exists(persist_dir):
files = os.listdir(persist_dir)
result["data"]["persistence"] = {
"directory_exists": True,
"files_count": len(files),
"files": files[:10] # First 10 files
}
else:
result["data"]["persistence"] = {
"directory_exists": False,
"using_memory_store": True
}
except Exception as e:
result["errors"].append(f"Persistence check failed: {e}")
result["status"] = "success" if not result["errors"] else "partial"
except ImportError as e:
result["errors"].append(f"Import error: {e}")
result["status"] = "failed"
except Exception as e:
result["errors"].append(f"Unexpected error: {e}")
result["status"] = "failed"
return result
def diagnose_fact_checking_pipeline(self) -> Dict[str, Any]:
"""Diagnose the complete fact-checking pipeline."""
print("π Diagnosing Fact-Checking Pipeline...")
result = {"status": "unknown", "errors": [], "data": {}}
try:
from cve_factchecker.orchestrator import FactCheckSystem
from cve_factchecker.config import load_openrouter_config
# Test system initialization
print(" Testing system initialization...")
try:
system = FactCheckSystem()
result["data"]["system_initialization"] = "success"
# Test configuration
config = load_openrouter_config()
result["data"]["config"] = {
"has_api_key": bool(config.api_key),
"model": config.model,
"max_tokens": config.max_tokens,
"temperature": config.temperature
}
except Exception as e:
result["errors"].append(f"System initialization failed: {e}")
result["data"]["system_initialization"] = "failed"
return result
# Test ingestion
print(" Testing Firebase ingestion...")
try:
start_time = time.time()
ingest_result = system.ingest_firebase(
collection="english_articles",
limit=5,
language="English"
)
ingest_time = time.time() - start_time
result["data"]["ingestion"] = {
"success": ingest_result.get("success", False),
"synced_count": ingest_result.get("synced", 0),
"time_seconds": round(ingest_time, 3),
"collection": ingest_result.get("collection"),
"error": ingest_result.get("error")
}
except Exception as e:
result["errors"].append(f"Ingestion test failed: {e}")
# Test fact-checking
print(" Testing fact-checking process...")
try:
test_claim = "Security researchers discovered a new vulnerability"
start_time = time.time()
fact_check_result = system.fact_check(test_claim)
fact_check_time = time.time() - start_time
result["data"]["fact_checking"] = {
"test_claim": test_claim,
"verdict": fact_check_result.get("verdict"),
"confidence": fact_check_result.get("confidence"),
"reasoning_length": len(fact_check_result.get("reasoning", "")),
"sources_used": fact_check_result.get("sources_used", 0),
"time_seconds": round(fact_check_time, 3),
"has_sources": len(fact_check_result.get("retrieved_articles", [])) > 0
}
except Exception as e:
result["errors"].append(f"Fact-checking test failed: {e}")
result["status"] = "success" if not result["errors"] else "partial"
except ImportError as e:
result["errors"].append(f"Import error: {e}")
result["status"] = "failed"
except Exception as e:
result["errors"].append(f"Unexpected error: {e}")
result["status"] = "failed"
return result
def diagnose_background_ingestion(self) -> Dict[str, Any]:
"""Diagnose background ingestion issues."""
print("π Diagnosing Background Ingestion...")
result = {"status": "unknown", "errors": [], "data": {}}
try:
# Check lock file issues
lock_file = "/tmp/ingest.lock" if os.name != 'nt' else "ingest.lock"
result["data"]["lock_file"] = {
"path": lock_file,
"exists": os.path.exists(lock_file),
"can_write_tmp": os.access("/tmp", os.W_OK) if os.path.exists("/tmp") else False
}
# Test lock mechanisms
if os.path.exists(lock_file):
try:
with open(lock_file, 'r') as f:
lock_content = f.read()
result["data"]["lock_content"] = lock_content
except:
result["data"]["lock_content"] = "unreadable"
# Test environment variables
result["data"]["environment"] = {
"AUTO_INGEST": os.environ.get("AUTO_INGEST", "not_set"),
"WERKZEUG_RUN_MAIN": os.environ.get("WERKZEUG_RUN_MAIN", "not_set"),
}
# Test threading
try:
import threading
result["data"]["threading"] = {
"active_threads": threading.active_count(),
"thread_names": [t.name for t in threading.enumerate()]
}
except Exception as e:
result["errors"].append(f"Threading check failed: {e}")
result["status"] = "success" if not result["errors"] else "partial"
except Exception as e:
result["errors"].append(f"Background ingestion diagnosis failed: {e}")
result["status"] = "failed"
return result
def run_complete_diagnosis(self) -> Dict[str, Any]:
"""Run complete system diagnosis."""
print("π₯ CVE Fact Checker - Complete System Diagnosis")
print("=" * 80)
# Run all diagnostic components
self.results["components"]["firebase"] = self.diagnose_firebase_connection()
self.results["components"]["chunking_embeddings"] = self.diagnose_chunking_and_embeddings()
self.results["components"]["vector_store"] = self.diagnose_vector_store()
self.results["components"]["fact_checking"] = self.diagnose_fact_checking_pipeline()
self.results["components"]["background_ingestion"] = self.diagnose_background_ingestion()
# Calculate overall status
component_statuses = [comp["status"] for comp in self.results["components"].values()]
if all(status == "success" for status in component_statuses):
self.results["overall_status"] = "healthy"
elif any(status == "success" for status in component_statuses):
self.results["overall_status"] = "partial"
else:
self.results["overall_status"] = "critical"
return self.results
def print_summary(self):
"""Print a human-readable summary of the diagnosis."""
print("\nπ Diagnosis Summary")
print("=" * 50)
overall = self.results.get("overall_status", "unknown")
print(f"Overall Status: {overall.upper()}")
for component, data in self.results["components"].items():
status = data["status"]
errors = len(data["errors"])
icon = "β
" if status == "success" else "β οΈ" if status == "partial" else "β"
print(f"{icon} {component.replace('_', ' ').title()}: {status} ({errors} errors)")
# Print critical errors
all_errors = []
for component, data in self.results["components"].items():
for error in data["errors"]:
all_errors.append(f"{component}: {error}")
if all_errors:
print(f"\nπ¨ Critical Issues Found:")
for error in all_errors[:10]: # Show first 10 errors
print(f" β’ {error}")
if len(all_errors) > 10:
print(f" ... and {len(all_errors) - 10} more")
def save_report(self, filename: str = "diagnosis_report.json"):
"""Save detailed diagnosis report to file."""
try:
with open(filename, 'w') as f:
json.dump(self.results, f, indent=2, default=str)
print(f"π Detailed report saved to: {filename}")
except Exception as e:
print(f"β Could not save report: {e}")
def main():
"""Main diagnostic function."""
print("π₯ CVE Fact Checker - Complete System Diagnostic")
print("=" * 80)
diagnostic = CVEFactCheckerDiagnostic()
results = diagnostic.run_complete_diagnosis()
diagnostic.print_summary()
diagnostic.save_report()
return results["overall_status"] in ["healthy", "partial"]
if __name__ == "__main__":
success = main()
sys.exit(0 if success else 1) |