Spaces:
Running
Running
File size: 14,397 Bytes
ee7d7b9 | 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 | """
🧠 AUTONOMOUS BRAIN API - DataVision Auto-Analysis Endpoint
============================================================
Drop ANY file → Get complete analysis in seconds.
Endpoints:
- POST /auto-analyze - Analyze uploaded file
- GET /brain/status - Get brain status
- POST /brain/insights - Generate AI insights
"""
import logging
from typing import Optional
from fastapi import APIRouter, HTTPException, UploadFile, File, Header
from pydantic import BaseModel, Field
import pandas as pd
import io
logger = logging.getLogger(__name__)
router = APIRouter()
# =============================================================================
# SECURITY HELPER - JWT Authentication
# =============================================================================
def get_secure_user_id(body_user_id: str, x_user_id: Optional[str], authorization: Optional[str]) -> str:
"""
Get verified user_id from JWT token or headers.
Priority: JWT token > X-User-ID header > Body data
"""
# 1. Try JWT token first (most secure)
if authorization:
try:
token = authorization.replace("Bearer ", "")
from core.auth import decode_jwt_token
payload = decode_jwt_token(token)
if payload and payload.get("sub"):
return payload["sub"]
except Exception as e:
logger.debug(f"JWT decode failed: {e}")
# 2. Try X-User-ID header (from authenticated frontend)
if x_user_id and x_user_id != "default":
return x_user_id
# 3. Fallback to body data (least secure)
if body_user_id and body_user_id != "default":
logger.warning(f"Using body user_id: {body_user_id} - consider using JWT")
return body_user_id
# 4. Generate guest fingerprint
import hashlib
import time
return f"guest_{hashlib.md5(str(time.time()).encode()).hexdigest()[:8]}"
# =============================================================================
# REQUEST/RESPONSE MODELS
# =============================================================================
class AnalysisRequest(BaseModel):
"""Request for analysis"""
user_id: str
file_name: Optional[str] = "data"
generate_insights: bool = True
class ColumnInfo(BaseModel):
"""Column information"""
name: str
type: str
null_percentage: float
unique_count: int
issues: list = []
fix_suggestions: list = []
stats: Optional[dict] = None
top_values: Optional[dict] = None
class RelationshipInfo(BaseModel):
"""Relationship between columns"""
column1: str
column2: str
type: str
strength: float
description: str
class InsightInfo(BaseModel):
"""AI-generated insight"""
title: str
description: str
importance: str
category: str
visualization: Optional[str] = None
class AnalysisResponse(BaseModel):
"""Response from brain analysis"""
success: bool
file_name: str
analysis_time_ms: int
# Overview
total_rows: int
total_columns: int
memory_mb: float
# Quality
quality_score: float
quality_level: str
quality_issues: list
# Details
columns: list
relationships: list
insights: list
suggested_charts: list
# Summary
summary: str
class QuickInsightRequest(BaseModel):
"""Request for quick insights"""
user_id: str
question: str
context: Optional[str] = None
# =============================================================================
# ENDPOINTS
# =============================================================================
@router.post("/auto-analyze", response_model=AnalysisResponse)
async def auto_analyze_file(
file: UploadFile = File(...),
user_id: str = "default",
x_user_id: Optional[str] = Header(None, alias="X-User-ID"),
authorization: Optional[str] = Header(None, alias="Authorization")
):
"""
🚀 Drop ANY file and get complete autonomous analysis - SECURED
Supports: CSV, Excel (.xlsx, .xls), JSON
Returns:
- Complete data profile
- Quality score with fix suggestions
- Column relationships
- AI-generated insights
- Visualization recommendations
"""
try:
# SECURITY: Get verified user_id from JWT
secure_user_id = get_secure_user_id(user_id, x_user_id, authorization)
# Read file content
content = await file.read()
file_name = file.filename or "uploaded_data"
# Detect file type and load DataFrame
df = await _load_dataframe(content, file_name)
if df is None or len(df) == 0:
raise HTTPException(status_code=400, detail="Could not parse file or file is empty")
# Import brain and analyze
from core.autonomous_brain import get_brain
brain = get_brain()
analysis = await brain.analyze(df, file_name, generate_insights=True)
result = brain.to_dict(analysis)
return AnalysisResponse(
success=True,
file_name=result["file_name"],
analysis_time_ms=result["analysis_duration_ms"],
total_rows=result["total_rows"],
total_columns=result["total_columns"],
memory_mb=result["memory_usage_mb"],
quality_score=result["quality_score"],
quality_level=result["quality_level"],
quality_issues=result["quality_issues"],
columns=result["columns"],
relationships=result["relationships"],
insights=result["insights"],
suggested_charts=result["suggested_charts"],
summary=result["summary"]
)
except HTTPException:
raise
except Exception as e:
logger.error(f"Auto-analyze error: {e}")
raise HTTPException(status_code=500, detail=str(e))
@router.post("/analyze-dataframe")
async def analyze_existing_data(
request: AnalysisRequest,
x_user_id: Optional[str] = Header(None, alias="X-User-ID"),
authorization: Optional[str] = Header(None, alias="Authorization")
):
"""
Analyze data already uploaded by user - SECURED
"""
try:
# SECURITY: Get verified user_id from JWT
secure_user_id = get_secure_user_id(request.user_id, x_user_id, authorization)
from utils.paths import get_user_paths
import os
paths = get_user_paths(secure_user_id)
# Find user's data file
data_file = None
for ext in ['.csv', '.xlsx', '.json']:
potential_file = os.path.join(paths["uploads"], f"data{ext}")
if os.path.exists(potential_file):
data_file = potential_file
break
if not data_file:
# Look for any file
if os.path.exists(paths["uploads"]):
files = os.listdir(paths["uploads"])
if files:
data_file = os.path.join(paths["uploads"], files[0])
if not data_file:
raise HTTPException(status_code=404, detail="No data found. Please upload a file first.")
# Load DataFrame
df = await _load_dataframe_from_path(data_file)
if df is None:
raise HTTPException(status_code=400, detail="Could not load data file")
# Analyze
from core.autonomous_brain import get_brain
brain = get_brain()
analysis = await brain.analyze(df, request.file_name, request.generate_insights)
result = brain.to_dict(analysis)
return {
"success": True,
**result
}
except HTTPException:
raise
except Exception as e:
logger.error(f"Analyze dataframe error: {e}")
raise HTTPException(status_code=500, detail=str(e))
@router.post("/quick-insight")
async def get_quick_insight(request: QuickInsightRequest):
"""
Get quick AI insight about a specific question
"""
try:
from core.reasoning_engine import reason, ReasoningMode
result = await reason(
query=request.question,
context=request.context or "",
mode=ReasoningMode.CHAIN_OF_THOUGHT
)
return {
"success": True,
"question": request.question,
"answer": result.final_answer,
"confidence": result.confidence,
"reasoning_steps": [
{"type": step.step_type, "content": step.content}
for step in result.steps
],
"reasoning_time_ms": result.reasoning_time_ms
}
except Exception as e:
logger.error(f"Quick insight error: {e}")
raise HTTPException(status_code=500, detail=str(e))
@router.post("/root-cause")
async def analyze_root_cause(
user_id: str,
target_column: str,
question: str,
time_column: Optional[str] = None
):
"""
Perform root cause analysis on data
"""
try:
# Load user's data
df = await _load_user_data(user_id)
if df is None:
raise HTTPException(status_code=404, detail="No data found")
if target_column not in df.columns:
raise HTTPException(status_code=400, detail=f"Column '{target_column}' not found")
from mcp.advanced_mcps import analyze_root_cause
result = await analyze_root_cause(df, target_column, question, time_column)
return {"success": True, **result}
except HTTPException:
raise
except Exception as e:
logger.error(f"Root cause error: {e}")
raise HTTPException(status_code=500, detail=str(e))
@router.post("/segment")
async def segment_data(
user_id: str,
n_segments: Optional[int] = None,
features: Optional[str] = None # Comma-separated
):
"""
Segment data using AI clustering
"""
try:
df = await _load_user_data(user_id)
if df is None:
raise HTTPException(status_code=404, detail="No data found")
feature_list = features.split(",") if features else None
from mcp.advanced_mcps import segment_data
result = await segment_data(df, feature_list, n_segments)
return {"success": True, **result}
except HTTPException:
raise
except Exception as e:
logger.error(f"Segmentation error: {e}")
raise HTTPException(status_code=500, detail=str(e))
@router.post("/trends")
async def detect_trends(
user_id: str,
time_column: Optional[str] = None
):
"""
Detect trends and anomalies in data
"""
try:
df = await _load_user_data(user_id)
if df is None:
raise HTTPException(status_code=404, detail="No data found")
from mcp.advanced_mcps import detect_trends
result = await detect_trends(df, time_column)
return {"success": True, **result}
except HTTPException:
raise
except Exception as e:
logger.error(f"Trend detection error: {e}")
raise HTTPException(status_code=500, detail=str(e))
@router.get("/brain/status")
async def get_brain_status():
"""
Get the status of the Autonomous Brain
"""
try:
from core.autonomous_brain import get_brain
brain = get_brain()
return {
"status": "active",
"cached_analyses": len(brain.analysis_cache),
"version": "1.0.0",
"capabilities": [
"auto_profiling",
"quality_scoring",
"relationship_discovery",
"ai_insights",
"visualization_suggestions",
"root_cause_analysis",
"segmentation",
"trend_detection"
]
}
except Exception as e:
logger.error(f"Brain status error: {e}")
return {"status": "error", "error": str(e)}
# =============================================================================
# HELPER FUNCTIONS
# =============================================================================
async def _load_dataframe(content: bytes, filename: str) -> Optional[pd.DataFrame]:
"""Load DataFrame from file content"""
try:
filename_lower = filename.lower()
if filename_lower.endswith('.csv'):
return pd.read_csv(io.BytesIO(content))
elif filename_lower.endswith(('.xlsx', '.xls')):
return pd.read_excel(io.BytesIO(content))
elif filename_lower.endswith('.json'):
return pd.read_json(io.BytesIO(content))
else:
# Try CSV as default
return pd.read_csv(io.BytesIO(content))
except Exception as e:
logger.error(f"Error loading DataFrame: {e}")
return None
async def _load_dataframe_from_path(path: str) -> Optional[pd.DataFrame]:
"""Load DataFrame from file path"""
try:
path_lower = path.lower()
if path_lower.endswith('.csv'):
return pd.read_csv(path)
elif path_lower.endswith(('.xlsx', '.xls')):
return pd.read_excel(path)
elif path_lower.endswith('.json'):
return pd.read_json(path)
else:
return pd.read_csv(path)
except Exception as e:
logger.error(f"Error loading DataFrame from path: {e}")
return None
async def _load_user_data(user_id: str) -> Optional[pd.DataFrame]:
"""Load user's uploaded data"""
try:
from utils.paths import get_user_paths
import os
paths = get_user_paths(user_id)
uploads_dir = paths.get("uploads", "")
if not os.path.exists(uploads_dir):
return None
# Find first data file
for filename in os.listdir(uploads_dir):
filepath = os.path.join(uploads_dir, filename)
if os.path.isfile(filepath):
return await _load_dataframe_from_path(filepath)
return None
except Exception as e:
logger.error(f"Error loading user data: {e}")
return None
|