-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
executable file
·204 lines (154 loc) · 6.63 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
from flask import Flask
from flask import render_template
from flask import request
from flask import redirect
from flask import send_file
from flask import send_from_directory
from flask import Markup
import jenkinsapi
from jenkinsapi.jenkins import Jenkins
import json
import os, uuid
# Extras
import extras
from datetime import datetime
# AI
import ai
app = Flask(__name__)
@app.route('/')
def hello_world():
return render_template('index.html')
@app.route('/upload', methods=['GET', 'POST'])
def upload_file():
errorMsgs = []
if request.method == 'POST':
dir_name = str(uuid.uuid4())
print("Going to store file in uploads/" + dir_name)
os.makedirs('uploads/' + dir_name, 0o755)
#LearnCard
LearnCard = request.files['learncard']
LearnFile = 'uploads/' + dir_name + '/LC_000_%s'%LearnCard.filename
#
LearnCard.save(LearnFile)
errorMsgs += extras.validateLearnCardFile(LearnFile)['errors']
#InCard
InCard = request.files['incard']
InFile = 'uploads/' + dir_name + '/IC_%03i_%s'%(0, InCard.filename)
#
InCard.save(InFile)
errorMsgs += extras.validateInCardFile(InFile, LearnFile)['errors']
#eMail
userEmail = request.form['email']
if not extras.validateEmail(userEmail):
errorMsgs += ['Error: Email not valid for this version!']
#NetworkParams
NetworkParams = 'uploads/' + dir_name + '/NetworkParams.json'
info = {}
for x in request.form.keys(): info[x] = request.form[x]
with open(NetworkParams, 'w') as f:
json.dump(info, f)
if len(errorMsgs) != 0:
message_title = 'Fail!'
message_body = Markup( '<br/>'.join(errorMsgs) )
return render_template('index.html', show_fail = True, show_success = False, message_title=message_title, message_body=message_body)
###################### Success
# Trigger job
#J = Jenkins('http://localhost:8080', username='admin', password='password', useCrumb=True)
TEST = False
if not TEST:
J = Jenkins('http://jenkins:8080', username='sequi', password='5werty', useCrumb=True)
params = {'IN_DIR': dir_name, 'IN_MAIL': userEmail}
J.build_job('run-ml', params)
else:
run_process_test(dir_name)
message_title = 'Success!'
message_body = Markup('We are processing your cards. We will send you the result by e-mail to <strong>' + userEmail + '</strong>.')
with open( "emails.txt" , "a+" ) as f:
f.write('%s\t%s\n'%(datetime.now().strftime("%m/%d/%Y %H:%M:%S"), userEmail) )
return render_template('index.html', show_fail = False, show_success = True, message_title=message_title, message_body=message_body)
def run_process_test(_id):
print(' ========== RUN PROCESS ========== ')
print('%s'%('uploads/' + _id))
result = ai.process('uploads/' + _id)
#result='OC_000_InCard_Islands.xlsx'
return
@app.route('/run/<_id>')
def run_process(_id):
print(' ========== RUN PROCESS ========== ')
print('%s'%('uploads/' + _id))
result = ai.process('uploads/' + _id)
#result='OC_000_InCard_Islands.xlsx'
return send_file('uploads/' +_id + r'/%s'%result, mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
@app.route('/results', methods=['GET', 'POST'])
def results():
print('RESULTS')
_id = request.args.get('id', default = '', type = str)
dir_name = 'uploads/' + _id
incards = extras.InCardsFiles(dir_name)
outcards = extras.OutCardsFiles(dir_name)
learncard = extras.LearnCardFile(dir_name)
#for rendering
from extras import uploadedFilesFilenameSize as S
_incards = [x[S:] for x in incards]
_outcards = ['out_' + x[S:] for x in outcards]
_learncard = learncard[S:]
#_id = dir_name
#_incards = ['in1', 'in2']
#_outcards = ['q','e']
#_learncard = 'sarasa'
computed = [extras.nCardFile(x) for x in outcards]
print('COMPUTED = ', computed)
for ICFILE in [x for x in incards if extras.nCardFile(x) not in computed]:
ai.loadModelAndProcess(dir_name, ICFILE)
return render_template('results.html', _id_ = _id, incards=_incards, outcards=_outcards, learncard=_learncard, processing=len(_incards)>len(_outcards))
@app.route('/newincard', methods=['GET', 'POST'])
def newincard():
print('NEWINCARD')
errorMsgs = []
from extras import uploadedFilesFilenameSize as S
if request.method == 'POST':
_id = request.args.to_dict()['id']
dir_name = 'uploads/' + _id
InCard = request.files['incard']
nInCards = len( extras.InCardsFiles(dir_name) )
InFile = dir_name + r'/IC_%03i_%s'%(nInCards, InCard.filename)
#
InCard.save(InFile)
LearnFile = dir_name + r'/%s'%extras.LearnCardFile(dir_name)
errorMsgs += extras.validateInCardFile(InFile, LearnFile)['errors']
print(errorMsgs)
if len(errorMsgs) == 0:
return redirect('results?id=%s'%_id)
else:
os.remove(InFile)
#for rendering
_incards = [x[S:] for x in extras.InCardsFiles(dir_name)]
_outcards = ['out_' + x[S:] for x in extras.OutCardsFiles(dir_name)]
_learncard = extras.LearnCardFile(dir_name)[S:]
message_body = Markup( '<br/>'.join(errorMsgs) )
return render_template('results.html', _id_ = _id, incards=_incards, outcards=_outcards, learncard=_learncard, incardError = True, message_body=message_body)
@app.route('/getcards', methods=['GET'])
def getcards():
from extras import uploadedFilesFilenameSize as S
_folder = 'uploads/' + request.args.get('id', default = '', type = str)
_item = request.args.get('item', default = '', type = int)
_type = request.args.get('type', default = '', type = str)
print('FOLDER %s'%_folder)
print('ITEM %s'%_item)
print('TYPE %s'%_type)
if _type == 'incard':
_file = extras.InCardsFiles(_folder)[_item]
_name = _file[S:]
if _type == 'outcard':
_file = extras.OutCardsFiles(_folder)[_item]
_name = 'out_' + _file[S:]
if _type == 'learncard':
_file = extras.LearnCardFile(_folder)
_name = _file[S:]
return send_from_directory(_folder, _file, attachment_filename=_name, as_attachment=True)
@app.route('/uploads/<path:filename>')
def send_img(filename):
print('te mando este archivo:: %s'%filename)
return send_from_directory('uploads', filename)
if __name__ == '__main__':
app.run(host='0.0.0.0')