File size: 11,805 Bytes
7ed2180
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# filename: api_service.py (ENHANCED FOR HF SPACES)

from fastapi import FastAPI, HTTPException, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from pydantic import BaseModel, Field
from typing import Dict, Any, Optional
import logging
import time

# Import the core logic and model loading functions
from backend_pam import load_agent as load_backend_agent, PAM
from frontend_pam import load_frontend_agent, FrontendPAM

# --- Configure Logging ---
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger("PAM_API")

# --- Global Agent Variables ---
backend_agent: Optional[PAM] = None
frontend_agent: Optional[FrontendPAM] = None

# --- Data Models for API Requests ---
class UserInput(BaseModel):
    user_input: str = Field(..., min_length=1, max_length=5000, description="User's message to PAM")
    user_id: Optional[str] = Field(None, description="Optional user identifier for tracking")
    backend_context: Optional[str] = Field(None, description="Optional backend context for frontend responses")

class HealthResponse(BaseModel):
    status: str
    backend_ready: bool
    frontend_ready: bool
    timestamp: str
    message: str

# --- FastAPI Initialization ---
app = FastAPI(
    title="PAM - Privacy-First AI Assistant",
    description="Unified inference service for Frontend (Chat) and Backend (Technical) PAM agents",
    version="2.1.0",
    docs_url="/docs",  # Enable docs for development
    redoc_url="/redoc"
)

# --- CORS Setup (Enhanced for HF Spaces) ---
origins = [
    "https://www.uminur.app",
    "https://api.uminur.app",
    "http://localhost:3000",  # Local development
    "http://localhost:7860",  # HF Spaces default port
    "https://*.hf.space",     # HF Spaces domain
]

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # Open for HF Spaces deployment (restrict in production)
    allow_credentials=True,
    allow_methods=["POST", "GET", "OPTIONS"],
    allow_headers=["*"],
)

# --- Request Timing Middleware ---
@app.middleware("http")
async def add_process_time_header(request: Request, call_next):
    start_time = time.time()
    response = await call_next(request)
    process_time = time.time() - start_time
    response.headers["X-Process-Time"] = str(process_time)
    logger.info(f"{request.method} {request.url.path} - {process_time:.3f}s")
    return response

# --- Global Exception Handler ---
@app.exception_handler(Exception)
async def global_exception_handler(request: Request, exc: Exception):
    logger.error(f"Unhandled exception: {exc}", exc_info=True)
    return JSONResponse(
        status_code=500,
        content={
            "error": "Internal server error",
            "message": "PAM encountered an unexpected issue. Please try again.",
            "type": str(type(exc).__name__)
        }
    )

# --- Startup Event ---
@app.on_event("startup")
async def startup_event():
    global backend_agent, frontend_agent
    logger.info("πŸš€ Starting PAM agents initialization...")

    try:
        # Load Backend PAM (Technical Assistant)
        logger.info("πŸ€“ Loading Backend PAM (Nerdy Lab Assistant)...")
        backend_agent = load_backend_agent()
        if backend_agent:
            logger.info("βœ… Backend PAM loaded successfully")
        else:
            logger.error("❌ Backend PAM failed to initialize")

        # Load Frontend PAM (Chat Assistant)
        logger.info("πŸ’• Loading Frontend PAM (Sweet Southern Receptionist)...")
        frontend_agent = load_frontend_agent()
        if frontend_agent:
            logger.info("βœ… Frontend PAM loaded successfully")
        else:
            logger.error("❌ Frontend PAM failed to initialize")

        if backend_agent and frontend_agent:
            logger.info("πŸŽ‰ Both PAM agents initialized successfully!")
        else:
            logger.warning("⚠️ One or both agents failed to initialize - service will run in degraded mode")

    except Exception as e:
        logger.error(f"❌ Critical error during startup: {e}", exc_info=True)

# --- Shutdown Event ---
@app.on_event("shutdown")
async def shutdown_event():
    logger.info("πŸ‘‹ Shutting down PAM service...")

# --- Root Endpoint ---
@app.get("/", tags=["Info"])
async def root():
    return {
        "service": "PAM - Privacy-First AI Assistant",
        "version": "2.1.0",
        "status": "operational",
        "endpoints": {
            "health": "/health",
            "technical": "/ai/technical/",
            "chat": "/ai/chat/",
            "docs": "/docs"
        },
        "message": "Welcome to PAM! Use /ai/chat/ for conversational support or /ai/technical/ for backend analysis."
    }

# --- Health Check ---
@app.get("/health", response_model=HealthResponse, tags=["Status"])
async def health_check():
    """Check the health status of both PAM agents"""
    backend_ok = backend_agent is not None
    frontend_ok = frontend_agent is not None
    
    status = "healthy" if (backend_ok and frontend_ok) else "degraded"
    
    if not backend_ok and not frontend_ok:
        status = "unavailable"
    
    response = HealthResponse(
        status=status,
        backend_ready=backend_ok,
        frontend_ready=frontend_ok,
        timestamp=time.strftime("%Y-%m-%d %H:%M:%S"),
        message=f"Backend PAM: {'βœ…' if backend_ok else '❌'} | Frontend PAM: {'βœ…' if frontend_ok else '❌'}"
    )
    
    if status == "unavailable":
        raise HTTPException(
            status_code=503,
            detail="Service Unavailable: Both agents failed to initialize"
        )
    
    return response

