File size: 25,935 Bytes
0594535
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
"""
FastAPI Application for Driving Behavior Analysis
==================================================

Comprehensive REST API with Swagger documentation for testing the driving behavior
classification model. Includes batch predictions, real-time classification, and 
detailed confidence scores.

Run: uvicorn main:app --reload --host 0.0.0.0 --port 8000
Swagger UI: http://localhost:8000/docs
"""

from fastapi import FastAPI, HTTPException, Query
from pydantic import BaseModel, Field
from typing import List, Dict, Optional
import numpy as np
import pandas as pd
from sklearn.preprocessing import StandardScaler, LabelEncoder
import pickle
import json
from datetime import datetime
import logging
import uvicorn
from collections import deque
import threading
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# ============================================================================
# LOAD MODEL & PREPROCESSING OBJECTS
# ============================================================================

try:
    import joblib
    import os
    
    # Use actual relative paths so they work on Hugging Face servers
    BASE_DIR = os.path.dirname(os.path.abspath(__file__))
    model_path = os.path.join(BASE_DIR, 'model.pkl')
    scaler_path = os.path.join(BASE_DIR, 'scaler.pkl')
    le_path = os.path.join(BASE_DIR, 'label_encoder.pkl')
    fc_path = os.path.join(BASE_DIR, 'feature_columns.pkl')
    
    best_model = joblib.load(model_path)
    scaler = joblib.load(scaler_path)
    label_encoder = joblib.load(le_path)
    
    # Feature names (in correct order)
    with open(fc_path, 'rb') as f:
        feature_columns = pickle.load(f)
    
    logger.info("✅ All models and preprocessors loaded successfully!")
    
except FileNotFoundError as e:
    logger.warning(f"⚠️ Could not load model files: {e}")
    logger.warning("⚠️ Using mock models for demonstration")
    best_model = None
    scaler = None
    label_encoder = None
    feature_columns = None


# Global buffer to store recent sensor readings for proper time-series feature engineering
reading_history = deque(maxlen=15)
history_lock = threading.Lock()

# ============================================================================
# PYDANTIC MODELS (Request/Response Schemas)
# ============================================================================

class SensorInput(BaseModel):
    """Raw sensor input from accelerometer and gyroscope"""
    acc_x: float = Field(
        ..., 
        description="Acceleration in X direction (m/s²)",
        example=0.5
    )
    acc_y: float = Field(
        ..., 
        description="Acceleration in Y direction (m/s²)",
        example=0.2
    )
    acc_z: float = Field(
        ..., 
        description="Acceleration in Z direction (m/s²)",
        example=9.8
    )
    gyro_x: float = Field(
        ..., 
        description="Angular velocity around X axis (rad/s)",
        example=0.01
    )
    gyro_y: float = Field(
        ..., 
        description="Angular velocity around Y axis (rad/s)",
        example=0.02
    )
    gyro_z: float = Field(
        ..., 
        description="Angular velocity around Z axis (rad/s)",
        example=0.03
    )

    class Config:
        json_schema_extra = {
            "example": {
                "acc_x": 0.5,
                "acc_y": 0.2,
                "acc_z": 9.8,
                "gyro_x": 0.01,
                "gyro_y": 0.02,
                "gyro_z": 0.03
            }
        }


class PredictionResponse(BaseModel):
    """Response with prediction and confidence scores"""
    prediction: str = Field(..., description="Predicted driving behavior class")
    confidence: Dict[str, float] = Field(..., description="Confidence scores for each class")
    timestamp: str = Field(..., description="Prediction timestamp")
    
    class Config:
        json_schema_extra = {
            "example": {
                "prediction": "NORMAL",
                "confidence": {
                    "AGGRESSIVE": 0.02,
                    "NORMAL": 0.88,
                    "SLOW": 0.10
                },
                "timestamp": "2024-04-17T12:34:56"
            }
        }


