-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
98 lines (82 loc) · 2.29 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import sys
import uvicorn
import yaml
from fastapi import (
FastAPI,
File,
HTTPException,
Request,
UploadFile,
)
from fastapi.datastructures import UploadFile
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
from src.convnet.nodes.etl import read_convert_image
from src.convnet.nodes.prep import to_gray
from src.convnet.pipes import inference
# web page templating
# get jinja templates' path
templates = Jinja2Templates(directory="templates")
# instantiate web server
app = FastAPI()
# get parameters
params_path = "conf/parameters.yml"
with open(params_path) as file:
PARAMS = yaml.safe_load(file)
# @app.get("/")
# def home():
# return {"Home"}
@app.get("/")
# async def predict(img: UploadFile = File(...)):
async def predict():
# # handle exceptions
# if img is None or img.file is None:
# raise HTTPException(
# status_code=400,
# detail="Please provide an image",
# )
# ext = img.filename.split(".")[-1] in (
# "jpg",
# "jpeg",
# "png",
# )
# # check extension
# if not ext:
# raise HTTPException(
# status_code=400,
# detail="Please load a .jpg or .png",
# )
# # preprocesisng
# # make (height, width, 3) RGB
# img_data = read_convert_image(
# img.file.read(), height=28, width=28
# )
# # make (height, width, 1) gray
# img_data = to_gray(img_data)
# predicted = inference.run(img_data)
from ipdb import set_trace; set_trace()
predicted = inference.run()
return {"prediction:", predicted}
@app.get("/items/{id}", response_class=HTMLResponse)
async def read_item(request: Request, id: str):
return templates.TemplateResponse(
"item.html", {"request": request, "id": id}
)
if __name__ == "__main__":
"""
entry point
usage:
# training
python main.py train
"""
# get run argument
args = sys.argv
is_arg = len(args) == 2
# # choose task
# if is_arg and args[1] == "train":
# print("training")
# train.run(params=PARAMS)
if is_arg and args[1] == "web_serve":
print("serving web")
uvicorn.run(app,host="localhost", port=5000)
# uvicorn.run(app,host="0.0.0.0", port=5000)