-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
51 lines (38 loc) · 1.59 KB
/
app.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
from fastapi import FastAPI, File, UploadFile, Form, Request, BackgroundTasks
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from time import sleep
app = FastAPI()
app.mount("/static", StaticFiles(directory="static"), name="static")
templates = Jinja2Templates(directory='templates')
model = None
def model_load():
print("-------------importing module right now-------------")
global model
from model_load import LoadModel
model = LoadModel()
print("-------------Importing completed-------------")
@app.get("/")
async def home(request: Request, bg_task: BackgroundTasks):
'''home page'''
bg_task.add_task(model_load)
return templates.TemplateResponse('index.html', context={'request': request})
@app.post("/uploadfile")
async def create_upload_file(file: UploadFile = File(...), plant = Form(...)):
'''Uploading file'''
path = f"static/images/{file.filename}"
print(file.filename, "Requested plant classifier " + plant)
if 'image' in file.content_type:
contents = await file.read()
with open(path, 'wb') as f:
f.write(contents)
try:
prediction = model.predict(filename = path, plant = plant)
except:
print("-----------------Putting sleep mode----------------")
sleep(5)
print("-----------------Off sleep mode----------------")
prediction = model.predict(filename = path, plant = plant)
print(prediction)
model.remove_it(path)
return {"File": file.filename, "predicted": prediction}