mirror of
https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
synced 2024-12-27 11:54:19 +00:00
75 lines
3.2 KiB
Python
75 lines
3.2 KiB
Python
from app import db
|
|
from app.jobs.forms import AddNLPJobForm, AddOCRJobForm, AddMergeImagesJobForm
|
|
from app.models import Job, JobInput
|
|
from flask import (abort, current_app, flash, make_response, render_template,
|
|
url_for)
|
|
from flask_login import current_user, login_required
|
|
from werkzeug.utils import secure_filename
|
|
from . import services
|
|
import json
|
|
import os
|
|
|
|
|
|
SERVICES = {'merge_images': {'name': 'Merge Images',
|
|
'resources': {'mem_mb': 4096, 'n_cores': 4},
|
|
'add_job_form': AddMergeImagesJobForm},
|
|
'nlp': {'name': 'Natural Language Processing',
|
|
'resources': {'mem_mb': 4096, 'n_cores': 2},
|
|
'add_job_form': AddNLPJobForm},
|
|
'ocr': {'name': 'Optical Character Recognition',
|
|
'resources': {'mem_mb': 8192, 'n_cores': 4},
|
|
'add_job_form': AddOCRJobForm}}
|
|
|
|
|
|
@services.route('/<service>', methods=['GET', 'POST'])
|
|
@login_required
|
|
def service(service):
|
|
if service not in SERVICES:
|
|
abort(404)
|
|
add_job_form = SERVICES[service]['add_job_form']()
|
|
if add_job_form.is_submitted():
|
|
if not add_job_form.validate():
|
|
return make_response(add_job_form.errors, 400)
|
|
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)
|
|
db.session.add(job)
|
|
db.session.commit()
|
|
relative_dir = os.path.join(str(job.user_id), 'jobs', str(job.id))
|
|
absolut_dir = os.path.join(
|
|
current_app.config['NOPAQUE_STORAGE'], relative_dir)
|
|
try:
|
|
os.makedirs(absolut_dir)
|
|
except OSError:
|
|
job.delete()
|
|
flash('Internal Server Error')
|
|
return make_response(
|
|
{'redirect_url': url_for('services.service', service='ocr')},
|
|
500)
|
|
else:
|
|
for file in add_job_form.files.data:
|
|
filename = secure_filename(file.filename)
|
|
file.save(os.path.join(absolut_dir, filename))
|
|
job_input = JobInput(dir=relative_dir, filename=filename,
|
|
job=job)
|
|
db.session.add(job_input)
|
|
job.status = 'submitted'
|
|
db.session.commit()
|
|
flash('Job added!')
|
|
return make_response(
|
|
{'redirect_url': url_for('jobs.job', job_id=job.id)}, 201)
|
|
return render_template('services/{}.html.j2'.format(service),
|
|
title=SERVICES[service]['name'],
|
|
add_job_form=add_job_form)
|