Skip to content

Latest commit

Β 

History

History
445 lines (323 loc) Β· 9.98 KB

File metadata and controls

445 lines (323 loc) Β· 9.98 KB

πŸŽ‰ START HERE - Your FastAPI ML Deployment Template is Ready!

βœ… What You Have

A complete, production-ready FastAPI ML deployment template that lets you deploy ANY machine learning model with just 3 steps:

  1. Drop your model into app/models/
  2. Register it in .env
  3. Deploy!

πŸš€ Quick Start (Choose Your Path)

Path 1: Docker (Recommended - 2 Minutes)

# Start the API
docker-compose up -d

# Check it's running
curl http://localhost:8000/api/v1/health

# Test the forecast model
curl -X POST "http://localhost:8000/api/v1/predict/forecast" \
  -H "Content-Type: application/json" \
  -d '{"periods": 7, "freq": "D"}'

# Open the interactive docs
# Visit: http://localhost:8000/docs

Path 2: Local Python (5 Minutes)

# Windows
scripts\setup.bat

# Linux/Mac
chmod +x scripts/setup.sh
./scripts/setup.sh

# Run the API
python run.py

# Visit: http://localhost:8000/docs

πŸ“š What to Read Next

Document Read When Time
QUICKSTART.md You want to start NOW 5 min
README.md You want complete details 20 min
PROJECT_OVERVIEW.md You want to understand architecture 10 min
STRUCTURE.md You want to navigate the project 5 min
DEPLOYMENT.md You're ready for production 30 min
SUMMARY.md You want a quick overview 5 min

Recommendation: Start with QUICKSTART.md β†’ Then README.md β†’ Then others as needed


🎯 Your First Task: Test the Example Models

1. Start the API (choose Docker or local above)

2. Visit the Interactive Docs

http://localhost:8000/docs

3. Try the Forecast Model

Click on POST /api/v1/predict/forecast β†’ "Try it out" β†’ Paste:

{
  "periods": 30,
  "freq": "D"
}

Click "Execute"

4. Try the Classifier Model

Click on POST /api/v1/predict/classifier β†’ "Try it out" β†’ Paste:

{
  "features": [1.2, 3.4, 5.6, 7.8]
}

Click "Execute"


πŸ”§ Your Second Task: Add Your Own Model

Step 1: Create Your Model File

Create app/models/mymodel.py:

from app.models.base import BaseMLModel

class MymodelModel(BaseMLModel):
    def __init__(self):
        super().__init__(model_name="mymodel", version="1.0")
        self.model = None
    
    async def load_model(self):
        # Load your trained model here
        # Example:
        # import joblib
        # self.model = joblib.load("model_storage/mymodel.pkl")
        
        self.is_loaded = True
    
    async def predict(self, input_data):
        if not self.is_loaded:
            raise RuntimeError(f"Model {self.model_name} is not loaded")
        
        # Your prediction logic here
        result = "your_prediction"
        
        return {
            "prediction": result,
            "model": self.model_name,
            "version": self.version
        }

Step 2: Register Your Model

Copy env.example to .env (if not already done), then edit:

MODEL_REGISTRY={"forecast": "forecast.py", "classifier": "classifier.py", "mymodel": "mymodel.py"}

Step 3: Restart and Test

# Docker
docker-compose restart

# Local
# Stop (Ctrl+C) and run again: python run.py

Test your model:

curl -X POST "http://localhost:8000/api/v1/predict/mymodel" \
  -H "Content-Type: application/json" \
  -d '{"your": "input"}'

πŸ“ Project Structure (Simple View)

your-project/
β”‚
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ models/           ← 🎯 ADD YOUR MODELS HERE
β”‚   β”‚   β”œβ”€β”€ base.py       (Base class - inherit from this)
β”‚   β”‚   β”œβ”€β”€ forecast.py   (Example 1)
β”‚   β”‚   └── classifier.py (Example 2)
β”‚   β”œβ”€β”€ api/              (Routes & schemas)
β”‚   β”œβ”€β”€ core/             (Config & logging)
β”‚   β”œβ”€β”€ services/         (Model service)
β”‚   └── main.py           (FastAPI app)
β”‚
β”œβ”€β”€ examples/             (Example API requests)
β”œβ”€β”€ tests/                (Unit tests)
β”œβ”€β”€ scripts/              (Setup scripts)
β”œβ”€β”€ model_storage/        ← Store your .pkl, .joblib files here
β”‚
β”œβ”€β”€ Dockerfile            (Docker config)
β”œβ”€β”€ docker-compose.yml    (Docker Compose)
β”œβ”€β”€ requirements.txt      (Dependencies)
β”œβ”€β”€ env.example           ← Copy to .env and edit
β”‚
└── Documentation/
    β”œβ”€β”€ START_HERE.md     (This file)
    β”œβ”€β”€ QUICKSTART.md     (Quick start guide)
    β”œβ”€β”€ README.md         (Complete guide)
    └── ...

πŸ”Œ Available Endpoints

Once running, you have these endpoints:

Endpoint Method Purpose
/docs GET Interactive API documentation
/api/v1/health GET Health check
/api/v1/models GET List all loaded models
/api/v1/models/{name} GET Get info about a model
/api/v1/predict/{model_name} POST Make prediction (path-based)
/api/v1/predict POST Make prediction (body-based)
/api/v1/models/{name}/reload POST Reload a model

🎨 Example Requests

All examples are in the examples/ folder:

  • examples/forecast_request.json - Forecast prediction
  • examples/classifier_request.json - Classification
  • examples/test_requests.http - REST Client format
  • examples/postman_collection.json - Import into Postman

πŸ§ͺ Running Tests

# Run all tests
pytest

# With coverage report
pytest --cov=app

# Verbose output
pytest -v

# Specific test file
pytest tests/test_api.py

πŸ› Troubleshooting

Port 8000 Already in Use

Edit .env:

PORT=8001

Or in docker-compose.yml:

ports:
  - "8001:8000"

Models Not Loading

  1. Check .env exists (copy from env.example)
  2. Verify MODEL_REGISTRY format is correct JSON
  3. Check model file names match registry
  4. View logs: docker-compose logs or check terminal output

Import Errors

# Reinstall dependencies
pip install -r requirements.txt

# Or rebuild Docker
docker-compose build --no-cache

Can't Access from Browser

  • Make sure HOST=0.0.0.0 in .env
  • Check firewall settings
  • Try http://localhost:8000/docs and http://127.0.0.1:8000/docs

πŸ’‘ Common Questions

How do I use my own trained model?

  1. Save your model: joblib.dump(model, "model_storage/mymodel.pkl")
  2. Create model file in app/models/mymodel.py
  3. In load_model(): self.model = joblib.load("model_storage/mymodel.pkl")
  4. Register in .env

Can I use different ML libraries?

Yes! The template works with:

  • scikit-learn
  • XGBoost
  • TensorFlow/Keras
  • PyTorch
  • Prophet
  • Any Python ML library

Just add the library to requirements.txt and use it in your model file.

How do I deploy to production?

See DEPLOYMENT.md for detailed guides:

  • AWS (EC2, ECS, Lambda)
  • Google Cloud (Cloud Run, GKE)
  • Azure (Container Instances)
  • Kubernetes
  • Traditional VPS

Can I add authentication?

Yes! See DEPLOYMENT.md security section for:

  • API key authentication
  • JWT tokens
  • Rate limiting
  • HTTPS setup

How do I add multiple versions of a model?

# In your model registry or code
model_service.load_model("forecast", "forecast.py", version="1")
model_service.load_model("forecast", "forecast_v2.py", version="2")

# Use specific version
curl "http://localhost:8000/api/v1/predict/forecast?version=2"

πŸŽ“ Learning Path

Beginner:

  1. βœ… Run the template with Docker
  2. βœ… Test example models
  3. βœ… Read through one model file (forecast.py)
  4. βœ… Modify example to return different data

Intermediate:

  1. βœ… Create your own simple model
  2. βœ… Add custom validation schemas
  3. βœ… Run tests and add new tests
  4. βœ… Deploy locally with nginx

Advanced:

  1. βœ… Add authentication
  2. βœ… Implement caching
  3. βœ… Set up monitoring
  4. βœ… Deploy to cloud (AWS/GCP/Azure)
  5. βœ… Set up CI/CD pipeline

πŸ“¦ What's Included

βœ… 17 Python files (application code)
βœ… 4 test files (comprehensive testing)
βœ… 7 documentation files (2,500+ lines)
βœ… 6 example files (request templates)
βœ… 4 setup scripts (easy installation)
βœ… 3 Docker files (containerization)
βœ… 2 example ML models (forecast & classifier)

Total: 48+ files, production-ready!


🌟 Key Benefits

  1. Reusable - Works with ANY ML model
  2. Fast - Deploy in minutes, not days
  3. Complete - Nothing left to implement
  4. Documented - Extensive guides included
  5. Production-Ready - Docker, logging, error handling
  6. Tested - Unit tests included
  7. Flexible - Easy to customize

🎯 Your Roadmap

Today (30 minutes)

  • Template created βœ…
  • Run with Docker
  • Test example models
  • Read QUICKSTART.md

This Week

  • Add your first model
  • Read full README.md
  • Run tests
  • Customize for your needs

This Month

  • Deploy to production
  • Add authentication
  • Set up monitoring
  • Add more models

πŸ”— Quick Links


πŸ†˜ Need Help?

  1. Quick Start Issues: β†’ Read QUICKSTART.md
  2. How does it work: β†’ Read PROJECT_OVERVIEW.md
  3. Where is X: β†’ Read STRUCTURE.md
  4. Deployment: β†’ Read DEPLOYMENT.md
  5. Everything: β†’ Read README.md

✨ Final Checklist

Before you proceed, make sure:

  • Template files created βœ…
  • You've chosen your path (Docker or local)
  • You've started the API
  • You've visited http://localhost:8000/docs
  • You've tested an example model
  • You've read QUICKSTART.md

If all checked, you're ready to go! πŸš€


πŸŽ‰ Congratulations!

You now have a complete, production-ready ML deployment template that you can use for any project.

Next Step: Open QUICKSTART.md and start using it in 5 minutes!


Happy Coding! πŸš€

P.S. - Star this template if you find it useful!