Skip to content

Commit

Permalink
fix(all): clean
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaume-chervet committed Oct 22, 2024
1 parent eee5d91 commit e3a5dab
Show file tree
Hide file tree
Showing 6 changed files with 253 additions and 201 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ env:
PNPM_VERSION: 8.5.1
PYTHON_VERSION: 3.10.15
NODE_VERSION: 18
AZURE_RESOURCE_GROUP_NAME: "azure-ml-gui"
AZURE_RESOURCE_GROUP_NAME: "azure-ml-guil"
AZURE_LOCATION: "northeurope"
AZURE_ML_WORKSPACE_NAME: "cats-dogs-gui"
AZURE_WEBAPP_NAME: "cats-dogs-gui"
AZURE_ML_WORKSPACE_NAME: "cats-dogs-guil"
AZURE_API_NAME: "cats-dogs-guil"
DELETE_WEBAPP: "false"
DOCKER_API_IMAGE_NAME: "mlopspython-api"
DOCKER_REPOSITORY: ${{ github.repository_owner }}
Expand Down Expand Up @@ -276,7 +276,7 @@ jobs:
- name: Deploy container
run: |
# https://learn.microsoft.com/en-us/cli/azure/container?view=azure-cli-latest#az-container-create()
az container create --resource-group ${{ env.AZURE_RESOURCE_GROUP_NAME}} --name ${{ env.AZURE_WEBAPP_NAME }} --dns-name-label ${{ env.AZURE_WEBAPP_NAME }} --image ${{ env.DOCKER_REGISTRY }}/${{ env.DOCKER_REPOSITORY }}/${{ env.DOCKER_API_IMAGE_NAME }}:${{ needs.tags.outputs.new_version }} --ports 5000
az container create --resource-group ${{ env.AZURE_RESOURCE_GROUP_NAME}} --name ${{ env.AZURE_API_NAME }} --dns-name-label ${{ env.AZURE_API_NAME }} --image ${{ env.DOCKER_REGISTRY }}/${{ env.DOCKER_REPOSITORY }}/${{ env.DOCKER_API_IMAGE_NAME }}:${{ needs.tags.outputs.new_version }} --ports 5000
integration_tests:
environment: MLOpsPython
runs-on: ubuntu-latest
Expand Down Expand Up @@ -307,12 +307,12 @@ jobs:
--experiment_id ${{ needs.train.outputs.EXPERIMENT_ID }} \
--integration_dataset_name "cats-dogs-others-integration" \
--integration_dataset_version ${{ needs.train.outputs.INTEGRATION_DATASET_VERSION }} \
--url "http://${{ env.AZURE_WEBAPP_NAME }}.${{ env.AZURE_LOCATION }}.azurecontainer.io:5000/upload" > integration_output.txt
--url "http://${{ env.AZURE_API_NAME }}.${{ env.AZURE_LOCATION }}.azurecontainer.io:5000/upload" > integration_output.txt
cat integration_output.txt
- name: Delete Environment
run: |
if [ "${{ env.DELETE_WEBAPP }}" = "true" ]; then
az container delete --resource-group ${{ env.AZURE_RESOURCE_GROUP_NAME}} --name ${{ env.AZURE_WEBAPP_NAME }} --yes
az container delete --resource-group ${{ env.AZURE_RESOURCE_GROUP_NAME}} --name ${{ env.AZURE_API_NAME }} --yes
fi
webapp_unit_tests:
runs-on: ubuntu-latest
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,61 @@
from abc import ABC
from io import BytesIO

import numpy as np
from tensorflow.keras.preprocessing.image import load_img, img_to_array
from PIL import Image
from keras.models import load_model
from pathlib import Path


# load and prepare the image
def load_image(filename: str|BytesIO):
# load the image
img = load_img(filename, target_size=(224, 224))
# convert to array
img = img_to_array(img)
# reshape into a single sample with 3 channels
img = img.reshape(1, 224, 224, 3)
# center pixel data
img = img.astype('float32')
img = img - [123.68, 116.779, 103.939]
return img
def load_image(filename: str | BytesIO):
# Open the image
if isinstance(filename, BytesIO):
img = Image.open(filename)
else:
img = Image.open(Path(filename))

# Resize the image to 224x224
img = img.resize((224, 224))

# Convert image to NumPy array (shape: height x width x channels)
img_array = np.array(img)

# Ensure it's 3 channels (RGB)
if img_array.shape[-1] != 3:
img_array = img_array[:, :, :3] # Convert to RGB if it has an alpha channel

# Convert the array to float32 and reshape into (1, 224, 224, 3)
img_array = img_array.astype('float32')
img_array = img_array.reshape((1, 224, 224, 3))

# Center pixel values (subtract mean pixel values)
img_array -= [123.68, 116.779, 103.939]

return img_array


BASE_PATH = Path(__file__).resolve().parent

class IModel(ABC):
def predict(self, img: np.ndarray) -> np.ndarray:
pass

class Model(IModel):
def __init__(self, model_path: str):
self.model = load_model(model_path)

def predict(self, img: np.ndarray) -> np.ndarray:
return self.model.predict(img)

class ModelMock(IModel):
def predict(self, img: np.ndarray) -> np.ndarray:
return np.array([[0.1, 0.2, 0.7]])


class Inference:
def __init__(self, logging, model_path: str):
def __init__(self, logging, model: IModel):
self.logger = logging.getLogger(__name__)
self.model = load_model(model_path)
self.model = model

def execute(self, filepath:str|BytesIO):
img = load_image(filepath)
Expand Down
Loading

0 comments on commit e3a5dab

Please sign in to comment.