nopaque/web/app/services/views.py

77 lines
3.2 KiB
Python
Raw Normal View History

from flask import abort, flash, make_response, 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
2020-03-27 11:06:11 +00:00
from .. import db
from ..jobs.forms import AddFileSetupJobForm, AddNLPJobForm, AddOCRJobForm
from ..models import Job, JobInput
import json
import logging
2019-08-09 09:48:43 +00:00
import os
2019-07-19 11:28:17 +00:00
2020-02-07 14:21:59 +00:00
SERVICES = {'corpus_analysis': {'name': 'Corpus analysis'},
2020-04-09 07:53:56 +00:00
'file-setup': {'name': 'File setup',
'resources': {'mem_mb': 4096, 'n_cores': 4},
'form': AddFileSetupJobForm},
2020-01-07 12:03:42 +00:00
'nlp': {'name': 'Natural Language Processing',
2019-09-27 11:56:52 +00:00
'resources': {'mem_mb': 4096, 'n_cores': 2},
'form': AddNLPJobForm},
2019-09-27 11:56:52 +00:00
'ocr': {'name': 'Optical Character Recognition',
'resources': {'mem_mb': 8192, 'n_cores': 4},
'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)
2020-02-07 14:21:59 +00:00
if service == 'corpus_analysis':
return render_template('services/{}.html.j2'.format(service),
title=SERVICES[service]['name'])
form = SERVICES[service]['form'](prefix='add-job-form')
if form.is_submitted():
if not form.validate():
return make_response(form.errors, 400)
2019-11-08 11:21:59 +00:00
service_args = []
if service == 'nlp':
service_args.append('-l {}'.format(form.language.data))
if form.check_encoding.data:
2020-02-13 13:41:02 +00:00
service_args.append('--check-encoding')
2019-11-08 11:21:59 +00:00
if service == 'ocr':
service_args.append('-l {}'.format(form.language.data))
if form.binarization.data:
service_args.append('--binarize')
2019-11-08 11:21:59 +00:00
job = Job(creator=current_user,
description=form.description.data,
2019-11-08 11:21:59 +00:00
mem_mb=SERVICES[service]['resources']['mem_mb'],
n_cores=SERVICES[service]['resources']['n_cores'],
service=service, service_args=json.dumps(service_args),
service_version=form.version.data,
status='preparing', title=form.title.data)
2019-09-24 14:55:24 +00:00
db.session.add(job)
db.session.commit()
2019-08-05 13:35:18 +00:00
try:
os.makedirs(job.path)
2019-08-05 13:35:18 +00:00
except OSError:
logging.error('Make dir {} led to an OSError!'.format(job.path))
db.session.delete(job)
db.session.commit()
flash('Internal Server Error', 'error')
return make_response(
{'redirect_url': url_for('.service', service=service)}, 500)
2019-08-05 13:35:18 +00:00
else:
for file in form.files.data:
2019-10-16 14:52:05 +00:00
filename = secure_filename(file.filename)
2020-11-13 12:33:32 +00:00
job_input = JobInput(filename=filename, job=job)
file.save(job_input.path)
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()
flash('Job "{}" added'.format(job.title), 'job')
return make_response(
{'redirect_url': url_for('jobs.job', job_id=job.id)}, 201)
2019-11-08 11:21:59 +00:00
return render_template('services/{}.html.j2'.format(service),
form=form, title=SERVICES[service]['name'])