# Clone and setup
cd AI-Service
python -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate
pip install -r requirements-minimal.txt
- Prepare data (CSV format with 'question' and 'answer' columns)
- Run training:
python train.py --data data/qa_pairs.csv --model distilbert --epochs 3
uvicorn app:app --reload --host 0.0.0.0
import requests
response = requests.post("http://localhost:8000/ask", json={"question": "How to get from Circle to Madina?"})
print(response.json())
- Accuracy: 92% (validation set)
- Response time: < 500ms
- Supports: English, Twi, Ga
A robust microservice for AI-powered transportation analytics and predictions. This service provides intelligent insights into Ghana's trotro transportation network through advanced machine learning models and data processing.
- Question Answering: Advanced NLP for transport queries
- Demand Prediction: Machine learning-based demand forecasting
- Route Optimization: Intelligent route planning
- Real-time Analytics: Live transport data analysis
- Multi-language Support: English and local languages
- Automated Data Pipeline: Real-time data synchronization
- Model Versioning: Track and manage ML model versions
- Performance Metrics: Monitor model accuracy and performance
- API Integration: REST API for external integrations
- Real-time Learning: Continuous model improvement
- REST API Integration: Connects with Trotro API (api.trotro.live)
- FastAPI Server: Modern asynchronous API server
- WebSocket Support: Real-time updates
- Container Ready: Docker support
AI-Service/
├── core/
│ ├── ai_engine.py # Main AI engine with model management
│ ├── data_pipeline.py # Data synchronization and preprocessing
│ └── models.py # Model definitions and status tracking
├── app.py # FastAPI application server
├── views.py # API endpoints
├── urls.py # URL routing
├── models/ # Trained ML models
├── requirements.txt # Python dependencies
└── README.md # Documentation
- Python 3.8+
- Docker (optional)
- API.trotro.live credentials (optional)
- OpenAI API key (optional)
Create a .env
file with the following variables:
# Database Configuration
DATABASE_URL=postgresql://user:password@localhost/trotro
# API Keys
DEEPSEEK_API_KEY=your_deepseek_key
OPENAI_API_KEY=your_openai_key
# Service Configuration
BACKEND_SERVICE_URL=https://api.trotro.live
SERVICE_HOST=0.0.0.0
SERVICE_PORT=8001
# Model Configuration
MODEL_CACHE_DIR=./models
EMBEDDING_MODEL=sentence-transformers/all-MiniLM-L6-v2
MAX_CONTEXT_LENGTH=512
# Install Python dependencies
pip install -r requirements.txt
# Install system dependencies
sudo apt-get install -y postgresql-client
Add to your .env
file:
# AI Configuration
DEEPSEEK_API_KEY=sk-0ad33e6b96df4866b8c7baabb5983f8d
# WhatsApp Configuration (optional)
WHATSAPP_ACCESS_TOKEN=your_whatsapp_access_token
WHATSAPP_PHONE_NUMBER_ID=your_phone_number_id
WHATSAPP_VERIFY_TOKEN=your_verify_token
Add to your INSTALLED_APPS
in Django settings:
INSTALLED_APPS = [
# ... other apps
'AI',
]
Add to your main urls.py
:
urlpatterns = [
# ... other patterns
path('ai/', include('AI.urls')),
]
Run Django migrations to ensure the stations app is properly set up:
python manage.py migrate
# Interactive AI chat
python enhanced_ai_system.py
# Ask a specific question
python enhanced_ai_system.py --question "How do I get from Madina to Circle?"
# Use Deepseek API
python enhanced_ai_system.py --question "What is a trotro?" --use-api
# Train the model
python enhanced_ai_system.py --train
# Export the model
python enhanced_ai_system.py --export
-
Start Django development server:
python manage.py runserver
-
Access AI chat interface:
http://localhost:8000/ai/chat/
curl -X POST http://localhost:8000/ai/api/question/ \
-H "Content-Type: application/json" \
-d '{"question": "How do I get from Madina to Circle?", "use_api": true}'
curl http://localhost:8000/ai/api/suggestions/
curl http://localhost:8000/ai/model/status/
-
Setup WhatsApp Business API:
- Create a Facebook Developer account
- Set up WhatsApp Business API
- Get access token and phone number ID
-
Configure Webhook:
Webhook URL: https://your-domain.com/ai/whatsapp/webhook/ Verify Token: your_verify_token
-
Test WhatsApp Bot:
python whatsapp_integration.py
Core AI system with the following capabilities:
- Question Generation: Automatically generates training questions from database
- Semantic Matching: Uses sentence transformers for similarity matching
- API Integration: Deepseek API for advanced responses
- Model Management: Save, load, and export model states
- Fine-tuning: Continuous learning from new data
Multi-Channel Platform supporting:
- WhatsApp: Formatted responses for WhatsApp
- Telegram: HTML-formatted messages
- Web: JSON responses for web interface
- API: Structured API responses
-
Station Information:
- "Where is Kaneshie station?"
- "What city is Madina in?"
- "Is Circle a bus stop?"
-
Route Planning:
- "How do I get from Madina to Circle?"
- "What's the best route to Kumasi?"
- "Can I travel from Accra to Lagos?"
-
Fare Inquiries:
- "How much from Madina to Circle?"
- "What's the fare to Kumasi?"
- "Are there discounts for students?"
-
General Information:
- "What is a trotro?"
- "How do I pay for trotro?"
- "What transport options are available?"
Add custom Q&A pairs:
from AI.enhanced_ai_system import TrotroAI
ai = TrotroAI()
additional_qa = [
{
'question': 'How do I get to the airport?',
'answer': 'You can take a trotro from Circle to Kotoka Airport...',
'context': 'airport_travel',
'type': 'route_planning'
}
]
ai.fine_tune_model(additional_qa)
Replace the default embedding model:
class CustomTrotroAI(TrotroAI):
def load_embedding_model(self):
# Load your custom model
self.tokenizer = AutoTokenizer.from_pretrained('your-model')
self.model = AutoModel.from_pretrained('your-model')
- Question Types: 5 categories (station_info, route_info, trip_planning, general, api_response)
- Confidence Scoring: 0.0 - 1.0 scale
- Response Time: < 2 seconds average
- Accuracy: 85%+ for trained question types
- Deepseek API: Enhanced responses for complex queries
- Fallback System: Local model when API unavailable
- Rate Limiting: Built-in request throttling
- Environment Variables: Secure API key management
- Input Validation: Sanitized user inputs
- Error Handling: Graceful error responses
- Rate Limiting: Protection against abuse
- Authentication: Admin-only model management
- Total questions processed
- Question type distribution
- Confidence score distribution
- Response time analytics
- API usage statistics
curl http://localhost:8000/ai/admin/analytics/
-
Run Tests:
python -m pytest AI/tests/
-
Debug Mode:
python enhanced_ai_system.py --debug
-
Environment Setup:
pip install -r requirements.txt python manage.py collectstatic
-
Model Export:
python enhanced_ai_system.py --export
-
Docker Deployment:
FROM python:3.9 COPY AI/ /app/AI/ WORKDIR /app RUN pip install -r AI/requirements.txt CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
-
New Question Types:
- Add to
generate_sample_questions()
method - Update question type enum
- Add specific handlers
- Add to
-
New Channels:
- Extend
TrotroMCP
class - Add channel-specific formatting
- Update URL patterns
- Extend
-
API Integrations:
- Add new API clients
- Implement fallback mechanisms
- Update configuration
- Follow PEP 8 guidelines
- Use type hints
- Document all public methods
- Add unit tests for new features
class TrotroAI:
def __init__(self, deepseek_api_key: str = None)
def answer_question(self, question: str, use_api: bool = False) -> Dict
def fine_tune_model(self, additional_qa: List[Dict])
def save_model()
def load_model()
def export_model(self, format: str = 'json') -> str
class TrotroMCP:
def __init__(self, ai_system: TrotroAI)
def process_message(self, message: str, channel: str, sender_id: str) -> Dict
def handle_whatsapp(self, message: str, sender_id: str) -> Dict
def handle_telegram(self, message: str, sender_id: str) -> Dict
def handle_web(self, message: str, session_id: str) -> Dict
def handle_api(self, message: str, client_id: str) -> Dict
{
"success": true,
"question": "How do I get from Madina to Circle?",
"answer": "To travel from Madina to Circle...",
"confidence": 0.85,
"context": "route_planning",
"type": "trip_planning"
}
-
Import Errors:
pip install -r requirements.txt export PYTHONPATH="${PYTHONPATH}:/path/to/project"
-
Django Integration:
python manage.py migrate python manage.py collectstatic
-
API Connection:
- Check internet connection
- Verify API keys
- Check rate limits
import logging
logging.basicConfig(level=logging.DEBUG)
For support and questions:
- GitHub Issues: Submit bug reports and feature requests
- Email: support@trotro.live
- Documentation: Check this README and code comments
Built with ❤️ for Ghana's transportation ecosystem
This folder contains AI-powered transport optimization and prediction tools for the Trotro system. The AI models analyze transport data to provide insights for route optimization, demand prediction, and stop clustering.
# Install AI dependencies
pip install -r requirements.txt
# Install Django dependencies (if not already installed)
pip install django pandas numpy
# Run all AI models for all cities
python manage.py run_all_ai_models
# Run AI models for specific city
python manage.py run_all_ai_models --city accra
# Run specific models
python manage.py run_all_ai_models --models route_optimization demand_prediction
# Save results to database
python manage.py run_all_ai_models --save-to-db
AI/
├── README.md # This file
├── requirements.txt # Python dependencies
├── route_optimizer.py # Route optimization AI model
├── demand_predictor.py # Demand prediction AI model
├── visualization.py # Data visualization and clustering
├── dataset_explorer.py # Data exploration tools
├── quick_start.py # Quick demonstration
└── output/ # Generated results
├── route_optimization_results.json
├── demand_predictions.json
├── stop_clusters.json
└── all_ai_results.json
Purpose: Optimize transport routes for efficiency and cost reduction
Features:
- Analyzes route efficiency based on distance, stops, and trips
- Identifies optimization opportunities
- Suggests route improvements
- Calculates efficiency scores
Usage:
python manage.py run_ai_predictions --model route_optimization --city accra
Output: route_optimization_results.json
Purpose: Predict passenger demand by time and location
Features:
- Time series analysis of passenger demand
- Peak hour identification
- Predictive scheduling recommendations
- Demand pattern analysis
Usage:
python manage.py run_ai_predictions --model demand_prediction --city kumasi
Output: demand_predictions.json
Purpose: Identify redundant stops and suggest consolidation
Features:
- Geographic clustering of nearby stops
- Redundancy analysis
- Consolidation recommendations
- Coverage optimization
Usage:
python manage.py run_ai_predictions --model stop_clustering --city lagos
Output: stop_clusters.json
The AI models use data from the Django database, which includes:
- Stations: Bus stops with coordinates and metadata
- Routes: Transport routes with schedules
- Trips: Individual journey data
- Fares: Pricing information
- Shapes: Geographic route shapes
The AI models support multiple cities:
- Accra, Ghana
- Kumasi, Ghana
- Lagos, Nigeria
- Nairobi, Kenya
- Kampala, Uganda
- Freetown, Sierra Leone
- Bamako, Mali
- Abidjan, Ivory Coast
- Addis Ababa, Ethiopia
- Alexandria, Egypt
# Specify output directory
python manage.py run_all_ai_models --output-dir AI/results
# Save results to database
python manage.py run_all_ai_models --save-to-db
# Run specific models only
python manage.py run_all_ai_models --models route_optimization stop_clustering
{
"route_id": "route_123",
"current_efficiency": 0.75,
"optimized_efficiency": 0.89,
"improvement": 0.14,
"recommendations": [
"Reduce stops by 3",
"Optimize route shape",
"Adjust timing"
]
}
{
"station_id": "stop_456",
"hour": 8,
"predicted_demand": 45,
"confidence": 0.92,
"peak_hour": true
}
{
"cluster_id": 1,
"stops": ["stop_1", "stop_2", "stop_3"],
"center_lat": 5.5600,
"center_lon": -0.2057,
"recommendation": "consolidate"
}
Ask natural language questions about routes, stations, and fares:
python ai_qa.py --question "Madina to Krofrom" --city=accra
python ai_qa.py --question "Kumasi to Lagos"
- Fuzzy-matches station names and finds direct routes and fares (if available).
- City names are case-insensitive (e.g.,
accra
,Accra
,ACCRA
). - If no fare or direct route is found, you'll get a clear message.
- Live ORM Data: All tools use your Django database directly—no CSVs. Changes in the admin or database are instantly reflected.
- Robust to Bad Data: Invalid coordinates are skipped; missing tables or empty queries are handled gracefully.
- Common Issues:
- Empty DataFrames: Check your city name (any case is accepted).
- No Results: Try alternate spellings or partial names.
- Debug Output: If a column is missing, the tool will print available columns to help you debug.
- AI Q&A: Multi-hop, cross-city, and landmark-based queries; fare estimation using distance/city averages.
- Open Data: User-contributed fare updates via web/API.
- Real-Time & Analytics: Live vehicle data integration; interactive dashboards.
- User Experience: Web UI for Q&A; advanced error handling and suggestions.
- Create a new Python file in the AI folder
- Implement the model class with required methods
- Add the model to the
run_all_ai_models.py
command - Update the README with usage instructions
class MyAIModel:
def __init__(self, data):
self.data = data
def predict(self):
# Implement prediction logic
return results
def evaluate(self):
# Implement evaluation logic
return metrics
The AI models integrate with Django through:
- Database Access: Models read data from Django ORM
- Management Commands: AI models run via Django commands
- Results Storage: Results can be saved to database
- API Integration: Results can be exposed via Django REST API
- torch>=1.9.0
- pandas>=1.3.0
- numpy>=1.21.0
- scikit-learn>=1.0.0
- matplotlib>=3.4.0
- seaborn>=0.11.0
- plotly>=5.0.0
- folium>=0.12.0
- geopy>=2.2.0
- scipy>=1.7.0
- xgboost>=1.5.0
- lightgbm>=3.3.0
- joblib>=1.1.0
- tqdm>=4.62.0
- Python 3.8+
- Django 3.2+
- PostgreSQL (recommended for large datasets)
- 4GB+ RAM for large datasets
- Install dependencies:
pip install -r requirements.txt
- Configure database:
python manage.py migrate
- Import data:
python manage.py import_all_datasets --city accra
- Run AI models:
python manage.py run_all_ai_models --city accra
Set up cron jobs for regular AI analysis:
# Daily AI analysis for Accra
0 2 * * * cd /path/to/trotro && python manage.py run_all_ai_models --city accra
# Weekly full analysis
0 3 * * 0 cd /path/to/trotro && python manage.py run_all_ai_models
- Fork the repository
- Create a feature branch
- Implement your AI model
- Add tests
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
Ready to optimize the future of urban mobility! 🚀