Spaces:
Runtime error
Runtime error
Download app.py from dharshannn/Aurora: direct link, hf CLI and curl.
- Browser
- Download file 15.6 kB
-
https://huggingface.co/spaces/dharshannn/Aurora/resolve/main/app.py
- Command line
-
hf download hf://spaces/dharshannn/Aurora/app.py
-
curl -L -o app.py https://huggingface.co/spaces/dharshannn/Aurora/resolve/main/app.py
15.6 kB
| from fastapi import FastAPI, HTTPException | |
| from fastapi.responses import StreamingResponse | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from pydantic import BaseModel | |
| from typing import Any, Dict | |
| import uuid, time, orjson, json | |
| from sqlalchemy import text, select, func | |
| import os | |
| from settings import settings | |
| from audit_store import init_db, now_ts, hmac_sha256, preview | |
| from audit_async import audit_enqueue | |
| from registry import execute_agent | |
| from orchestrator import route as route_agent | |
| app = FastAPI(title="Aurora API") | |
| # Add CORS middleware for demo deployment | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], # Allow all origins for demo - restrict in production | |
| allow_credentials=False, # Changed to False to allow wildcard origins | |
| allow_methods=["*"], # Allow all HTTP methods | |
| allow_headers=["*"], # Allow all headers | |
| ) | |
| # Add additional CORS headers for HuggingFace Spaces | |
| async def add_cors_headers(request, call_next): | |
| response = await call_next(request) | |
| response.headers["Access-Control-Allow-Origin"] = "*" | |
| response.headers["Access-Control-Allow-Methods"] = "GET, POST, PUT, DELETE, OPTIONS" | |
| response.headers["Access-Control-Allow-Headers"] = "*" | |
| response.headers["Access-Control-Allow-Credentials"] = "false" # Changed to false | |
| return response | |
| class ExecReq(BaseModel): | |
| agent_id: str | |
| org_id: str = "demo_org" | |
| user_id: str = "demo_user" | |
| input: Dict[str, Any] = {} | |
| consent: bool = True | |
| class OrchestrateReq(BaseModel): | |
| action: str = "" # "faq" | "plan" | "progress" | |
| org_id: str = "demo_org" | |
| user_id: str = "demo_user" | |
| input: Dict[str, Any] = {} | |
| consent: bool = True | |
| class StreamReq(BaseModel): | |
| msg: str | |
| org_id: str = "demo_org" | |
| user_id: str = "demo_user" | |
| consent: bool = True | |
| def startup(): | |
| """Application startup with logging""" | |
| print("π Aurora Backend starting up...") | |
| # Initialize database (this will log dialect info) | |
| init_db() | |
| # Initialize vector store with auto-ingestion | |
| from rag import initialize_vectorstore_with_auto_ingest | |
| initialize_vectorstore_with_auto_ingest() | |
| print("β Aurora Backend startup complete") | |
| def healthz(): | |
| """Health check endpoint for monitoring - returns JSON with DB ping""" | |
| try: | |
| # Test database connection | |
| from db import engine | |
| with engine.connect() as conn: | |
| conn.execute(text("SELECT 1")) | |
| db_status = "connected" | |
| except Exception as e: | |
| error_msg = str(e).lower() | |
| if "readonly" in error_msg or "read-only" in error_msg: | |
| db_status = "connected (read-only)" | |
| print(f"Database is read-only: {e}") | |
| else: | |
| db_status = f"error: {str(e)}" | |
| print(f"Database connection error: {e}") | |
| # Check vector store status | |
| try: | |
| from rag import get_vectorstore_info | |
| vector_info = get_vectorstore_info() | |
| except Exception as e: | |
| print(f"Vector store check error: {e}") | |
| vector_info = { | |
| "vector_store": "qdrant", | |
| "vector_ok": False, | |
| "vector_docs": 0, | |
| "vector_collection": "unknown" | |
| } | |
| return { | |
| "ok": True, | |
| "env": settings.ENV, | |
| "database": db_status, | |
| **vector_info, | |
| "timestamp": time.time() | |
| } | |
| async def options_handler(path: str): | |
| """Handle CORS preflight requests""" | |
| return {"message": "CORS preflight handled"} | |
| def agents_execute(req: ExecReq): | |
| if not req.consent: | |
| return {"status":"ok","output":{"answer":"Consent required."},"meta":{}} | |
| trace_id = str(uuid.uuid4()) | |
| t0 = time.time() | |
| try: | |
| output, meta = execute_agent(req.agent_id, req.dict()) | |
| except KeyError as e: | |
| raise HTTPException(404, str(e)) | |
| latency_ms = int((time.time()-t0)*1000) | |
| meta = {**meta, "agent_id": req.agent_id, "trace_id": trace_id, "latency_ms": latency_ms} | |
| ans_text = "" | |
| if isinstance(output, dict): | |
| ans_text = output.get("answer") or output.get("summary") or orjson.dumps(output).decode()[:400] | |
| event = { | |
| "ts": now_ts(), | |
| "trace_id": trace_id, | |
| "env": settings.ENV, | |
| "org_id": req.org_id, | |
| "user_id": req.user_id, | |
| "agent_id": req.agent_id, | |
| "action": req.agent_id, | |
| "consent": req.consent, | |
| "question": req.input.get("question",""), | |
| "retrieved_docs": [], | |
| "citations": output.get("citations") if isinstance(output, dict) else None, | |
| "answer_preview": preview(ans_text), | |
| "answer_hash": hmac_sha256(ans_text), | |
| "model_id": settings.OPENAI_MODEL, | |
| "model_params": {"provider": "openai"}, | |
| "index_version": settings.INDEX_VERSION, | |
| "latency_ms": latency_ms, | |
| "tokens_in": 0, | |
| "tokens_out": 0, | |
| "pii_redactions": 0, | |
| "decision_flags": {"routed": True} | |
| } | |
| audit_enqueue(event) | |
| return {"status":"ok","output":output,"meta":meta} | |
| def aurora_orchestrate(req: OrchestrateReq): | |
| if not req.consent: | |
| return {"status":"ok","output":{"answer":"Consent required."},"meta":{}} | |
| agent_id = route_agent(req.dict()) | |
| return agents_execute(ExecReq(agent_id=agent_id, org_id=req.org_id, user_id=req.user_id, input=req.input, consent=req.consent)) | |
| def audit_count(): | |
| from backend.audit_store import SessionLocal, AuditEvent | |
| with SessionLocal() as s: | |
| result = s.execute(select(func.count(AuditEvent.id))) | |
| return {"count": result.scalar()} | |
| def audit_export(): | |
| from backend.audit_store import SessionLocal, AuditEvent | |
| with SessionLocal() as s: | |
| result = s.execute(select(AuditEvent).order_by(AuditEvent.ts.desc()).limit(500)) | |
| rows = result.scalars().all() | |
| return [ | |
| {"ts": r.ts, "trace_id": r.trace_id, "agent_id": r.agent_id, "answer_preview": r.answer_preview, "latency_ms": r.latency_ms} | |
| for r in rows | |
| ] | |
| def admin_ingest(): | |
| """Admin endpoint to manually trigger data ingestion.""" | |
| # Check if admin access is enabled | |
| if os.getenv("ALLOW_ADMIN", "0").strip() != "1": | |
| raise HTTPException(status_code=403, detail="Admin access not enabled") | |
| try: | |
| from rag import ingest_data_corpus, get_document_count | |
| # Get count before ingestion | |
| count_before = get_document_count() | |
| # Run ingestion | |
| success = ingest_data_corpus() | |
| # Get count after ingestion | |
| count_after = get_document_count() | |
| return { | |
| "success": success, | |
| "count_before": count_before, | |
| "count_after": count_after, | |
| "documents_added": count_after - count_before if count_after and count_before else "unknown", | |
| "message": f"Ingestion completed. Documents: {count_before} β {count_after}" | |
| } | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=f"Ingestion failed: {str(e)}") | |
| def admin_reindex(): | |
| """Admin endpoint to rebuild vector store on demand.""" | |
| # Check if admin access is enabled | |
| if os.getenv("ALLOW_ADMIN", "0").strip() != "1": | |
| raise HTTPException(status_code=403, detail="Admin access not enabled") | |
| try: | |
| from rag import ingest_data_corpus, get_document_count | |
| # Force re-initialization of vector store | |
| from rag import _vectorstore | |
| globals()['_vectorstore'] = None | |
| # Run ingestion | |
| success = ingest_data_corpus() | |
| # Get new document count | |
| doc_count = get_document_count() | |
| return { | |
| "success": success, | |
| "doc_count": doc_count, | |
| "message": f"Reindex completed. Document count: {doc_count}" | |
| } | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=f"Reindex failed: {str(e)}") | |
| def stream_response(text: str): | |
| """Stream text token by token for better UX""" | |
| words = text.split() | |
| for i, word in enumerate(words): | |
| if i == 0: | |
| yield word | |
| else: | |
| yield " " + word | |
| time.sleep(0.05) # Small delay for streaming effect | |
| def welcome_stream(req: StreamReq): | |
| """Streaming endpoint for Welcome Agent - matches frontend expectations""" | |
| print(f"π Welcome Stream Request - Message: '{req.msg}'") | |
| print(f"π Request details - org_id: {req.org_id}, user_id: {req.user_id}, consent: {req.consent}") | |
| print(f"π Request type: {type(req)}") | |
| print(f"π Request dict: {req.dict()}") | |
| if not req.consent: | |
| print("β Consent not provided - returning error") | |
| def error_stream(): | |
| yield "Consent required to proceed." | |
| return StreamingResponse(error_stream(), media_type="text/plain") | |
| # Route to onboarding agent | |
| payload = { | |
| "org_id": req.org_id, | |
| "user_id": req.user_id, | |
| "input": {"question": req.msg}, | |
| "consent": req.consent | |
| } | |
| print(f"π Payload to onboarding agent: {payload}") | |
| try: | |
| print("π Calling onboarding agent...") | |
| output, meta = execute_agent("onboarding", payload) | |
| answer = output.get("answer", "I couldn't generate a response.") | |
| print(f"β Onboarding agent response received (length: {len(answer)} chars)") | |
| print(f"π Final answer preview: {answer[:200]}...") | |
| return StreamingResponse(stream_response(answer), media_type="text/plain") | |
| except Exception as e: | |
| error_msg = str(e) | |
| print(f"β Error in welcome stream: {error_msg}") | |
| import traceback | |
| traceback.print_exc() | |
| def error_stream(): | |
| yield f"Error: {error_msg}" | |
| return StreamingResponse(error_stream(), media_type="text/plain") | |
| def skillnav_stream(req: StreamReq): | |
| """Streaming endpoint for Skill Navigator Agent""" | |
| if not req.consent: | |
| def error_stream(): | |
| yield "Consent required to proceed." | |
| return StreamingResponse(error_stream(), media_type="text/plain") | |
| # Parse skill navigation request from message | |
| payload = { | |
| "org_id": req.org_id, | |
| "user_id": req.user_id, | |
| "input": {"question": req.msg}, | |
| "consent": req.consent | |
| } | |
| try: | |
| print(f"π§ Skill Navigator - Question: '{req.msg}'") | |
| output, meta = execute_agent("skillnav", payload) | |
| print(f"π§ Skill Navigator output keys: {list(output.keys())}") | |
| # Format the AI-powered plan as readable text | |
| plan = output.get("plan_30d", []) | |
| explainability = output.get("explainability", "AI-generated learning plan") | |
| ai_insights = output.get("ai_insights", "") | |
| print(f"π§ Plan has {len(plan)} weeks") | |
| print(f"π§ Explainability: {explainability[:100]}...") | |
| print(f"π§ AI Insights length: {len(ai_insights)}") | |
| def plan_stream(): | |
| yield f"{explainability}\n\n" | |
| for week_plan in plan: | |
| week_num = week_plan.get('week', '?') | |
| week_title = week_plan.get('title', '') | |
| if week_title: | |
| yield f"Week {week_num}: {week_title}\n" | |
| else: | |
| yield f"Week {week_num}:\n" | |
| # Goals | |
| goals = week_plan.get('goals', []) | |
| if goals: | |
| yield "π― Goals:\n" | |
| for goal in goals: | |
| if isinstance(goal, str): | |
| yield f" β’ {goal}\n" | |
| else: | |
| yield f" β’ {str(goal)}\n" | |
| # Technologies | |
| technologies = week_plan.get('technologies', []) | |
| if technologies: | |
| yield f"π» Key Technologies: {', '.join(technologies)}\n" | |
| # Resources | |
| resources = week_plan.get('resources', []) | |
| if resources: | |
| yield "π Resources:\n" | |
| for res in resources[:3]: # Limit to 3 resources per week | |
| if isinstance(res, str): | |
| yield f" β’ {res}\n" | |
| elif isinstance(res, dict): | |
| yield f" β’ {res.get('title', str(res))}\n" | |
| else: | |
| yield f" β’ {str(res)}\n" | |
| # Projects | |
| projects = week_plan.get('projects', []) | |
| if projects: | |
| yield "π Projects:\n" | |
| for project in projects[:2]: # Limit to 2 projects per week | |
| if isinstance(project, str): | |
| yield f" β’ {project}\n" | |
| else: | |
| yield f" β’ {str(project)}\n" | |
| # Project connection | |
| project_connection = week_plan.get('project_connection', '') | |
| if project_connection: | |
| yield f"π Project Connection: {project_connection}\n" | |
| yield "\n" | |
| if ai_insights: | |
| # Don't truncate AI insights - show full content | |
| yield f"π€ AI Insights:\n{ai_insights}\n" | |
| return StreamingResponse(plan_stream(), media_type="text/plain") | |
| except Exception as e: | |
| error_msg = str(e) # Capture the error message | |
| def error_stream(): | |
| yield f"Error generating learning plan: {error_msg}\n" | |
| yield "Please try rephrasing your question or try again later." | |
| return StreamingResponse(error_stream(), media_type="text/plain") | |
| def progress_stream(req: StreamReq): | |
| """Streaming endpoint for Progress Companion Agent""" | |
| if not req.consent: | |
| def error_stream(): | |
| yield "Consent required to proceed." | |
| return StreamingResponse(error_stream(), media_type="text/plain") | |
| payload = { | |
| "org_id": req.org_id, | |
| "user_id": req.user_id, | |
| "input": {"question": req.msg}, | |
| "consent": req.consent | |
| } | |
| try: | |
| output, meta = execute_agent("progress", payload) | |
| summary = output.get("summary", "No progress data available.") | |
| courses = output.get("courses_completed", []) | |
| response_text = f"{summary}\n\n" | |
| if courses: | |
| response_text += "Recent course completions:\n" | |
| for course in courses[:3]: # Show top 3 | |
| response_text += f"β {course.get('title', 'Course')}\n" | |
| response_text += "\nKeep up the great work! π" | |
| return StreamingResponse(stream_response(response_text), media_type="text/plain") | |
| except Exception as e: | |
| error_msg = str(e) # Capture the error message | |
| def error_stream(): | |
| yield f"Error: {error_msg}" | |
| return StreamingResponse(error_stream(), media_type="text/plain") | |