Real-time traffic simulation with AI-powered voice assistance and 3D visualization
Features β’ Quick Start β’ Architecture β’ Documentation
- Real-time Speech Recognition with AssemblyAI WebSocket API
- AI-Powered Responses using Google Gemini 2.0
- Text-to-Speech with ElevenLabs
- Conversation Memory - Contextual 10-message history
- Anti-Spam Protection - Prevents request flooding
- SUMO Integration - Realistic traffic simulation
- Multi-Vehicle Support - Handle 100+ vehicles
- Reinforcement Learning - PPO-trained autonomous agents
- V2X Communication - Vehicle-to-Everything hazard detection
- Dynamic Hazard Broadcasting - Real-time hazard sharing
- Real-Time Rendering with React Three Fiber
- Smooth Camera Tracking - Cinematic vehicle following
- Route Visualization - Dynamic path display
- Interactive Map - SUMO network in 3D
- Hazard Markers - Visual hazard indicators
- WebSocket Server - Low-latency vehicle data streaming
- Live Updates - 20Hz simulation data broadcast
- Automatic Reconnection - Robust connection handling
- JSON Protocol - Structured vehicle & hazard data
- Python 3.10+
- Node.js 18+
- SUMO 1.24.0 (Download)
- Git
git clone https://github.com/yousefalwahami/hazardnet.git
cd hazardnetcd backend
# Create virtual environment
python -m venv venv
# Activate virtual environment
# Windows:
venv\Scripts\activate
# Mac/Linux:
source venv/bin/activate
# Install dependencies
pip install -r requirements.txt
# Create .env file
copy .env.example .env # Windows
# cp .env.example .env # Mac/Linux
# Add your API keys to .env:
# GEMINI_API_KEY=your_gemini_key_here
# ELEVENLABS_API_KEY=your_elevenlabs_key_hereGet API Keys:
- π§ Gemini: https://aistudio.google.com/app/apikey
- π ElevenLabs: https://elevenlabs.io/ β Profile β API Keys
- π€ AssemblyAI: https://www.assemblyai.com/ β Dashboard
# Run the backend server
python app.pyServer runs on http://localhost:5000
cd frontend
# Install dependencies
npm install
# Create .env file (optional for AssemblyAI)
echo VITE_ASSEMBLYAI_API_KEY=your_assemblyai_key_here > .env
# Run development server
npm run devFrontend runs on http://localhost:5173
cd simulation
# Install dependencies (if using separate venv)
pip install -r requirements.txt
# Ensure SUMO_HOME is set
# Windows: set SUMO_HOME=C:\Program Files (x86)\Eclipse\Sumo
# Mac/Linux: export SUMO_HOME=/usr/share/sumo
# Run simulation with WebSocket server
python eval_demo_simple.pyWebSocket server runs on ws://localhost:8765
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β HazardNet System β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
ββββββββββββββββββββ ββββββββββββββββββββ ββββββββββββββββββββ
β SUMO Simulation β β WebSocket β β React Frontend β
β β ββββββ> β Port 8765 β ββββββ> β β
β eval_demo_ β Real- β β Live β MapViewer 3D β
β simple.py β time β Vehicle Data β Updates β + Voice UI β
β β Data β Hazard Events β β β
ββββββββββββββββββββ ββββββββββββββββββββ ββββββββββββββββββββ
β β
ββββββββββββββββββββ ββββββββββββββββββββ
β V2X Hazard β β Voice Assistant β
β Detection β β β
β - V2E Events β β AssemblyAI STT β
β - V2V Comms β β Gemini AI β
β - Broadcasting β β ElevenLabs TTS β
ββββββββββββββββββββ ββββββββββββββββββββ
β
ββββββββββββββββββββ
β Flask Backend β
β Port 5000 β
β AI Processing β
ββββββββββββββββββββ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β User speaks β AssemblyAI β Gemini AI β ElevenLabs β Audio β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β SUMO β TraCI β Python β WebSocket β React β Three.js β GPU β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
hazardnet/
βββ backend/ # Python Flask API server
β βββ app.py # Main Flask application
β βββ services/
β β βββ gemini_service.py # Gemini AI integration
β β βββ elevenlabs_service.py # Text-to-speech
β βββ requirements.txt
β
βββ frontend/ # React + TypeScript frontend
β βββ src/
β β βββ components/
β β β βββ MapViewer.tsx # 3D SUMO visualization
β β β βββ pages/
β β β β βββ driver.tsx # Driver interface
β β β β βββ system.tsx # System view
β β β βββ voice/ # Voice UI components
β β βββ hooks/
β β βββ useVoiceAssistant.ts # Voice logic
β βββ package.json
β
βββ simulation/ # SUMO traffic simulation
β βββ eval_demo_simple.py # Main simulation with WebSocket
β βββ train_ppo_multi.py # RL training script
β βββ envs/
β β βββ city_grid_multi_env.py # Custom SUMO environment
β βββ sim/
β βββ city.net.xml # SUMO network
β βββ city.sumocfg # SUMO configuration
β
βββ docs/ # Documentation
βββ VOICE_ASSISTANT_SETUP.md
βββ INTEGRATION_GUIDE.md
βββ ASSEMBLYAI_SETUP.md
- Navigate to http://localhost:5173
- Toggle to "Driver" view (top-right)
- Click the microphone button
- Speak your command:
- "Where is the nearest gas station?"
- "What's the traffic like ahead?"
- "Navigate to the hospital"
- "Show me hazards on my route"
Open browser DevTools (F12) to see real-time logs:
β
AssemblyAI WebSocket connected
π§ Session started
π€ Partial: Hello how
β
Final: Hello how are you
π Transcript received: Hello how are you
π€ Response received: I'm doing well, thanks for asking!
In SUMO GUI:
Space- Play/PauseD- Delay (slower simulation)A- AccelerateS- Single step
Python console shows:
π Vehicle count: 45
π‘ Broadcasting to 1 clients
β οΈ Hazard detected at (123.45, 678.90)
- π Voice Assistant Setup - Complete voice system guide
- π€ AssemblyAI Setup - Speech recognition configuration
- π Integration Guide - Connect SUMO to frontend
- π¬ Voice Console - Debugging voice features
| Method | Endpoint | Description |
|---|---|---|
GET |
/health |
Health check |
POST |
/api/voice/process |
Process voice command |
POST |
/api/voice/text-to-speech |
Generate speech audio |
| Event | Description | Data Format |
|---|---|---|
connect |
Client connected | - |
message |
Vehicle update | { vehicles: [...], hazards: [...], communications: [...] } |
disconnect |
Client disconnected | - |
{
"vehicles": [
{
"id": "vehicle_0",
"x": 123.45,
"y": 678.90,
"speed": 10.5,
"angle": 90.0,
"type": "passenger",
"edge": "edge_123",
"route": [{"x": 130, "y": 680}, ...],
"isEgo": false
}
],
"hazards": [
{
"type": "obstacle",
"position": {"x": 100, "y": 200},
"severity": "high",
"timestamp": 1234567890
}
],
"communications": [
{
"sender": "vehicle_0",
"receiver": "vehicle_1",
"type": "V2V",
"message": "hazard_ahead"
}
]
}Edit simulation/eval_demo_simple.py:
# Simulation speed
SIMULATION_STEP_LENGTH = 0.05 # 50ms per step
# Hazard detection range
HAZARD_DETECTION_RADIUS = 50.0 # meters
# V2X communication range
V2X_RANGE = 100.0 # metersEdit frontend/src/components/MapViewer.tsx:
// Camera follow distance
const cameraDistance = 6; // units behind vehicle
// Camera height
const cameraHeight = 4; // units above ground
// Smoothing factor (0-1)
const lerpFactor = 0.05; // lower = smootherEdit frontend/src/hooks/useVoiceAssistant.ts:
// Conversation history length
const MAX_HISTORY = 10; // messages
// Audio sample rate
const SAMPLE_RATE = 16000; // HzWebSocket not connecting
- Ensure
eval_demo_simple.pyis running - Check port 8765 is not blocked by firewall
- Verify frontend WebSocket URL:
ws://localhost:8765
No vehicles showing in 3D view
- Check browser console for errors
- Verify SUMO simulation has spawned vehicles
- Ensure WebSocket is sending data (check Python console)
Voice assistant not working
- Grant microphone permissions in browser
- Check API keys in
.envfiles - Verify backend is running on port 5000
- Open DevTools console to see error messages
SUMO won't start
- Set
SUMO_HOMEenvironment variable:- Windows:
C:\Program Files (x86)\Eclipse\Sumo - Mac:
/usr/local/opt/sumo/share/sumo - Linux:
/usr/share/sumo
- Windows:
- Add
%SUMO_HOME%\binto PATH - Verify installation:
sumo --version
Camera jittering in 3D view
- Already fixed! Uses ref-based Vector3 lerping
- Adjust
lerpFactorin MapViewer.tsx if needed - Ensure stable 60 FPS (check GPU usage)
- Reduce WebSocket broadcast rate:
# In eval_demo_simple.py
time.sleep(0.05) # 20 Hz instead of 60 Hz- Enable frustum culling:
// In MapViewer.tsx
<mesh frustumCulled={true}>- Use Level of Detail (LOD):
<Lod distances={[0, 20, 50]}>
<DetailedCar />
<SimpleCar />
<DotCar />
</Lod>cd backend
pytest tests/cd frontend
npm testcd simulation
python -m pytest tests/Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- SUMO - Traffic simulation platform
- React Three Fiber - 3D rendering in React
- AssemblyAI - Real-time speech recognition
- Google Gemini - AI conversation engine
- ElevenLabs - Natural text-to-speech
β Star this repo if you find it helpful!
Made with β€οΈ and lots of β