forked from ajitsingh98/cattle-breed-classifier-webapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
151 lines (118 loc) · 4.2 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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
from fastai import *
from fastai.vision import *
import fastai
import yaml
import sys
from io import BytesIO
from typing import List, Dict, Union, ByteString, Any
import os
import flask
from flask import Flask
import requests
import torch
import json
import wget
import aiohttp
import asyncio
import uvicorn
import nest_asyncio
import warnings
warnings.filterwarnings("ignore")
nest_asyncio.apply()
export_file_url = 'https://drive.google.com/uc?export=download&id=1d87maowrFqe3-TiiywM1HPqcbyYnJVfS'
export_file_name = 'model.pkl'
path = Path("models/")
async def download_file(url, dest):
if dest.exists(): return
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
data = await response.read()
with open(dest, 'wb') as f:
f.write(data)
async def setup_learner():
await download_file(export_file_url, path / export_file_name)
try:
learn = load_learner(path, export_file_name)
return learn
except RuntimeError as e:
if len(e.args) > 0 and 'CPU-only machine' in e.args[0]:
print(e)
message = "\n\nThis model was trained with an old version of fastai and will not work in a CPU environment.\n\nPlease update the fastai library in your training environment and export your model again.\n\nSee instructions for 'Returning to work' at https://course.fast.ai."
raise RuntimeError(message)
else:
raise
loop = asyncio.get_event_loop()
tasks = [asyncio.ensure_future(setup_learner())]
model = loop.run_until_complete(asyncio.gather(*tasks))[0]
with open("config.yaml", 'r') as stream:
APP_CONFIG = yaml.full_load(stream)
app = Flask(__name__)
def load_model(path=".", model_name="model.pkl"):
learn = load_learner(path, model_name)
return learn
def load_image_url(url: str) -> Image:
response = requests.get(url)
img = open_image(BytesIO(response.content))
return img
def load_image_bytes(raw_bytes: ByteString) -> Image:
img = open_image(BytesIO(raw_bytes))
return img
def predict(img, n: int = 3) -> Dict[str, Union[str, List]]:
pred_class, pred_idx, outputs = model.predict(img)
pred_probs = outputs / sum(outputs)
pred_probs = pred_probs.tolist()
predictions = []
for image_class, output, prob in zip(model.data.classes, outputs.tolist(), pred_probs):
output = round(output, 1)
prob = round(prob, 2)
predictions.append(
{"class": image_class.replace("_", " "), "output": output, "prob": prob}
)
predictions = sorted(predictions, key=lambda x: x["output"], reverse=True)
predictions = predictions[0:n]
return {"class": str(pred_class), "predictions": predictions}
@app.route('/api/classify', methods=['POST', 'GET'])
def upload_file():
if flask.request.method == 'GET':
url = flask.request.args.get("url")
img = load_image_url(url)
else:
bytes = flask.request.files['file'].read()
img = load_image_bytes(bytes)
res = predict(img)
return flask.jsonify(res)
@app.route('/api/classes', methods=['GET'])
def classes():
classes = sorted(model.data.classes)
return flask.jsonify(classes)
@app.route('/ping', methods=['GET'])
def ping():
return "pong"
@app.route('/config')
def config():
return flask.jsonify(APP_CONFIG)
@app.after_request
def add_header(response):
response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate, public, max-age=0"
response.headers["Expires"] = 0
response.headers["Pragma"] = "no-cache"
response.cache_control.max_age = 0
return response
@app.route('/<path:path>')
def static_file(path):
if ".js" in path or ".css" in path:
return app.send_static_file(path)
else:
return app.send_static_file('index.html')
@app.route('/')
def root():
return app.send_static_file('index.html')
def before_request():
app.jinja_env.cache = {}
if __name__ == '__main__':
port = os.environ.get('PORT', 5000)
if "prepare" not in sys.argv:
app.jinja_env.auto_reload = True
app.config['TEMPLATES_AUTO_RELOAD'] = True
app.run(debug=False, host='0.0.0.0', port=port)
# app.run(host='0.0.0.0', port=port)