-
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.
- Loading branch information
Showing
14 changed files
with
2,444 additions
and
41 deletions.
There are no files selected for viewing
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,7 @@ | ||
.gitignore | ||
.git | ||
Dockerfile* | ||
docker-compose | ||
README.md | ||
LICENSE | ||
.vscode |
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 |
---|---|---|
|
@@ -2,6 +2,8 @@ | |
__pycache__/ | ||
*.py[cod] | ||
*$py.class | ||
/node_modules | ||
.idea/ | ||
|
||
# C extensions | ||
*.so | ||
|
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,22 @@ | ||
FROM python:latest | ||
|
||
# Maintainer info | ||
LABEL maintainer="[email protected]" | ||
|
||
# Make working directories | ||
WORKDIR /weather-recognition-api | ||
|
||
# Upgrade pip with no cache | ||
RUN pip install --no-cache-dir -U pip | ||
|
||
# Copy application requirements file to the created working directory | ||
COPY requirements.txt . | ||
|
||
# Install application dependencies from the requirements file | ||
RUN pip install -r requirements.txt | ||
|
||
# Copy every file in the source folder to the created working directory | ||
COPY . . | ||
|
||
# Run the python application | ||
CMD ["python", "api.py"] |
File renamed without changes.
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,73 @@ | ||
# 1. Library Imports | ||
from http.client import HTTPException | ||
import tensorflow as tf | ||
from tensorflow.keras.models import load_model | ||
from tensorflow.keras.utils import get_file | ||
from tensorflow.keras.utils import load_img | ||
from tensorflow.keras.utils import img_to_array | ||
from tensorflow import expand_dims | ||
import numpy as np | ||
import json | ||
|
||
import uvicorn | ||
from fastapi import FastAPI | ||
from fastapi.encoders import jsonable_encoder | ||
from fastapi.responses import JSONResponse | ||
from fastapi import HTTPException | ||
import os | ||
ROOT_DIR = os.path.realpath(os.path.join(os.path.dirname(__file__), '.')) | ||
|
||
|
||
# Load labels from json file | ||
labels_path = os.path.join(ROOT_DIR, 'data', 'labels.json') | ||
labels = json.load(open(labels_path)) | ||
IMG_SIZE = (120, 120) # Same size as the model's input during training | ||
|
||
# 2. Create the app object | ||
app = FastAPI() | ||
|
||
# 3. Load models | ||
model_path = os.path.join(ROOT_DIR, 'models', 'ResNet50.h5') | ||
model = tf.keras.models.load_model(model_path) | ||
|
||
|
||
# 4. API Endpoints and methods | ||
@app.get("/") | ||
async def home(): | ||
return {"message": "Technical test"} | ||
|
||
|
||
@app.post("/predict") | ||
async def predict_image(image_link: str = ''): | ||
""" | ||
Predict the label from a given image (url) | ||
""" | ||
if image_link == '': | ||
raise HTTPException(status_code=400, detail='No image link provided') | ||
try: | ||
img_path = get_file(origin=image_link) | ||
except Exception as e: | ||
raise HTTPException(status_code=400, detail=str(e)) | ||
image = load_img(img_path, target_size=IMG_SIZE) | ||
image = img_to_array(image) | ||
image = expand_dims(image, axis=0) | ||
|
||
score = model.predict(image) | ||
model_score = round(score.max() * 100, 2) | ||
|
||
prediction = np.argmax(score, axis=1) | ||
label = labels[str(prediction[0])] | ||
response = { | ||
'label': label, | ||
'confidence': model_score, | ||
} | ||
return JSONResponse(content=response) | ||
|
||
|
||
def start_server(): | ||
"Launch the server with poetry run start at root level" | ||
uvicorn.run(app, host="127.0.0.1", port=8000, debug=True) | ||
|
||
|
||
if __name__ == "__main__": | ||
start_server() |
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.