-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
87 lines (65 loc) · 2.63 KB
/
main.py
File metadata and controls
87 lines (65 loc) · 2.63 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import os
import keys
import json
import sys
from flask import Flask, request, redirect, url_for, render_template
from werkzeug import secure_filename
from clarifai_basic import ClarifaiCustomModel
from flask.ext.sqlalchemy import SQLAlchemy
clarifai_api = ClarifaiCustomModel(app_id=keys.clientId, app_secret=keys.clientSecret)
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg'])
UPLOAD_FOLDER = 'static/'
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
def guess_disease(result):
'''Given the result from Clarifai, scan the results for possible matches.'''
## Data will be from res['results'][0]['result']['tag'], so we can just do result['classes'] and result['probs']
possibleDiseases = ['melanoma'] ## Can be extended later
## List of tuples containing name and probability of disease.
diseaseProbability = []
for disease in possibleDiseases:
if disease in result['classes']:
index = result['classes'].index(disease)
currentProbability = result['probs'][index]
diseaseProbability.append((disease, float(currentProbability)))
diseaseProbability = sorted(diseaseProbability, key=lambda x: x[1], reverse=True)
return diseaseProbability
@app.route('/about/', methods=['GET'])
def about():
return render_template("about.html")
@app.route('/data/', methods=['GET'])
def data():
return render_template("data.html")
# @app.route('/results/', methods=['GET'])
# def results():
# return render_template("results.html")
@app.route('/', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
print request.files
file = request.files['file']
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
## Change first part if we change domain names
filePath = "http://ec2-52-88-123-145.us-west-2.compute.amazonaws.com/" + os.path.join(app.config['UPLOAD_FOLDER'], filename)
result = clarifai_api.predict(filePath, 'test4') ## melanoma
# res = json.loads(json.dumps(result))
# if str(res["status_code"]) == "OK":
# tmp = res['results'][0]['result']['tag']
return render_template("results.html", res=result['urls'][0]['score'] > .55, res1=result['urls'][0]['score'])
# else:
# return str(res["status_msg"])
return render_template('index.html')
if __name__ == '__main__':
if len(sys.argv) == 2:
if sys.argv[1] == "recreate":
db_recreate()
sys.exit(0)
if sys.argv[1] == "create":
db_create()
sys.exit(0)
app.run(debug=True, host='0.0.0.0', port=80, threading=True)