-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.py
36 lines (30 loc) · 1.15 KB
/
api.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
import os
from flask import Flask
from flask import request
from flask import render_template
from torch_utils import transform_image, prediction
import torch
UPLOAD_FOLDER = 'D:\learn_flask\Practice_Flask\static'
ALLOWED_FILE = ['jpg', 'png', 'jpeg' ]
DEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'
MODEL_PATH ='D:\learn_flask\Practice_Flask\model.pth'
model = None
model= torch.load(MODEL_PATH, map_location = DEVICE)
model.eval()
app = Flask(__name__)
@app.route("/", methods = ["GET", "POST"])
def upload_predict():
if request.method == "POST":
image_file = request.files["image"]
if image_file:
image_location = os.path.join(
UPLOAD_FOLDER, image_file.filename
)
image_file.save(image_location)
img_tensor = transform_image(image_location)
pred = prediction(img_tensor = img_tensor, model=model)
return render_template("index.html", prediction=pred, image_loc = image_file.filename)
pred = ""
return render_template("index.html", prediction=pred, image_file = None)
if __name__ == "__main__":
app.run(port=1200, debug=True)