Skip to content

N1KH1LT0X1N/Road-Safety-Intervention-Chatbot

Repository files navigation

🚦 Road Safety Intervention AI

An AI-powered system for recommending road safety interventions based on IRC standards, using Google Gemini API and hybrid RAG search.

🌟 Features

Core Features

  • Multi-Interface Support: Web App (Streamlit), REST API (FastAPI), and CLI Tool
  • Hybrid Search: Combines RAG (vector similarity) and structured queries
  • AI-Powered: Google Gemini for embeddings, entity extraction, and synthesis
  • Comprehensive Database: 105+ road safety interventions from IRC standards
  • Smart Recommendations: Confidence scoring, detailed specifications, and IRC citations

πŸŽ‰ WOW Features (Advanced Capabilities)

🎨 Visual Generation

  • Auto-generate road sign images from IRC specifications
  • Create road marking diagrams (arrows, zebra crossings, chevrons, etc.)
  • Instant visual previews with accurate colors and dimensions

πŸ“„ PDF Report Generation

  • Professional, multi-page PDF reports with branding
  • Executive summaries and detailed specifications
  • Download-ready reports for stakeholders

πŸ“Έ Image Analysis with AI

  • Upload photos of road signs/markings for instant analysis
  • Automatic identification and condition assessment
  • Auto-generate search queries from images
  • Gemini Vision-powered recognition

πŸ“Š Multi-Intervention Planning

  • Create comprehensive implementation plans
  • Timeline generation with start/end dates
  • Cost aggregation and budget tracking
  • Priority-based optimization

βš–οΈ Budget Optimization

  • Maximize safety impact within budget constraints
  • Value/cost ratio analysis
  • Smart intervention selection algorithm

πŸ“ˆ Interactive Comparison

  • Compare interventions side-by-side
  • Multi-factor winner analysis
  • Trade-off identification

πŸ“Š Analytics Dashboard

  • Comprehensive statistics and insights
  • Category and problem distributions
  • Search history analytics
  • Actionable recommendations

πŸ‘‰ See WOW_FEATURES.md for detailed documentation!

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  User Interfaces                         β”‚
β”‚  - Streamlit Web App (Streamlit Cloud)  β”‚
β”‚  - REST API (Railway)                    β”‚
β”‚  - CLI Tool                              β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
              β”‚
              β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Backend Core (FastAPI + Python)        β”‚
β”‚  - Query Orchestrator                    β”‚
β”‚  - Multi-Strategy Search                β”‚
β”‚    β€’ RAG (Vector Similarity)            β”‚
β”‚    β€’ Structured Queries                  β”‚
β”‚    β€’ Hybrid Fusion (RRF)                β”‚
β”‚  - Gemini Service                        β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
              β”‚
              β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Data Layer                              β”‚
β”‚  - ChromaDB (Vector Store)              β”‚
β”‚  - SQLite/JSON (Structured Data)        β”‚
β”‚  - Cache (In-Memory TTL)                β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ“‹ Prerequisites

πŸš€ Quick Start

1. Clone the Repository

git clone <repository-url>
cd Road-Safety-Intervention-Chatbot

2. Set Up Environment

# Create virtual environment
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Install backend dependencies
cd backend
pip install -r requirements.txt
cd ..

3. Configure Environment Variables

# Copy example env file
cp .env.example .env

# Edit .env and add your Gemini API key
# GEMINI_API_KEY=your_gemini_api_key_here
# API_KEYS=key1,key2,key3  # Generate random keys for API access

4. Process Data and Create Databases

python backend/scripts/setup_database.py

This will:

  • Clean and enrich the CSV data
  • Generate embeddings using Gemini
  • Create vector store (ChromaDB)
  • Save processed data as JSON

5. Start the Backend API

cd backend
uvicorn app.main:app --reload

API will be available at: http://localhost:8000 API Docs: http://localhost:8000/docs

6. Start the Frontend (Optional)

cd frontend
pip install -r requirements.txt

