Skip to content

Instantly share code, notes, and snippets.

@LucasMagnum
Created May 16, 2019 22:36
Show Gist options
  • Save LucasMagnum/9f057c83e69322c605d7d4649ee989af to your computer and use it in GitHub Desktop.
Save LucasMagnum/9f057c83e69322c605d7d4649ee989af to your computer and use it in GitHub Desktop.
Flask App - Prediction
import json
from flask import Flask, request
from fastai.vision import open_image, load_learner
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = "./uploads"
learner = load_learner("./")
@app.route('/', methods=['GET', 'POST'])
def upload_file():
predicts = ""
if request.method == 'POST' and 'file' in request.files:
# check if the post request has the file part
file = request.files['file']
img = open_image(file)
_, _, losses = learner.predict(img)
predicts = json.dumps({
"filename": file.filename,
"predictions": sorted(
zip(learner.data.classes, map(lambda l: "%2.f%%" % (float(l) * 100), losses)),
key=lambda p: p[1],
reverse=True
)
})
return '''
<!doctype html>
<title>Quem é?</title>
<h1>Quem é?</h1>
<form method=post enctype=multipart/form-data>
<p><input type=file name=file>
<input type=submit value=Upload>
</form>
%s
''' % predicts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment