Skip to content

Commit 3088285

Browse files
committed
For Docker deployment
1 parent ff22b2f commit 3088285

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

Dockerfile

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Use an official lightweight Python image
2+
FROM python:3.12-slim
3+
4+
# Set environment variables (better logging)
5+
ENV PYTHONUNBUFFERED=1
6+
7+
# Set the working directory inside the container
8+
WORKDIR /app
9+
10+
# Copy only requirements first (better caching)
11+
COPY requirements.txt .
12+
13+
# Install dependencies
14+
RUN pip install --no-cache-dir -r requirements.txt
15+
16+
# Create the models directory (fix missing folder issue)
17+
RUN mkdir -p /app/models
18+
19+
# Copy application files from the root folder
20+
COPY app.py /app/
21+
COPY models/k8s_failure_model.pkl /app/models/k8s_failure_model.pkl
22+
23+
# Expose the FastAPI port
24+
EXPOSE 8000
25+
26+
# Run the FastAPI app using Uvicorn
27+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"]
28+

app.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import pandas as pd
2+
import joblib
3+
from fastapi import FastAPI
4+
from pydantic import BaseModel
5+
6+
# Load the trained model
7+
model = joblib.load("models/k8s_failure_model.pkl")
8+
9+
app = FastAPI()
10+
11+
# Define request model
12+
class PredictionRequest(BaseModel):
13+
cpu_usage: float
14+
memory_usage: float
15+
container_network_receive_bytes_total: float
16+
container_network_transmit_bytes_total: float
17+
container_fs_usage_bytes: float
18+
cpu_usage_avg: float
19+
memory_usage_avg: float
20+
container_network_receive_bytes_total_avg: float
21+
container_network_transmit_bytes_total_avg: float
22+
container_fs_usage_bytes_avg: float
23+
container_restart_count_avg: float
24+
25+
@app.post("/predict")
26+
async def predict_failure(data: PredictionRequest):
27+
# Convert request data to a DataFrame
28+
input_data = pd.DataFrame([data.dict()])
29+
30+
# Temporary fix: Add a dummy 'target_avg' column
31+
input_data["target_avg"] = 0
32+
33+
# Make prediction
34+
prediction = model.predict(input_data)
35+
36+
return {"failure_predicted": "YES" if prediction[0] == 1 else "NO"}
37+

0 commit comments

Comments
 (0)