-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
49 lines (35 loc) · 1.19 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from fastapi import FastAPI, UploadFile, File
import uvicorn
import numpy as np
import tensorflow as tf
from utils import read_file_as_image
app = FastAPI()
model_verison = 6
MODEL = tf.keras.models.load_model(f"../training/saved-models/models/{model_verison}")
CLASS_NAMES = ["Early Blight", "Late Blight", "Healthy"] # As defined in the training notebook
@app.get("/ping")
async def ping():
return "Hello, I am alive"
@app.post("/predict")
async def predict(
file: UploadFile = File(...)
):
"""
Upload a file to the api and get a prediction
Args:
file: A file that can be uploaded and used in the payload of the request
Returns:
"""
# The await is to allow multiple requests to read the file without stopping here
image = read_file_as_image(await file.read())
# Let's make the image into a batch
img_batch = np.expand_dims(image, 0)
predictions = MODEL.predict(img_batch)
predicted_class = CLASS_NAMES[np.argmax(predictions[0])]
confidence = np.max(predictions[0])
return {
'class': predicted_class,
'confidence': float(confidence)
}
if __name__ == "__main__":
uvicorn.run(app, host='localhost', port=8080)