File size: 23,056 Bytes
c293f7c | 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 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 | #!/usr/bin/env python3
"""
Misinformation Heatmap β Unified Server v2.0
Clean, single entry point. ML models load lazily in the background.
Run:
python server.py
"""
import os
import sys
import time
import asyncio
import logging
import sqlite3
import json
import threading
import torch
import traceback
from transformers import AutoTokenizer, AutoModel
from datetime import datetime
from pathlib import Path
from typing import Optional
# βββ PATH SETUP ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
ROOT_DIR = Path(__file__).parent
BACKEND_DIR = ROOT_DIR / "backend"
FRONTEND_DIR = ROOT_DIR / "frontend"
MAP_DIR = FRONTEND_DIR / "map"
DATA_DIR = ROOT_DIR / "data"
DATA_DIR.mkdir(exist_ok=True)
DB_PATH = DATA_DIR / "enhanced_fake_news.db"
sys.path.insert(0, str(BACKEND_DIR))
# Force IST timezone for all datetime.now() calls and log timestamps
import time as _time
os.environ.setdefault("TZ", "Asia/Kolkata")
# Apply on Unix systems (no-op on Windows, but Dockerfile ENV handles it there)
try:
_time.tzset()
except AttributeError:
pass # Windows doesn't have tzset()
# βββ EARLY IMPORTS (fast) ββββββββββββββββββββββββββββββββββββββββββββββββββββ
from fastapi import FastAPI, HTTPException, Query, Response
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import HTMLResponse, JSONResponse, StreamingResponse
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel
# βββ JSON SERIALIZATION HELPER ββββββββββββββββββββββββββββββββββββββββββββββββ
from datetime import datetime, date
def json_serial(obj):
"""JSON serializer for objects not serializable by default (datetime, date)."""
if isinstance(obj, (datetime, date)):
return obj.isoformat()
raise TypeError(f"Object of type {type(obj).__name__} is not JSON serializable")
def safe_json_dumps(data):
"""json.dumps with datetime support."""
return json.dumps(data, default=json_serial)
import uvicorn
# βββ COLORIZED LOGGING ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
class ColorFormatter(logging.Formatter):
"""ANSI-colored log formatter for readable server output."""
COLORS = {
logging.DEBUG: "\033[36m", # Cyan
logging.INFO: "\033[32m", # Green
logging.WARNING: "\033[33m", # Yellow
logging.ERROR: "\033[31m", # Red
logging.CRITICAL: "\033[35m", # Magenta
}
DIM = "\033[2m"
BOLD = "\033[1m"
RESET = "\033[0m"
LEVEL_LABELS = {
logging.DEBUG: "DBG",
logging.INFO: "INF",
logging.WARNING: "WRN",
logging.ERROR: "ERR",
logging.CRITICAL: "CRT",
}
def format(self, record: logging.LogRecord) -> str:
color = self.COLORS.get(record.levelno, "")
label = self.LEVEL_LABELS.get(record.levelno, record.levelname[:3])
ts = self.formatTime(record, "%H:%M:%S")
name = record.name.split(".")[-1][:18]
msg = record.getMessage()
return (
f"{self.DIM}{ts}{self.RESET} "
f"{color}{self.BOLD}[{label}]{self.RESET} "
f"{self.DIM}{name:>18}{self.RESET} "
f"{color if record.levelno >= logging.WARNING else ''}{msg}{self.RESET}"
)
def _setup_logging():
root = logging.getLogger()
root.setLevel(logging.INFO)
# Remove default handlers
root.handlers.clear()
handler = logging.StreamHandler()
handler.setFormatter(ColorFormatter())
root.addHandler(handler)
# Quieten noisy libraries
for noisy in ("httpx", "httpcore", "urllib3", "filelock", "watchfiles"):
logging.getLogger(noisy).setLevel(logging.WARNING)
_setup_logging()
logger = logging.getLogger("misinfo_heatmap")
# βββ LAZY ML STATE βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# These are populated in background threads so the server starts instantly.
_ml_ready = threading.Event()
_fake_detector = None
_proc_stats_fn = None
_ingestion_fn = None
_is_active_fn = None
_INDIAN_STATES = {}
def _load_ml_models():
"""Load heavy ML models in a background thread."""
global _fake_detector, _proc_stats_fn, _ingestion_fn, _is_active_fn, _INDIAN_STATES
try:
logger.info("β³ Loading backend modules (ML models initialising)β¦")
# Optimization: Temporarily increase threads for faster decompression/loading
original_threads = torch.get_num_threads()
torch.set_num_threads(min(4, os.cpu_count() or 1))
logger.info(f"β‘ Initialization speed-up: Using {torch.get_num_threads()} threads for loading.")
# 1. Realtime Processor & States
try:
from realtime_processor import get_processing_stats, INDIAN_STATES
_proc_stats_fn = get_processing_stats
_INDIAN_STATES = INDIAN_STATES
logger.info(f"β
realtime_processor loaded ({len(INDIAN_STATES)} states)")
except Exception as e:
logger.error(f"β Error loading realtime_processor: {e}")
# 2. Ingestion loop β start directly after ML models ready
try:
from massive_data_ingestion import high_volume_processing_loop, is_processing_active
_ingestion_fn = high_volume_processing_loop
_is_active_fn = is_processing_active
logger.info("β
massive_data_ingestion loop ready")
except Exception as e:
logger.warning(f"β οΈ massive_data_ingestion failed to load: {e}")
_ingestion_fn = None
# 3. Fake News Detector & Custom Ensemble
try:
from enhanced_fake_news_detector import fake_news_detector
_fake_detector = fake_news_detector
logger.info("β
enhanced_fake_news_detector loaded")
except Exception as e:
logger.error(f"β Error loading fake_news_detector: {e}")
# Revert to storage-saving mode for inference
torch.set_num_threads(1)
logger.info(f"π‘οΈ Safety-mode active: Reverted to {torch.get_num_threads()} thread for inference.")
except Exception as exc:
logger.error(f"β Critical ML model loading error: {exc}")
logger.error(traceback.format_exc())
finally:
_ml_ready.set() # unblock any waiter
def _get_processing_stats():
return _proc_stats_fn() if _proc_stats_fn else {"processing_active": False}
def _is_processing_active():
return _is_active_fn() if _is_active_fn else False
# βββ DATABASE HELPER βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Ingest β enhanced_fake_news.db (full article schema with title/state/verdict)
# API server reads from this SAME file β single source of truth.
DB_PATH = DATA_DIR / "enhanced_fake_news.db"
def get_db():
from db_adapter import get_db_connection
return get_db_connection(str(DB_PATH))
# βββ IN-MEMORY CACHE βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
_cache: dict = {}
def _cache_get(key: str, ttl: int):
if key in _cache:
data, ts = _cache[key]
if time.time() - ts < ttl:
return data
return None
def _cache_set(key: str, data):
_cache[key] = (data, time.time())
# βββ FASTAPI βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
app = FastAPI(
title="Misinformation Heatmap API",
description="Real-time misinformation detection across India",
version="2.0.0",
docs_url="/api/docs",
redoc_url="/api/redoc",
)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["GET", "POST"],
allow_headers=["*"],
)
# Static files
if MAP_DIR.exists():
app.mount("/map", StaticFiles(directory=str(MAP_DIR)), name="map")
assets_dir = FRONTEND_DIR / "assets"
if assets_dir.exists():
app.mount("/assets", StaticFiles(directory=str(assets_dir)), name="assets")
public_dir = ROOT_DIR / "public"
if public_dir.exists():
app.mount("/public", StaticFiles(directory=str(public_dir)), name="public")
# βββ LIFECYCLE βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
@app.on_event("startup")
async def on_startup():
logger.info("π Server listening β ML models loading in backgroundβ¦")
loop = asyncio.get_event_loop()
def _ml_then_ingest():
_load_ml_models() # blocks until models done
# Now that models are ready, schedule the ingestion loop on the event loop
if _ingestion_fn:
logger.info("π Scheduling ingestion loop on event loopβ¦")
asyncio.run_coroutine_threadsafe(_ingestion_fn(), loop)
else:
logger.warning("β οΈ No ingestion function registered β data will not update automatically.")
threading.Thread(target=_ml_then_ingest, daemon=True).start()
# βββ PYDANTIC MODELS βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
class AnalyzeRequest(BaseModel):
title: str = ""
content: str
source: str = ""
# βββ PAGE ROUTES βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def _read_html(name: str) -> str:
return (FRONTEND_DIR / name).read_text(encoding="utf-8")
@app.get("/", response_class=HTMLResponse, include_in_schema=False)
async def home():
return _read_html("index.html")
@app.get("/dashboard", response_class=HTMLResponse, include_in_schema=False)
async def dashboard():
return _read_html("dashboard.html")
# βββ API: STATS ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
@app.get("/api/v1/stats", tags=["Analytics"])
async def get_stats(response: Response = None):
"""Aggregate stats for the last 24 hours (30 s cache)."""
if response:
response.headers["Cache-Control"] = "public, max-age=30"
cached = _cache_get("stats", 30)
if cached:
return cached
total = fake_n = real_n = uncertain = 0
try:
with get_db() as conn:
row = conn.execute("""
SELECT
COUNT(*) AS total,
SUM(CASE WHEN fake_news_verdict = 'fake' THEN 1 ELSE 0 END) AS fake,
SUM(CASE WHEN fake_news_verdict = 'real' THEN 1 ELSE 0 END) AS real,
SUM(CASE WHEN fake_news_verdict = 'uncertain' THEN 1 ELSE 0 END) AS uncertain
FROM events
WHERE timestamp > datetime('now', '-24 hours')
""").fetchone()
total, fake_n, real_n, uncertain = (row[k] or 0 for k in ("total","fake","real","uncertain"))
except Exception as exc:
logger.error(f"Stats DB error: {exc}")
result = {
"total_events": total,
"fake_events": fake_n,
"real_events": real_n,
"uncertain_events": uncertain,
"processing_active": _is_processing_active(),
"classification_accuracy": 0.91 if total > 0 else 0.5,
"system_status": "LIVE" if _is_processing_active() else "READY",
"total_states": len(_INDIAN_STATES) or 36,
"ml_ready": _ml_ready.is_set(),
"last_updated": datetime.now().isoformat(),
}
_cache_set("stats", result)
return result
# βββ API: HEATMAP DATA βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
@app.get("/api/v1/heatmap/data", tags=["Analytics"])
async def get_heatmap_data(response: Response = None, days: int = Query(7, ge=1, le=30)):
"""State-wise misinformation event counts (60 s cache)."""
if response:
response.headers["Cache-Control"] = "public, max-age=60"
cache_key = f"heatmap_{days}"
cached = _cache_get(cache_key, 60)
if cached:
return cached
rows = []
try:
with get_db() as conn:
rows = conn.execute(f"""
SELECT
state,
COUNT(*) AS event_count,
AVG(fake_news_confidence) AS avg_confidence,
SUM(CASE WHEN fake_news_verdict = 'fake' THEN 1 ELSE 0 END) AS fake_count,
SUM(CASE WHEN fake_news_verdict = 'real' THEN 1 ELSE 0 END) AS real_count
FROM events
WHERE state IS NOT NULL
AND timestamp > datetime('now', '-{days} days')
GROUP BY state
ORDER BY event_count DESC
LIMIT 50
""").fetchall()
except Exception as exc:
logger.error(f"Heatmap DB error: {exc}")
heatmap = []
for r in rows:
count = r["event_count"] or 0
fake_c = r["fake_count"] or 0
ratio = fake_c / count if count else 0
if count < 5:
risk = "insufficient_data"
elif ratio > 0.15:
risk = "high"
elif ratio > 0.08:
risk = "medium"
elif ratio > 0.03:
risk = "low_medium"
else:
risk = "low"
heatmap.append({
"state": r["state"],
"event_count": count,
"fake_probability": round(ratio, 4),
"ai_confidence": round(r["avg_confidence"] or 0.0, 3),
"fake_count": fake_c,
"real_count": r["real_count"] or 0,
"fake_ratio": round(ratio, 4),
"risk_level": risk,
})
result = {"heatmap_data": heatmap, "total_states": len(heatmap)}
_cache_set(cache_key, result)
return result
# βββ API: LIVE EVENTS ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
@app.get("/api/v1/events/live", tags=["Events"])
async def get_live_events(response: Response = None, limit: int = Query(10, ge=1, le=100)):
"""Recent events from the last hour."""
if response:
response.headers["Cache-Control"] = "public, max-age=30"
cache_key = f"live_events_{limit}"
cached = _cache_get(cache_key, 30)
if cached:
return cached
rows = []
try:
with get_db() as conn:
rows = conn.execute("""
SELECT title, SUBSTR(content, 1, 150) as content, source, state,
fake_news_confidence, fake_news_verdict, timestamp
FROM events
WHERE timestamp > datetime('now', '-24 hours')
ORDER BY timestamp DESC
LIMIT ?
""", (limit,)).fetchall()
except Exception as exc:
logger.error(f"Live events error: {exc}")
events = []
for r in rows:
body = r["content"] or ""
events.append({
"title": (r["title"] or "Processingβ¦")[:120],
"content": body[:200] + ("β¦" if len(body) > 200 else ""),
"source": r["source"] or "Unknown",
"state": r["state"] or "India",
"fake_probability": round(r["fake_news_confidence"] or 0.5, 2),
"classification": r["fake_news_verdict"] or "uncertain",
"confidence": round(r["fake_news_confidence"] or 0.5, 2),
"timestamp": r["timestamp"].isoformat() if hasattr(r["timestamp"], "isoformat") else str(r["timestamp"] or ""),
})
result = {
"events": events,
"total_count": len(events),
"processing_active": _is_processing_active(),
}
_cache_set(cache_key, result)
return result
# βββ API: SSE STREAM βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
@app.get("/api/v1/stream", tags=["Events"])
async def sse_stream():
"""Server-Sent Events for real-time dashboard updates."""
async def event_generator():
while True:
stats = await get_stats()
yield f"event: stats\ndata: {safe_json_dumps(stats)}\n\n"
events_data = await get_live_events(limit=12)
yield f"event: live_events\ndata: {safe_json_dumps(events_data)}\n\n"
await asyncio.sleep(5)
return StreamingResponse(event_generator(), media_type="text/event-stream")
# βββ API: STATE EVENTS βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
@app.get("/api/v1/events/state/{state}", tags=["Events"])
async def get_state_events(state: str, response: Response = None, limit: int = Query(10, ge=1, le=50)):
if response:
response.headers["Cache-Control"] = "public, max-age=30"
cache_key = f"state_events_{state}_{limit}"
cached = _cache_get(cache_key, 30)
if cached:
return cached
rows = []
try:
with get_db() as conn:
rows = conn.execute("""
SELECT title, SUBSTR(content, 1, 150) as content, source,
fake_news_confidence, fake_news_verdict, timestamp
FROM events WHERE state = ?
ORDER BY timestamp DESC LIMIT ?
""", (state, limit)).fetchall()
except Exception as exc:
logger.error(f"State events error [{state}]: {exc}")
events = []
for r in rows:
body = r["content"] or ""
events.append({
"title": r["title"] or "Processingβ¦",
"content": body[:200] + ("β¦" if len(body) > 200 else ""),
"source": r["source"] or "Unknown",
"fake_probability": round(r["fake_news_confidence"] or 0.5, 2),
"classification": r["fake_news_verdict"] or "uncertain",
"confidence": round(r["fake_news_confidence"] or 0.5, 2),
"timestamp": r["timestamp"],
})
result = {"state": state, "events": events, "total_count": len(events)}
_cache_set(cache_key, result)
return result
# βββ API: ANALYZE ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
@app.post("/api/v1/analyze", tags=["Analysis"])
async def analyze_article(req: AnalyzeRequest):
"""Submit a news article for misinformation analysis."""
if not req.content.strip():
raise HTTPException(status_code=400, detail="'content' is required")
if _fake_detector is None:
# Return 503 if models still loading
if not _ml_ready.is_set():
raise HTTPException(status_code=503, detail="ML models are still loading, try again in a moment")
raise HTTPException(status_code=503, detail="Analysis engine unavailable")
try:
result = _fake_detector.analyze_article(req.title, req.content, req.source)
return {
"fake_probability": result.get("fake_probability", 0.5),
"classification": result.get("classification", "uncertain"),
"confidence": result.get("confidence", 0.5),
"analysis_components": result.get("components", {}),
"processing_time_ms": result.get("processing_time", 0.0),
}
except Exception as exc:
logger.error(f"Analysis error: {exc}")
raise HTTPException(status_code=500, detail="Analysis failed")
# βββ API: HEALTH βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
@app.get("/health", tags=["System"])
async def health():
db_ok = False
try:
with get_db() as conn:
conn.execute("SELECT 1")
db_ok = True
except Exception:
pass
return {
"status": "healthy" if db_ok else "degraded",
"version": "2.0.0",
"database": "connected" if db_ok else "error",
"ml_models_ready": _ml_ready.is_set(),
"processing_active": _is_processing_active(),
"states_covered": len(_INDIAN_STATES) or 36,
"timestamp": datetime.now().isoformat(),
}
# βββ ENTRYPOINT ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
if __name__ == "__main__":
W, G, S, R = "\033[1;37m", "\033[1;32m", "\033[1;33m", "\033[0m"
print()
print(f" {W}+------------------------------------------+{R}")
print(f" {W}| {S}Misinformation Heatmap v2.0.0{W} |{R}")
print(f" {W}| {G}ML models load lazily in background{W} |{R}")
print(f" {W}+------------------------------------------+{R}")
print(f" {W}| {G}Home {W}-> {S}http://localhost:8000{W} |{R}")
print(f" {W}| {G}Dashboard{W}-> {S}http://localhost:8000/dashboard{W} |{R}")
print(f" {W}| {G}Heatmap {W}-> {S}http://localhost:8000/map/...{W} |{R}")
print(f" {W}| {G}API Docs {W}-> {S}http://localhost:8000/api/docs{W} |{R}")
print(f" {W}+------------------------------------------+{R}")
print()
uvicorn.run(app, host="0.0.0.0", port=8000, log_level="warning")
|