File size: 2,481 Bytes
95b078e
6c9c901
 
 
1de8f23
 
 
6c9c901
95b078e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
---
title: Marine Guard API
emoji: 🌊
colorFrom: blue
colorTo: green
sdk: docker
pinned: false
license: mit
app_port: 7860
---

# Marine Guard API 🌊

FastAPI backend for the Marine Pollution Detection and Incident Management System.

## πŸš€ Features

- πŸ” **JWT Authentication** - Secure user registration and login
- πŸ€– **ML-Powered Classification** - AI incident analysis with 87.8% threat accuracy, 81.0% severity accuracy
- πŸ—„οΈ **MongoDB Atlas Integration** - Cloud database with graceful fallback
- 🌐 **CORS-Enabled** - Ready for frontend integration
- πŸ₯ **Health Monitoring** - Built-in health checks and monitoring
- πŸ“Š **Interactive API Docs** - Swagger UI available at `/docs`

## 🎯 API Endpoints

### Authentication
- `POST /api/auth/register` - User registration
- `POST /api/auth/login` - User login

### Incidents
- `GET /api/incidents` - Get user incidents (requires auth)
- `POST /api/incidents` - Create new incident (requires auth)
- `POST /api/incidents/classify` - Classify incident text using ML

### Monitoring
- `GET /health` - Health check endpoint
- `GET /` - API status

## πŸ”§ Environment Variables

Set these in your Hugging Face Spaces environment:

```
MONGODB_URI=your_mongodb_atlas_connection_string
JWT_SECRET_KEY=your_secure_secret_key
ALLOWED_ORIGINS=https://your-frontend-domain.com
```

## πŸ€– ML Model Details

### Threat Classification Model
- **Accuracy**: 87.8%
- **Classes**: Oil, Chemical, Other
- **Algorithm**: TF-IDF + Random Forest

### Severity Classification Model  
- **Accuracy**: 81.0%
- **Classes**: high, medium, low
- **Algorithm**: TF-IDF + Random Forest

### Fallback System
- Automatic rule-based classification if ML models unavailable
- Confidence scores provided for all predictions

## πŸ“± Frontend Integration

This API is designed to work with the Marine Pollution Detection frontend. Set your frontend's API base URL to:

```
VITE_API_BASE_URL=https://your-space-name.hf.space/api
```

## πŸ› οΈ Development

Built with:
- **FastAPI** 0.115.2 - Modern Python web framework
- **Motor** - Async MongoDB driver
- **scikit-learn** - Machine learning models
- **Pydantic** - Data validation
- **Passlib** - Password hashing
- **PyJWT** - JSON Web Token handling

## πŸ“Š Health & Monitoring

The API includes comprehensive health checking:
- Database connectivity testing
- Model loading verification
- Graceful degradation for service issues

Visit `/health` for real-time status information.