-
Notifications
You must be signed in to change notification settings - Fork 11
/
app.py
52 lines (41 loc) · 1.41 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
import os
import base64
from io import BytesIO
from flask import Flask, make_response, request, render_template
from werkzeug.exceptions import BadRequest
from serving import (
load_model,
evaluate_input,
)
"""
floyd run --cpu --data floydhub/datasets/colornet/1:colornet --mode serve --env tensorflow-1.7
"""
app = Flask(__name__)
app.config['DEBUG'] = False
load_model()
@app.route('/', methods=['GET'])
def index():
return render_template('serving_template.html')
@app.route('/image', methods=["POST"])
def eval_image():
""""Preprocessing the data and evaluate the model"""
# check if the post request has the file part
input_file = request.files.get('file')
if not input_file:
return BadRequest("File not present in request")
if input_file.filename == '':
return BadRequest("File name is not present in request")
if not input_file.filename.lower().endswith(('.jpg', '.jpeg', '.png')):
return BadRequest("Invalid file type")
# # Save Image to process
input_buffer = BytesIO()
output_buffer = BytesIO()
input_file.save(input_buffer)
img = evaluate_input(input_buffer)
img.save(output_buffer, format="JPEG")
img_str = base64.b64encode(output_buffer.getvalue())
response = make_response(img_str)
response.headers.set('Content-Type', 'image/jpeg')
return response
if __name__ == "__main__":
app.run(host='0.0.0.0', threaded=False)