-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.py
99 lines (72 loc) · 2.45 KB
/
application.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
from flask import Flask, jsonify, render_template, request, url_for
from flask_jsglue import JSGlue
from google.cloud import storage
import ocr
import SGDpredict
import allocator
import urllib.request
import getpq
from oauth2client import service_account
def list_images(bucket_name):
"""Lists all the images in the bucket."""
storage_client = storage.Client()
bucket = storage_client.get_bucket(bucket_name)
blobs = bucket.list_blobs()
image_list = []
for blob in blobs:
image_list.append(blob.name)
return image_list
#Load SGDmodel
sgdmodel = SGDpredict.SGDModel()
sgdmodel.load('model/sk_SGD_model.pickle')
print('SGD loaded')
#load NeuralNet
neuralnet = allocator.NeuralNet('model/model.tflearn', 'model/words.pickle', 'model/categories.pickle')
neuralnet.load_model()
print('neuralnet loaded')
app = Flask(__name__)
JSGlue(app)
#get list of images from bucket
image_list = list_images('chapterimages')
if app.config["DEBUG"]:
@app.after_request
def after_request(response):
response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
response.headers["Expires"] = 0
response.headers["Pragma"] = "no-cache"
return response
@app.route("/")
def index():
return render_template('index.html')
@app.route("/draft")
def draft():
return render_template('draft.html')
@app.route("/parliamentaryquestions")
def parliamentaryquestions():
return render_template('parliamentaryquestions.html')
@app.route("/getunit")
def getunit():
if request.args.get("question"):
question = request.args.get("question")
prediction1 = sgdmodel.predict(question)
prediction2 = neuralnet.predict(question)
return(jsonify(unit1 = prediction1, unit2 = prediction2))
@app.route("/getpqs")
def getpqs():
return(jsonify(getpq.get_pqs()))
@app.route("/getcase")
def getcase():
if request.args.get("number"):
image_number = int(request.args.get("number"))
case_details = ocr.get_case(image_list[image_number])
print(case_details.email)
print(case_details.date)
print(case_details.name)
return jsonify(image_name = image_list[image_number],
name = case_details.name, email = case_details.email,
date = case_details.date, post = case_details.post,
id = case_details.id, to = case_details.to)
else:
print('Error, no image index')
if __name__ == "__main__":
app.run(host='0.0.0.0')