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-05 13:35:18 +00:00
|
|
|
from .forms import NewOCRJobForm, NewNLPJobForm
|
2019-08-06 12:27:41 +00:00
|
|
|
from ..models import Job
|
2019-07-19 11:28:17 +00:00
|
|
|
from ..import swarm
|
2019-08-06 12:27:41 +00:00
|
|
|
from .. import db
|
2019-07-19 11:28:17 +00:00
|
|
|
from threading import Thread
|
2019-08-06 12:27:41 +00:00
|
|
|
import json
|
2019-08-09 09:48:43 +00:00
|
|
|
import os
|
2019-07-19 11:28:17 +00:00
|
|
|
|
|
|
|
|
2019-08-09 09:48:43 +00:00
|
|
|
@services.route('/nlp', methods=['GET', 'POST'])
|
2019-07-19 11:28:17 +00:00
|
|
|
@login_required
|
2019-08-09 09:48:43 +00:00
|
|
|
def nlp():
|
|
|
|
new_nlp_job_form = NewNLPJobForm()
|
|
|
|
if new_nlp_job_form.validate_on_submit():
|
|
|
|
nlp_job = Job(creator=current_user,
|
|
|
|
description=new_nlp_job_form.description.data,
|
|
|
|
mem_mb=4096,
|
|
|
|
n_cores=2,
|
|
|
|
service='nlp',
|
|
|
|
service_args=json.dumps(['-l {}'.format(new_nlp_job_form.language.data)]),
|
|
|
|
service_version=new_nlp_job_form.version.data,
|
|
|
|
status='submitted',
|
|
|
|
title=new_nlp_job_form.title.data)
|
2019-08-08 10:06:01 +00:00
|
|
|
|
2019-08-09 09:48:43 +00:00
|
|
|
db.session.add(nlp_job)
|
2019-08-06 12:27:41 +00:00
|
|
|
db.session.commit()
|
|
|
|
|
2019-08-08 10:06:01 +00:00
|
|
|
dir = os.path.join(current_app.config['OPAQUE_STORAGE'],
|
2019-08-09 09:48:43 +00:00
|
|
|
str(nlp_job.user_id),
|
2019-08-06 12:54:00 +00:00
|
|
|
'jobs',
|
2019-08-09 09:48:43 +00:00
|
|
|
str(nlp_job.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-09 09:48:43 +00:00
|
|
|
db.session.remove(nlp_job)
|
2019-08-08 10:06:01 +00:00
|
|
|
db.session.commit()
|
2019-08-01 06:22:17 +00:00
|
|
|
else:
|
2019-08-09 09:48:43 +00:00
|
|
|
for file in new_nlp_job_form.files.data:
|
2019-08-01 10:16:31 +00:00
|
|
|
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.
|
|
|
|
'''
|
2019-08-11 17:01:19 +00:00
|
|
|
thread = Thread(target=swarm.run, args=(nlp_job.id,))
|
2019-08-01 06:22:17 +00:00
|
|
|
thread.start()
|
2019-08-01 09:47:14 +00:00
|
|
|
flash('Job created!')
|
2019-08-09 09:48:43 +00:00
|
|
|
return redirect(url_for('services.nlp'))
|
2019-07-19 11:28:17 +00:00
|
|
|
|
|
|
|
return render_template(
|
2019-08-09 09:48:43 +00:00
|
|
|
'services/nlp.html.j2',
|
|
|
|
title='Natrual Language Processing',
|
|
|
|
new_nlp_job_form=new_nlp_job_form
|
2019-07-19 11:28:17 +00:00
|
|
|
)
|
2019-08-05 13:35:18 +00:00
|
|
|
|
|
|
|
|
2019-08-09 09:48:43 +00:00
|
|
|
@services.route('/ocr', methods=['GET', 'POST'])
|
2019-08-05 13:35:18 +00:00
|
|
|
@login_required
|
2019-08-09 09:48:43 +00:00
|
|
|
def ocr():
|
|
|
|
new_ocr_job_form = NewOCRJobForm()
|
|
|
|
if new_ocr_job_form.validate_on_submit():
|
|
|
|
ocr_job = Job(creator=current_user,
|
|
|
|
description=new_ocr_job_form.description.data,
|
|
|
|
mem_mb=8192,
|
|
|
|
n_cores=4,
|
|
|
|
service='ocr',
|
|
|
|
service_args=json.dumps([
|
|
|
|
'-l {}'.format(new_ocr_job_form.language.data),
|
|
|
|
'--skip-binarisation']),
|
|
|
|
service_version=new_ocr_job_form.version.data,
|
|
|
|
status='submitted',
|
|
|
|
title=new_ocr_job_form.title.data)
|
2019-08-08 10:06:01 +00:00
|
|
|
|
2019-08-09 09:48:43 +00:00
|
|
|
db.session.add(ocr_job)
|
2019-08-06 12:27:41 +00:00
|
|
|
db.session.commit()
|
2019-08-06 12:54:00 +00:00
|
|
|
|
2019-08-08 10:06:01 +00:00
|
|
|
dir = os.path.join(current_app.config['OPAQUE_STORAGE'],
|
2019-08-09 09:48:43 +00:00
|
|
|
str(ocr_job.user_id),
|
2019-08-06 12:54:00 +00:00
|
|
|
'jobs',
|
2019-08-09 09:48:43 +00:00
|
|
|
str(ocr_job.id))
|
2019-08-05 13:35:18 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
os.makedirs(dir)
|
|
|
|
except OSError:
|
|
|
|
flash('OSError!')
|
2019-08-09 09:48:43 +00:00
|
|
|
db.session.remove(ocr_job)
|
2019-08-08 10:06:01 +00:00
|
|
|
db.session.commit()
|
2019-08-05 13:35:18 +00:00
|
|
|
else:
|
2019-08-09 09:48:43 +00:00
|
|
|
for file in new_ocr_job_form.files.data:
|
2019-08-05 13:35:18 +00:00
|
|
|
file.save(os.path.join(dir, file.filename))
|
|
|
|
'''
|
|
|
|
' 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.
|
|
|
|
'''
|
2019-08-11 17:01:19 +00:00
|
|
|
thread = Thread(target=swarm.run, args=(ocr_job.id,))
|
2019-08-05 13:35:18 +00:00
|
|
|
thread.start()
|
|
|
|
flash('Job created!')
|
2019-08-09 09:48:43 +00:00
|
|
|
return redirect(url_for('services.ocr'))
|
2019-08-05 13:35:18 +00:00
|
|
|
|
|
|
|
return render_template(
|
2019-08-09 09:48:43 +00:00
|
|
|
'services/ocr.html.j2',
|
|
|
|
title='Optical Character Recognition',
|
|
|
|
new_ocr_job_form=new_ocr_job_form
|
2019-08-05 13:35:18 +00:00
|
|
|
)
|