from flask import current_app, flash, redirect, render_template, url_for from . import services from flask_login import current_user, login_required from .forms import NewOCRJobForm, NewNLPJobForm from ..models import Job from .. import db import json import os @services.route('/nlp', methods=['GET', 'POST']) @login_required def nlp(): new_nlp_job_form = NewNLPJobForm() if new_nlp_job_form.validate_on_submit(): nlp_job = Job(creator=current_user, description=new_nlp_job_form.description.data, mem_mb=4096, n_cores=2, service='nlp', service_args=json.dumps(['-l {}'.format(new_nlp_job_form.language.data)]), service_version=new_nlp_job_form.version.data, status='preparing', title=new_nlp_job_form.title.data) db.session.add(nlp_job) db.session.commit() dir = os.path.join(current_app.config['OPAQUE_STORAGE'], str(nlp_job.user_id), 'jobs', str(nlp_job.id)) try: os.makedirs(dir) except OSError: flash('OSError!') db.session.remove(nlp_job) db.session.commit() else: for file in new_nlp_job_form.files.data: file.save(os.path.join(dir, file.filename)) nlp_job.status = 'submitted' db.session.commit() flash('Job created!') return redirect(url_for('services.nlp')) return render_template( 'services/nlp.html.j2', title='Natrual Language Processing', new_nlp_job_form=new_nlp_job_form ) @services.route('/ocr', methods=['GET', 'POST']) @login_required def ocr(): new_ocr_job_form = NewOCRJobForm() if new_ocr_job_form.validate_on_submit(): ocr_job = Job(creator=current_user, description=new_ocr_job_form.description.data, mem_mb=8192, n_cores=4, service='ocr', service_args=json.dumps([ '-l {}'.format(new_ocr_job_form.language.data), '--skip-binarisation']), service_version=new_ocr_job_form.version.data, status='preparing', title=new_ocr_job_form.title.data) db.session.add(ocr_job) db.session.commit() dir = os.path.join(current_app.config['OPAQUE_STORAGE'], str(ocr_job.user_id), 'jobs', str(ocr_job.id)) try: os.makedirs(dir) except OSError: flash('OSError!') db.session.remove(ocr_job) db.session.commit() else: for file in new_ocr_job_form.files.data: file.save(os.path.join(dir, file.filename)) ocr_job.status = 'submitted' db.session.commit() flash('Job created!') return redirect(url_for('services.ocr')) return render_template( 'services/ocr.html.j2', title='Optical Character Recognition', new_ocr_job_form=new_ocr_job_form )