-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
70 lines (56 loc) · 2.27 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
import base64
import json
import requests
from urllib.parse import urlparse
from flask import Flask, flash, request, redirect, jsonify, render_template
from werkzeug.utils import secure_filename
app = Flask(__name__)
debug = True
host = '0.0.0.0'
port = 5000
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg'}
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/')
def main_page():
return render_template('index.html')
@app.route('/api/predictions', methods=['GET', 'POST'])
def upload_file():
result_map = ['roses', 'sunflowers', 'daisy', 'dandelion', 'tulips']
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
# if user does not select file, browser also
# submit an empty part without filename
if file.filename == '':
flash('No selected file')
return redirect(request.url)
if file and allowed_file(file.filename):
# filename = secure_filename(file.filename)
input_image = file.read()
payload = {
"instances": [{'bytes_data': {"b64": base64.b64encode(input_image).decode("utf-8")}}]
}
h = urlparse(request.url)
r = requests.post('http://{}:9000/v1/models/ImageCLF:predict'.format(h.hostname), json=payload)
result = r.content.decode('utf-8')
if 'predictions' in result:
results = [{'name': result_map[json.loads(result)['predictions'][0].index(prediction)], 'percentage':
round(prediction * 100, 2)} for prediction in json.loads(result)['predictions'][0]
if round(prediction * 100, 2) > 0]
return jsonify(sorted(results, key=lambda i: i['percentage'], reverse=True))
return '''
<!doctype html>
<title>Upload new File</title>
<h1>Upload new File</h1>
<form method=post enctype=multipart/form-data>
<input type=file name=file>
<input type=submit value=Upload>
</form>
'''
if __name__ == "__main__":
app.run(host=host, port=port, debug=debug)