class BatchPredictionRequest(BaseModel):
    """Request for batch predictions"""
    samples: List[SensorInput] = Field(..., description="List of sensor readings")
    return_features: bool = Field(
        False, 
        description="Include engineered features in response"
    )


class BatchPredictionResponse(BaseModel):
    """Response with batch predictions"""
    total_samples: int
    successful_predictions: int
    failed_predictions: int
    predictions: List[Dict] = Field(..., description="List of predictions")
    processing_time_ms: float


class HealthResponse(BaseModel):
    """Health check response"""
    status: str
    model_loaded: bool
    model_version: str
    timestamp: str
    features_count: Optional[int] = None


# ============================================================================
# FEATURE ENGINEERING FUNCTION
# ============================================================================

def engineer_features(data_list: list) -> pd.DataFrame:
    """
    Apply feature engineering to raw sensor data using a sequence of historical readings
    to correctly compute rates of changes (Jerk) and rolling statistics.
    """
    try:
        # Create DataFrame from list
        df = pd.DataFrame(data_list)
        
        # Rename columns to match training
        df = df.rename(columns={
            'acc_x': 'AccX', 'acc_y': 'AccY', 'acc_z': 'AccZ',
            'gyro_x': 'GyroX', 'gyro_y': 'GyroY', 'gyro_z': 'GyroZ'
        })
        
        # ===== JERK CALCULATION =====
        # For single row: jerk = 0
        df['JerkX'] = df['AccX'].diff().fillna(0)
        df['JerkY'] = df['AccY'].diff().fillna(0)
        df['JerkZ'] = df['AccZ'].diff().fillna(0)
        
        # ===== MAGNITUDE FEATURES =====
        df['AccMagnitude'] = np.sqrt(df['AccX']**2 + df['AccY']**2 + df['AccZ']**2)
        df['GyroMagnitude'] = np.sqrt(df['GyroX']**2 + df['GyroY']**2 + df['GyroZ']**2)
        df['JerkMagnitude'] = np.sqrt(df['JerkX']**2 + df['JerkY']**2 + df['JerkZ']**2)
        
        # ===== ROLLING STATISTICS =====
        window_size = 5
        df['AccX_rolling_mean'] = df['AccX'].rolling(window=window_size, min_periods=1).mean()
        df['AccY_rolling_mean'] = df['AccY'].rolling(window=window_size, min_periods=1).mean()
        df['AccZ_rolling_mean'] = df['AccZ'].rolling(window=window_size, min_periods=1).mean()
        
        df['AccX_rolling_std'] = df['AccX'].rolling(window=window_size, min_periods=1).std().fillna(0)
        df['AccY_rolling_std'] = df['AccY'].rolling(window=window_size, min_periods=1).std().fillna(0)
        df['AccZ_rolling_std'] = df['AccZ'].rolling(window=window_size, min_periods=1).std().fillna(0)
        
        df['JerkX_rolling_mean'] = df['JerkX'].rolling(window=window_size, min_periods=1).mean()
        df['JerkY_rolling_mean'] = df['JerkY'].rolling(window=window_size, min_periods=1).mean()
        df['JerkZ_rolling_mean'] = df['JerkZ'].rolling(window=window_size, min_periods=1).mean()
        
        df['JerkX_rolling_max'] = df['JerkX'].rolling(window=window_size, min_periods=1).max()
        df['JerkY_rolling_max'] = df['JerkY'].rolling(window=window_size, min_periods=1).max()
        df['JerkZ_rolling_max'] = df['JerkZ'].rolling(window=window_size, min_periods=1).max()
        
        # ===== VARIANCE & ENERGY =====
        df['AccX_var'] = df['AccX'] ** 2
        df['AccY_var'] = df['AccY'] ** 2
        df['AccZ_var'] = df['AccZ'] ** 2
        
        df['JerkX_var'] = df['JerkX'] ** 2
        df['JerkY_var'] = df['JerkY'] ** 2
        df['JerkZ_var'] = df['JerkZ'] ** 2
        
        # ===== ABSOLUTE VALUES =====
        df['AbsAccX'] = abs(df['AccX'])
        df['AbsAccY'] = abs(df['AccY'])
        df['AbsAccZ'] = abs(df['AccZ'])
        df['AbsJerkX'] = abs(df['JerkX'])
        df['AbsJerkY'] = abs(df['JerkY'])
        df['AbsJerkZ'] = abs(df['JerkZ'])
        
        # ===== ENERGY FEATURES =====
        df['Acc_Energy'] = (df['AccX']**2 + df['AccY']**2 + df['AccZ']**2) / 3
        df['Jerk_Energy'] = (df['JerkX']**2 + df['JerkY']**2 + df['JerkZ']**2) / 3
        
        return df
    
    except Exception as e:
        logger.error(f"Error in feature engineering: {str(e)}")
        raise


