Skip to content

About

A production-grade ML API built with Flask and scikit-learn, delivering real-time student performance predictions via REST endpoints and a web interface.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

Β 

History

3 Commits

Folders and files

Repository files navigation

Student ML API

Python 3.8+ Flask License: MIT Deployed on Render

A production-ready machine learning API that predicts student performance (Pass/Fail) based on study hours, attendance, and previous scores. Built with Flask, scikit-learn, and deployed on Render with both REST API and web interface.

πŸ”— Live API: https://student-ml-api-4z9d.onrender.com


πŸ“‹ Table of Contents


🎯 Overview

This project demonstrates a complete machine learning workflow from data exploration to production deployment. The API predicts whether a student will pass or fail based on three key factors: hours studied, attendance percentage, and previous exam scores.

Tech Stack:

  • Machine Learning: scikit-learn (Random Forest Classifier)
  • Backend: Flask (Python web framework)
  • Deployment: Render (cloud platform)
  • Model Serialization: joblib

Use Cases:

  • Learning end-to-end ML deployment
  • Portfolio demonstration for data science roles
  • Template for productionizing classification models
  • Reference implementation for Flask-based ML APIs

✨ Features

  • βœ… Dual Interface β€” REST API + Web form for predictions
  • βœ… Optimized Model β€” Random Forest with 200 estimators, 75% accuracy
  • βœ… Robust API β€” Input validation and error handling
  • βœ… Production Ready β€” Deployed and accessible via HTTPS
  • βœ… Clean Architecture β€” Separation of concerns (model, API, templates)
  • βœ… Complete ML Pipeline β€” Data exploration to deployment
  • βœ… Reproducible β€” Pinned dependencies and random state

πŸš€ Quick Start

Test the Live API

Using cURL (JSON API):

# Make a prediction via REST API
curl -X POST https://student-ml-api-4z9d.onrender.com/predict \
  -H "Content-Type: application/json" \
  -d '{
    "hours_studied": 7,
    "attendance": 85,
    "previous_score": 72
  }'

# Expected response: {"result": 1}  (1 = Pass, 0 = Fail)

Using the Web Interface: Simply visit https://student-ml-api-4z9d.onrender.com and fill out the form!

Run Locally

# Clone the repository
git clone https://github.com/TechGenDM/student-ml-api.git
cd student-ml-api

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

# Install dependencies
pip install -r requirements.txt

# Start the server
python app.py

The application will be available at http://127.0.0.1:8000


πŸ“š API Documentation

Base URLs

Production: https://student-ml-api-9qkf.onrender.com
Local: http://127.0.0.1:8000

Endpoints

GET /

Returns the web interface for predictions.

Response: HTML form


POST /predict

Generate predictions via JSON API.

Request:

{
  "hours_studied": 7,
  "attendance": 85,
  "previous_score": 72
}

Response (Success - 200):

{
  "result": 1
}
  • result: Integer (0 = Fail, 1 = Pass)

Response (Error - 400):

{
  "error": "hours_studied is missing"
}

Required Fields:

  • hours_studied (float): Hours spent studying (e.g., 1-10)
  • attendance (float): Attendance percentage (e.g., 50-100)
  • previous_score (float): Previous exam score (e.g., 30-90)

Status Codes:

  • 200 β€” Success
  • 400 β€” Invalid input (missing fields or invalid JSON)

POST /predict-form

Form-based prediction endpoint (used by web interface).

Form Data:

  • hours_studied
  • attendance
  • previous_score

Response: HTML page with prediction result


🌐 Web Interface

The application includes a user-friendly web form for non-technical users:

  1. Navigate to the home page (/)
  2. Enter the three required values:
    • Hours Studied
    • Attendance (%)
    • Previous Score
  3. Click "Predict"
  4. View the result (Pass/Fail) on the page

Perfect for demonstrations, non-technical stakeholders, or quick manual tests!


πŸ“ Project Structure

