-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserve.py
62 lines (48 loc) · 1.72 KB
/
serve.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
50
51
52
53
54
55
56
57
58
59
60
61
62
from fastapi import FastAPI, HTTPException
from fastapi.responses import JSONResponse, RedirectResponse
from pydantic import BaseModel
import uvicorn
import tensorflow as tf
from tensorflow.keras.models import load_model
import os
import base64
import calendar
import time
from PIL import Image
import numpy as np
# Creating FastAPI instance
app = FastAPI()
# Creating class to define the request body and the type hints of each attribute
class request_body(BaseModel):
instance : str
model_name = os.path.join(os.path.join(os.path.dirname( __file__ ), 'models'), 'model.h5')
model = load_model(model_name, compile=False)
# Defining path operation for root endpoint
@app.get('/')
def main():
return {'message': 'Welcome to Model Predict!'}
# Creating an Endpoint to receive the data to make prediction on.
@app.post('/predict/image')
def predict(data : request_body):
# Making the data in a form suitable for prediction
ts = calendar.timegm(time.gmtime())
if data.instance:
path = os.path.join(os.path.join(os.path.dirname(
__file__), 'uploads'), "b64-" + str(ts) + ".png")
with open(path, "wb") as fh:
fh.write(base64.b64decode(data.instance))
# fh.write(bytes(data.instance).decode('base64'))
else:
raise HTTPException(status_code=400, detail="'instance' (b64) not found in request")
img = Image.open(path).convert('RGB')
img = np.asarray(img.resize((500,500)))
img = tf.cast(img, tf.float32)
img = np.expand_dims(img, axis=0)
result = model.predict(img)
print(type(result))
# if ENV_MODE=='prod':
# # Delete uploaded file
if os.path.isfile(path):
os.remove(path)
# Return the results
return { 'result' : result}