2021-02-19 13:00:52 +01:00
|
|
|
from flask import (abort, flash, make_response, render_template, request,
|
|
|
|
url_for)
|
2019-07-19 13:28:17 +02:00
|
|
|
from flask_login import current_user, login_required
|
2019-10-16 16:52:05 +02:00
|
|
|
from werkzeug.utils import secure_filename
|
2021-09-13 11:45:43 +02:00
|
|
|
from . import bp
|
2021-02-19 13:00:52 +01:00
|
|
|
from . import SERVICES
|
2021-08-18 15:11:11 +02:00
|
|
|
from .. import db
|
2021-08-04 12:26:49 +02:00
|
|
|
from .forms import AddJobForms
|
2020-07-07 15:08:15 +02:00
|
|
|
from ..models import Job, JobInput
|
2019-08-06 14:27:41 +02:00
|
|
|
import json
|
2020-11-13 10:01:51 +01:00
|
|
|
import logging
|
2019-08-09 11:48:43 +02:00
|
|
|
import os
|
2019-07-19 13:28:17 +02:00
|
|
|
|
|
|
|
|
2021-09-13 11:45:43 +02:00
|
|
|
@bp.route('/corpus-analysis')
|
2021-08-04 12:26:49 +02:00
|
|
|
@login_required
|
|
|
|
def corpus_analysis():
|
2021-08-24 09:21:43 +02:00
|
|
|
return render_template('services/corpus_analysis.html.j2',
|
|
|
|
title='Corpus analysis')
|
2021-08-04 12:26:49 +02:00
|
|
|
|
|
|
|
|
2021-09-13 11:45:43 +02:00
|
|
|
@bp.route('/<service>', methods=['GET', 'POST'])
|
2019-08-05 15:35:18 +02:00
|
|
|
@login_required
|
2019-11-08 12:21:59 +01:00
|
|
|
def service(service):
|
2021-08-04 12:26:49 +02:00
|
|
|
# Check if the requested service exist
|
|
|
|
if service not in SERVICES or service not in AddJobForms:
|
2019-09-24 16:55:24 +02:00
|
|
|
abort(404)
|
2021-08-04 12:26:49 +02:00
|
|
|
version = request.args.get('version',
|
|
|
|
SERVICES[service]['versions']['latest'])
|
|
|
|
if version not in SERVICES[service]['versions']:
|
|
|
|
abort(404)
|
|
|
|
form = AddJobForms[service](prefix='add-job-form', version=version)
|
|
|
|
form.version.data = version
|
|
|
|
title = SERVICES[service]['name']
|
|
|
|
versions = SERVICES[service]['versions']
|
2020-11-13 10:01:51 +01:00
|
|
|
if form.is_submitted():
|
|
|
|
if not form.validate():
|
|
|
|
return make_response(form.errors, 400)
|
2019-11-08 12:21:59 +01:00
|
|
|
service_args = []
|
|
|
|
if service == 'nlp':
|
2021-03-26 13:10:42 +01:00
|
|
|
service_args.append('-l {}'.format(form.language.data))
|
2020-11-13 10:01:51 +01:00
|
|
|
if form.check_encoding.data:
|
2020-02-13 14:41:02 +01:00
|
|
|
service_args.append('--check-encoding')
|
2019-11-08 12:21:59 +01:00
|
|
|
if service == 'ocr':
|
2021-03-26 13:10:42 +01:00
|
|
|
service_args.append('-l {}'.format(form.language.data))
|
2020-11-13 10:01:51 +01:00
|
|
|
if form.binarization.data:
|
2020-04-03 17:36:38 +02:00
|
|
|
service_args.append('--binarize')
|
2019-11-08 12:21:59 +01:00
|
|
|
job = Job(creator=current_user,
|
2020-11-13 10:01:51 +01:00
|
|
|
description=form.description.data,
|
2019-11-08 12:21:59 +01:00
|
|
|
service=service, service_args=json.dumps(service_args),
|
2020-11-13 10:01:51 +01:00
|
|
|
service_version=form.version.data,
|
|
|
|
status='preparing', title=form.title.data)
|
2019-09-24 16:55:24 +02:00
|
|
|
db.session.add(job)
|
2021-01-28 11:25:02 +01:00
|
|
|
db.session.flush()
|
|
|
|
db.session.refresh(job)
|
2019-08-05 15:35:18 +02:00
|
|
|
try:
|
2020-11-13 10:01:51 +01:00
|
|
|
os.makedirs(job.path)
|
2019-08-05 15:35:18 +02:00
|
|
|
except OSError:
|
2020-11-13 10:01:51 +01:00
|
|
|
logging.error('Make dir {} led to an OSError!'.format(job.path))
|
2021-01-28 11:25:02 +01:00
|
|
|
db.session.rollback()
|
2020-11-13 10:01:51 +01:00
|
|
|
flash('Internal Server Error', 'error')
|
|
|
|
return make_response(
|
|
|
|
{'redirect_url': url_for('.service', service=service)}, 500)
|
2019-08-05 15:35:18 +02:00
|
|
|
else:
|
2020-11-13 10:01:51 +01:00
|
|
|
for file in form.files.data:
|
2019-10-16 16:52:05 +02:00
|
|
|
filename = secure_filename(file.filename)
|
2020-11-13 13:33:32 +01:00
|
|
|
job_input = JobInput(filename=filename, job=job)
|
2020-11-13 10:01:51 +01:00
|
|
|
file.save(job_input.path)
|
2019-10-16 16:52:05 +02:00
|
|
|
db.session.add(job_input)
|
2019-09-24 16:55:24 +02:00
|
|
|
job.status = 'submitted'
|
2019-08-12 17:03:12 +02:00
|
|
|
db.session.commit()
|
2020-11-13 10:01:51 +01:00
|
|
|
flash('Job "{}" added'.format(job.title), 'job')
|
2019-11-18 14:23:53 +01:00
|
|
|
return make_response(
|
|
|
|
{'redirect_url': url_for('jobs.job', job_id=job.id)}, 201)
|
2021-08-04 12:26:49 +02:00
|
|
|
return render_template('services/{}.html.j2'.format(service.replace('-', '_')),
|
|
|
|
form=form, title=title, versions=versions)
|