From 2f06e5364c64294beabb1e04dbe651319ff99104 Mon Sep 17 00:00:00 2001 From: "arno.nikpoosh" Date: Sat, 11 Jul 2026 15:45:38 +0330 Subject: [PATCH] add autoencoder and dockerize --- Dockerfile.mlflow-server | 25 ++++++------- Dockerfile.model-serve | 37 +++++++++++-------- docker-compose.yml | 32 ++++++++++++++++ .../fetch_clean_sessions.sql | 0 test.py => training/test.py | 0 .../train_and_register_autoencoder.py | 0 .../train_and_register_forest.py | 0 .../train_and_register_nusvm.py | 0 8 files changed, 65 insertions(+), 29 deletions(-) create mode 100644 docker-compose.yml rename fetch_clean_sessions.sql => training/fetch_clean_sessions.sql (100%) rename test.py => training/test.py (100%) rename train_and_register_autoencoder.py => training/train_and_register_autoencoder.py (100%) rename train_and_register_forest.py => training/train_and_register_forest.py (100%) rename train_and_register_nusvm.py => training/train_and_register_nusvm.py (100%) diff --git a/Dockerfile.mlflow-server b/Dockerfile.mlflow-server index 1f2629e..fb7c3d8 100644 --- a/Dockerfile.mlflow-server +++ b/Dockerfile.mlflow-server @@ -1,20 +1,17 @@ -# Dockerfile.mlflow-server -# Runs a local MLflow tracking server with the repository's model registry and artifacts. +FROM python:3.11-slim -FROM python:3.13-slim - -ENV PYTHONUNBUFFERED=1 +# Install uv +COPY --from=ghcr.io/astral-sh/uv:latest /uv /bin/uv WORKDIR /app -RUN apt-get update && apt-get install -y --no-install-recommends \ - curl \ - && rm -rf /var/lib/apt/lists/* +# Create a virtual environment and install mlflow inside it +RUN uv venv /app/.venv && \ + uv pip install --python /app/.venv mlflow -RUN pip install --no-cache-dir mlflow==3.14.0 sqlalchemy +# Add the virtual environment to the PATH so the mlflow command is recognized +ENV PATH="/app/.venv/bin:$PATH" -COPY mlflow.db /app/mlflow.db -COPY mlartifacts /app/mlartifacts +EXPOSE 8000 -EXPOSE 8080 - -CMD ["mlflow", "server", "--backend-store-uri", "sqlite:///app/mlflow.db", "--default-artifact-root", "file:///app/mlartifacts", "--host", "0.0.0.0", "--port", "8080", "--workers", "1"] +# Start the server +CMD ["mlflow", "server", "--host", "0.0.0.0", "--port", "8000", "--backend-store-uri", "sqlite:///mlflow.db", "--default-artifact-root", "./mlartifacts"] \ No newline at end of file diff --git a/Dockerfile.model-serve b/Dockerfile.model-serve index f0fb891..894022a 100644 --- a/Dockerfile.model-serve +++ b/Dockerfile.model-serve @@ -1,22 +1,29 @@ -# Dockerfile.model-serve -# Builds a container that serves an MLflow registry model by name and version. - -FROM python:3.13-slim - -ENV PYTHONUNBUFFERED=1 +FROM astral/uv:python3.12-bookworm-slim +# Install uv +COPY --from=ghcr.io/astral-sh/uv:latest /uv /bin/uv WORKDIR /app -RUN apt-get update && apt-get install -y --no-install-recommends \ - curl \ - && rm -rf /var/lib/apt/lists/* +# Copy dependency files first to leverage Docker layer caching +COPY pyproject.toml uv.lock ./ -RUN pip install --no-cache-dir mlflow==3.14.0 +# Create a virtual environment and sync dependencies +# --no-dev ensures testing/formatting tools aren't installed in production +RUN uv venv && uv sync --frozen --no-dev -ARG MODEL_NAME=IsolationForest_Anomaly_Detector -ARG MODEL_VERSION=6 -ENV MODEL_NAME=${MODEL_NAME} -ENV MODEL_VERSION=${MODEL_VERSION} +# Put the venv in the system PATH so we don't need to manually activate it +ENV PATH="/app/.venv/bin:$PATH" + +# Copy the rest of the project +COPY . . + +# Set the environment variable for the model as requested +ENV MODEL_URI="models:/Autoencoder_Anomaly_Detector/2" + +# The tracking URI where this container will fetch the model. +# (Set this to the MLflow server's hostname/IP at runtime, e.g., via Docker Compose) +ENV MLFLOW_TRACKING_URI="http://localhost:8000" EXPOSE 5001 -CMD ["sh", "-c", "mlflow models serve -m \"models:/$MODEL_NAME/$MODEL_VERSION\" --host 0.0.0.0 --port 5001 --env-manager local"] +# Serve the model using the environment variable +CMD mlflow models serve -m "$MODEL_URI" --host 0.0.0.0 --port 5001 --env-manager local \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..83cdd69 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,32 @@ +services: + mlflow-server: + build: + context: . + dockerfile: Dockerfile.mlflow-server + ports: + - "8000:8000" + volumes: + # Mount the database file and artifacts directory to persist them + - ./mlflow.db:/app/mlflow.db + - ./mlartifacts:/app/mlartifacts + restart: unless-stopped + + model-serve: + build: + context: . + dockerfile: Dockerfile.model-serve + ports: + - "5001:5001" + environment: + # Points to the service name defined above, not localhost + - MLFLOW_TRACKING_URI=http://mlflow-server:8000 + # You can override this at runtime: docker compose run -e MODEL_URI="..." model-serve + - MODEL_URI=${MODEL_URI:-models:/Autoencoder_Anomaly_Detector/2} + volumes: + # Crucial: Since MLflow is using a local disk artifact store, + # the server passes local file paths back to the client. + # The serving container must have access to the exact same artifact paths. + - ./mlartifacts:/app/mlartifacts + depends_on: + - mlflow-server + restart: unless-stopped \ No newline at end of file diff --git a/fetch_clean_sessions.sql b/training/fetch_clean_sessions.sql similarity index 100% rename from fetch_clean_sessions.sql rename to training/fetch_clean_sessions.sql diff --git a/test.py b/training/test.py similarity index 100% rename from test.py rename to training/test.py diff --git a/train_and_register_autoencoder.py b/training/train_and_register_autoencoder.py similarity index 100% rename from train_and_register_autoencoder.py rename to training/train_and_register_autoencoder.py diff --git a/train_and_register_forest.py b/training/train_and_register_forest.py similarity index 100% rename from train_and_register_forest.py rename to training/train_and_register_forest.py diff --git a/train_and_register_nusvm.py b/training/train_and_register_nusvm.py similarity index 100% rename from train_and_register_nusvm.py rename to training/train_and_register_nusvm.py