-
Notifications
You must be signed in to change notification settings - Fork 0
/
serve.py
54 lines (41 loc) · 1.14 KB
/
serve.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
from sklearn.externals import joblib
import web
from web import form
import os
myform = form.Form(
form.Textarea("text")
)
render = web.template.render('templates/')
urls = (
'/', 'hello',
'/images/(.*)', 'images',
)
app = web.application(urls, globals())
clf = joblib.load('clf.pkl')
class hello:
def GET(self):
form = myform()
return render.index(form, "", "")
def POST(self):
form = myform()
text = ""
result = ""
if form.validates():
text = form['text'].value
predicted = clf.predict([text])
result = predicted[0]
return render.index(form, text, result)
class images:
def GET(self, name):
ext = name.split(".")[-1] # Gather extension
cType = {
"png": "images/png",
"jpg": "images/jpeg"
}
if name in os.listdir('images'): # Security
web.header("Content-Type", cType[ext]) # Set the Header
return open('images/%s' % name, "rb").read() # Notice 'rb' for reading images
else:
raise web.notfound()
if __name__ == "__main__":
app.run()