# ============================================================================
# PREDICTION FUNCTION
# ============================================================================

def predict_driving_behavior(data_list: list) -> Dict:
    """
    Predict driving behavior from sensor data sequence
    
    Args:
        data_list: List of dictionaries with sensor readings
        
    Returns:
        Dictionary with prediction and confidence scores
    """
    try:
        # Check if models are loaded
        if best_model is None or scaler is None or label_encoder is None:
            raise ValueError("Models not loaded. Cannot make predictions.")
        
        # Engineer features
        df = engineer_features(data_list)
        
        # Select features in correct order
        df_processed = df[feature_columns]
        
        # Scale features
        df_scaled = scaler.transform(df_processed)
        
        # Extract only the latest row for prediction
        latest_row_scaled = df_scaled[-1].reshape(1, -1)
        
        # Make prediction
        pred = best_model.predict(latest_row_scaled)
        proba = best_model.predict_proba(latest_row_scaled)
        
        # Decode prediction
        prediction = label_encoder.inverse_transform(pred)[0]
        
        # Get confidence scores
        confidence_dict = {
            label: float(proba[0][i]) 
            for i, label in enumerate(label_encoder.classes_)
        }
        
        return {
            "prediction": prediction,
            "confidence": confidence_dict,
            "raw_probability": proba[0].tolist()
        }
    
    except Exception as e:
        logger.error(f"Prediction error: {str(e)}")
        raise


# ============================================================================
# FASTAPI APPLICATION
# ============================================================================

app = FastAPI(
    title="🚗 Driving Behavior Analysis API",
    description="""
    Real-time driving behavior classification API using machine learning.
    
    Classify driving patterns into three categories:
    - **NORMAL**: Regular, safe driving
    - **SLOW**: Cautious, slower driving
    - **AGGRESSIVE**: Risky, aggressive driving
    
    ## Features
    - Single prediction endpoint
    - Batch prediction support
    - Real-time confidence scores
    - Feature engineering included
    - Health check endpoint
    - Comprehensive API documentation
    
    ## How to Use
    1. Provide raw sensor data (acceleration & gyroscope readings)
    2. API automatically engineers features
    3. Get driving behavior classification with confidence scores
    
    ## Example Request
    ```json
    {
        "acc_x": 0.5,
        "acc_y": 0.2,
        "acc_z": 9.8,
        "gyro_x": 0.01,
        "gyro_y": 0.02,
        "gyro_z": 0.03
    }
    ```
    """,
    version="1.0.0",
    docs_url="/docs",
    redoc_url="/redoc",
    openapi_url="/openapi.json",
    contact={
        "name": "ML Team",
        "email": "ml@example.com"
    }
)


# ============================================================================
# HEALTH CHECK ENDPOINT
# ============================================================================

@app.get(
    "/health",
    response_model=HealthResponse,
    tags=["Health"],
    summary="Health Check",
    description="Check API status and model availability"
)
async def health_check():
    """
    Check the health status of the API and model availability
    """
    return HealthResponse(
        status="healthy",
        model_loaded=best_model is not None,
        model_version="1.0.0",
        timestamp=datetime.now().isoformat(),
        features_count=len(feature_columns) if feature_columns else 0
    )


