Upload 4 files
Browse files- Dockerfile +20 -0
- main.py +57 -0
- malaria.h5 +3 -0
- requirements.txt +7 -0
Dockerfile
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# you will also find guides on how best to write your Dockerfile
|
| 2 |
+
|
| 3 |
+
FROM python:3.9
|
| 4 |
+
|
| 5 |
+
WORKDIR /code
|
| 6 |
+
|
| 7 |
+
COPY ./requirements.txt /code/requirements.txt
|
| 8 |
+
|
| 9 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
| 10 |
+
|
| 11 |
+
RUN useradd -m -u 1000 user
|
| 12 |
+
USER user
|
| 13 |
+
ENV HOME=/home/user \
|
| 14 |
+
PATH=/home/user/.local/bin:$PATH
|
| 15 |
+
|
| 16 |
+
WORKDIR $HOME/app
|
| 17 |
+
|
| 18 |
+
COPY --chown=user . $HOME/app
|
| 19 |
+
|
| 20 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
main.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import uvicorn
|
| 2 |
+
import numpy as np
|
| 3 |
+
from io import BytesIO
|
| 4 |
+
from fastapi import FastAPI, File, UploadFile
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import tensorflow as tf
|
| 7 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 8 |
+
|
| 9 |
+
app = FastAPI()
|
| 10 |
+
|
| 11 |
+
CHANNELS = 3
|
| 12 |
+
IMAGE_SIZE = 256
|
| 13 |
+
|
| 14 |
+
origins = [
|
| 15 |
+
"http://localhost",
|
| 16 |
+
"http://localhost:3000",
|
| 17 |
+
]
|
| 18 |
+
app.add_middleware(
|
| 19 |
+
CORSMiddleware,
|
| 20 |
+
allow_origins=origins,
|
| 21 |
+
allow_credentials=True,
|
| 22 |
+
allow_methods=["*"],
|
| 23 |
+
allow_headers=["*"],
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
MODEL = tf.keras.models.load_model("malaria.h5")
|
| 27 |
+
|
| 28 |
+
CLASS_NAMES = ['uninfected', 'parasitized']
|
| 29 |
+
|
| 30 |
+
@app.get("/ping")
|
| 31 |
+
async def ping():
|
| 32 |
+
return "Hello, I am alive"
|
| 33 |
+
|
| 34 |
+
if __name__ == "__main__":
|
| 35 |
+
uvicorn.run(app, host='localhost', port=8000)
|
| 36 |
+
|
| 37 |
+
def read_file_as_image(data) -> np.ndarray:
|
| 38 |
+
image = np.array(Image.open(BytesIO(data)))
|
| 39 |
+
image = tf.image.resize_with_crop_or_pad(image,IMAGE_SIZE,IMAGE_SIZE)
|
| 40 |
+
image = tf.reshape(image, (-1,IMAGE_SIZE, IMAGE_SIZE, CHANNELS))
|
| 41 |
+
|
| 42 |
+
return image
|
| 43 |
+
|
| 44 |
+
@app.post("/predict")
|
| 45 |
+
async def predict(file: UploadFile):
|
| 46 |
+
image = read_file_as_image(await file.read())
|
| 47 |
+
|
| 48 |
+
# image_batch = np.expand_dims(image, 0)
|
| 49 |
+
predictions = MODEL.predict(image)
|
| 50 |
+
|
| 51 |
+
predicted_class = CLASS_NAMES[predictions]
|
| 52 |
+
confidence = predictions
|
| 53 |
+
|
| 54 |
+
return {
|
| 55 |
+
'class': predicted_class,
|
| 56 |
+
"confidence": float(confidence)
|
| 57 |
+
}
|
malaria.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:03ce668a3563af463870e42b280d9a3ba6b669ca376afd99aea9ec2a27dbca96
|
| 3 |
+
size 1885128
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
tensorflow
|
| 2 |
+
fastapi
|
| 3 |
+
uvicorn
|
| 4 |
+
python-multipart
|
| 5 |
+
pillow
|
| 6 |
+
matplotlib
|
| 7 |
+
numpy
|