# Copy frontend env
cp .env.example .env
# Edit and add API_URL and API_KEY

streamlit run app.py

Frontend will be available at: http://localhost:8501

7. Install CLI Tool (Optional)

cd cli
pip install -e .

# Configure CLI
road-safety config set api_url http://localhost:8000
road-safety config set api_key your_api_key_here

# Test CLI
road-safety search query "faded stop sign on highway"

πŸ“– Usage

Web App

  1. Open http://localhost:8501
  2. Enter your road safety query in natural language
  3. Apply filters (optional)
  4. View ranked recommendations with AI analysis

REST API

Local Development:

curl -X POST "http://localhost:8000/api/v1/search" \
  -H "X-API-Key: your_api_key" \
  -H "Content-Type: application/json" \
  -d '{
    "query": "faded stop sign on highway",
    "max_results": 5
  }'

Production (Deployed Backend):

curl -X POST "https://road-safety-intervention-chatbot-production.up.railway.app/api/v1/search" \
  -H "X-API-Key: your_api_key" \
  -H "Content-Type: application/json" \
  -d '{
    "query": "faded stop sign on highway",
    "max_results": 5
  }'

CLI

# Direct search
road-safety search query "damaged speed breaker"

# Interactive mode
road-safety interactive start

# With filters
road-safety search query "faded marking" \
  --category "Road Marking" \
  --speed-min 50 \
  --speed-max 100

πŸ”§ Configuration

Environment Variables

Variable Description Default
GEMINI_API_KEY Google Gemini API key Required
API_KEYS Comma-separated API keys Required
ENVIRONMENT Environment (development/production) development
LOG_LEVEL Logging level info
DEFAULT_SEARCH_STRATEGY Default search strategy hybrid
MAX_RESULTS Default max results 5

🐳 Docker Deployment

Using Docker Compose

# Set environment variables in .env file
cp .env.example .env

# Build and run
docker-compose up --build

# Access services:
# Backend: http://localhost:8000
# Frontend: http://localhost:8501

Individual Containers

Backend:

cd backend
docker build -t road-safety-backend .
docker run -p 8000:8000 \
  -e GEMINI_API_KEY=your_key \
  -e API_KEYS=key1,key2 \
  -e ENVIRONMENT=production \
  road-safety-backend

Frontend:

cd frontend
docker build -t road-safety-frontend .
docker run -p 8501:8501 \
  -e API_URL=http://localhost:8000 \
  -e API_KEY=your_api_key \
  road-safety-frontend

Note: The backend Dockerfile uses a multi-stage build and automatically generates interventions.json from CSV on first startup if missing.

πŸš€ Deployment

βœ… Backend on Railway (Deployed)

The backend is deployed and running on Railway:

🌐 Backend URL: https://road-safety-intervention-chatbot-production.up.railway.app

Deployment Steps:

  1. Push code to GitHub
  2. Create new project on Railway
  3. Connect GitHub repository
  4. Set build context to root (.)
  5. Set Dockerfile path to backend/Dockerfile
  6. Set environment variables in Railway:
    • GEMINI_API_KEY - Your Google Gemini API key
    • API_KEYS - Comma-separated API keys for authentication
    • ENVIRONMENT=production
  7. Railway will auto-deploy on push

Configuration Files:

  • railway.json - Railway deployment configuration
  • backend/Dockerfile - Multi-stage Docker build
  • .dockerignore - Excludes unnecessary files from build

Key Features:

  • Auto-generates interventions.json from CSV on first startup
  • Handles empty vector store gracefully (falls back to structured search)
  • CORS enabled for frontend connections

βœ… Frontend on Streamlit Cloud (Recommended)

⚠️ Note: Streamlit apps cannot run on Vercel (requires WebSocket connections). Use Streamlit Cloud instead.

