snehapatra19/nagar-alert-hub
GitHub: snehapatra19/nagar-alert-hub
基于机器学习与 NLP 的智慧城市公共安全事件实时监控、自动分级与可视化指挥平台。
Stars: 0 | Forks: 0
# 🛡️ Nagar Alert Hub
### AI 驱动的公共安全监控系统
[](https://python.org)
[](https://flask.palletsprojects.com)
[](https://scikit-learn.org)
[](https://mlflow.org)
## ✨ 功能
- **AI 分类** — Logistic Regression + TF-IDF vectorizer(准确率 85-90%)
- **关键词覆盖** — 30+ 个关键术语(bomb, murder, acid attack)立即判定为高风险 (HIGH RISK)
- **Google Maps + Leaflet** — 带有地理定位的实时事件地图
- **实时仪表盘** — 带有图表的自动刷新指挥中心
- **MLflow 集成** — 模型版本控制、实验跟踪
- **REST API** — 用于集成的完整 JSON API
- **部署就绪** — Render, Railway, Heroku, Docker, VPS
## 🚀 快速开始(本地)
```
# 1. Clone 或解压项目
cd nagar-alert-hub
# 2. 一键 setup 并启动
chmod +x start.sh && ./start.sh
# 3. 打开浏览器
open http://localhost:5000
```
### 手动设置
```
python3 -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate
pip install -r requirements.txt
# 训练 ML 模型
python models/train_model.py
# 配置环境
cp .env.example .env
# 编辑 .env 并添加你的 GOOGLE_MAPS_API_KEY
# 运行开发服务器
python app.py
```
## 🗺️ Google Maps 设置
1. 访问 [console.cloud.google.com](https://console.cloud.google.com)
2. 创建项目 → 启用 API:
- **Maps JavaScript API**
- **Geocoding API**(可选)
3. 创建 API Key → 限制为您自己的域名
4. 添加到 `.env`:
GOOGLE_MAPS_API_KEY=AIza...your_key_here
5. 重启服务器 — Google Maps 将会被自动使用!
## ☁️ 部署选项
### 选项 1: Render.com(推荐 — 免费套餐)
1. 推送代码到 GitHub:
git init && git add . && git commit -m "Initial commit"
gh repo create nagar-alert-hub --public --push
2. 前往 [render.com](https://render.com) → New Web Service
3. 连接您的 GitHub 仓库
4. Render 会自动检测 `render.yaml` ✅
5. 添加环境变量:`GOOGLE_MAPS_API_KEY`
6. 部署! 🎉
**您的 URL:** `https://nagar-alert-hub.onrender.com`
### 选项 2: Railway.app(最简单)
```
# 安装 Railway CLI
npm install -g @railway/cli
# Deploy
railway login
railway init
railway up
railway open
```
在 Railway 仪表盘中添加环境变量。
### 选项 3: Heroku
```
# 安装 Heroku CLI
heroku create nagar-alert-hub
heroku config:set SECRET_KEY=your-secret
heroku config:set GOOGLE_MAPS_API_KEY=your-key
git push heroku main
heroku open
```
### 选项 4: Docker
```
# Build
docker build -t nagar-alert-hub .
# Run
docker run -p 5000:5000 \
-e SECRET_KEY=your-secret \
-e GOOGLE_MAPS_API_KEY=your-key \
nagar-alert-hub
# 或使用 docker-compose
docker-compose up --build
```
### 选项 5: VPS / Ubuntu 服务器
```
# 在你的服务器上
sudo apt update && sudo apt install python3-pip nginx -y
# Clone 并 setup
git clone https://github.com/yourname/nagar-alert-hub
cd nagar-alert-hub
pip3 install -r requirements.txt
python3 models/train_model.py
# 使用 gunicorn 运行
gunicorn app:app --workers=2 --bind=0.0.0.0:5000 --daemon
# Nginx 配置(可选,用于自定义域名)
sudo nano /etc/nginx/sites-available/nagar
# 添加 proxy_pass http://127.0.0.1:5000;
sudo nginx -t && sudo systemctl restart nginx
```
## 📡 API 参考
| 端点 | 方法 | 描述 |
|----------|--------|-------------|
| `/api/predict` | POST | 分类事件文本 |
| `/api/incidents` | GET | 列出所有事件 |
| `/api/incidents/:id` | GET | 获取事件详情 |
| `/api/incidents/:id/status` | PATCH | 更新状态 |
| `/api/stats` | GET | 仪表盘统计数据 |
| `/api/model-info` | GET | ML 模型元数据 |
### 示例:提交事件
```
curl -X POST https://your-app.onrender.com/api/predict \
-H "Content-Type: application/json" \
-d '{
"text": "Bomb found near market, police called",
"location_name": "MG Road, Bengaluru",
"latitude": 12.9716,
"longitude": 77.5946,
"reporter_name": "Ravi Kumar"
}'
```
响应:
```
{
"incident_id": 1,
"label": "high_risk",
"confidence": 0.99,
"confidence_pct": 99.0,
"keyword": "bomb",
"source": "keyword_override",
"actions": ["Evacuate area immediately", "Alert NSG/ATS", "Cordon 500m radius"],
"authorities": [{"name": "Police Control Room", "number": "100", "icon": "🚔"}],
"explanation": "⚠️ Critical keyword detected: 'bomb'..."
}
```
## 🧠 ML Pipeline
```
Input Text
│
▼
Keyword Override Check (30 critical terms)
│ (if no keyword)
▼
Text Preprocessing (lowercase, remove punctuation)
│
▼
TF-IDF Vectorization (5000 features, bigrams)
│
▼
Logistic Regression Classifier
│
▼
Risk Label + Confidence Score
```
**评估的模型:**
| 模型 | 准确率 |
|-------|----------|
| Logistic Regression ✅ | ~88% |
| Naive Bayes | ~84% |
| Random Forest | ~86% |
| Linear SVC | ~87% |
## 📁 项目结构
```
nagar-alert-hub/
├── app.py # Flask application
├── requirements.txt
├── Procfile # Heroku/Render
├── render.yaml # Render deployment
├── railway.json # Railway deployment
├── Dockerfile
├── start.sh # Local startup script
├── .env.example
├── models/
│ ├── train_model.py # ML training pipeline
│ ├── incident_classifier.pkl
│ └── model_meta.json
├── utils/
│ └── nlp_utils.py # NLP helpers
├── templates/
│ ├── base.html
│ ├── index.html # Report form
│ ├── result.html # Classification result
│ └── dashboard.html # Command dashboard
├── static/
│ ├── css/main.css
│ └── js/main.js
├── instance/
│ └── incidents.db # SQLite database
└── mlflow_runs/ # MLflow artifacts
```
## 🎓 大学演示提示
1. **演示流程**:提交报告 → 展示结果页面 → 展示仪表盘地图
2. **亮点展示**:输入 "bomb explosion at railway station" 以触发关键词覆盖
3. **展示** MLflow 实验日志:在项目文件夹中运行 `mlflow ui`
4. **API 演示**:在浏览器中打开 `/api/incidents` 以展示 JSON 输出
## 📞 紧急号码(印度)
| 服务 | 号码 |
|---------|--------|
| 警察 | 100 |
| 消防 | 101 |
| 救护车 | 108 |
| 灾难救援 | 1077 |
| 妇女求助热线 | 1091 |
| 儿童求助热线 | 1098 |
*Built with ❤️ for Smart City Safety · Nagar Alert Hub v1.0*
标签:AI监控, Apex, Docker, Flask, Google Maps, Leaflet, MLflow, MLOps, NLP分类, Python, REST API, Scikit-learn, TF-IDF, 事件报告系统, 公共安全, 后端开发, 命令中心, 城市安全, 安全防御评估, 实时事件报告, 实时仪表盘, 实时地图, 库, 应急响应, 应急指挥中心, 文本分类, 无后门, 智慧城市, 机器学习, 请求拦截, 逆向工具, 逻辑回归, 风险预警