-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
96 lines (71 loc) · 2.44 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
## App Utilities
import os
# import env
from db import db
from flask_bootstrap import Bootstrap
from flask_restful import Api
from flask import Flask, render_template, request, jsonify
from flask_uploads import UploadSet, configure_uploads, IMAGES
from resources.user import UserRegister, UserLogin, UserLogout, login_manager
from resources.utils import allowed_file, image_classification
from resources.blog_posts import blog_posts
## App SettingsT
app = Flask(__name__)
app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY')
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('SQLALCHEMY_DATABASE_URI')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['PROPAGATE_EXCEPTIONS'] = True
app.config['UPLOADED_PHOTOS_DEST'] = 'static/uploads'
app.config['FLASKS3_BUCKET_NAME'] = os.environ.get('FLASKS3_BUCKET_NAME')
photos = UploadSet('photos', IMAGES) # image upload handling
configure_uploads(app, photos)
app.config['DEBUG'] = False
api = Api(app)
Bootstrap(app)
login_manager.init_app(app)
## Register Resources
api.add_resource(UserRegister, '/register')
api.add_resource(UserLogin, '/login')
api.add_resource(UserLogout, '/logout')
@app.route('/', methods=['GET', 'POST'])
def dashboard():
"""Main Dashboard"""
return render_template("dashboard.html")
@app.route('/predict', methods=['GET', 'POST'])
def predict():
"""Image Classification"""
if request.method == 'POST' and 'file' in request.files:
image = request.files['file']
# .jpg file extension check
if allowed_file(image.filename):
# Apply neural network
guess = image_classification(image)
return jsonify({'guess': guess})
else:
return jsonify({'error': "Only .jpg files allowed"})
else:
return jsonify({'error': "Please upload a .jpg file"})
@app.route('/blog')
def blog():
"""Blog"""
blog_list = blog_posts[::-1]
return render_template("blog.html", blog_list=blog_list)
### ERROR HANDLING
@app.errorhandler(404)
def error404(error):
return render_template('404.html'), 404
@app.errorhandler(500)
def error500(error):
return render_template('500.html'), 500
## DB INIT
db.init_app(app)
## APP INITIATION
if __name__ == '__main__':
if app.config['DEBUG']:
@app.before_first_request
def create_tables():
db.create_all()
# app.run()
# Heroku
port = int(os.environ.get('PORT', 5000))
app.run(host='localhost', port=port)