How lakshya0402/parkpulse-ai works
1. Overall shape
- This is a two-tier system: a React/Vite frontend for investigation and planning workflows, and a FastAPI backend that owns data ingestion, storage, ML inference, and patrol prioritization.
- The product goal is operational: turn historical Bengaluru traffic violation records into a map-based enforcement dashboard.
2. Frontend boundary
- The frontend is a single-page app built with React 18 + Vite + Tailwind.
- It is organized by user task into pages: City Map, Analytics, Hotspot Explorer, Predict, Planner, Coverage, and Offenders.
- Shared UI lives in components/common, map rendering lives in components/map, and API access is centralized in services/api.js.
3. Mapping and geospatial visualization
- The frontend uses the Mappls Web Map SDK to render the city map, heatmaps, cluster markers, and location popups.
- This is the main visualization boundary: the backend provides cluster/risk data, while the frontend is responsible for map presentation and user interaction.
4. Backend API layer
- The backend is a FastAPI app with route modules grouped by feature: analytics, coverage, hotspots, offenders, planner, and predict.
- These routes are thin HTTP boundaries that expose precomputed analytics, live model predictions, patrol plans, and lookup data to the UI.
5. Core data pipeline
- app/core/data_pipeline.py is the central bootstrap path for turning raw historical records into usable application assets.
- On first startup, it downloads/processes the dataset, builds the database contents, and prepares model artifacts. The README indicates this is automatic and takes a few minutes on initial run.
6. Persistence layer
- app/models/db_models.py defines the SQLAlchemy ORM schema, and app/core/database.py manages the DB connection/session.
- The app supports SQLite by default for local use and PostgreSQL as an alternative deployment database.
- This database stores the operational data the API serves: violation records, clusters, analytics aggregates, and related entities.
7. ML artifacts and inference
- The repository includes a persisted risk model in backend/model_cache/ (risk_model.pkl plus metadata).
- The backend uses this cached model for prediction endpoints rather than retraining on each request, which keeps inference fast and isolates training from serving.
8. Enforcement planner subsystem
- app/services/planner.py contains the logic for translating hotspot and risk outputs into an enforcement plan.
- This is the decision-support layer: it takes officer count as input and returns prioritized patrol zones, allocation, and patrol windows.
9. Analytics and exploration flow
- The backend exposes multiple derived views over the same violation dataset: temporal patterns, hotspot clusters, offender history, coverage gaps, and risk predictions.
- The frontend consumes these outputs to support different workflows: broad situational awareness, hotspot drill-down, repeat-offender review, and tactical planning.
10. Data and model generation dependencies
- The architecture relies on pandas/numpy/geopandas for data processing, scikit-learn DBSCAN for hotspot clustering, and XGBoost for risk prediction.
- These are not just library details; they define the pipeline boundary between raw records, spatial clusters, and operational risk scores.
11. Runtime and deployment boundary
- Backend runtime is Python 3.10+ with async web/database support via FastAPI and SQLAlchemy async; frontend runtime is Node.js 18+.
- The only external service that materially affects the UI is Mappls, which requires a frontend SDK key and supplies map rendering.
12. End-to-end flow
- Historical BTP violation data enters the backend pipeline, is cleaned and clustered, stored in the database, and summarized into analytics/model artifacts.
- The frontend then queries the API, overlays the results on a live map, and exposes drill-down and planning pages for police operations.
Main entry points: api Route layer · database.py Database · App.jsx UI app · pages Pages
Components 19
Frontend
React/Vite SPA for map-based investigation and planning
- UI app · Single-page frontend shell and routing entry
frontend/src/App.jsx - Pages · City map, analytics, hotspots, prediction, planning, coverage, and offenders screens
frontend/src/pages - Map view · City map, heatmaps, cluster markers, and popups
frontend/src/components/map/MapContainer.jsx - Shared UI · Reusable sidebar and interface elements
frontend/src/components/common - API client · Centralized backend API access for all pages
frontend/src/services/api.js - Hooks · Data fetching and Mappls loader integration
frontend/src/hooks
Backend
FastAPI services for ingestion, analytics, inference, and planning
- API app · Backend application bootstrap and route assembly
backend/app/main.py - Route layer · Feature routes for analytics, coverage, hotspots, offenders, planner, and predict
backend/app/api - Analytics · Temporal patterns and aggregate violation analysis
backend/app/api/analytics.py - Hotspots · Hotspot and spatial risk lookup endpoints
backend/app/api/hotspots.py - Offenders · Repeat-offender and offender detail endpoints
backend/app/api/offenders.py - Coverage · Patrol coverage and enforcement gap endpoints
backend/app/api/coverage.py - Predict · Live risk scoring and prediction endpoints
backend/app/api/predict.py - Planner API · Patrol planning endpoints that turn risk into allocations
backend/app/api/planner.py - Data pipeline · Initial download, cleaning, clustering, and artifact generation
backend/app/core/data_pipeline.py - Database · Connection and session management for persistent operational data
backend/app/core/database.py - ORM models · Database schema for violations, clusters, analytics, and related entities
backend/app/models/db_models.py - Plan engine · Transforms hotspot and risk outputs into patrol zones and windows
backend/app/services/planner.py - Risk model · Persisted model and metadata used for fast inference
backend/model_cache/risk_model.pkl
Connections 28
- UI app → Pages: routes · App shell organizes the task pages
- UI app → Map view: renders · Primary city map visualization boundary
- UI app → Shared UI: uses · Shared layout and controls
- UI app → API client: fetches · Central API access for all views
- UI app → Hooks: loads · Client-side data and map loader hooks
- Pages → Map view: drives · Map-centric pages consume map rendering
- Pages → API client: queries · Pages request analytics, hotspots, offenders, coverage, predictions, and plans
- Hooks → Map view: initializes · Loads the Mappls SDK before map rendering
- API client → API app: calls · Frontend talks to the FastAPI backend
- API app → Route layer: mounts · Main app wires feature routes
- Route layer → Analytics: exposes · Analytics endpoints
- Route layer → Hotspots: exposes · Hotspot lookup endpoints
- Route layer → Offenders: exposes · Offender history endpoints
- Route layer → Coverage: exposes · Coverage endpoints
- Route layer → Predict: exposes · Risk prediction endpoints
- Route layer → Planner API: exposes · Patrol planning endpoints
- Analytics → Database: reads · Aggregates stored operational data
- Hotspots → Database: reads · Spatial queries over persisted records and clusters
- Offenders → Database: reads · Retrieves offender and violation history
- Coverage → Database: reads · Coverage calculations from stored data
- Predict → Risk model: loads · Uses cached risk model artifacts
- Predict → Database: reads · Pulls features and context for scoring
- Planner API → Plan engine: delegates · Planner API calls the decision engine
- Plan engine → Database: reads · Uses hotspot and risk data from persistence
- Data pipeline → Database: builds · Initial pipeline populates the database
- Data pipeline → Risk model: produces · Pipeline prepares reusable model artifacts
- ORM models → Database: maps · ORM layer defines persisted entities
- API app → Data pipeline: bootstraps · Initial startup triggers data preparation