-
Notifications
You must be signed in to change notification settings - Fork 0
/
FAST_API_app.py
50 lines (37 loc) · 1.69 KB
/
FAST_API_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
from fastapi import FastAPI, File, UploadFile
from fastapi.responses import FileResponse
from fastapi.staticfiles import StaticFiles
import uvicorn
import io
from PIL import Image
from src.pipeline.predict_pipeline import Operations
op = Operations()
loaded_models = {}
plants = ["Potato", "Pepper", "Tomato"]
for plant in plants:
model, class_names = op.select_model(plant)
loaded_models[plant] = {"model": model, "class_names": class_names}
application = FastAPI()
app = application
app.mount("/ui/static", StaticFiles(directory="ui/static"), name="static")
@app.get("/")
async def render_home():
html_home_page = "ui/templates/home.html"
return FileResponse(html_home_page)
@app.get("/classify/{plant}")
async def render_classification():
html_classification_page = "ui/templates/classifier.html"
return FileResponse(html_classification_page)
# UploadFile is a class from FastAPI's fastapi.UploadFile module. It represents a file uploaded via an HTTP request.
# File(...) is a parameter type annotation used in FastAPI to specify that the parameter should receive files uploaded in the request.
# The ellipsis (...) indicates that the file is required.
@app.post("/classify/{plant}/predict")
async def predict( plant: str, file: UploadFile = File(...) ):
# Read the contents of the uploaded file
contents = await file.read()
# Convert the file contents to an image
image = Image.open(io.BytesIO(contents))
result_dict = op.make_prediction(plant_name=plant, model=loaded_models[plant]["model"], class_names=loaded_models[plant]["class_names"], img=image)
return result_dict
if __name__ == "__main__":
uvicorn.run(app, host='localhost', port=8000)