student-ml-api/
β”œβ”€β”€ app.py                    # Flask application & API endpoints
β”œβ”€β”€ student_model.pkl         # Serialized trained Random Forest model
β”œβ”€β”€ ML_End_to_End.ipynb      # Complete ML training pipeline
β”œβ”€β”€ requirements.txt          # Python dependencies
β”œβ”€β”€ students.csv             # Training dataset (20 samples)
β”œβ”€β”€ templates/
β”‚   └── index.html           # Web form interface
└── README.md                # Project documentation

Key Files

  • app.py β€” Main Flask application with three routes:

    • / - Serves the web form
    • /predict - JSON API endpoint
    • /predict-form - Form submission handler
  • ML_End_to_End.ipynb β€” Complete ML workflow:

    • Data exploration and statistics
    • Train/test split (80/20)
    • Model comparison (Logistic Regression vs Random Forest)
    • Hyperparameter tuning (n_estimators=200, max_depth=5)
    • Evaluation metrics (accuracy, precision, recall, F1-score)
    • Model serialization with joblib
  • student_model.pkl β€” Trained Random Forest classifier ready for inference

  • templates/index.html β€” Clean HTML form for web-based predictions


πŸ€– Model Details

Algorithm: Random Forest Classifier

Hyperparameters:

  • n_estimators: 200 (number of decision trees)
  • max_depth: 5 (maximum tree depth)
  • min_samples_split: 4 (minimum samples to split node)
  • random_state: 42 (reproducibility)

Features:

  1. Hours Studied (1-10)
  2. Attendance Percentage (50-96)
  3. Previous Score (30-90)

Target:

  • 0 = Fail
  • 1 = Pass

Performance:

  • Accuracy: 75% on test set
  • Precision: 100% (no false positives)
  • Recall: 50% (misses some pass cases)
  • F1-Score: 0.67

Training Data:

  • 20 samples total
  • 16 training, 4 test samples
  • Balanced classes (55% pass rate)

Model Comparison: Both Logistic Regression and Random Forest achieved 75% accuracy. Random Forest was selected for its ability to capture non-linear relationships and better generalization potential with more data.


πŸ’» Local Development

Prerequisites

  • Python 3.8 or higher
  • pip package manager
  • Virtual environment tool (venv recommended)

Setup Instructions

  1. Clone and navigate:

    git clone https://github.com/TechGenDM/student-ml-api.git
    cd student-ml-api
  2. Create virtual environment:

    python -m venv venv
    source venv/bin/activate  # Windows: venv\Scripts\activate
  3. Install dependencies:

    pip install -r requirements.txt

    Dependencies:

    • flask β€” Web framework
    • numpy β€” Numerical computing
    • joblib β€” Model serialization
    • scikit-learn β€” Machine learning library
  4. Verify model file exists:

    ls student_model.pkl  # Should exist in root directory
  5. Start the server:

    python app.py

    Server starts on port 8000 with debug mode enabled.

  6. Test the API:

    # Test web interface
    open http://127.0.0.1:8000  # or visit in browser
    
    # Test JSON API
    curl -X POST http://127.0.0.1:8000/predict \
      -H "Content-Type: application/json" \
      -d '{"hours_studied": 8, "attendance": 90, "previous_score": 75}'

Retraining the Model

  1. Open ML_End_to_End.ipynb in Jupyter Notebook
  2. Modify hyperparameters or add more data
  3. Run all cells to retrain
  4. The last cell exports the new model: joblib.dump(rf, "student_model.pkl")
  5. Restart app.py to load the updated model

🌐 Deployment

The API is deployed on Render with automatic deployments from the main branch.

Deployment URL: https://student-ml-api-9qkf.onrender.com

Deploy Your Own Instance

  1. Fork this repository to your GitHub account

  2. Create a Render account at render.com

  3. Create a new Web Service:

    • Connect your forked repository
    • Runtime: Python 3
    • Build command: pip install -r requirements.txt
    • Start command: python app.py
  4. Environment variables (optional):

    • PORT: Automatically set by Render
    • Add any custom variables if needed
  5. Deploy and access via the provided Render URL