# ============================================================================
# SINGLE PREDICTION ENDPOINT
# ============================================================================

@app.post(
    "/predict",
    response_model=PredictionResponse,
    tags=["Prediction"],
    summary="Predict Driving Behavior",
    description="Predict driving behavior from a single sensor reading"
)
async def predict(sensor_input: SensorInput):
    """
    Predict driving behavior from raw sensor data.
    
    **Input Parameters:**
    - acc_x: Acceleration in X direction (m/s²)
    - acc_y: Acceleration in Y direction (m/s²)
    - acc_z: Acceleration in Z direction (m/s²)
    - gyro_x: Angular velocity around X axis (rad/s)
    - gyro_y: Angular velocity around Y axis (rad/s)
    - gyro_z: Angular velocity around Z axis (rad/s)
    
    **Response:**
    - prediction: One of [AGGRESSIVE, NORMAL, SLOW]
    - confidence: Confidence scores for each class
    - timestamp: When prediction was made
    
    **Example Request:**
    ```json
    {
        "acc_x": 0.5,
        "acc_y": 0.2,
        "acc_z": 9.8,
        "gyro_x": 0.01,
        "gyro_y": 0.02,
        "gyro_z": 0.03
    }
    ```
    
    **Example Response:**
    ```json
    {
        "prediction": "NORMAL",
        "confidence": {
            "AGGRESSIVE": 0.02,
            "NORMAL": 0.88,
            "SLOW": 0.10
        },
        "timestamp": "2024-04-17T12:34:56"
    }
    ```
    """
    try:
        # Convert input to dictionary
        input_dict = sensor_input.dict()
        
        # Append to global history
        with history_lock:
            reading_history.append(input_dict)
            history_snapshot = list(reading_history)
        
        # Make prediction using history
        result = predict_driving_behavior(history_snapshot)
        
        return PredictionResponse(
            prediction=result["prediction"],
            confidence=result["confidence"],
            timestamp=datetime.now().isoformat()
        )
    
    except Exception as e:
        logger.error(f"Prediction error: {str(e)}")
        raise HTTPException(
            status_code=500,
            detail=f"Prediction failed: {str(e)}"
        )


# ============================================================================
# BATCH PREDICTION ENDPOINT
# ============================================================================

@app.post(
    "/predict-batch",
    response_model=BatchPredictionResponse,
    tags=["Batch Prediction"],
    summary="Batch Predictions",
    description="Make predictions on multiple sensor readings at once"
)
async def predict_batch(request: BatchPredictionRequest):
    """
    Predict driving behavior for multiple sensor readings.
    
    Useful for processing streams or datasets efficiently.
    
    **Request Parameters:**
    - samples: List of sensor readings
    - return_features: Whether to include engineered features in response
    
    **Returns:**
    - total_samples: Number of samples processed
    - successful_predictions: Number of successful predictions
    - failed_predictions: Number of failed predictions
    - predictions: List of prediction results
    - processing_time_ms: Total processing time
    """
    import time
    
    start_time = time.time()
    predictions = []
    successful = 0
    failed = 0
    
    try:
        for i, sensor_input in enumerate(request.samples):
            try:
                input_dict = sensor_input.dict()
                
                # Using the same global history mechanism to accumulate batch over time
                with history_lock:
                    reading_history.append(input_dict)
                    history_snapshot = list(reading_history)
                
                result = predict_driving_behavior(history_snapshot)
                
                prediction_result = {
                    "sample_index": i,
                    "prediction": result["prediction"],
                    "confidence": result["confidence"],
                    "timestamp": datetime.now().isoformat()
                }
                
                predictions.append(prediction_result)
                successful += 1
            
            except Exception as e:
                logger.error(f"Error on sample {i}: {str(e)}")
                predictions.append({
                    "sample_index": i,
                    "error": str(e)
                })
                failed += 1
        
        processing_time = (time.time() - start_time) * 1000  # Convert to ms
        
        return BatchPredictionResponse(
            total_samples=len(request.samples),
            successful_predictions=successful,
            failed_predictions=failed,
            predictions=predictions,
            processing_time_ms=processing_time
        )
    
    except Exception as e:
        logger.error(f"Batch prediction error: {str(e)}")
        raise HTTPException(
            status_code=500,
            detail=f"Batch prediction failed: {str(e)}"
        )


