diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..09bda02 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,18 @@ +# Use Python 3.9 slim image +FROM python:3.9-slim + +# Set working directory +WORKDIR /app + +# Copy requirements and install +COPY requirements.txt . +RUN pip install --no-cache-dir -r requirements.txt + +# Copy source code +COPY src/ ./src/ + +# Expose port +EXPOSE 8000 + +# Run the application +CMD ["uvicorn", "src.api.main:app", "--host", "0.0.0.0", "--port", "8000"] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..c016597 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,11 @@ +version: '3.8' + +services: + api: + build: . + ports: + - "8000:8000" + volumes: + - ./mlruns:/app/mlruns # If MLflow uses local storage + environment: + - MLFLOW_TRACKING_URI=http://localhost:5000 # If running MLflow server separately \ No newline at end of file diff --git a/src/api/__init__.py b/src/api/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/api/main.py b/src/api/main.py new file mode 100644 index 0000000..feceb22 --- /dev/null +++ b/src/api/main.py @@ -0,0 +1,31 @@ +from fastapi import FastAPI +import mlflow.sklearn +import pandas as pd +from .pydantic_models import TransactionData, PredictionResponse + +app = FastAPI(title="Credit Risk Prediction API", version="1.0.0") + +# Load the model from MLflow registry +model_name = "CreditRiskLogisticRegression" +model_version = "latest" # or specify version +model = mlflow.sklearn.load_model(f"models:/{model_name}/{model_version}") + +@app.post("/predict", response_model=PredictionResponse) +def predict_risk(transaction: TransactionData): + # Convert to DataFrame + data = pd.DataFrame([transaction.dict()]) + + # Predict probability + proba = model.predict_proba(data)[:, 1][0] + + # Predict class + prediction = model.predict(data)[0] + + return PredictionResponse( + risk_probability=float(proba), + is_high_risk=bool(prediction) + ) + +@app.get("/") +def read_root(): + return {"message": "Credit Risk Prediction API"} \ No newline at end of file diff --git a/src/api/pydantic_models.py b/src/api/pydantic_models.py new file mode 100644 index 0000000..b476c85 --- /dev/null +++ b/src/api/pydantic_models.py @@ -0,0 +1,17 @@ +from pydantic import BaseModel +from typing import Optional + +class TransactionData(BaseModel): + CurrencyCode: str + CountryCode: int + ProviderId: str + ProductId: str + ProductCategory: str + ChannelId: str + Amount: float + Value: int + PricingStrategy: int + +class PredictionResponse(BaseModel): + risk_probability: float + is_high_risk: bool \ No newline at end of file