-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
224 lines (170 loc) · 7.02 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# -*- coding: utf-8 -*-
import json
import sys
import os
import stripe
import aiohttp
import asyncio
import uvicorn
import stripe
from keras.preprocessing import image
from fastai import *
from fastai.vision import *
from io import BytesIO
from starlette.applications import Starlette
from starlette.middleware.cors import CORSMiddleware
from starlette.responses import HTMLResponse, JSONResponse
from starlette.staticfiles import StaticFiles
from scripts import tabledef
from scripts import forms
from scripts import helpers
from flask import Flask, redirect, url_for, render_template, request, session
export_file_url = 'https://drive.google.com/uc?export=download&id=1-Rlv4jsQa0XGsDNMvadntQhQj5r93sj-'
export_file_name = 'export.pkt'
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg'])
classes = ['fake', 'real']
path = Path(__file__).parent
async def download_file(url, dest):
if dest.exists(): return
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
data = await response.read()
with open(dest, 'wb') as f:
f.write(data)
async def setup_learner():
await download_file(export_file_url, path / export_file_name)
try:
learn = load_learner(path, export_file_name)
return learn
except RuntimeError as e:
if len(e.args) > 0 and 'CPU-only machine' in e.args[0]:
print(e)
message = "\n\nThis model was trained with an old version of fastai and will not work in a CPU environment.\n\nPlease update the fastai library in your training environment and export your model again.\n\nSee instructions for 'Returning to work' at https://course.fast.ai."
raise RuntimeError(message)
else:
raise
loop = asyncio.get_event_loop()
tasks = [asyncio.ensure_future(setup_learner())]
learn = loop.run_until_complete(asyncio.gather(*tasks))[0]
loop.close()
def load_image(img_path):
img = image.load_img(img_path, target_size=(128, 128, 3))
img = image.img_to_array(img)
#mg = np.expand_dims(img, axis=0)
img /= 255.
img = pil2tensor(img,dtype= np.float32)
return img
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
app = Flask(__name__)
app.secret_key = os.urandom(12) # Generic key for dev purposes only
stripe_keys = {
'secret_key': "sk_test_j1cTyFx2AT75lGhGqTxIzRkl00RVplFhK3",
'publishable_key': "pk_test_zfscr65Y5Is9xZFL0jMXwmhm00xUH1xXn0"
}
stripe.api_key = stripe_keys['secret_key']
# Heroku
#from flask_heroku import Heroku
#heroku = Heroku(app)
# ======== Routing =========================================================== #
# -------- Login ------------------------------------------------------------- #
@app.route('/', methods=['GET', 'POST'])
def login():
if not session.get('logged_in'):
form = forms.LoginForm(request.form)
if request.method == 'POST':
username = request.form['username'].lower()
password = request.form['password']
if form.validate():
if helpers.credentials_valid(username, password):
session['logged_in'] = True
session['username'] = username
return json.dumps({'status': 'Login successful'})
return json.dumps({'status': 'Invalid user/pass'})
return json.dumps({'status': 'Both fields required'})
return render_template('login.html', form=form)
user = helpers.get_user()
return render_template('home.html', user=user, key=stripe_keys['publishable_key'])
@app.route("/logout")
def logout():
session['logged_in'] = False
return redirect(url_for('login'))
@app.route('/analyze', methods=['POST', 'GET'])
#async def analyze(request):
#
# img_data = await request.form()
# img_bytes = await (img_data['file'].read())
# img = open_image(BytesIO(img_bytes))
# prediction = learn.predict(img)[0]
#
# return render_template('image_upload.html', predictions=prediction)
def analyze():
if 'file' not in request.files:
return render_template('image_upload.html', predictions=[])
file = request.files['file']
if file.filename == '':
return render_template('image_upload.html', predictions=[])
if file and allowed_file(file.filename):
image = load_image(file)
print("The image was loaded")
prediction = learn.predict(Image(image))[0]
print("The prediction was made")
print(prediction)
return render_template('image_upload.html', predictions=prediction )
return render_template('image_upload.html', predictions=[])
#return JSONResponse({'result': str(prediction)})
@app.route('/image_upload')
def image_upload():
return render_template('image_upload.html', predictions=[])
# -------- Signup ---------------------------------------------------------- #
@app.route('/signup', methods=['GET', 'POST'])
def signup():
if not session.get('logged_in'):
form = forms.LoginForm(request.form)
if request.method == 'POST':
username = request.form['username'].lower()
password = helpers.hash_password(request.form['password'])
email = request.form['email']
if form.validate():
if not helpers.username_taken(username):
helpers.add_user(username, password, email)
session['logged_in'] = True
session['username'] = username
return json.dumps({'status': 'Signup successful'})
return json.dumps({'status': 'Username taken'})
return json.dumps({'status': 'User/Pass required'})
return render_template('login.html', form=form)
return redirect(url_for('login'))
# -------- Settings ---------------------------------------------------------- #
@app.route('/settings', methods=['GET', 'POST'])
def settings():
if session.get('logged_in'):
if request.method == 'POST':
password = request.form['password']
if password != "":
password = helpers.hash_password(password)
email = request.form['email']
helpers.change_user(password=password, email=email)
return json.dumps({'status': 'Saved'})
user = helpers.get_user()
return render_template('settings.html', user=user)
return redirect(url_for('login'))
#============== Stripe ========================================================#
@app.route('/charge', methods=['POST'])
def charge():
# amount in cents
amount = 500 # I don't know how much the pricing should be ...
customer = stripe.Customer.create(
email=request.form['stripeEmail'],
source=request.form['stripeToken']
)
stripe.Charge.create(
customer=customer.id,
amount=amount,
currency='usd',
description='Flask Charge'
)
return redirect(url_for('image_upload'))
# ======== Main ============================================================== #
if __name__ == "__main__":
app.run(debug=True, use_reloader=True)