-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·97 lines (87 loc) · 3.47 KB
/
main.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
import os, io, time
import image_prescale, resize
import googlecloudaccess
import logging
from google.cloud import storage
from flask import Flask, render_template, request, redirect, url_for, flash, jsonify
from flask_mobility import Mobility
from flask_mobility.decorators import mobile_template
from werkzeug.utils import secure_filename
from shutil import copyfile
from google.appengine.api import app_identity
ALLOWED_EXTENSIONS = set(['jpg', 'jpeg'])
CLOUD_STORAGE_BUCKET = os.environ['CLOUD_STORAGE_BUCKET']
app = Flask(__name__)
app.secret_key = "@ritsukabbsecret_key"
Mobility(app)
# Image Upload
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/', methods=['GET', 'POST'])
@mobile_template('{mobile/}index.html')
def upload(template):
if request.method == 'POST':
# check if the post request has the file part
if 'photo' not in request.files:
flash( 'Upload fail! Maybe try a different image file (JPG format only)' )
return redirect(request.url)
file = request.files['photo']
# if user does not select file, browser also
# submit a empty part without filename
if file.filename == '':
flash('No file submitted! Try uploading an image file (JPG format only)')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
filename = filename + str(int(time.time()))
imageFile = file
# Create a Cloud Storage client.
gcs = storage.Client()
# Get the bucket that the file will be uploaded to.
bucket = gcs.get_bucket(CLOUD_STORAGE_BUCKET)
# Create a new blob and upload the file's content.
blob = bucket.blob(filename)
blob.upload_from_file(
file,
content_type='image/jpeg'
)
blobURL = blob.media_link
gcs_file = bucket.blob(filename)
contents = gcs_file.download_as_string()
jsFile = image_prescale.createJSON(io.BytesIO(contents))
result = googlecloudaccess.getPrediction(jsFile)
if result and type(result)!=str:
result = int(result["predictions"][0]["aestheticScore"][0])
flash(result)
flash(blobURL)
return redirect(url_for('result'))
else:
flash("Cannot get prediction, try again later. \n Error message: " + str(result))
return redirect(request.url)
else:
flash( 'Upload fail! Maybe try a different image file (JPG format only)' )
return redirect(request.url)
elif request.method == 'GET':
return render_template(template)
@app.errorhandler(500)
def server_error(e):
logging.exception('An error occurred during a request.')
flash("""
An internal error occurred: <pre>{}</pre>
See logs for full stacktrace. Please try again.
""".format(e), 500)
return redirect(url_for('result'))
# result page
@app.route("/result", methods=['GET'])
@mobile_template('{mobile/}result.html')
def result(template):
return render_template(template)
# about page
@app.route("/about", methods=['GET'])
@mobile_template('{mobile/}about.html')
def about(template):
return render_template(template)
# for dev test use
if __name__ == "__main__":
app.run(debug=True, host='0.0.0.0', port=5000)