How aaditya02123/nagarsetu works
1. Overall shape
- NagarSetu is a three-part system: a React frontend, a FastAPI backend, and a separate ML inference service.
- The backend is the main application boundary: it handles authentication, complaint persistence, admin operations, and orchestration of ML predictions.
2. Frontend subsystem
- NagarSetu-Frontend is a Vite-based React app organized around user-facing pages (Home, Login, Register, Report, UserDashboard) and a separate admin area under src/pages/admin.
- src/Api.js is the client-side API boundary to the backend.
- ProtectedRoute.jsx indicates route guarding for authenticated and role-based access, especially for admin pages.
3. Backend application structure
- NagarSetu-Backend/app is organized in a typical FastAPI layered layout:
- core for configuration, database wiring, and security
- models for SQLAlchemy entities
- schemas for Pydantic request/response contracts
- routers for HTTP endpoints
- services for business logic
- middleware for request-level auth enforcement
- main.py is the backend entrypoint.
4. Backend API boundaries
- The routers split responsibilities:
- auth.py for registration/login and token handling
- complaints.py for citizen complaint submission and complaint retrieval
- admin.py for admin-only complaint management and analytics-style access
- This separation suggests a clean API surface where frontend pages map to distinct backend resources.
5. Authentication and access control
- Security is JWT-based, with password hashing handled in core/security.py.
- middleware/auth.py implies request interception or dependency-based protection for privileged routes.
- The architecture supports at least two roles: citizen and admin, with admin functions isolated behind protected endpoints and UI routes.
6. Complaint domain model
- The main business object is the complaint, represented in both database models and API schemas (models/complaint.py, schemas/complaint.py).
- Complaints include user-submitted metadata such as title, description, category, location/city, image upload, status, department assignment, and likely AI-derived fields such as severity/priority.
7. Citizen complaint flow
- A citizen submits a report from the frontend, which sends data to the backend complaint endpoints.
- The backend stores complaint data in PostgreSQL, saves uploaded media under uploads/, and forwards the image to the ML inference layer for classification.
- The result comes back as issue category, confidence, severity, and suggested department, which are then persisted with the complaint record.
8. Admin complaint management flow
- Admin pages consume backend endpoints that expose the full complaint set and operational actions.
- Admins can update status, reassign departments, inspect citizen details, review complaint images, and track resolution progress.
- The admin dashboard also consumes aggregate data for overview cards, analytics, and departmental performance.
9. Machine learning boundary
- ML is not embedded directly into the frontend; it is a distinct inference subsystem.
- NagarSetu-Backend/inference_api.py serves as the runtime inference API, while NagarSetu-ML/ contains the training/evaluation/export pipeline.
- The model artifacts live in ml_models/ and models/, including ONNX and PyTorch exports plus class labels, which indicates the deployed inference path is optimized for serving rather than training.
10. Data storage and persistence
- PostgreSQL is the primary transactional store, accessed through SQLAlchemy and initialized via Alembic configuration (alembic.ini).
- seed.py suggests initial bootstrap data for users, admins, or reference records.
- Complaint images are stored on disk in an uploads/ hierarchy, so the system has both relational data and file-based media storage.
11. Configuration and environment
- Backend runtime configuration comes from .env, including database URL, JWT secret, token expiry, and frontend origin.
- The frontend also has its own .env, indicating the UI is configured separately from the API.
- This implies a decoupled deployment model where frontend, backend, and ML service can be started and scaled independently.
12. Training vs serving split
- NagarSetu-ML is the offline ML development environment: dataset download/organization, training, evaluation, export, and dataset verification scripts live there.
- The backend repository then consumes the exported model for production inference, which is a clear boundary between model development and application runtime.
13. Operational flow summary
- Browser → React app → FastAPI backend → PostgreSQL and uploads storage → ML inference API/model artifacts → backend persists enriched complaint → admin dashboard consumes the updated complaint state.
- That is the core architecture to capture in a diagram: presentation layer, application/API layer, ML service layer, and persistence layer.
Main entry points: complaint_service.py Complaint service · main.py API app · routers Routers · auth.py Auth API
Components 20
Frontend
React Vite UI and client-side routing
- React app · Browser-facing presentation layer and routed pages
NagarSetu-Frontend/src/App.jsx - API client · Frontend boundary for backend requests
NagarSetu-Frontend/src/Api.js - Route guard · Protects authenticated and role-based routes
NagarSetu-Frontend/src/components/ProtectedRoute.jsx - Citizen pages · Home, login, register, report, dashboard, and about screens
NagarSetu-Frontend/src/pages - Admin UI · Admin dashboard, complaints, analytics, departments, and settings
NagarSetu-Frontend/src/pages/admin
Backend
FastAPI application and domain services
- API app · Backend application bootstrap and router assembly
NagarSetu-Backend/main.py - Core · Configuration, database wiring, and password/JWT security
NagarSetu-Backend/app/core - Auth middleware · Request-level protection for privileged backend routes
NagarSetu-Backend/app/middleware/auth.py - Routers · Public and admin endpoint surfaces
NagarSetu-Backend/app/routers - Auth API · Registration, login, and token handling
NagarSetu-Backend/app/routers/auth.py - Complaint API · Complaint submission and retrieval endpoints
NagarSetu-Backend/app/routers/complaints.py - Admin API · Admin-only management and analytics endpoints
NagarSetu-Backend/app/routers/admin.py - Complaint service · Orchestrates complaint persistence and ML enrichment
NagarSetu-Backend/app/services/complaint_service.py - Models · User and complaint database entities
NagarSetu-Backend/app/models - Schemas · Request and response shapes for users and complaints
NagarSetu-Backend/app/schemas - Inference API · Backend runtime that serves image classification predictions
NagarSetu-Backend/inference_api.py
ML
Inference runtime and training pipeline
- ML pipeline · Offline dataset preparation, training, evaluation, and model export
NagarSetu-ML/train.py - Model artifacts · Deployed ONNX and PyTorch artifacts plus class labels
NagarSetu-Backend/ml_models/nagarsetu.onnx
Data
Persistent storage and local artifacts
- PostgreSQL · Primary transactional persistence for users and complaints
NagarSetu-Backend/app/core/database.py - Uploads · Stored complaint images and media files
NagarSetu-Backend/uploads
Connections 21
- React app → API client: calls · UI uses the API client to reach the backend
- React app → Route guard: uses · Routes are wrapped by the auth guard where needed
- Citizen pages → React app: rendered by · Citizen pages are part of the main React app
- Admin UI → Route guard: protected by · Admin routes are gated from unprivileged access
- API client → API app: HTTP · Frontend submits auth, complaint, and admin requests
- API app → Routers: mounts · Application entrypoint registers API routers
- API app → Core: initializes · Loads config, database, and security setup
- API app → Auth middleware: applies · Installs request protection for privileged routes
- Routers → Auth API: includes · Exposes authentication endpoints
- Routers → Complaint API: includes · Exposes citizen complaint endpoints
- Routers → Admin API: includes · Exposes admin management endpoints
- Auth API → Core: uses · Auth depends on security and database wiring
- Auth API → Schemas: validates · Uses user/auth request and response contracts
- Complaint API → Complaint service: delegates · Complaint routes hand off to business logic
- Complaint service → Models: persists · Service writes complaint and user records
- Complaint service → Schemas: maps · Transforms domain data into API responses
- Complaint service → Inference API: predicts · Forwards complaint media for classification
- Inference API → Model artifacts: loads · Serving runtime uses exported model files
- Complaint service → PostgreSQL: reads/writes · Complaint and user data are stored in PostgreSQL
- Complaint service → Uploads: stores · Complaint images are saved on disk
- ML pipeline → Model artifacts: produces · Training pipeline exports deployable model artifacts