Deployment Steps:

  1. Go to share.streamlit.io
  2. Sign in with your GitHub account
  3. Click "New app"
  4. Configure:
    • Repository: N1KH1LT0X1N/Road-Safety-Intervention-Chatbot
    • Branch: main
    • Main file path: frontend/app.py
  5. Click "Deploy!"
  6. After deployment, go to Settings β†’ Secrets and add:
    API_URL = "https://road-safety-intervention-chatbot-production.up.railway.app"
    API_KEY = "your-api-key-from-railway"

Alternative: Deploy Frontend on Railway

  • Create a new Railway service
  • Set root directory to frontend/
  • Use the frontend/Dockerfile
  • Set environment variables: API_URL and API_KEY

See DEPLOYMENT_STEPS.md for detailed deployment instructions.

πŸ“Š API Endpoints

Base URL: https://road-safety-intervention-chatbot-production.up.railway.app

Endpoint Method Description
/health GET Health check
/stats GET Database statistics
/api/v1/search POST Search interventions
/api/v1/interventions GET List interventions
/api/v1/interventions/{id} GET Get specific intervention
/api/v1/interventions/categories/list GET List categories
/api/v1/interventions/problems/list GET List problem types

API Documentation: Visit https://road-safety-intervention-chatbot-production.up.railway.app/docs for interactive API docs.

Authentication: All API endpoints require an X-API-Key header with a valid API key.

πŸ§ͺ Testing

# Run test queries
python backend/scripts/test_queries.py

# Run unit tests (if implemented)
cd backend
pytest

πŸ“ Project Structure

Road-Safety-Intervention-Chatbot/
β”œβ”€β”€ backend/               # FastAPI backend
β”‚   β”œβ”€β”€ app/
β”‚   β”‚   β”œβ”€β”€ api/          # API routes
β”‚   β”‚   β”œβ”€β”€ core/         # Business logic
β”‚   β”‚   β”œβ”€β”€ models/       # Data models
β”‚   β”‚   β”œβ”€β”€ services/     # Services
β”‚   β”‚   └── utils/        # Utilities
β”‚   β”œβ”€β”€ data/             # Data files
β”‚   β”‚   β”œβ”€β”€ raw/         # Raw CSV data
β”‚   β”‚   β”œβ”€β”€ processed/   # Processed JSON data
β”‚   β”‚   └── chroma_db/   # Vector store
β”‚   β”œβ”€β”€ scripts/          # Setup scripts
β”‚   β”œβ”€β”€ Dockerfile        # Docker configuration
β”‚   └── requirements.txt  # Python dependencies
β”œβ”€β”€ frontend/             # Streamlit web app
β”‚   β”œβ”€β”€ utils/            # Utilities (API client)
β”‚   β”œβ”€β”€ app.py            # Main app
β”‚   β”œβ”€β”€ Dockerfile        # Docker configuration
β”‚   β”œβ”€β”€ requirements.txt  # Python dependencies
β”‚   └── vercel.json       # Vercel config (not used - use Streamlit Cloud)
β”œβ”€β”€ cli/                  # CLI tool
β”‚   └── road_safety_cli/  # CLI package
β”œβ”€β”€ docs/                 # Documentation
β”œβ”€β”€ railway.json          # Railway deployment config
β”œβ”€β”€ .dockerignore         # Docker ignore rules
β”œβ”€β”€ DEPLOYMENT_STEPS.md   # Deployment guide
└── README.md             # This file

🀝 Contributing

  1. Fork the repository
  2. Create feature branch (git checkout -b feature/AmazingFeature)
  3. Commit changes (git commit -m 'Add AmazingFeature')
  4. Push to branch (git push origin feature/AmazingFeature)
  5. Open Pull Request

πŸ“ License

This project is licensed under the MIT License.

πŸ™ Acknowledgments

  • Google Gemini API for AI capabilities
  • IRC Standards for road safety data
  • FastAPI, Streamlit, and Typer frameworks

πŸ“§ Contact

For questions or support, please open an issue on GitHub.


Built with ❀️ for Road Safety

About

πŸ›‘ An AI-powered system for recommending road safety interventions based on IRC standards, using Google Gemini API and hybrid RAG search.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors