-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathapp.py
More file actions
73 lines (56 loc) · 2.65 KB
/
app.py
File metadata and controls
73 lines (56 loc) · 2.65 KB
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
import os
import json
import urllib
import h5py
import pickle as pk
import numpy as np
from os.path import join, dirname, realpath
from flask import Flask, request, redirect, url_for, send_from_directory, render_template, flash, Response
from werkzeug.utils import secure_filename
import engine # remember to reinclude this
# A <form> tag is marked with enctype=multipart/form-data and an <input type=file> is placed in that form.
# The application accesses the file from the files dictionary on the request object.
# use the save() method of the file to save the file permanently somewhere on the filesystem.
UPLOAD_FOLDER = join(dirname(realpath(__file__)), 'static/uploads/') # where uploaded files are stored
ALLOWED_EXTENSIONS = set(['png', 'PNG', 'jpg', 'JPG', 'jpeg', 'JPEG', 'gif', 'GIF']) # models support png and gif as well
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['MAX_CONTENT_LENGTH'] = 10 * 1024 * 1024 # max upload - 10MB
app.secret_key = 'secret'
# check if an extension is valid and that uploads the file and redirects the user to the URL for the uploaded file
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
@app.route('/')
def home():
return render_template('index.html', result=None)
@app.route('/assessment')
def assess():
return render_template('index.html', result=None)
@app.route('/assessment', methods=['GET', 'POST'])
def upload_and_classify():
if request.method == 'POST':
if 'file' not in request.files:
flash('No file part')
return redirect(url_for('assess'))
file = request.files['file']
if file.filename == '':
flash('No selected file')
return redirect(url_for('assess'))
if file and allowed_file(file.filename):
filename = secure_filename(file.filename) # used to secure a filename before storing it directly on the filesystem
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
model_results = engine.engine(filepath)
return render_template('results.html', result=model_results, filename=filename)
flash('Invalid file format - please try your upload again.')
return redirect(url_for('assess'))
@app.route('/uploads/<filename>')
def send_file(filename):
return send_from_directory(UPLOAD_FOLDER, filename)
@app.route('/uploads/<filename>')
def uploaded_file(filename):
return send_from_directory(app.config['UPLOAD_FOLDER'],
filename)
if __name__ == '__main__':
app.run(host='localhost', port=4000, debug=True, use_reloader=False) # remember to set back to False