Tutorial Lengkap FastAPI untuk Machine Learning: Building Production ML APIs

# Tutorial Lengkap FastAPI untuk ML: Build Production ML APIs FastAPI adalah framework web Python modern dengan performa tinggi untuk membangun APIs. Dengan dokumentasi OpenAPI otomatis, type hints,...

By Ruby Abdullah · · tutorial
FastAPIMachine LearningAPIPythonMLOpsREST API

Tutorial Lengkap FastAPI untuk ML: Build Production ML APIs

FastAPI adalah framework web Python modern dengan performa tinggi untuk membangun APIs. Dengan dokumentasi OpenAPI otomatis, type hints, dan dukungan async, FastAPI adalah pilihan ideal untuk deploy model machine learning sebagai production-ready APIs.

Mengapa FastAPI untuk ML?

Keunggulan FastAPI:
  • High performance: Setara dengan NodeJS dan Go
  • Type safety: Pydantic validation
  • Auto documentation: Swagger UI dan ReDoc
  • Async support: Handle concurrent requests
  • Easy testing: Built-in test client

Use Cases:
  • ML model serving APIs
  • Real-time inference endpoints
  • Batch prediction services
  • Feature engineering APIs
  • Model management systems

Instalasi

pip install fastapi uvicorn

Dengan ML dependencies

pip install fastapi uvicorn scikit-learn joblib numpy pandas

Untuk async database

pip install fastapi[all] sqlalchemy asyncpg

Verify installation

python -c "import fastapi; print(fastapi.version)"

Quick Start

1. Hello World API

# main.py

from fastapi import FastAPI

app = FastAPI(

title="ML API",

description="Machine Learning API dengan FastAPI",

version="1.0.0"

)

@app.get("/")

def readroot():

return {"message": "Selamat datang di ML API"}

@app.get("/health")

def healthcheck():

return {"status": "healthy"}

# Run server

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

Akses docs di http://localhost:8000/docs

2. Simple ML Prediction Endpoint

from fastapi import FastAPI

from pydantic import BaseModel

import joblib

import numpy as np

app = FastAPI()

Load model saat startup

model = joblib.load("model.joblib")

class PredictionRequest(BaseModel):

features: list[float]

class PredictionResponse(BaseModel):

prediction: float

probability: list[float] | None = None

@app.post("/predict", responsemodel=PredictionResponse)

def predict(request: PredictionRequest):

features = np.array(request.features).reshape(1, -1)

prediction = model.predict(features)[0]

# Get probability jika classifier

probability = None

if hasattr(model, "predictproba"):

probability = model.predictproba(features)[0].tolist()

return PredictionResponse(

prediction=float(prediction),

probability=probability

)

Request/Response Models

1. Pydantic Models

from pydantic import BaseModel, Field, validator

from typing import Optional, List

from enum import Enum

class ModelType(str, Enum):

classification = "classification"

regression = "regression"

class FeatureInput(BaseModel):

age: int = Field(..., ge=0, le=120, description="Umur dalam tahun")

income: float = Field(..., gt=0, description="Pendapatan tahunan")

education: str = Field(..., description="Tingkat pendidikan")

class Config:

schemaextra = {

"example": {

"age": 35,

"income": 75000.0,

"education": "sarjana"

}

}

class BatchPredictionRequest(BaseModel):

instances: List[FeatureInput]

modelversion: Optional[str] = "latest"

class PredictionResult(BaseModel):

prediction: float

confidence: float

modelversion: str

class BatchPredictionResponse(BaseModel):

predictions: List[PredictionResult]

processingtimems: float

2. Validation

from pydantic import BaseModel, validator, rootvalidator

class MLRequest(BaseModel):

features: List[float]

@validator('features')

def checkfeaturecount(cls, v):

if len(v) != 10:

raise ValueError('Harus menyediakan tepat 10 features')

return v

@validator('features', eachitem=True)

def checkfeaturerange(cls, v):

Artikel Terkait

Tutorial Lengkap Vertex AI: Platform ML Terpadu Google Cloud

Tutorial Lengkap Vertex AI: Platform ML Terpadu di Google Cloud Vertex AI adalah platform machine learning terpadu Googl...

Tutorial Lengkap Azure Machine Learning: End-to-End ML Platform

Tutorial Lengkap Azure Machine Learning: ML End-to-End di Azure Azure Machine Learning adalah platform berbasis cloud un...

Tutorial Lengkap AWS SageMaker: Machine Learning di Cloud

Tutorial Lengkap AWS SageMaker: End-to-End ML Pipeline Amazon SageMaker adalah layanan machine learning terkelola penuh ...

Tutorial Lengkap Ray Serve: Scalable ML Model Serving

Tutorial Lengkap Ray Serve: Scalable ML Model Serving Ray Serve adalah library model serving yang scalable dibangun di a...