A Flask-based campus safety management system that provides crime data analysis, danger scoring, and predictive forecasting for university campuses.
- Crime Data Management: Process and serve campus crime incident data
- Danger Scoring: Calculate location-based danger scores and risk assessments
- Predictive Analytics: Machine learning-powered crime forecasting using Facebook Prophet
- Hot Zone Detection: Identify high-risk areas based on historical crime patterns
- Safety Reports: Generate comprehensive PDF safety reports
- AI-Powered Insights: Gemini AI integration for intelligent crime pattern analysis
GET /data.json- Retrieve crime dataGET /incidents/all- Get all incident recordsGET /incidents/update- Update database with latest incidentsGET /api/locations- Get list of all campus locationsGET /api/clean-data- Clean and normalize crime data
POST /api/danger-score- Get danger score for specific locationPOST /api/danger-score-by-hour- Get hourly danger scoresPOST /api/danger-forecast- Generate danger forecasts for locationsPOST /api/risk-predict- Predict risk level for location and timeGET /api/hot-zones- Identify high-crime areas
POST /api/forecast-prophet- Facebook Prophet-based incident forecastingGET /api/crime-forecast- Comprehensive crime forecast with coordinatesPOST /api/compare-days- Compare safety between different days/periods
POST /api/gemini-summary- AI-generated incident summariesPOST /api/test-gemini- Test Gemini AI integrationPOST /api/export-pdf- Generate PDF safety reports
- Backend Framework: Flask 2.3.2
- Database: PostgreSQL with psycopg2
- AI Integration: Google Gemini AI
- Machine Learning: Facebook Prophet, scikit-learn
- Data Processing: pandas, numpy
- Geospatial: geopandas, geopy
- PDF Generation: ReportLab
- Environment Management: python-dotenv
- Install dependencies:
pip install -r requirements.txt- Set up environment variables in
.env:
GEMINI_API_KEY=your_gemini_api_key
DB_USER=your_db_user
DB_PASSWORD=your_db_password
DB_HOST=your_db_host
DB_PORT=your_db_port
DB_NAME=your_db_name
- Run the application:
python app.pybackend/
├── app.py # Main Flask application
├── config.py # Configuration management
├── prompts.py # AI prompt templates
├── gemini.py # Gemini AI integration
├── danger_score.py # Danger scoring algorithms
├── danger_forecast.py # Danger forecasting logic
├── hot_zones.py # Hot zone detection
├── database/
│ ├── db.py # Database connection
│ ├── incidents.py # Incident management
│ └── update_database.py # Database updates
├── ml/
│ ├── forecast_prophet.py # Prophet forecasting
│ ├── risk_classifier.py # Risk classification
│ └── plot_danger_by_hour.py # Visualization
├── static/ # Static assets (images, JS)
├── templates/ # HTML templates
└── tests/ # Test suite
curl -X POST http://localhost:5000/api/danger-score \
-H "Content-Type: application/json" \
-d '{"location": "Library"}'curl -X POST http://localhost:5000/api/forecast-prophet \
-H "Content-Type: application/json" \
-d '{"location": "Student Union", "days": 7}'curl -X POST http://localhost:5000/api/export-pdf \
-H "Content-Type: application/json" \
-d '{"summary": "Monthly safety report content..."}'- The application runs in debug mode by default
- CORS is enabled for cross-origin requests
- Static files are served from
/static/directory - Templates use Jinja2 templating engine
Run tests with:
python -m pytest tests/- Follow existing code style and patterns
- Add tests for new functionality
- Update documentation as needed
- Ensure all endpoints handle errors gracefully