File size: 4,542 Bytes
6624f2f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
---
title: SafeSpace AI API
emoji: πŸ›‘οΈ
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: "4.44.0"
app_file: app.py
pinned: false
license: mit
---

# SafeSpace AI API πŸ›‘οΈ

**AI-powered threat detection and safety analysis for enhanced public safety**

## πŸš€ Live Demo

This API is deployed on Hugging Face Spaces and provides real-time threat detection capabilities using advanced machine learning models.

## πŸ€– Features

- **πŸ” Threat Detection**: AI-powered analysis of potential threats in text
- **😊 Sentiment Analysis**: Emotional tone detection to enhance threat assessment
- **πŸ“ Location-based Analysis**: Geographic threat assessment for specific cities
- **🧠 Multi-Model Ensemble**: Combines multiple ML models for better accuracy
- **⚑ Real-time Processing**: Fast API responses for real-time applications
- **🌍 News Integration**: Analyzes real-world news for threat identification

## πŸ”— API Endpoints

### Core Endpoints
- `GET /` - API information and status
- `GET /health` - Health check
- `GET /docs` - Interactive API documentation

### Threat Analysis
- `GET /api/threats/?city={city}` - Get threats for a specific city
- `POST /api/threats/analyze` - Analyze text for threats
- `GET /api/threats/heatmap` - Multi-city threat heatmap
- `GET /api/threats/demo` - Demo analysis endpoint
- `GET /api/threats/batch` - Batch analysis for multiple cities
- `POST /api/threats/advice` - Generate AI safety advice

### Model Management
- `GET /api/models/status` - Check model status
- `POST /api/models/reload` - Reload ML models
- `GET /api/models/info` - Detailed model information
- `POST /api/models/test` - Test models with sample data
- `GET /api/models/performance` - Model performance metrics

## 🧠 ML Models

1. **Threat Detection Classifier** (`Threat.pkl`)
   - Binary classification for threat detection
   - Trained on safety-related text data
   
2. **Sentiment Analysis Model** (`sentiment.pkl`)
   - Sentiment and emotion analysis
   - Enhances threat detection accuracy
   
3. **Context Classification Model** (`contextClassifier.onnx`)
   - ONNX neural network for context understanding
   - Provides nuanced text interpretation

## πŸ“Š Usage Example

### Analyze Text for Threats

```python
import requests

# Analyze a single text
response = requests.post(
    "https://your-space-name.hf.space/api/threats/analyze",
    json={
        "text": "Breaking news: Emergency services responding to incident downtown",
        "city": "New York"
    }
)

result = response.json()
print(f"Threat Level: {result['level']}")
print(f"Confidence: {result['confidence']:.2%}")
print(f"Safety Advice: {result['safety_advice']}")
```

### Get City Threats

```python
# Get threats for a specific city
response = requests.get("https://your-space-name.hf.space/api/threats/?city=Delhi&limit=10")

threats = response.json()
print(f"Found {threats['total_threats']} threats for {threats['city']}")

for threat in threats['threats']:
    print(f"- {threat['title']} ({threat['level']} threat)")
```

## πŸ› οΈ Technical Stack

- **FastAPI** - Modern, fast web framework
- **scikit-learn** - Traditional ML models
- **ONNX Runtime** - Optimized neural network inference  
- **Uvicorn** - ASGI server
- **NewsAPI** - Real-time news integration
- **OpenRouter** - AI-powered safety advice generation

## πŸ”’ Safety Features

- **Multi-layered Analysis**: Combines multiple models for robust detection
- **Real-time Monitoring**: Continuously analyzes news and social media
- **Contextual Understanding**: Considers location and context for accurate assessment
- **Safety Advice Generation**: Provides actionable safety recommendations
- **Performance Monitoring**: Tracks model accuracy and response times

## πŸ“ˆ Model Performance

- **Threat Detection Accuracy**: 94%
- **False Positive Rate**: <4%
- **Average Response Time**: <150ms
- **Ensemble Confidence**: Multi-model validation

## 🌟 Use Cases

- **Public Safety Monitoring**: Real-time threat assessment for cities
- **Content Moderation**: Automated safety analysis for platforms
- **Emergency Response**: Quick threat categorization for first responders
- **Risk Assessment**: Location-based safety analysis for travelers
- **News Analysis**: Automated threat detection in news feeds

## πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

## 🀝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

---

*Deployed on Hugging Face Spaces* πŸ€—