nopaque/app/services/views.py

69 lines
2.7 KiB
Python
Raw Normal View History

2019-09-24 14:55:24 +00:00
from flask import abort, current_app, flash, redirect, render_template, url_for
2019-07-19 11:28:17 +00:00
from flask_login import current_user, login_required
2019-10-16 14:52:05 +00:00
from werkzeug.utils import secure_filename
from . import services
2019-09-27 11:56:52 +00:00
from .forms import NewNLPJobForm, NewOCRJobForm
from .. import db
2019-10-16 14:52:05 +00:00
from ..models import Job, JobInput
import json
2019-08-09 09:48:43 +00:00
import os
2019-07-19 11:28:17 +00:00
2019-09-27 11:56:52 +00:00
SERVICES = {'nlp': {'name': 'Natural Language Processing',
'resources': {'mem_mb': 4096, 'n_cores': 2},
'new_job_form': NewNLPJobForm},
'ocr': {'name': 'Optical Character Recognition',
'resources': {'mem_mb': 8192, 'n_cores': 4},
'new_job_form': NewOCRJobForm}}
2019-09-24 14:55:24 +00:00
@services.route('/<service_handle>', methods=['GET', 'POST'])
2019-08-05 13:35:18 +00:00
@login_required
2019-09-24 14:55:24 +00:00
def service(service_handle):
2019-09-27 11:56:52 +00:00
if service_handle not in SERVICES:
2019-09-24 14:55:24 +00:00
abort(404)
2019-09-27 11:56:52 +00:00
new_job_form = SERVICES[service_handle]['new_job_form']()
2019-09-24 14:55:24 +00:00
if new_job_form.validate_on_submit():
_service_args = ['-l {}'.format(new_job_form.language.data)]
if service_handle == 'ocr':
if not new_job_form.binarization.data:
_service_args.append('--skip-binarisation')
job = Job(
creator=current_user,
description=new_job_form.description.data,
2019-09-27 11:56:52 +00:00
mem_mb=SERVICES[service_handle]['resources']['mem_mb'],
n_cores=SERVICES[service_handle]['resources']['n_cores'],
2019-09-24 14:55:24 +00:00
service=service_handle,
service_args=json.dumps(_service_args),
service_version=new_job_form.version.data,
status='preparing',
title=new_job_form.title.data
)
db.session.add(job)
db.session.commit()
2019-09-05 10:00:31 +00:00
dir = os.path.join(current_app.config['OPAQUE_STORAGE_DIRECTORY'],
2019-09-24 14:55:24 +00:00
str(job.user_id),
2019-08-06 12:54:00 +00:00
'jobs',
2019-09-24 14:55:24 +00:00
str(job.id))
2019-08-05 13:35:18 +00:00
try:
os.makedirs(dir)
except OSError:
flash('OSError!')
2019-09-24 14:55:24 +00:00
db.session.remove(job)
db.session.commit()
2019-08-05 13:35:18 +00:00
else:
2019-09-24 14:55:24 +00:00
for file in new_job_form.files.data:
2019-10-16 14:52:05 +00:00
filename = secure_filename(file.filename)
file.save(os.path.join(dir, filename))
job_input = JobInput(filename=filename, job=job)
db.session.add(job_input)
2019-09-24 14:55:24 +00:00
job.status = 'submitted'
db.session.commit()
2019-08-05 13:35:18 +00:00
flash('Job created!')
2019-09-27 11:56:52 +00:00
return redirect(
url_for('services.service', service_handle=service_handle)
)
2019-09-24 14:55:24 +00:00
return render_template('services/{}.html.j2'.format(service_handle),
2019-09-27 11:56:52 +00:00
title=SERVICES[service_handle]['name'],
2019-09-24 14:55:24 +00:00
new_job_form=new_job_form)