# ============================================================================
# DETAILED PREDICTION ENDPOINT
# ============================================================================

@app.post(
    "/predict-detailed",
    tags=["Prediction"],
    summary="Detailed Prediction",
    description="Get detailed prediction with engineered features"
)
async def predict_detailed(sensor_input: SensorInput):
    """
    Get detailed prediction including engineered features.
    
    Useful for understanding which features influenced the prediction.
    """
    try:
        input_dict = sensor_input.dict()
        
        with history_lock:
            reading_history.append(input_dict)
            history_snapshot = list(reading_history)
        
        # Engineer features
        df = engineer_features(history_snapshot)
        
        # Make prediction
        result = predict_driving_behavior(history_snapshot)
        
        return {
            "prediction": result["prediction"],
            "confidence": result["confidence"],
            "engineered_features": df.to_dict(orient='records')[-1],
            "timestamp": datetime.now().isoformat()
        }
    
    except Exception as e:
        logger.error(f"Detailed prediction error: {str(e)}")
        raise HTTPException(
            status_code=500,
            detail=f"Detailed prediction failed: {str(e)}"
        )


# ============================================================================
# TEST DATA ENDPOINT
# ============================================================================

@app.get(
    "/test-samples",
    tags=["Testing"],
    summary="Get Test Samples",
    description="Get example sensor readings for testing"
)
async def get_test_samples():
    """
    Get example sensor readings for different driving behaviors.
    
    Useful for testing the API without real sensor data.
    """
    return {
        "NORMAL": {
            "description": "Normal driving - balanced sensor readings",
            "sample": {
                "acc_x": 0.05,
                "acc_y": -0.08,
                "acc_z": 9.81,
                "gyro_x": 0.002,
                "gyro_y": -0.001,
                "gyro_z": 0.008
            }
        },
        "SLOW": {
            "description": "Slow driving - smooth, low acceleration",
            "sample": {
                "acc_x": 0.1,
                "acc_y": -0.02,
                "acc_z": 9.8,
                "gyro_x": 0.0,
                "gyro_y": 0.0,
                "gyro_z": 0.001
            }
        },
        "AGGRESSIVE": {
            "description": "Aggressive driving - high accelerations and jerky movements",
            "sample": {
                "acc_x": 0.8,
                "acc_y": -0.5,
                "acc_z": 9.7,
                "gyro_x": 0.05,
                "gyro_y": 0.03,
                "gyro_z": 0.1
            }
        }
    }


# ============================================================================
# INFO ENDPOINT
# ============================================================================

@app.get(
    "/info",
    tags=["Info"],
    summary="API Information",
    description="Get information about the API and model"
)
async def get_info():
    """
    Get detailed information about the API and trained model.
    """
    return {
        "api_name": "Driving Behavior Analysis API",
        "version": "1.0.0",
        "model_status": "loaded" if best_model is not None else "not_loaded",
        "supported_classes": [
            "AGGRESSIVE",
            "NORMAL",
            "SLOW"
        ],
        "features_count": len(feature_columns) if feature_columns else "unknown",
        "endpoints": {
            "predict": "POST /predict - Single prediction",
            "predict_batch": "POST /predict-batch - Batch predictions",
            "predict_detailed": "POST /predict-detailed - Detailed prediction with features",
            "health_check": "GET /health - Health check",
            "test_samples": "GET /test-samples - Get example test data",
            "info": "GET /info - API information"
        },
        "documentation": {
            "swagger": "http://localhost:8000/docs",
            "redoc": "http://localhost:8000/redoc",
            "openapi": "http://localhost:8000/openapi.json"
        }
    }


