Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

11 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Student Score Prediction API πŸŽ“

A FastAPI-based machine learning service that predicts student assessment scores based on their learning characteristics. The API uses a trained ML model to predict student performance based on comprehension, attention, focus, retention, and engagement time metrics.

πŸš€ Live Demo

API Base URL: https://student-backend-2919.onrender.com

API Documentation: https://student-backend-2919.onrender.com/docs

πŸ“‹ Features

  • Machine Learning Predictions: Predict student assessment scores using trained ML models
  • Batch Processing: Support for predicting multiple students (up to 200) in a single request
  • Authentication & Authorization: JWT-based authentication with API key validation
  • Caching: Redis-based caching for improved performance
  • Monitoring: Prometheus metrics integration for monitoring API performance
  • Logging: Comprehensive logging middleware for request tracking
  • Containerized: Docker and Docker Compose support for easy deployment

πŸ› οΈ Tech Stack

  • Framework: FastAPI 0.110.0
  • ML Libraries: scikit-learn 1.3.2, pandas 2.0.3, numpy 1.26.4
  • Authentication: python-jose 3.3.0 (JWT tokens)
  • Caching: Redis 5.0.4
  • Monitoring: Prometheus + Grafana
  • Deployment: Docker, Docker Compose
  • Server: Uvicorn

πŸ“Š API Endpoints

Authentication

  • POST /login - Authenticate and get JWT token

Predictions

  • POST /predict - Predict student score (single or batch)

Monitoring

  • /metrics - Prometheus metrics endpoint

πŸ”§ Installation & Setup

Prerequisites

  • Python 3.10+
  • Docker & Docker Compose (optional)
  • Redis (if running locally)

Local Development

  1. Clone the repository

    git clone <repository-url>
    cd student-backend
  2. Install dependencies

    pip install -r requirements.txt
  3. Set environment variables

    # Create .env file
    API_KEY=your-api-key
    JWT_SECRET_KEY=your-secret-key
    REDIS_URL=redis://localhost:6379
  4. Start Redis (if running locally)

    redis-server
  5. Run the application

    uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload

Docker Deployment

  1. Using Docker Compose (Recommended)

    docker-compose up -d

    This will start:

    • FastAPI application on port 8000
    • Redis on port 6379
    • Prometheus on port 9090
    • Grafana on port 3000
  2. Using Docker only

    docker build -t student-backend .
    docker run -p 8000:8000 student-backend

πŸ“ API Usage

1. Authentication

First, get an authentication token:

curl -X POST "https://student-backend-2919.onrender.com/login" \
     -H "Content-Type: application/json" \
     -d '{
       "username": "admin",
       "password": "admin"
     }'

Response:

{
  "access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
}

2. Single Student Prediction

curl -X POST "https://student-backend-2919.onrender.com/predict" \
     -H "Content-Type: application/json" \
     -H "Authorization: Bearer YOUR_TOKEN" \
     -H "X-API-Key: demo-key" \
     -d '{
       "comprehension": 75.5,
       "attention": 82.3,
       "focus": 78.9,
       "retention": 80.1,
       "engagement_time": 95
     }'

Response:

{
  "predicted_score": "78.45"
}

3. Batch Student Prediction

curl -X POST "https://student-backend-2919.onrender.com/predict" \
     -H "Content-Type: application/json" \
     -H "Authorization: Bearer YOUR_TOKEN" \
     -H "X-API-Key: demo-key" \
     -d '{
       "students": [
         {
           "comprehension": 75.5,
           "attention": 82.3,
           "focus": 78.9,
           "retention": 80.1,
           "engagement_time": 95
         },
         {
           "comprehension": 68.2,
           "attention": 74.8,
           "focus": 71.3,
           "retention": 72.5,
           "engagement_time": 87
         }
       ]
     }'

Response:

{
  "batch_size": 2,
  "predictions": [
    {"predicted_score": "78.45"},
    {"predicted_score": "71.23"}
  ]
}

πŸ“Š Input Parameters

The API expects the following student characteristics:

Parameter Type Description Range
comprehension float Student's comprehension level 0-100
attention float Student's attention span 0-100
focus float Student's focus ability 0-100
retention float Student's retention capacity 0-100
engagement_time integer Time spent engaged (minutes) 0+

πŸ”’ Security

  • JWT Authentication: All prediction endpoints require valid JWT tokens
  • API Key Validation: Additional API key validation for enhanced security
  • Rate Limiting: Built-in protection against abuse
  • Input Validation: Comprehensive input validation using Pydantic models

πŸ“ˆ Monitoring & Observability

Prometheus Metrics

Access metrics at: https://student-backend-2919.onrender.com/metrics

Grafana Dashboard

Logging

The application includes comprehensive logging middleware that tracks:

  • Request/response times
  • API usage patterns
  • Error rates
  • Cache hit/miss ratios

πŸ—‚οΈ Project Structure

student-backend/
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ api/
β”‚   β”‚   β”œβ”€β”€ routes_auth.py      # Authentication endpoints
β”‚   β”‚   └── routes_predict.py   # Prediction endpoints
β”‚   β”œβ”€β”€ cache/
β”‚   β”‚   └── redis_cache.py      # Redis caching logic
β”‚   β”œβ”€β”€ core/
β”‚   β”‚   β”œβ”€β”€ config.py           # Application configuration
β”‚   β”‚   β”œβ”€β”€ dependencies.py     # Dependency injection
β”‚   β”‚   β”œβ”€β”€ exceptions.py       # Exception handlers
β”‚   β”‚   └── security.py         # JWT and security utilities
β”‚   β”œβ”€β”€ middleware/
β”‚   β”‚   └── logging_middleware.py # Request logging
β”‚   β”œβ”€β”€ models/
β”‚   β”‚   └── model.joblib        # Trained ML model
β”‚   β”œβ”€β”€ services/
β”‚   β”‚   └── model_service.py    # ML prediction logic
β”‚   └── main.py                 # FastAPI application entry point
β”œβ”€β”€ data/
β”‚   β”œβ”€β”€ student_performance.csv # Raw data
β”‚   └── students_cleaned.csv    # Processed data
β”œβ”€β”€ notebooks/
β”‚   β”œβ”€β”€ analysis.ipynb          # Data analysis
β”‚   β”œβ”€β”€ assignment_score.ipynb  # Score analysis
β”‚   └── synthetic_data.ipynb    # Data generation
β”œβ”€β”€ training/
β”‚   β”œβ”€β”€ train_model.py          # Model training script
β”‚   └── train_utils.py          # Training utilities
β”œβ”€β”€ docker-compose.yml          # Multi-service setup
β”œβ”€β”€ Dockerfile                  # Container definition
β”œβ”€β”€ requirements.txt            # Python dependencies
└── prometheus.yml              # Prometheus configuration

🀝 Contributing

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

πŸ“„ License

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

πŸ†˜ Support

For support and questions:

  • Create an issue in the repository
  • Check the API documentation at /docs endpoint
  • Review the monitoring dashboards for system health

πŸ”„ Version History

  • v1.0.0 - Initial release with basic prediction functionality
  • v1.1.0 - Added batch prediction support
  • v1.2.0 - Implemented caching and monitoring
  • v1.3.0 - Enhanced security and authentication

Built with ❀️ using FastAPI and scikit-learn

About

A FastAPI-based backend service that predicts student assessment scores using a trained ML model. Supports batch processing, JWT authentication, Redis caching, and Prometheus monitoring. Fully containerized with Docker and deployed on Render for scalable performance.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages