-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
83 lines (62 loc) · 2.25 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
# Imports
import json
import web
from web import form
from scripts.predict import predict_penguin
# Set up the URL routes
urls = (
'/', 'Index',
'/predict', 'Predict',
'/predict_form', 'PredictForm'
)
# Use HTML templates
render = web.template.render('templates') # your templates
# Create a form
predict_form = form.Form(form.Textbox("culmen_length_mm",
description="Culmen length (mm)"),
form.Textbox("culmen_depth_mm",
description="Culmen depth (mm)"),
form.Textbox("flipper_length_mm",
description="Flipper length (mm)"),
form.Textbox("body_mass_g",
description="Body mass (g)"),
form.Button("submit", type="submit",
description="Predict")
)
# Define how the app will respond to routes
class Index:
def GET(self):
return "This app provides penguin-prediction services."
class Predict:
def POST(self):
# Extract the JSON data from the request
data = json.loads(web.data())
# Make the prediction
pred = predict_penguin(data)
# Return the response
return json.dumps({'prediction': pred})
class PredictForm:
def GET(self):
f = predict_form()
return render.predict_form(f)
def POST(self):
# Extract the data
data = web.input()
# Convert all the values to numbers
safe_data = {}
for k, v in data.items():
if v != '':
safe_data[k] = float(v)
# Make the prediction
pred = predict_penguin(safe_data)
# Fill & return the prediction template
return render.prediction(safe_data['culmen_length_mm'],
safe_data['culmen_depth_mm'],
safe_data['flipper_length_mm'],
safe_data['body_mass_g'],
pred)
if __name__ == "__main__":
# Create an application with the routes set up
app = web.application(urls, globals())
# Run the app
app.run()