Skip to content

404SolutionsGH/TrotroAI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TrotroLive AI Service

🚀 Quick Start

Installation

# Clone and setup
cd AI-Service
python -m venv venv
source venv/bin/activate  # Windows: venv\Scripts\activate
pip install -r requirements-minimal.txt

🏗️ Model Training

  1. Prepare data (CSV format with 'question' and 'answer' columns)
  2. Run training:
python train.py --data data/qa_pairs.csv --model distilbert --epochs 3

🚀 Using the Model

Start API Server

uvicorn app:app --reload --host 0.0.0.0

Example API Request

import requests
response = requests.post("http://localhost:8000/ask", json={"question": "How to get from Circle to Madina?"})
print(response.json())

📊 Model Performance

  • 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.

🚀 Features

Core AI Capabilities

  • 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

Advanced Features

  • 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

Integration Capabilities

  • 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

📁 Project Structure

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

🔧 Installation & Setup

1. Prerequisites

  • Python 3.8+
  • Docker (optional)
  • API.trotro.live credentials (optional)
  • OpenAI API key (optional)

2. Environment Setup

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

3. Install Dependencies

# Install Python dependencies
pip install -r requirements.txt

# Install system dependencies
sudo apt-get install -y postgresql-client

2. Environment Configuration

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

3. Django Integration

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')),
]

4. Database Migration

Run Django migrations to ensure the stations app is properly set up:

python manage.py migrate

🎯 Usage

Command Line Interface

# 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

Web Interface

  1. Start Django development server:

    python manage.py runserver
  2. Access AI chat interface:

    http://localhost:8000/ai/chat/
    

API Endpoints

Ask a Question

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}'

Get Suggestions

curl http://localhost:8000/ai/api/suggestions/

Model Status

curl http://localhost:8000/ai/model/status/

WhatsApp Integration

  1. Setup WhatsApp Business API:

    • Create a Facebook Developer account
    • Set up WhatsApp Business API
    • Get access token and phone number ID
  2. Configure Webhook:

    Webhook URL: https://your-domain.com/ai/whatsapp/webhook/
    Verify Token: your_verify_token
    
  3. Test WhatsApp Bot:

    python whatsapp_integration.py

🤖 AI System Architecture

TrotroAI Class

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

TrotroMCP Class

Multi-Channel Platform supporting:

  • WhatsApp: Formatted responses for WhatsApp
  • Telegram: HTML-formatted messages
  • Web: JSON responses for web interface
  • API: Structured API responses

Sample Question Types

  1. Station Information:

    • "Where is Kaneshie station?"
    • "What city is Madina in?"
    • "Is Circle a bus stop?"
  2. Route Planning:

    • "How do I get from Madina to Circle?"
    • "What's the best route to Kumasi?"
    • "Can I travel from Accra to Lagos?"
  3. Fare Inquiries:

    • "How much from Madina to Circle?"
    • "What's the fare to Kumasi?"
    • "Are there discounts for students?"
  4. General Information:

    • "What is a trotro?"
    • "How do I pay for trotro?"
    • "What transport options are available?"

🔧 Advanced Configuration

Fine-tuning the Model

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)

Custom Embeddings

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')

📊 Performance Metrics

Model Performance

  • 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

API Integration

  • Deepseek API: Enhanced responses for complex queries
  • Fallback System: Local model when API unavailable
  • Rate Limiting: Built-in request throttling

🔐 Security Features

  • 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

📈 Analytics & Monitoring

Available Metrics

  • Total questions processed
  • Question type distribution
  • Confidence score distribution
  • Response time analytics
  • API usage statistics

Access Analytics

curl http://localhost:8000/ai/admin/analytics/

🛠️ Development & Deployment

Local Development

  1. Run Tests:

    python -m pytest AI/tests/
  2. Debug Mode:

    python enhanced_ai_system.py --debug

Production Deployment

  1. Environment Setup:

    pip install -r requirements.txt
    python manage.py collectstatic
  2. Model Export:

    python enhanced_ai_system.py --export
  3. 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"]

🤝 Contributing

Adding New Features

  1. New Question Types:

    • Add to generate_sample_questions() method
    • Update question type enum
    • Add specific handlers
  2. New Channels:

    • Extend TrotroMCP class
    • Add channel-specific formatting
    • Update URL patterns
  3. API Integrations:

    • Add new API clients
    • Implement fallback mechanisms
    • Update configuration

Code Style

  • Follow PEP 8 guidelines
  • Use type hints
  • Document all public methods
  • Add unit tests for new features

📚 API Reference

Core Classes

TrotroAI

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

TrotroMCP

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

Response Format

{
    "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"
}

🐛 Troubleshooting

Common Issues

  1. Import Errors:

    pip install -r requirements.txt
    export PYTHONPATH="${PYTHONPATH}:/path/to/project"
  2. Django Integration:

    python manage.py migrate
    python manage.py collectstatic
  3. API Connection:

    • Check internet connection
    • Verify API keys
    • Check rate limits

Debug Mode

import logging
logging.basicConfig(level=logging.DEBUG)

📞 Support

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

AI Transport Predictions

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.

🚀 Quick Start

1. Install Dependencies

# Install AI dependencies
pip install -r requirements.txt

# Install Django dependencies (if not already installed)
pip install django pandas numpy

2. Run AI Models

# 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

📁 File Structure

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

🤖 AI Models

1. Route Optimization Model

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

2. Demand Prediction Model

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

3. Stop Clustering Model

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

📊 Data Sources

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

🔧 Configuration

City Support

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

Output Options

# 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

📈 Results Interpretation

Route Optimization Results

{
  "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"
  ]
}

Demand Prediction Results

{
  "station_id": "stop_456",
  "hour": 8,
  "predicted_demand": 45,
  "confidence": 0.92,
  "peak_hour": true
}

Stop Clustering Results

{
  "cluster_id": 1,
  "stops": ["stop_1", "stop_2", "stop_3"],
  "center_lat": 5.5600,
  "center_lon": -0.2057,
  "recommendation": "consolidate"
}

🧠 AI Q&A CLI Tool

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 Data & Troubleshooting

  • 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.

🚦 Roadmap / TODO

  • 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.

🛠️ Development

Adding New AI Models

  1. Create a new Python file in the AI folder
  2. Implement the model class with required methods
  3. Add the model to the run_all_ai_models.py command
  4. Update the README with usage instructions

Example Model Structure

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

Integration with Django

The AI models integrate with Django through:

  1. Database Access: Models read data from Django ORM
  2. Management Commands: AI models run via Django commands
  3. Results Storage: Results can be saved to database
  4. API Integration: Results can be exposed via Django REST API

📋 Requirements

Python Dependencies

  • 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

System Requirements

  • Python 3.8+
  • Django 3.2+
  • PostgreSQL (recommended for large datasets)
  • 4GB+ RAM for large datasets

🚀 Deployment

Production Setup

  1. Install dependencies:
pip install -r requirements.txt
  1. Configure database:
python manage.py migrate
  1. Import data:
python manage.py import_all_datasets --city accra
  1. Run AI models:
python manage.py run_all_ai_models --city accra

Scheduled Execution

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

📚 Additional Resources

🤝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Implement your AI model
  4. Add tests
  5. Submit a pull request

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.


Ready to optimize the future of urban mobility! 🚀

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •