nopaque/app/services/views.py

66 lines
2.8 KiB
Python
Raw Normal View History

2019-11-08 11:21:59 +00:00
from app import db
from app.jobs.forms import AddNLPJobForm, AddOCRJobForm
from app.models import Job, JobInput
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
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},
2019-11-08 11:21:59 +00:00
'add_job_form': AddNLPJobForm},
2019-09-27 11:56:52 +00:00
'ocr': {'name': 'Optical Character Recognition',
'resources': {'mem_mb': 8192, 'n_cores': 4},
2019-11-08 11:21:59 +00:00
'add_job_form': AddOCRJobForm}}
2019-11-08 11:21:59 +00:00
@services.route('/<service>', methods=['GET', 'POST'])
2019-08-05 13:35:18 +00:00
@login_required
2019-11-08 11:21:59 +00:00
def service(service):
if service not in SERVICES:
2019-09-24 14:55:24 +00:00
abort(404)
2019-11-08 11:21:59 +00:00
add_job_form = SERVICES[service]['add_job_form']()
if add_job_form.validate_on_submit():
service_args = []
if service == 'nlp':
service_args.append('-l {}'.format(add_job_form.language.data))
if service == 'ocr':
service_args.append('-l {}'.format(add_job_form.language.data))
if not add_job_form.binarization.data:
service_args.append('--skip-binarisation')
job = Job(creator=current_user,
description=add_job_form.description.data,
mem_mb=SERVICES[service]['resources']['mem_mb'],
n_cores=SERVICES[service]['resources']['n_cores'],
service=service, service_args=json.dumps(service_args),
service_version=add_job_form.version.data,
status='preparing', title=add_job_form.title.data)
2019-09-24 14:55:24 +00:00
db.session.add(job)
db.session.commit()
2019-11-08 11:21:59 +00:00
relative_dir = os.path.join(str(job.user_id), 'jobs', str(job.id))
absolut_dir = os.path.join(
current_app.config['OPAQUE_STORAGE_DIRECTORY'], relative_dir)
2019-08-05 13:35:18 +00:00
try:
2019-11-08 11:21:59 +00:00
os.makedirs(absolut_dir)
2019-08-05 13:35:18 +00:00
except OSError:
2019-11-08 11:21:59 +00:00
flash('[OSError] Could not add job!')
db.session.delete(job)
db.session.commit()
2019-08-05 13:35:18 +00:00
else:
2019-11-08 11:21:59 +00:00
for file in add_job_form.files.data:
2019-10-16 14:52:05 +00:00
filename = secure_filename(file.filename)
2019-11-08 11:21:59 +00:00
file.save(os.path.join(absolut_dir, filename))
job_input = JobInput(dir=relative_dir, filename=filename,
job=job)
2019-10-16 14:52:05 +00:00
db.session.add(job_input)
2019-09-24 14:55:24 +00:00
job.status = 'submitted'
db.session.commit()
2019-11-08 11:21:59 +00:00
flash('Job added!')
return redirect(url_for('jobs.job', job_id=job.id))
return render_template('services/{}.html.j2'.format(service),
title=SERVICES[service]['name'],
add_job_form=add_job_form)