File size: 31,203 Bytes
ed4d2a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
714e68d
 
 
 
 
ed4d2a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62fe0bb
ed4d2a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62fe0bb
ed4d2a2
 
 
62fe0bb
 
 
 
ed4d2a2
 
 
 
 
 
 
62fe0bb
ed4d2a2
 
 
 
 
 
 
 
62fe0bb
ed4d2a2
 
 
 
62fe0bb
 
 
 
 
 
ed4d2a2
62fe0bb
 
 
 
ed4d2a2
62fe0bb
ed4d2a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
714e68d
ed4d2a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
714e68d
ed4d2a2
 
 
 
 
 
 
 
 
 
 
714e68d
ed4d2a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
714e68d
ed4d2a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
714e68d
ed4d2a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
714e68d
ed4d2a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
714e68d
ed4d2a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f4fd56c
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
"""
FitScore Feedback Agent - Complete System for Hugging Face Deployment
"""

import os
import uuid
import time
import requests
import json
from datetime import datetime
from typing import Dict, Any, Optional, List
from fastapi import FastAPI, HTTPException, Depends, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from fastapi.exceptions import RequestValidationError
from sqlalchemy.orm import Session

from .database import get_db, create_tables, CandidateEvaluation, Feedback
from .models import (
    FitScoreRequest, CandidateRequest, FeedbackRequest,
    FitScoreResponse, FeedbackResponse, RecalculateResponse, 
    ComparisonResponse, HealthResponse, RootResponse
)
from .feedback_system import AdaptiveFeedbackSystem
from .reinforcement_learning import ReinforcementLearningSystem
from .advanced_learning import AdvancedLearningSystem
from .adaptive_hiring import AdaptiveHiringSystem
from .synapse_ai import SynapseAISystem


