|
Download README.md from rohanbelsare/Smart_Routing_System: direct link, hf CLI and curl.
- Browser
- Download file 11.1 kB
-
https://huggingface.co/spaces/rohanbelsare/Smart_Routing_System/resolve/main/README.md
- Command line
-
hf download hf://spaces/rohanbelsare/Smart_Routing_System/README.md
-
curl -L -o README.md https://huggingface.co/spaces/rohanbelsare/Smart_Routing_System/resolve/main/README.md
11.1 kB
| title: Smart Routing System | |
| emoji: ๐ | |
| colorFrom: red | |
| colorTo: gray | |
| sdk: docker | |
| app_port: 7860 | |
| pinned: false | |
| # ๐ SmartER โ AI-Powered Healthcare Routing & Emergency Management | |
| > **Hackathon:** Meta PyTorch OpenEnv Hackathon x SST 2026 | |
| > **Framework:** Meta's OpenEnv + PyTorch DQN | |
| > **Tech Stack:** Python ยท FastAPI ยท React ยท Leaflet ยท Gymnasium | |
| --- | |
| ## ๐ Table of Contents | |
| 1. [Project Overview](#project-overview) | |
| 2. [Folder Structure](#folder-structure) | |
| 3. [Quick Start (3 minutes)](#quick-start) | |
| 4. [RL Environment Details](#rl-environment) | |
| 5. [Backend API](#backend-api) | |
| 6. [Frontend Pages](#frontend-pages) | |
| 7. [Training the DQN Model](#training) | |
| 8. [Simulation & Live Demo](#simulation) | |
| 9. [Deployment](#deployment) | |
| 10. [OpenEnv Submission](#openenv-submission) | |
| --- | |
| ## ๐ง Project Overview | |
| SmartER uses **Reinforcement Learning** to solve real-world emergency healthcare routing: | |
| | Problem | RL Solution | | |
| |---------|-------------| | |
| | Which hospital has capacity? | State includes live bed counts | | |
| | Which ambulance is nearest? | State includes all GPS positions | | |
| | Critical patient needs ICU? | Reward function weights ICU access | | |
| | Minimize response time? | Distance + ETA encoded as penalties | | |
| **Core RL Setup (OpenEnv-compatible):** | |
| - **Environment:** `HealthcareRoutingEnv` (Gymnasium API) | |
| - **State:** Patient severity + location + hospital beds + ambulance positions | |
| - **Actions:** Discrete โ choose (hospital, ambulance) pair | |
| - **Reward:** +100 admit, +70 ICU for critical, +50 fast response, penalties for bad choices | |
| - **Agent:** Deep Q-Network (DQN) with PyTorch | |
| --- | |
| ## ๐ Folder Structure | |
| ``` | |
| healthcare_rl/ | |
| โโโ openenv_env/ โ ๐ Core OpenEnv submission | |
| โ โโโ healthcare_env.py โ Gymnasium RL environment | |
| โ โโโ task.py โ OpenEnv Task + Grader | |
| โ โโโ register.py โ gym.make() registration | |
| โ โโโ requirements.txt | |
| โ | |
| โโโ rl/ โ PyTorch DQN agent | |
| โ โโโ dqn_agent.py โ DQN network + replay buffer | |
| โ โโโ train.py โ Training loop | |
| โ โโโ models/ โ Saved model weights (created on train) | |
| โ | |
| โโโ backend/ โ FastAPI REST + WebSocket server | |
| โ โโโ api/ | |
| โ โ โโโ main.py โ All endpoints + simulation engine | |
| โ โโโ requirements.txt | |
| โ โโโ Dockerfile | |
| โ | |
| โโโ frontend/ โ React dashboard | |
| โ โโโ src/ | |
| โ โ โโโ App.jsx | |
| โ โ โโโ App.css | |
| โ โ โโโ pages/ | |
| โ โ โโโ Dashboard.jsx โ KPIs + charts | |
| โ โ โโโ MapView.jsx โ Live Leaflet map | |
| โ โ โโโ PatientForm.jsx โ Emergency intake | |
| โ โ โโโ AdminPanel.jsx โ Hospital/ambulance management | |
| โ โโโ index.html โ Leaflet CDN loaded here | |
| โ โโโ package.json | |
| โ โโโ vite.config.js | |
| โ โโโ Dockerfile | |
| โ | |
| โโโ data/ | |
| โ โโโ sample_data.json โ Seed data (hospitals, ambulances, patients) | |
| โ | |
| โโโ demo.py โ Terminal demo (no server needed) | |
| โโโ docker-compose.yml | |
| โโโ README.md | |
| ``` | |
| --- | |
| ## โก Quick Start | |
| ### Option A โ Terminal Demo (no setup needed) | |
| ```bash | |
| # 1. Install Python dependencies | |
| pip install gymnasium numpy torch | |
| # 2. Run the demo | |
| python demo.py | |
| # Options: | |
| python demo.py --episodes 5 --steps 20 # longer demo | |
| python demo.py --random # compare with random policy | |
| python demo.py --fast # no delay between steps | |
| ``` | |
| ### Option B โ Full Stack (Backend + Frontend) | |
| **Step 1: Backend** | |
| ```bash | |
| cd backend | |
| pip install -r requirements.txt | |
| # Copy RL modules to backend path | |
| export PYTHONPATH=../openenv_env:../rl:$PYTHONPATH | |
| cd api | |
| uvicorn main:app --reload --port 8000 | |
| ``` | |
| > API is now live at http://localhost:8000 | |
| > Interactive docs: http://localhost:8000/docs | |
| **Step 2: Frontend** | |
| ```bash | |
| cd frontend | |
| cp .env.example .env # sets VITE_API_URL=http://localhost:8000 | |
| npm install | |
| npm run dev | |
| ``` | |
| > UI is now live at http://localhost:5173 | |
| ### Option C โ Docker (one command) | |
| ```bash | |
| docker-compose up --build | |
| ``` | |
| > Frontend: http://localhost:3000 | |
| > Backend: http://localhost:8000 | |
| --- | |
| ## ๐ค RL Environment | |
| ### State Space | |
| The environment builds a flat numpy array with: | |
| - Patient: severity (normalised), latitude, longitude, traffic factor | |
| - Per hospital (ร5): bed availability, ICU availability, distance, wait time | |
| - Per ambulance (ร4): distance to patient, availability flag | |
| **Total observation size: 4 + 5ร4 + 4ร2 = 32 floats** | |
| ### Action Space | |
| `Discrete(20)` โ one action per (hospital, ambulance) combination. | |
| ```python | |
| action = hospital_id * num_ambulances + ambulance_id | |
| ``` | |
| ### Reward Function | |
| ``` | |
| +100 patient admitted | |
| +70 critical patient (severity โฅ 8) assigned ICU bed | |
| +50 ambulance ETA < 10 minutes | |
| +20 ambulance ETA < 20 minutes | |
| -100 no bed available at hospital | |
| -50 ambulance is busy | |
| - distance_penalty (up to -40) | |
| - wait_time_penalty (up to -30) | |
| - severity_delay_penalty (critical patient + high ETA) | |
| ``` | |
| ### Usage | |
| ```python | |
| from openenv_env.healthcare_env import HealthcareRoutingEnv | |
| env = HealthcareRoutingEnv(render_mode="human") | |
| obs, info = env.reset() | |
| for _ in range(100): | |
| action = env.get_greedy_action() # or your RL policy | |
| obs, reward, terminated, truncated, info = env.step(action) | |
| if terminated or truncated: | |
| obs, info = env.reset() | |
| ``` | |
| --- | |
| ## ๐ Training the DQN Model | |
| ```bash | |
| cd rl | |
| # Quick training (300 episodes) | |
| python train.py | |
| # Production training | |
| python train.py --episodes 1000 --steps 150 --lr 0.0005 | |
| # With console rendering | |
| python train.py --episodes 100 --render | |
| ``` | |
| The trained model is saved to `rl/models/dqn_healthcare.pth`. | |
| **Once trained**, the backend API will automatically load it and use DQN instead of the greedy fallback. | |
| --- | |
| ## ๐ Backend API | |
| | Method | Endpoint | Description | | |
| |--------|----------|-------------| | |
| | GET | `/` | Health check | | |
| | POST | `/add_patient` | Register patient + get RL assignment | | |
| | GET | `/get_hospitals` | Live hospital bed counts | | |
| | GET | `/get_ambulances` | Live ambulance GPS positions | | |
| | POST | `/assign_resources` | RL assignment without persisting | | |
| | PUT | `/update_ambulance_location` | Update GPS position | | |
| | PUT | `/update_hospital_beds` | Update bed availability | | |
| | GET | `/get_live_tracking` | Full snapshot (map view) | | |
| | GET | `/stats` | Dashboard statistics | | |
| | GET | `/simulation/start` | Start auto simulation | | |
| | GET | `/simulation/stop` | Stop simulation | | |
| | WS | `/ws/live` | Real-time WebSocket push | | |
| **Example โ Add a patient:** | |
| ```bash | |
| curl -X POST http://localhost:8000/add_patient \ | |
| -H "Content-Type: application/json" \ | |
| -d '{"name":"Ravi Kumar","severity":9.2,"lat":12.972,"lon":77.601}' | |
| ``` | |
| --- | |
| ## ๐บ๏ธ Frontend Pages | |
| | Page | Description | | |
| |------|-------------| | |
| | **Dashboard** | KPI cards, bed occupancy bar chart, ambulance status pie, recent patients, simulation toggle | | |
| | **Live Map** | Leaflet map with moving ๐ ambulances, ๐ฅ hospitals (color-coded by occupancy), ๐ค patients. WebSocket updates every 2 seconds | | |
| | **New Emergency** | Patient intake form with severity slider โ instant RL dispatch result with reasoning | | |
| | **Admin Panel** | Edit hospital beds/ICU/wait times inline, view ambulance statuses, full assignment history | | |
| --- | |
| ## ๐ฎ Simulation & Live Demo | |
| 1. Open the Dashboard tab | |
| 2. Click **โถ๏ธ Start Simulation** | |
| 3. Switch to **Live Map** โ watch ambulances move in real-time | |
| 4. New simulated patients appear every ~6 seconds | |
| 5. Each patient is automatically assigned by the RL agent | |
| For judges: the simulation creates realistic emergency scenarios with varying severity levels and traffic conditions to showcase the RL decision-making. | |
| # ๐ Core Features | |
| ### ๐ง AI-Powered Routing (PyTorch DQN) | |
| The system's dispatch engine is powered by a custom-trained **Deep Q-Network (DQN)** built with PyTorch. | |
| - **Smart Dispatch:** Instead of using basic distance-based greedy algorithms, the AI evaluates patient severity, hospital bed/ICU availability, and real-time distance to make optimal routing decisions. | |
| - **Seamless Integration:** The FastAPI backend automatically detects and loads the trained model weights (`rl/models/dqn_healthcare.pth`) at startup. | |
| ### ๐๏ธ 3D Cyberpunk Radar (Three.js) | |
| The live tracking map features a fully interactive, real-time 3D cityscape of Solapur built with **Three.js**. | |
| - **Dynamic Visuals:** Procedurally generated hospital buildings, pulsing patient spheres, and hovering ambulances with directional aiming. | |
| - **Two-Phase Routing:** Visualizes the complete emergency lifecycle in real-timeโblue dotted paths when en-route to the patient, and green paths during hospital transport. | |
| - **Interactive:** Full drag, rotate, and zoom capabilities to monitor the dispatch grid. | |
| --- | |
| ## โ๏ธ Deployment | |
| ### Frontend โ Vercel | |
| ```bash | |
| cd frontend | |
| npm run build | |
| # Deploy dist/ folder to Vercel | |
| vercel deploy | |
| ``` | |
| ### Backend โ Render | |
| 1. Create new Web Service on Render | |
| 2. Set build command: `pip install -r backend/requirements.txt` | |
| 3. Set start command: `uvicorn backend.api.main:app --host 0.0.0.0 --port $PORT` | |
| 4. Set env var: `PYTHONPATH=./openenv_env:./rl` | |
| --- | |
| ## ๐ OpenEnv Submission | |
| The `openenv_env/` folder contains the complete OpenEnv-spec submission: | |
| ```python | |
| # Verify submission | |
| cd openenv_env | |
| python register.py # test gym.make() works | |
| python task.py # run grader on random + greedy policies | |
| ``` | |
| **Grading criteria (100 pts total):** | |
| - 40 pts โ Mean episode reward | |
| - 30 pts โ Success rate (% assignments that succeed) | |
| - 20 pts โ ICU assignment rate for critical patients | |
| - 10 pts โ Ambulance utilisation | |
| **Expected scores:** | |
| - Random policy: ~25โ35 pts | |
| - Greedy policy: ~55โ65 pts | |
| - Trained DQN: ~75โ90 pts | |
| --- | |
| ## ๐งช Running Tests | |
| ```bash | |
| # Test RL environment | |
| python -c " | |
| from openenv_env.healthcare_env import HealthcareRoutingEnv | |
| env = HealthcareRoutingEnv() | |
| obs, _ = env.reset() | |
| print('Obs shape:', obs.shape) | |
| obs, r, term, trunc, info = env.step(0) | |
| print('Reward:', r, '| Info:', info) | |
| print('โ Environment OK') | |
| " | |
| # Test grader | |
| cd openenv_env && python task.py | |
| # Test backend (requires uvicorn running) | |
| curl http://localhost:8000/stats | |
| ``` | |
| --- | |
| ## ๐ฅ Team & Acknowledgements | |
| Built for **Meta PyTorch OpenEnv Hackathon x SST 2026**. | |
| Team Members: | |
| 1. Rohan Belsare | |
| 2. Rajat Sahuji | |
| 3. Sanchita Patil | |
| Tech Stack: | |
| - [Meta OpenEnv](https://github.com/meta-pytorch/OpenEnv) โ environment spec | |
| - [HuggingFace openenv-course](https://github.com/huggingface/openenv-course) โ reference implementation | |
| - [PyTorch](https://pytorch.org) โ DQN training | |
| - [Gymnasium](https://gymnasium.farama.org) โ RL API | |
| - [FastAPI](https://fastapi.tiangolo.com) โ backend | |
| - [React](https://react.dev) + [Leaflet](https://leafletjs.com) โ frontend |