# ============================================================================
# ROOT ENDPOINT
# ============================================================================

@app.get(
    "/",
    tags=["Root"],
    summary="Welcome",
    description="Welcome message and quick start guide"
)
async def root():
    """
    Welcome to the Driving Behavior Analysis API!
    
    **Quick Start:**
    1. Go to http://localhost:8000/docs for interactive Swagger UI
    2. Try the /predict endpoint with sample data
    3. Check /test-samples for example inputs
    
    **Endpoints:**
    - POST /predict - Single prediction
    - POST /predict-batch - Batch predictions
    - GET /health - Health check
    - GET /test-samples - Test data
    - GET /info - API information
    """
    return {
        "message": "🚗 Welcome to Driving Behavior Analysis API",
        "status": "running",
        "docs": "http://localhost:8000/docs",
        "quick_start": [
            "1. Visit http://localhost:8000/docs",
            "2. Click on POST /predict",
            "3. Click 'Try it out'",
            "4. Enter sensor data or use example",
            "5. Click 'Execute' to get prediction"
        ]
    }


# ============================================================================
# EXCEPTION HANDLERS
# ============================================================================

@app.exception_handler(HTTPException)
async def http_exception_handler(request, exc):
    """Handle HTTP exceptions"""
    return {
        "error": exc.detail,
        "status_code": exc.status_code,
        "timestamp": datetime.now().isoformat()
    }


# ============================================================================
# RUN THE APPLICATION
# ============================================================================


# ==========================================================================
# SAFETY SCORE ENDPOINT (NEW)
# ==========================================================================

from enum import Enum

class SafetyColor(str, Enum):
    GREEN = "Green"
    YELLOW = "Yellow"
    RED = "Red"

class SafetyScoreResponse(BaseModel):
    score: int = Field(..., description="Safety score (0-100)")
    color: SafetyColor = Field(..., description="Color-coded safety status")
    events: Dict[str, bool] = Field(..., description="Detected driving events")
    timestamp: str = Field(..., description="Timestamp of evaluation")

@app.post(
    "/safety-score",
    response_model=SafetyScoreResponse,
    tags=["Safety"],
    summary="Real-Time Safety Score",
    description="Get a real-time safety score and color-coded status from a single sensor reading."
)
async def safety_score(sensor_input: SensorInput):
    """
    Calculate a real-time safety score and color-coded status from sensor data.
    Detects harsh braking, rapid acceleration, aggressive cornering.
    """
    input_dict = sensor_input.dict()
    acc_x = input_dict["acc_x"]
    acc_y = input_dict["acc_y"]
    acc_z = input_dict["acc_z"]
    gyro_x = input_dict["gyro_x"]
    gyro_y = input_dict["gyro_y"]
    gyro_z = input_dict["gyro_z"]

    # Simple event detection thresholds (tune as needed)
    harsh_braking = acc_x < -2.5
    rapid_acceleration = acc_x > 2.5
    aggressive_cornering = abs(acc_y) > 2.0
    # (Advanced: add tailgating/lane weaving if you have more data)

    # Score logic (deduct for each event)
    score = 100
    if harsh_braking:
        score -= 30
    if rapid_acceleration:
        score -= 25
    if aggressive_cornering:
        score -= 20
    score = max(0, min(100, score))

    # Color coding
    if score >= 80:
        color = SafetyColor.GREEN
    elif score >= 50:
        color = SafetyColor.YELLOW
    else:
        color = SafetyColor.RED

    return SafetyScoreResponse(
        score=score,
        color=color,
        events={
            "harsh_braking": harsh_braking,
            "rapid_acceleration": rapid_acceleration,
            "aggressive_cornering": aggressive_cornering
        },
        timestamp=datetime.now().isoformat()
    )

if __name__ == "__main__":

    uvicorn.run(
        "main:app",
        host="0.0.0.0",
        port=8000,
        reload=True,
        log_level="info"
    )