Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions lab5-doc/周昱名-2200012718/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
version: '3.8'

services:
training:
build:
context: .
dockerfile: docker/training/Dockerfile # TODO: Build the container using the training Dockerfile
volumes:
- model_storage:/app/models # TODO: Use the shared volume to store the trained model
# Set it to restart when it fails
restart: on-failure

inference:
build:
context: .
dockerfile: docker/inference/Dockerfile # TODO: Build the container using the inference Dockerfile
volumes:
- model_storage:/app/models # TODO: Use the shared volume to load the trained model
ports:
- "8080:8080" # TODO: Expose port 8080 for the Flask app
# Ensure that the inference service starts after the training service is complete
depends_on:
- training
restart: on-failure

#Define a shared volume for the model file
volumes:
model_storage:
driver: local
16 changes: 16 additions & 0 deletions lab5-doc/周昱名-2200012718/docker/inference/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Base image
FROM python:3.9-slim

WORKDIR /app

COPY docker/training/requirements.txt .
RUN pip install -r requirements.txt

COPY docker/inference/server.py ./server.py

VOLUME ["/app/models"]

EXPOSE 8080

CMD ["python", "server.py"]

30 changes: 30 additions & 0 deletions lab5-doc/周昱名-2200012718/docker/inference/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from flask import Flask, request, jsonify
import numpy as np
import joblib

app = Flask(__name__)

# TODO: Load the trained model from the shared volume (use the correct path)
model = joblib.load('/app/models/iris_model.pkl')

# TODO: Add request method to predict
@app.route('/predict', methods=['POST'])
def predict():
# TODO: Get the input array from the request body and make prediction using the model
get_json = request.get_json()
try:
iris_input = get_json['input']
# HINT: use np.array().reshape(1, -1) to convert input to 2D array
prediction = model.predict(np.array(iris_input, dtype=float).reshape(1, -1))

return jsonify({'prediction': prediction[0]})
except:
return jsonify({'error': 'something is wrong'}), 400

@app.route('/')
def hello():
return 'Welcome to Docker Lab'

if __name__ == '__main__':
#Run the Flask app (bind it to port 8080 or any other port)
app.run(debug=True, port=8080, host='0.0.0.0')
15 changes: 15 additions & 0 deletions lab5-doc/周昱名-2200012718/docker/training/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Base image
FROM python:3.9-slim

# Setup working directory
WORKDIR /app
# Copy requirements.txt and install dependencies
COPY docker/training/requirements.txt .
RUN pip install -r requirements.txt
# Copy the training script to the working directory
COPY docker/training/train.py ./train.py
# Store model in a shared volume
VOLUME ["/app/models"]
# TODO: Set the command to run the training script
CMD ["python", "train.py"]

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Flask>=2.2.2
scikit-learn==1.3.1
joblib==1.3.2
numpy==1.23.5
21 changes: 21 additions & 0 deletions lab5-doc/周昱名-2200012718/docker/training/train.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from sklearn.svm import SVC
from sklearn import datasets
import joblib
import os

#Load the Iris dataset
iris = datasets.load_iris()

#Create an SVM classifier
clf = SVC()

#Train the model using the iris dataset
model = clf.fit(iris.data, iris.target_names[iris.target])

# TODO: Save the trained model to the shared volume (make sure to use the correct path)
os.makedirs("/app/models", exist_ok=True)
joblib.dump(model, '/app/models/iris_model.pkl')

print("Model training complete and saved as iris_model.pkl")


Binary file added lab5-doc/周昱名-2200012718/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.