Skip to content

Commit

Permalink
feat: new server
Browse files Browse the repository at this point in the history
  • Loading branch information
likamee committed Aug 28, 2024
1 parent c7adfa6 commit fe8314e
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 19 deletions.
59 changes: 59 additions & 0 deletions .github/workflows/docker-publish-prd.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: Build and Push Docker Image

on:
push:
branches: [ main ]

jobs:
build-and-push:
runs-on: ubuntu-latest

steps:
- name: Check out code
uses: actions/checkout@v3

- name: Create credentials.json
run: echo '${{ secrets.GCS_SERVICE_ACCOUNT_OLD }}' > $GITHUB_WORKSPACE/credentials.json

- name: Authenticate to Google Cloud
uses: google-github-actions/auth@v2
with:
credentials_json: ${{ secrets.GCP_SERVICE_ACCOUNT }}

- name: "Set up Cloud SDK"
uses: "google-github-actions/setup-gcloud@v1"
with:
project_id: "healthy-highway-293513"
service_account_key: ${{ secrets.GCP_SERVICE_ACCOUNT }}
version: "latest"

- name: "Use gcloud CLI"
run: "gcloud info"

- name: "Download .env from GCS"
run: |
gsutil cp gs://action-envs/luzia-api.env ./.env
- name: "Docker auth"
run: |-
gcloud auth configure-docker us-central1-docker.pkg.dev --quiet
- name: Build image
run: docker build . --file ./infra/Dockerfile --tag us-central1-docker.pkg.dev/healthy-highway-293513/eagle-spirit/eagle:luzia-api-${{ github.sha }}

- name: Push image
run: docker push us-central1-docker.pkg.dev/healthy-highway-293513/eagle-spirit/eagle:luzia-api-${{ github.sha }}

- name: Update GitOps Repo
run: |
# Clone your GitOps repo #
git clone https://${{ secrets.PAT }}@github.com/eagle-spirit/deployment.git
# Change to the directory containing your manifests
cd deployment/luzia-api
# Replace the image tag in your manifests
sed -i "s/luzia-api-[0-9a-f]\{40\}/luzia-api-${{ github.sha }}/g" deployment.yaml
# Commit and push the changes to GitHub
git config --global user.email "[email protected]"
git config --global user.name "Gabriel Aranha"
git commit -am "Update image tag to luzia-api-${{ github.sha }}"
git push
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,4 @@ results/*
models/*
custom_metrics.py
app/credentials/*.json
credentials.json
28 changes: 18 additions & 10 deletions app/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import base64
import io, os
import io
import os

import cv2
import matplotlib.cm as cm
Expand All @@ -10,14 +11,13 @@
from IPython.display import Image
from PIL import Image
from tensorflow import keras

from utilities import cam_models


def save_heatmap(heatmap, output_path="heatmap.png"):
plt.imshow(heatmap, cmap='jet')
plt.axis('off')
plt.savefig(output_path, bbox_inches='tight', pad_inches=0)
plt.imshow(heatmap, cmap="jet")
plt.axis("off")
plt.savefig(output_path, bbox_inches="tight", pad_inches=0)


def preprocess_image(image: Image.Image) -> np.ndarray:
Expand All @@ -27,31 +27,39 @@ def preprocess_image(image: Image.Image) -> np.ndarray:
image_array = np.expand_dims(image_array, axis=0)
return image_array


def load_model_from_gcs(bucket_name: str, model_path: str):
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "./app/credentials/credentials.json"
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "./credentials/credentials.json"
print(f"Downloading model from {bucket_name}/{model_path}")
storage_client = storage.Client()
bucket = storage_client.get_bucket(bucket_name)
blob = storage.Blob(model_path, bucket)
local_model_path = f"/tmp/{model_path}"
blob.download_to_filename(local_model_path)
return cam_models.load_saved_model(local_model_path)


def evaluate_model(model, image_array: np.ndarray):
prediction = model.predict(image_array)
return prediction


def superimpose_gradcam(image_path, heatmap, alpha=0.4, cam_path="cam.jpg"):
# Load the original image
original_img = cv2.imread(image_path)
# resize the original image to the size of the heatmap
original_img = cv2.resize(original_img, (299, 299))
original_img = np.clip(original_img, 0, 255).astype(np.uint8) # Ensure values are still in the range [0, 255]
original_img = np.clip(original_img, 0, 255).astype(
np.uint8
) # Ensure values are still in the range [0, 255]

heatmap1 = heatmap - np.min(heatmap)
heatmap1 = heatmap1 / np.ptp(heatmap1)

# Rescale heatmap to a range 0-255
heatmap1 = np.uint8(255 * cv2.resize(heatmap1, (original_img.shape[0], original_img.shape[1])))
heatmap1 = np.uint8(
255 * cv2.resize(heatmap1, (original_img.shape[0], original_img.shape[1]))
)

# Use jet colormap to colorize heatmap
jet = cm.get_cmap("jet")
Expand All @@ -65,7 +73,7 @@ def superimpose_gradcam(image_path, heatmap, alpha=0.4, cam_path="cam.jpg"):
jet_heatmap = keras.preprocessing.image.img_to_array(jet_heatmap)

superimposed_img = jet_heatmap * alpha + original_img
#superimposed_img = (jet_heatmap * alpha) + (original_img * (1 - alpha))
# superimposed_img = (jet_heatmap * alpha) + (original_img * (1 - alpha))

# Ensure the resulting image is still in the range [0, 255]
superimposed_img = np.clip(superimposed_img, 0, 255).astype(np.uint8)
Expand All @@ -75,7 +83,7 @@ def superimpose_gradcam(image_path, heatmap, alpha=0.4, cam_path="cam.jpg"):
# Save the superimposed image in JPEG format
with io.BytesIO() as buffer:
img_pil = keras.preprocessing.image.array_to_img(superimposed_img_rgb)
img_pil.save(buffer, format='JPEG')
img_pil.save(buffer, format="JPEG")
buffer.seek(0)
superimposed_image = buffer.getvalue()

Expand Down
14 changes: 5 additions & 9 deletions infra/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,18 @@ ENV LANG pt_BR.UTF-8 \
LC_ALL pt_BR.UTF-8

# COPY EVERYTING FROM THE CURRENT DIRECTORY TO THE WORKDIR
COPY ./pyproject.toml ./
COPY ./poetry.lock ./
COPY ./app/ ./app
COPY ./app/boot.sh ./app/boot.sh
COPY ./.env ./app/.env
COPY ./pyproject.toml ./poetry.lock ./app/ ./app/boot.sh ./.env /home/$USER/app/
COPY ./credentials.json /home/$USER/app/credentials/

# Create .env file
RUN printenv | grep -v "no_proxy" >> ./app/.env
WORKDIR /home/$USER/app

RUN poetry install

RUN chmod +x ./app/boot.sh
RUN chmod +x ./boot.sh

RUN chown -R $USER:$USER /home/$USER
USER $USER

WORKDIR /home/$USER/app


ENTRYPOINT ["sh", "./boot.sh"]

0 comments on commit fe8314e

Please sign in to comment.