Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Devployment devlop ready to deploy #1

Merged
merged 2 commits into from
May 24, 2023
Merged
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
2 changes: 1 addition & 1 deletion .idea/Beefy-ML-Api.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,4 @@ RUN pip install python-multipart

COPY . /code

RUN wget --no-check-certificate 'https://download1980.mediafire.com/ob5057pwitpgMK9TOKsAQBJ0xNRIu5f2vCiNjyPtQjcrM0jcP1gRcGVTCTiDUeQWu1ov55f3eZFPB6bhdsIXoS4WlNNWSGUQRd8jnTrSy82bM9eu16nhpy_rposaaGHxiP62ZbDCOyHvW6xvuPw15BdHbmmh0SUvxciiXrqJ-UmvyhgV/fl67w6t0qx3mk1i/softmax_new_model.h5' -O '/code/models/softmax_new_model.h5'

CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "80"]
35 changes: 17 additions & 18 deletions controller.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
from fastapi import UploadFile

import tensorflow as tf
from keras.utils import load_img, img_to_array
from keras.models import load_model
from keras.utils import img_to_array
import numpy as np
from PIL import Image

def preprocessing(fileImage: UploadFile):
img = Image.open(fileImage.file).resize((224, 224))
img_array = img_to_array(img)
img_preprocessed = tf.keras.applications.resnet50.preprocess_input(np.expand_dims(img_array, axis=0))
img = Image.open(fileImage.file)
img = img.resize((224, 224))
img_array = img_to_array(img, dtype=np.float32)
img_preprocessed = np.expand_dims(img_array, axis=0)
return img_preprocessed

def post_preprocessing(img_postpreprocessing):
model = tf.keras.models.load_model('./models/softmax_new_model.h5')
predictions = model.predict(img_postpreprocessing)
predicted_class = np.argmax(predictions, axis=1)[0]
class_labels = ['fresh', 'spoiled']
predicted_label = class_labels[predicted_class]
predicted_probability = predictions[0][predicted_class] * 100
return predicted_label, predicted_probability
model = load_model('./models/beefy-mobilenetv2.h5')
predictions = model.predict(img_postpreprocessing).flatten()
predictions = tf.nn.sigmoid(predictions, name='sigmoid')
predicted_label = tf.where(predictions < 0.5, 'fresh', 'spoiled').numpy()[0]
label_final = bytes.decode(predicted_label, 'utf-8')
return label_final


def predictMeat(file: UploadFile):
def inference(file: UploadFile):
try:
image = preprocessing(fileImage=file)
label, probability = post_preprocessing(img_postpreprocessing=image)
response = {
label = post_preprocessing(img_postpreprocessing=image)
responseBody = {
'label': label,
'pobability': probability
}
return (True, response)
return True, responseBody
except:
return (False, None)
return False, None
16 changes: 12 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
from fastapi import Response, status
from fastapi import FastAPI, UploadFile
from fastapi.middleware.cors import CORSMiddleware

from controller import predictMeat
from controller import inference

app = FastAPI()

app.add_middleware(
CORSMiddleware,
allow_origins=['*'],
allow_credentials=False,
allow_methods=['GET', 'POST'],
allow_headers=["*"],
)


@app.post("/predict/", status_code=200)
async def predict(fileUpload: UploadFile, response: Response):
res_predict = predictMeat(file=fileUpload)
if (res_predict[0]):
responseBody = res_predict[1]
res, responseBody = inference(file=fileUpload)
if res:
return responseBody
else:
response.status_code = status.HTTP_500_INTERNAL_SERVER_ERROR
Expand Down
Binary file added models/beefy-mobilenetv2.h5
Binary file not shown.
Empty file removed models/note.txt
Empty file.