class FitScoreFeedbackAgent:
    """
    Complete FitScore Feedback Agent for Hugging Face deployment.
    Includes all subsystems for comprehensive functionality.
    """
    
    def __init__(self, config: Dict[str, Any]):
        self.config = config
        
        # FitScore API configuration
        self.ANALYZE_URL = config.get("fitscore_api_url", "")
        self.auth_email = config.get("auth_email", "")
        self.auth_password = config.get("auth_password", "")
        self.auth_login_url = config.get("auth_login_url", "")
        
        # Global variable to store access token
        self.access_token = None
        
        self.app = FastAPI(
            title="FitScore Feedback Agent",
            description="Advanced feedback loop system for candidate evaluation and model improvement",
            version="1.0.0"
        )
        
        # Initialize all subsystems
        self.feedback_system = AdaptiveFeedbackSystem()
        self.reinforcement_system = ReinforcementLearningSystem()
        self.advanced_learning = AdvancedLearningSystem()
        self.adaptive_hiring = AdaptiveHiringSystem()
        self.synapse_ai = SynapseAISystem()
        
        # Setup CORS
        self.app.add_middleware(
            CORSMiddleware,
            allow_origins=["*"],
            allow_credentials=True,
            allow_methods=["*"],
            allow_headers=["*"],
        )
        
        # Add custom exception handlers
        self._add_exception_handlers()
        
        # Add JSON error middleware
        self.app.middleware("http")(self._json_error_middleware)
        
        # Register endpoints
        self._register_endpoints()
        
        # Initialize database
        self._init_database()
    
    def _get_access_token(self):
        """
        Get access token for the external API with better error handling
        """
        # If we already have a token, return it
        if self.access_token:
            return self.access_token
        
        try:
            login_data = {
                "email": self.auth_email,
                "password": self.auth_password
            }
            login_headers = {
                'accept': 'application/json',
                'Content-Type': 'application/json'
            }
            
            # Add timeout to prevent hanging
            login_response = requests.post(self.auth_login_url, headers=login_headers, json=login_data, timeout=None)
            
            if login_response.status_code == 200:
                login_result = login_response.json()
                self.access_token = login_result.get('data', {}).get('tokens', {}).get('accessToken')
                if self.access_token:
                    print("✅ Successfully obtained access token")
                    return self.access_token
                else:
                    print("⚠️ Login successful but no access token found in response")
                    return None
            else:
                print(f"⚠️ Login failed with status {login_response.status_code}: {login_response.text}")
                return None
        except requests.exceptions.Timeout:
            print("⚠️ Login request timed out")
            return None
        except requests.exceptions.RequestException as e:
            print(f"⚠️ Network error during login: {e}")
            return None
        except Exception as e:
            print(f"⚠️ Unexpected error getting access token: {e}")
            return None
    
    def _reset_access_token(self):
        """Reset the access token to force a new login"""
        self.access_token = None
    
    def _add_exception_handlers(self):
        """Add custom exception handlers for better error messages"""
        
        @self.app.exception_handler(RequestValidationError)
        async def validation_exception_handler(request: Request, exc: RequestValidationError):
            """Handle validation errors with better messages"""
            return JSONResponse(
                status_code=422,
                content={
                    "detail": "Request validation failed. Please check your JSON data for invalid characters or malformed content.",
                    "errors": exc.errors(),
                    "help": "Make sure your JSON is properly formatted and doesn't contain invalid control characters."
                }
            )
        
        @self.app.exception_handler(json.JSONDecodeError)
        async def json_decode_exception_handler(request: Request, exc: json.JSONDecodeError):
            """Handle JSON decode errors with helpful messages"""
            return JSONResponse(
                status_code=422,
                content={
                    "detail": "Invalid JSON format",
                    "error": str(exc),
                    "help": "Please check your JSON syntax and remove any invalid control characters.",
                    "position": exc.pos,
                    "line": exc.lineno,
                    "column": exc.colno
                }
            )
    
    async def _json_error_middleware(self, request: Request, call_next):
        """Middleware to handle JSON parsing errors"""
        try:
            # Try to read the body to catch JSON errors early
            if request.method in ["POST", "PUT", "PATCH"]:
                content_type = request.headers.get("content-type", "")
                if "application/json" in content_type:
                    try:
                        body = await request.body()
                        if body:
                            json.loads(body.decode('utf-8'))
                    except json.JSONDecodeError as e:
                        return JSONResponse(
                            status_code=422,
                            content={
                                "detail": "Invalid JSON in request body",
                                "error": str(e),
                                "help": "Please check your JSON syntax and remove any invalid control characters.",
                                "position": e.pos
                            }
                        )
            
            response = await call_next(request)
            return response
            
        except Exception as e:
            return JSONResponse(
                status_code=500,
                content={
                    "detail": "Internal server error",
                    "error": str(e)
                }
            )
    
    def _init_database(self):
        """Initialize database tables"""
        try:
            create_tables()
            # Create initial global prompt
            self.feedback_system.create_initial_global_prompt()
            print("✅ Database initialized successfully!")
        except Exception as e:
            print(f"⚠️ Database initialization warning: {e}")
    
    def _call_fitscore_api(self, candidate_data: Dict[str, Any]) -> Dict[str, Any]:
        """Call the FitScore API for analysis"""
        try:
            # Get access token for authentication
            auth_token = self._get_access_token()
            if not auth_token:
                print("⚠️ Failed to obtain access token, using fallback evaluation")
                return self._fallback_evaluation(candidate_data)
            
            # Prepare the request data (form data, not JSON)
            data = {
                'job_id': candidate_data.get("job_id", str(uuid.uuid4())),
                'jd_text': candidate_data.get("job_description", "Software Engineer position"),
                'resume_text': candidate_data.get("resume_text", "")
            }
            
            # Validate required fields
            if not data['resume_text']:
                raise ValueError("resume_text must be provided")
            
            # Prepare headers with Bearer token
            headers = {
                'accept': 'application/json',
                'Authorization': f'Bearer {auth_token}'
            }
            
            # Make the API call
            response = requests.post(self.ANALYZE_URL, headers=headers, data=data, timeout=None)
            
            # If we get an authentication error, try to get a fresh token and retry once
            if response.status_code == 401:
                print("⚠️ Authentication failed, getting fresh token...")
                self._reset_access_token()
                new_token = self._get_access_token()
                if new_token:
                    headers['Authorization'] = f'Bearer {new_token}'
                    response = requests.post(self.ANALYZE_URL, headers=headers, data=data, timeout=None)
                else:
                    print("⚠️ Could not obtain fresh token, using fallback evaluation")
                    return self._fallback_evaluation(candidate_data)
            
            # Use raise_for_status like your working code
            response.raise_for_status()
            
            # Parse and return the response
            result = response.json()
            return result
                
        except requests.exceptions.RequestException as e:
            print(f"⚠️ API request failed: {e}")
            # Fallback to local evaluation
            return self._fallback_evaluation(candidate_data)
        except Exception as e:
            print(f"⚠️ Unexpected error in FitScore API call: {e}")
            # Fallback to local evaluation
            return self._fallback_evaluation(candidate_data)
    
    def _fallback_evaluation(self, candidate_data: Dict[str, Any]) -> Dict[str, Any]:
        """Fallback evaluation when FitScore API is unavailable"""
        try:
            # Use adaptive hiring system for local evaluation
            candidate_info = {
                "education_level": "Bachelors",  # Simplified
                "years_experience": 5,  # Simplified
                "skills": ["Python", "React"],  # Simplified
                "company_size": "Medium",
                "location": candidate_data.get("location", "Unknown"),
                "industry": "Technology"
            }
            
            evaluation_result = self.adaptive_hiring.evaluate_candidate(
                candidate_info, candidate_data.get("job_id", "default_job")
            )
            
            return evaluation_result
            
        except Exception as e:
            print(f"⚠️ Fallback evaluation failed: {e}")
            # Return a basic evaluation
            return {
                "fitscore": 7.5,
                "verdict": "Review",
                "confidence": 0.7,
                "category_scores": {
                    "education": 0.8,
                    "career_trajectory": 0.7,
                    "company_relevance": 0.7,
                    "tenure": 0.7,
                    "skills": 0.8,
                    "bonus": 0.1
                },
                "justification": "Basic evaluation completed. Manual review recommended.",
                "model_version": "v1.0-fallback"
            }
    
    def _register_endpoints(self):
        """Register all API endpoints"""
        
        @self.app.get("/", response_model=RootResponse)
        async def root():
            """Root endpoint"""
            return {
                "message": "FitScore Feedback Agent",
                "version": "1.0.0",
                "status": "running",
                "endpoints": [
                    "POST /fitscore/calculate",
                    "POST /fitscore/simple",
                    "POST /fitscore/feedback", 
                    "POST /fitscore/recalculate",
                    "GET /fitscore/compare/{candidate_id}/{job_id}",
                    "GET /analytics/feedback",
                    "GET /analytics/reinforcement",
                    "POST /reinforcement/submit",
                    "POST /reinforcement/outcome",
                    "POST /advanced-learning/event",
                    "GET /advanced-learning/analytics"
                ]
            }
        
        @self.app.get("/health", response_model=HealthResponse)
        async def health_check():
            """Health check endpoint"""
            return {
                "status": "healthy",
                "service": "FitScore Feedback Agent",
                "version": "1.0.0",
                "timestamp": datetime.utcnow().isoformat()
            }
        

        
        @self.app.post("/fitscore/simple", response_model=FitScoreResponse)
        async def calculate_fitscore_simple(
            request: FitScoreRequest,
            db: Session = Depends(get_db)
        ):
            """Calculate FitScore with simplified request - only essential fields"""
            try:
                # Prepare candidate data for FitScore API
                candidate_data = {
                    "job_id": request.job_id,
                    "job_description": request.jd_text,
                    "resume_text": request.resume_text
                }
                
                # Call FitScore API for evaluation
                evaluation_result = self._call_fitscore_api(candidate_data)
                
                # Create evaluation record
                evaluation_id = str(uuid.uuid4())
                evaluation = CandidateEvaluation(
                    evaluation_id=evaluation_id,
                    candidate_id=f"auto_{evaluation_id[:8]}",  # Auto-generate candidate ID
                    job_id=request.job_id,
                    fitscore=evaluation_result['fitscore'],
                    verdict=evaluation_result['verdict'],
                    confidence=evaluation_result['confidence'],
                    category_scores=evaluation_result['category_scores'],
                    justification=evaluation_result['justification'],
                    model_version=evaluation_result['model_version']
                )
                
                db.add(evaluation)
                db.commit()
                
                return {
                    "success": True,
                    "evaluation_id": evaluation_id,
                    "fitscore": evaluation_result['fitscore'],
                    "verdict": evaluation_result['verdict'],
                    "confidence": evaluation_result['confidence'],
                    "category_scores": evaluation_result['category_scores'],
                    "justification": evaluation_result['justification'],
                    "model_version": evaluation_result['model_version'],
                    "timestamp": datetime.utcnow().isoformat()
                }
                
            except Exception as e:
                raise HTTPException(status_code=500, detail=f"Error calculating FitScore: {str(e)}")
        
        @self.app.post("/fitscore/calculate", response_model=FitScoreResponse)
        async def calculate_fitscore(
            request: CandidateRequest,
            db: Session = Depends(get_db)
        ):
            """Calculate FitScore for candidate evaluation"""
            try:
                # Prepare candidate data for FitScore API
                candidate_data = {
                    "candidate_id": request.candidate_id,
                    "job_id": request.job_id,
                    "recruiter_id": request.recruiter_id,
                    "name": request.name,
                    "email": request.email,
                    "phone": request.phone,
                    "location": request.location,
                    "resume_text": request.resume_text,
                    "job_description": request.job_description
                }
                
                # Call FitScore API for evaluation
                evaluation_result = self._call_fitscore_api(candidate_data)
                
                # Create evaluation record
                evaluation_id = str(uuid.uuid4())
                evaluation = CandidateEvaluation(
                    evaluation_id=evaluation_id,
                    candidate_id=request.candidate_id,
                    job_id=request.job_id,
                    # recruiter_id removed to match existing PostgreSQL schema
                    fitscore=evaluation_result['fitscore'],
                    verdict=evaluation_result['verdict'],
                    confidence=evaluation_result['confidence'],
                    category_scores=evaluation_result['category_scores'],
                    justification=evaluation_result['justification'],
                    model_version=evaluation_result['model_version']
                )
                
                db.add(evaluation)
                db.commit()
                
                return {
                    "success": True,
                    "evaluation_id": evaluation_id,
                    "fitscore": evaluation_result['fitscore'],
                    "verdict": evaluation_result['verdict'],
                    "confidence": evaluation_result['confidence'],
                    "category_scores": evaluation_result['category_scores'],
                    "justification": evaluation_result['justification'],
                    "model_version": evaluation_result['model_version'],
                    "timestamp": datetime.utcnow().isoformat()
                }
                
            except Exception as e:
                raise HTTPException(status_code=500, detail=f"Error calculating FitScore: {str(e)}")
        
        @self.app.post("/fitscore/feedback", response_model=FeedbackResponse)
        async def submit_feedback(
            request: FeedbackRequest,
            db: Session = Depends(get_db)
        ):
            """Submit feedback for model improvement"""
            try:
                # Add feedback to system
                feedback = self.feedback_system.add_feedback(
                    job_id=request.job_id,
                    company_id=request.company_id,
                    analysis_id=request.analysis_id,
                    feedback_type=request.feedback_type,
                    feedback_text=request.feedback_text,
                    feedback_category=request.feedback_category,
                    confidence_score=request.confidence_score,
                    email=request.email,
                    linkedin_url=request.linkedin_url
                )
                
                # Create learning event
                learning_event_id = str(uuid.uuid4())
                
                return {
                    "success": True,
                    "feedback_id": feedback.feedback_id,
                    "learning_event_id": learning_event_id,
                    "message": "Feedback recorded and learning event created"
                }
                
            except Exception as e:
                raise HTTPException(status_code=500, detail=f"Error submitting feedback: {str(e)}")
        
        @self.app.post("/fitscore/recalculate", response_model=RecalculateResponse)
        async def recalculate_fitscore(
            candidate_id: str,
            job_id: str,
            feedback_id: str,
            db: Session = Depends(get_db)
        ):
            """Recalculate FitScore after feedback processing"""
            try:
                # Get original evaluation
                original_evaluation = db.query(CandidateEvaluation).filter(
                    CandidateEvaluation.candidate_id == candidate_id,
                    CandidateEvaluation.job_id == job_id
                ).order_by(CandidateEvaluation.created_at.desc()).first()
                
                if not original_evaluation:
                    raise HTTPException(status_code=404, detail="Original evaluation not found")
                
                # Get feedback
                feedback = db.query(Feedback).filter(Feedback.feedback_id == feedback_id).first()
                if not feedback:
                    raise HTTPException(status_code=404, detail="Feedback not found")
                
                # Recalculate with feedback
                updated_result = self.adaptive_hiring.recalculate_with_feedback(
                    original_evaluation, feedback
                )
                
                # Create updated evaluation
                updated_evaluation_id = str(uuid.uuid4())
                updated_evaluation = CandidateEvaluation(
                    evaluation_id=updated_evaluation_id,
                    candidate_id=candidate_id,
                    job_id=job_id,
                    # recruiter_id removed to match existing PostgreSQL schema
                    fitscore=updated_result['fitscore'],
                    verdict=updated_result['verdict'],
                    confidence=updated_result['confidence'],
                    category_scores=updated_result['category_scores'],
                    justification=updated_result['justification'],
                    model_version=updated_result['model_version']
                )
                
                db.add(updated_evaluation)
                db.commit()
                
                score_change = updated_result['fitscore'] - original_evaluation.fitscore
                
                return {
                    "success": True,
                    "original_evaluation_id": original_evaluation.evaluation_id,
                    "updated_evaluation_id": updated_evaluation_id,
                    "original_fitscore": original_evaluation.fitscore,
                    "updated_fitscore": updated_result['fitscore'],
                    "score_change": round(score_change, 2),
                    "original_verdict": original_evaluation.verdict,
                    "updated_verdict": updated_result['verdict'],
                    "verdict_changed": original_evaluation.verdict != updated_result['verdict'],
                    "model_version": updated_result['model_version'],
                    "timestamp": datetime.utcnow().isoformat()
                }
                
            except HTTPException:
                raise
            except Exception as e:
                raise HTTPException(status_code=500, detail=f"Error recalculating FitScore: {str(e)}")
        
        @self.app.get("/fitscore/compare/{candidate_id}/{job_id}", response_model=ComparisonResponse)
        async def compare_results(
            candidate_id: str,
            job_id: str,
            db: Session = Depends(get_db)
        ):
            """Compare original and updated FitScore results"""
            try:
                # Get evaluations for this candidate/job pair
                evaluations = db.query(CandidateEvaluation).filter(
                    CandidateEvaluation.candidate_id == candidate_id,
                    CandidateEvaluation.job_id == job_id
                ).order_by(CandidateEvaluation.created_at).all()
                
                if len(evaluations) < 2:
                    raise HTTPException(status_code=404, detail="No comparison data available")
                
                original = evaluations[0]
                updated = evaluations[-1]
                
                # Calculate changes
                score_change = updated.fitscore - original.fitscore
                score_change_percentage = (score_change / original.fitscore * 100) if original.fitscore > 0 else 0
                confidence_change = updated.confidence - original.confidence
                
                # Calculate category changes
                category_changes = {}
                for category in original.category_scores:
                    if category in updated.category_scores:
                        category_changes[category] = updated.category_scores[category] - original.category_scores[category]
                
                # Get feedback if available
                feedback = db.query(Feedback).filter(
                    Feedback.job_id == job_id
                ).order_by(Feedback.created_at.desc()).first()
                
                return {
                    "success": True,
                    "comparison": {
                        "original": {
                            "evaluation_id": original.evaluation_id,
                            "fitscore": original.fitscore,
                            "verdict": original.verdict,
                            "confidence": original.confidence,
                            "model_version": original.model_version,
                            "timestamp": original.created_at.isoformat(),
                            "category_scores": original.category_scores
                        },
                        "updated": {
                            "evaluation_id": updated.evaluation_id,
                            "fitscore": updated.fitscore,
                            "verdict": updated.verdict,
                            "confidence": updated.confidence,
                            "model_version": updated.model_version,
                            "timestamp": updated.created_at.isoformat(),
                            "category_scores": updated.category_scores
                        },
                        "changes": {
                            "score_change": round(score_change, 2),
                            "score_change_percentage": round(score_change_percentage, 1),
                            "verdict_changed": original.verdict != updated.verdict,
                            "confidence_change": round(confidence_change, 3),
                            "category_changes": category_changes
                        },
                        "feedback": {
                            "feedback_id": feedback.feedback_id if feedback else None,
                            "feedback_type": feedback.feedback_type if feedback else None,
                            "feedback_text": feedback.feedback_text if feedback else None,
                            "feedback_category": feedback.feedback_category if feedback else None,
                            "timestamp": feedback.created_at.isoformat() if feedback else None
                        },
                        "justification": f"FitScore changed from {original.fitscore:.2f} to {updated.fitscore:.2f} ({score_change:+.2f} points, {score_change_percentage:+.1f}%) after processing feedback."
                    }
                }
                
            except HTTPException:
                raise
            except Exception as e:
                raise HTTPException(status_code=500, detail=f"Error comparing results: {str(e)}")
        
        @self.app.get("/analytics/feedback")
        async def get_feedback_analytics():
            """Get feedback analytics"""
            try:
                analytics = self.feedback_system.get_feedback_analytics()
                return analytics
            except Exception as e:
                raise HTTPException(status_code=500, detail=f"Error getting analytics: {str(e)}")
        
        @self.app.get("/analytics/reinforcement")
        async def get_reinforcement_analytics():
            """Get reinforcement learning analytics"""
            try:
                analytics = self.reinforcement_system.get_learning_analytics()
                return analytics
            except Exception as e:
                raise HTTPException(status_code=500, detail=f"Error getting reinforcement analytics: {str(e)}")
        
        @self.app.post("/reinforcement/submit")
        async def submit_candidate_reinforcement(
            candidate_data: Dict[str, Any],
            job_data: Dict[str, Any],
            recruiter_id: str
        ):
            """Submit candidate for reinforcement learning"""
            try:
                submission_data = {
                    "candidate_id": candidate_data.get("candidate_id"),
                    "job_id": job_data.get("job_id"),
                    "recruiter_id": recruiter_id,  # Keep for API but don't store in database
                    "candidate_data": candidate_data,
                    "job_data": job_data
                }
                
                result = self.reinforcement_system.submit_candidate(submission_data)
                return result
            except Exception as e:
                raise HTTPException(status_code=500, detail=f"Error submitting candidate: {str(e)}")
        
        @self.app.post("/reinforcement/outcome")
        async def record_outcome(
            submission_id: str,
            outcome: str,
            notes: str = ""
        ):
            """Record outcome for reinforcement learning"""
            try:
                result = self.reinforcement_system.record_outcome(submission_id, outcome, notes)
                return result
            except Exception as e:
                raise HTTPException(status_code=500, detail=f"Error recording outcome: {str(e)}")
        
        @self.app.post("/advanced-learning/event")
        async def process_learning_event(
            event_data: Dict[str, Any]
        ):
            """Process advanced learning event"""
            try:
                result = self.advanced_learning.process_learning_event(event_data)
                return result
            except Exception as e:
                raise HTTPException(status_code=500, detail=f"Error processing learning event: {str(e)}")
        
        @self.app.get("/advanced-learning/analytics")
        async def get_advanced_learning_analytics():
            """Get advanced learning analytics"""
            try:
                analytics = self.advanced_learning.get_learning_analytics()
                return analytics
            except Exception as e:
                raise HTTPException(status_code=500, detail=f"Error getting advanced learning analytics: {str(e)}")
    
    def get_app(self) -> FastAPI:
        """Get the FastAPI application"""
        return self.app
    
    def run(self):
        """Run the application"""
        import uvicorn
        uvicorn.run(
            self.app,
            host=self.config.get("host", "0.0.0.0"),
            port=self.config.get("port", 7860)
        )