Production Checklist

  • βœ… requirements.txt with all dependencies
  • βœ… student_model.pkl in repository root
  • βœ… app.py configured for 0.0.0.0 host
  • βœ… templates/ folder with index.html
  • βœ… Error handling for all endpoints
  • ⚠️ Consider disabling debug mode in production (currently enabled)

πŸ§ͺ Testing

Manual Testing

Test Web Interface:

# Open in browser
http://127.0.0.1:8000

# Enter values like:
# Hours Studied: 8
# Attendance: 85
# Previous Score: 70

Test JSON API with valid data:

curl -X POST http://127.0.0.1:8000/predict \
  -H "Content-Type: application/json" \
  -d '{"hours_studied": 7, "attendance": 85, "previous_score": 72}'

# Expected: {"result": 1}

Test with missing fields (should return 400):

curl -X POST http://127.0.0.1:8000/predict \
  -H "Content-Type: application/json" \
  -d '{"hours_studied": 7}'

# Expected: {"error": "attendance is missing"}

Test with invalid JSON (should return 400):

curl -X POST http://127.0.0.1:8000/predict \
  -H "Content-Type: application/json" \
  -d 'not json'

# Expected: {"error": "Request body must be JSON"}

Test Cases Coverage

  • βœ… Model loads successfully on startup
  • βœ… Valid predictions return 0 or 1
  • βœ… Missing fields return appropriate error messages
  • βœ… Invalid JSON returns 400 error
  • βœ… Non-numeric values are rejected
  • βœ… Web form renders correctly
  • βœ… Form submission returns result page

Recommended Automated Tests

Create test_app.py:

import pytest
from app import app

@pytest.fixture
def client():
    with app.test_client() as client:
        yield client

def test_home_page(client):
    rv = client.get('/')
    assert rv.status_code == 200

def test_valid_prediction(client):
    rv = client.post('/predict', json={
        'hours_studied': 7,
        'attendance': 85,
        'previous_score': 72
    })
    assert rv.status_code == 200
    assert 'result' in rv.json

def test_missing_field(client):
    rv = client.post('/predict', json={'hours_studied': 7})
    assert rv.status_code == 400
    assert 'error' in rv.json

🚧 Future Enhancements

Model Improvements

  • Collect more training data (currently only 20 samples)
  • Add feature engineering (interaction terms, polynomial features)
  • Experiment with other algorithms (XGBoost, Neural Networks)
  • Implement cross-validation for better evaluation
  • Add confidence scores to predictions

API Enhancements

  • Authentication β€” API keys or JWT tokens
  • Rate Limiting β€” Prevent abuse with throttling
  • Batch Predictions β€” Process multiple students at once
  • Input Validation β€” Use Pydantic for robust schema validation
  • Swagger/OpenAPI β€” Interactive API documentation
  • CORS Support β€” Allow frontend applications to consume API

MLOps & Production

  • Model Versioning β€” Track and serve multiple model versions
  • Monitoring β€” Log predictions and track model drift
  • A/B Testing β€” Compare model performance in production
  • CI/CD Pipeline β€” Automated testing with GitHub Actions
  • Docker Support β€” Containerize for easier deployment
  • Database Integration β€” Store predictions and feedback
  • Model Retraining β€” Automated pipeline for retraining with new data

User Experience

  • Improve web interface styling (Bootstrap, Tailwind CSS)
  • Add data visualization (feature importance, prediction confidence)
  • Provide explanations for predictions (SHAP values, LIME)
  • Mobile-responsive design
  • Multi-language support

🀝 Contributing

Contributions are welcome! Please follow these guidelines:

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

Contribution Ideas

  • Improve model accuracy
  • Add more comprehensive tests
  • Enhance documentation
  • Optimize API performance
  • Add new features from the enhancement list

πŸ“„ License

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


πŸ”— Links & Resources

Learning Resources


πŸ“§ Contact

For questions, feedback, or collaboration opportunities, please open an issue on GitHub or reach out via LinkedIn.


Built with ❀️ by TechGenDM
From Jupyter notebooks to production β€” one commit at a time

About

A production-grade ML API built with Flask and scikit-learn, delivering real-time student performance predictions via REST endpoints and a web interface.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages