2019-08-01 06:22:17 +00:00
|
|
|
from datetime import datetime
|
2019-08-01 08:33:05 +00:00
|
|
|
from flask import current_app, flash, redirect, render_template, url_for
|
2019-07-19 11:28:17 +00:00
|
|
|
from . import services
|
|
|
|
from flask_login import current_user, login_required
|
2019-08-01 10:16:31 +00:00
|
|
|
from .forms import CreateOCRJobForm
|
2019-07-19 11:28:17 +00:00
|
|
|
from ..import swarm
|
|
|
|
from threading import Thread
|
2019-08-01 06:22:17 +00:00
|
|
|
import hashlib
|
|
|
|
import os
|
2019-07-19 11:28:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
@services.route('/ocr', methods=['GET', 'POST'])
|
|
|
|
@login_required
|
|
|
|
def ocr():
|
2019-08-01 10:16:31 +00:00
|
|
|
create_ocr_job_form = CreateOCRJobForm()
|
|
|
|
if create_ocr_job_form.validate_on_submit():
|
2019-08-01 06:22:17 +00:00
|
|
|
app = current_app._get_current_object()
|
|
|
|
id = hashlib.md5(
|
|
|
|
(current_user.username + '_' + datetime.now().isoformat()).encode()
|
|
|
|
).hexdigest()
|
2019-08-01 09:47:14 +00:00
|
|
|
'''
|
|
|
|
' TODO: Implement a Job class. For now a dictionary representation
|
|
|
|
' is enough.
|
|
|
|
'''
|
|
|
|
job = {'creator': current_user.id,
|
|
|
|
'id': id,
|
|
|
|
'requested_cpus': 2,
|
|
|
|
'requested_memory': 2048,
|
|
|
|
'service': 'ocr',
|
2019-08-01 10:16:31 +00:00
|
|
|
'service_args': {'lang': create_ocr_job_form.language.data,
|
2019-08-01 09:47:14 +00:00
|
|
|
'version': 'latest'
|
|
|
|
},
|
|
|
|
'status': 'queued'
|
|
|
|
}
|
2019-08-01 08:33:05 +00:00
|
|
|
dir = os.path.join(app.config['OPAQUE_FILES'], 'jobs', id)
|
2019-08-01 06:22:17 +00:00
|
|
|
|
|
|
|
try:
|
2019-08-01 09:47:14 +00:00
|
|
|
os.makedirs(dir)
|
|
|
|
except OSError:
|
|
|
|
flash('OSError!')
|
2019-08-01 06:22:17 +00:00
|
|
|
else:
|
2019-08-01 10:16:31 +00:00
|
|
|
for file in create_ocr_job_form.files.data:
|
|
|
|
file.save(os.path.join(dir, file.filename))
|
2019-08-01 06:22:17 +00:00
|
|
|
'''
|
|
|
|
' TODO: Let the scheduler run this job in the background.
|
|
|
|
'
|
|
|
|
' NOTE: Using self created threads is just for testing purpose as
|
|
|
|
' there is no scheduler available.
|
|
|
|
'''
|
|
|
|
thread = Thread(target=swarm.run, args=(job,))
|
|
|
|
thread.start()
|
2019-08-01 09:47:14 +00:00
|
|
|
flash('Job created!')
|
2019-07-19 11:28:17 +00:00
|
|
|
return redirect(url_for('services.ocr'))
|
|
|
|
|
|
|
|
return render_template(
|
|
|
|
'services/ocr.html.j2',
|
|
|
|
title='Optical Character Recognition',
|
2019-08-01 10:16:31 +00:00
|
|
|
create_ocr_job_form=create_ocr_job_form
|
2019-07-19 11:28:17 +00:00
|
|
|
)
|