# --- Technical Endpoint (Backend PAM) ---
@app.post("/ai/technical/", tags=["Technical"])
async def technical_endpoint(input_data: UserInput) -> Dict[str, Any]:
    """

    Backend PAM - Technical Assistant Endpoint

    Handles: PHI detection, log parsing, compliance checks, SIEM analysis

    Personality: Nerdy, proactive lab assistant

    """
    if backend_agent is None:
        logger.error("Technical endpoint called but Backend PAM is not initialized")
        raise HTTPException(
            status_code=503, 
            detail="πŸ€“ Backend PAM is still warming up... Give me a moment to get the lab equipment ready!"
        )

    try:
        logger.info(f"Technical request: {input_data.user_input[:100]}...")
        
        # Process through Backend PAM
        pam_reply = backend_agent.process_input(input_data.user_input)
        
        # Add metadata
        pam_reply["agent_type"] = "backend"
        pam_reply["personality"] = "nerdy_lab_assistant"
        pam_reply["timestamp"] = time.strftime("%Y-%m-%d %H:%M:%S")
        
        if input_data.user_id:
            pam_reply["user_id"] = input_data.user_id
        
        logger.info("Technical request processed successfully")
        return pam_reply
        
    except Exception as e:
        logger.error(f"Error during technical inference: {e}", exc_info=True)
        raise HTTPException(
            status_code=500, 
            detail="πŸ€” Oops, I hit a technical snag while processing that. Can you try rephrasing or breaking it into smaller parts?"
        )

# --- Chat Endpoint (Frontend PAM) ---
@app.post("/ai/chat/", tags=["Chat"])
async def chat_endpoint(input_data: UserInput) -> Dict[str, Any]:
    """

    Frontend PAM - Conversational Assistant Endpoint

    Handles: Appointments, resources, health inquiries, general chat

    Personality: Sweet southern receptionist

    """
    if frontend_agent is None:
        logger.error("Chat endpoint called but Frontend PAM is not initialized")
        raise HTTPException(
            status_code=503, 
            detail="πŸ’• Frontend PAM is getting ready to help you, honey. Just a moment!"
        )

    try:
        logger.info(f"Chat request: {input_data.user_input[:100]}...")
        
        # Set user_id if provided
        if input_data.user_id:
            frontend_agent.user_id = input_data.user_id
        
        # Process through Frontend PAM with optional backend context
        pam_reply = frontend_agent.respond(
            user_text=input_data.user_input,
            backend_brief=input_data.backend_context
        )
        
        # Add metadata
        pam_reply["agent_type"] = "frontend"
        pam_reply["personality"] = "sweet_southern_receptionist"
        pam_reply["timestamp"] = time.strftime("%Y-%m-%d %H:%M:%S")
        
        if input_data.user_id:
            pam_reply["user_id"] = input_data.user_id
        
        logger.info("Chat request processed successfully")
        return pam_reply
        
    except Exception as e:
        logger.error(f"Error during chat inference: {e}", exc_info=True)
        raise HTTPException(
            status_code=500, 
            detail="Sorry dear, I'm having a little technical hiccup. Could you try that again for me?"
        )

# --- Unified Endpoint (Both Agents) ---
@app.post("/ai/unified/", tags=["Unified"])
async def unified_endpoint(input_data: UserInput) -> Dict[str, Any]:
    """

    Unified endpoint that intelligently routes to the appropriate PAM agent

    Based on intent detection or explicit routing

    """
    if not backend_agent or not frontend_agent:
        raise HTTPException(
            status_code=503,
            detail="One or both PAM agents are not ready. Please try again shortly."
        )
    
    try:
        user_text = input_data.user_input.lower()
        
        # Determine routing based on keywords
        backend_keywords = ["compliance", "logs", "phi", "parse", "scan", "analyze", "siem", "alert"]
        is_technical = any(keyword in user_text for keyword in backend_keywords)
        
        if is_technical:
            logger.info("Routing to Backend PAM (technical keywords detected)")
            return await technical_endpoint(input_data)
        else:
            logger.info("Routing to Frontend PAM (conversational/support)")
            return await chat_endpoint(input_data)
            
    except Exception as e:
        logger.error(f"Error in unified endpoint: {e}", exc_info=True)
        raise HTTPException(status_code=500, detail="Error processing request through unified endpoint")

# --- Metrics Endpoint ---
@app.get("/metrics", tags=["Status"])
async def get_metrics():
    """Basic metrics for monitoring"""
    return {
        "service": "PAM",
        "backend_status": "online" if backend_agent else "offline",
        "frontend_status": "online" if frontend_agent else "offline",
        "timestamp": time.strftime("%Y-%m-%d %H:%M:%S"),
        "uptime": "tracking_not_implemented"  # Can add process start time tracking
    }

# --- Development/Debug Endpoint (Remove in production) ---
@app.get("/debug/test-agents", tags=["Debug"])
async def test_agents():
    """Quick test of both agents (for development only)"""
    results = {
        "backend_test": None,
        "frontend_test": None
    }
    
    if backend_agent:
        try:
            results["backend_test"] = backend_agent.process_input("check compliance")
        except Exception as e:
            results["backend_test"] = {"error": str(e)}
    
    if frontend_agent:
        try:
            results["frontend_test"] = frontend_agent.respond("Hey PAM, how are you?")
        except Exception as e:
            results["frontend_test"] = {"error": str(e)}
    
    return results