-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added app.py step and flask web application step
- Loading branch information
Showing
7 changed files
with
429 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
name: workflow | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
paths-ignore: | ||
- 'README.md' | ||
|
||
permissions: | ||
id-token: write | ||
contents: read | ||
|
||
jobs: | ||
integration: | ||
name: Continuous Integration | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout Code | ||
uses: actions/checkout@v3 | ||
|
||
- name: Lint code | ||
run: echo "Linting repository" | ||
|
||
- name: Run unit tests | ||
run: echo "Running unit tests" | ||
|
||
build-and-push-ecr-image: | ||
name: Continuous Delivery | ||
needs: integration | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout Code | ||
uses: actions/checkout@v3 | ||
|
||
- name: Install Utilities | ||
run: | | ||
sudo apt-get update | ||
sudo apt-get install -y jq unzip | ||
- name: Configure AWS credentials | ||
uses: aws-actions/configure-aws-credentials@v1 | ||
with: | ||
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} | ||
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | ||
aws-region: ${{ secrets.AWS_REGION }} | ||
|
||
- name: Login to Amazon ECR | ||
id: login-ecr | ||
uses: aws-actions/amazon-ecr-login@v1 | ||
|
||
- name: Build, tag, and push image to Amazon ECR | ||
id: build-image | ||
env: | ||
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} | ||
ECR_REPOSITORY: ${{ secrets.ECR_REPOSITORY_NAME }} | ||
IMAGE_TAG: latest | ||
run: | | ||
# Build a docker container and | ||
# push it to ECR so that it can | ||
# be deployed to ECS. | ||
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG . | ||
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG | ||
echo "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" | ||
Continuous-Deployment: | ||
needs: build-and-push-ecr-image | ||
runs-on: self-hosted | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v3 | ||
|
||
- name: Configure AWS credentials | ||
uses: aws-actions/configure-aws-credentials@v1 | ||
with: | ||
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} | ||
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | ||
aws-region: ${{ secrets.AWS_REGION }} | ||
|
||
- name: Login to Amazon ECR | ||
id: login-ecr | ||
uses: aws-actions/amazon-ecr-login@v1 | ||
|
||
|
||
- name: Pull latest images | ||
run: | | ||
docker pull ${{secrets.AWS_ECR_LOGIN_URI}}/${{ secrets.ECR_REPOSITORY_NAME }}:latest | ||
# - name: Stop and remove container if running | ||
# run: | | ||
# docker ps -q --filter "name=cnncls" | grep -q . && docker stop cnncls && docker rm -fv cnncls | ||
|
||
- name: Run Docker Image to serve users | ||
run: | | ||
docker run -d -p 8080:8080 --name=cnncls -e 'AWS_ACCESS_KEY_ID=${{ secrets.AWS_ACCESS_KEY_ID }}' -e 'AWS_SECRET_ACCESS_KEY=${{ secrets.AWS_SECRET_ACCESS_KEY }}' -e 'AWS_REGION=${{ secrets.AWS_REGION }}' ${{secrets.AWS_ECR_LOGIN_URI}}/${{ secrets.ECR_REPOSITORY_NAME }}:latest | ||
- name: Clean previous images and containers | ||
run: | | ||
docker system prune -f |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
FROM python:3.8-slim-buster | ||
|
||
RUN apt update -y && apt install awscli -y | ||
WORKDIR /app | ||
|
||
COPY . /app | ||
RUN pip install -r requirements.txt | ||
|
||
CMD ["python3", "app.py"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,3 +40,5 @@ DVC cmd | |
dvc init | ||
dvc repro | ||
dvc dag | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from flask import Flask, request, jsonify, render_template | ||
import os | ||
from flask_cors import CORS, cross_origin | ||
from cnnClassifier.utils.common import decodeImage | ||
from cnnClassifier.pipeline.prediction import PredictionPipeline | ||
|
||
|
||
os.putenv('LANG', 'en_US.UTF-8') | ||
os.putenv('LC_ALL', 'en_US.UTF-8') | ||
|
||
app = Flask(__name__) | ||
CORS(app) | ||
|
||
|
||
class ClientApp: | ||
def __init__(self): | ||
self.filename = "inputImage.jpg" | ||
self.classifier = PredictionPipeline(self.filename) | ||
|
||
|
||
|
||
|
||
|
||
@app.route("/", methods=['GET']) | ||
@cross_origin() | ||
def home(): | ||
return render_template('index.html') | ||
|
||
|
||
|
||
@app.route("/train", methods=['GET','POST']) | ||
@cross_origin() | ||
def trainRoute(): | ||
os.system("python main.py") | ||
# os.system("dvc repro") | ||
return "Training done successfully!" | ||
|
||
|
||
|
||
|
||
@app.route("/predict", methods=['POST']) | ||
@cross_origin() | ||
def predictRoute(): | ||
image = request.json['image'] | ||
decodeImage(image, clApp.filename) | ||
result = clApp.classifier.predict() | ||
return jsonify(result) | ||
|
||
|
||
|
||
if __name__ == "__main__": | ||
clApp = ClientApp() | ||
app.run(host='0.0.0.0', port=8080) #for AWS |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import numpy as np | ||
from tensorflow.keras.models import load_model | ||
from tensorflow.keras.preprocessing import image | ||
import os | ||
|
||
|
||
|
||
class PredictionPipeline: | ||
def __init__(self,filename): | ||
self.filename =filename | ||
|
||
|
||
|
||
def predict(self): | ||
## load model | ||
|
||
# model = load_model(os.path.join("artifacts","training", "model.h5")) | ||
model = load_model(os.path.join("model", "model.h5")) | ||
|
||
imagename = self.filename | ||
test_image = image.load_img(imagename, target_size = (224,224)) | ||
test_image = image.img_to_array(test_image) | ||
test_image = np.expand_dims(test_image, axis = 0) | ||
result = np.argmax(model.predict(test_image), axis=1) | ||
print(result) | ||
|
||
if result[0] == 1: | ||
prediction = 'Normal' | ||
return [{ "image" : prediction}] | ||
else: | ||
prediction = 'Adenocarcinoma Cancer' | ||
return [{ "image" : prediction}] |
Oops, something went wrong.