mirror of
https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
synced 2024-12-25 10:54:18 +00:00
115 lines
4.1 KiB
Python
115 lines
4.1 KiB
Python
from datetime import datetime
|
|
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 swarm
|
|
from .. import db
|
|
from threading import Thread
|
|
import os
|
|
import json
|
|
|
|
|
|
@services.route('/ocr', methods=['GET', 'POST'])
|
|
@login_required
|
|
def ocr():
|
|
new_ocr_job_form = NewOCRJobForm()
|
|
if new_ocr_job_form.validate_on_submit():
|
|
app = current_app._get_current_object()
|
|
ocr_job = Job()
|
|
ocr_job.title = new_ocr_job_form.title.data
|
|
ocr_job.description = new_ocr_job_form.description.data
|
|
ocr_job.user_id = current_user.id
|
|
ocr_job.creation_date = datetime.utcnow()
|
|
ocr_job.service = "ocr"
|
|
ocr_job.ressources = json.dumps({"n_cores": 2,
|
|
"mem_mb": 4096})
|
|
ocr_job.service_args = json.dumps({"args": ["--keep-intermediates",
|
|
"--skip-binarisation"],
|
|
"lang": new_ocr_job_form.language.data,
|
|
"version": new_ocr_job_form.version.data})
|
|
ocr_job.status = "queued"
|
|
db.session.add(ocr_job)
|
|
db.session.commit()
|
|
|
|
dir = os.path.join(app.config['OPAQUE_STORAGE'],
|
|
str(ocr_job.user_id),
|
|
'jobs',
|
|
str(ocr_job.id))
|
|
|
|
try:
|
|
os.makedirs(dir)
|
|
except OSError:
|
|
flash('OSError!')
|
|
else:
|
|
for file in new_ocr_job_form.files.data:
|
|
file.save(os.path.join(dir, file.filename))
|
|
'''
|
|
' TODO: Let the scheduler run this job in the background.
|
|
'
|
|
' NOTE: Using self created threads is just for testing purpose as
|
|
' there is no scheduler available.
|
|
'''
|
|
thread = Thread(target=swarm.run, args=(ocr_job,))
|
|
thread.start()
|
|
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
|
|
)
|
|
|
|
|
|
@services.route('/nlp', methods=['GET', 'POST'])
|
|
@login_required
|
|
def nlp():
|
|
new_nlp_job_form = NewNLPJobForm()
|
|
if new_nlp_job_form.validate_on_submit():
|
|
app = current_app._get_current_object()
|
|
nlp_job = Job()
|
|
nlp_job.title = new_nlp_job_form.title.data
|
|
nlp_job.description = new_nlp_job_form.description.data
|
|
nlp_job.user_id = current_user.id
|
|
nlp_job.creation_date = datetime.utcnow()
|
|
nlp_job.service = "nlp"
|
|
nlp_job.ressources = json.dumps({"n_cores": 2,
|
|
"mem_mb": 4096})
|
|
nlp_job.service_args = json.dumps({"args": [],
|
|
"lang": new_nlp_job_form.language.data,
|
|
"version": new_nlp_job_form.version.data})
|
|
nlp_job.status = "queued"
|
|
db.session.add(nlp_job)
|
|
db.session.commit()
|
|
|
|
dir = os.path.join(app.config['OPAQUE_STORAGE'],
|
|
str(nlp_job.user_id),
|
|
'jobs',
|
|
str(nlp_job.id))
|
|
|
|
try:
|
|
os.makedirs(dir)
|
|
except OSError:
|
|
flash('OSError!')
|
|
else:
|
|
for file in new_nlp_job_form.files.data:
|
|
file.save(os.path.join(dir, file.filename))
|
|
'''
|
|
' TODO: Let the scheduler run this job in the background.
|
|
'
|
|
' NOTE: Using self created threads is just for testing purpose as
|
|
' there is no scheduler available.
|
|
'''
|
|
thread = Thread(target=swarm.run, args=(nlp_job,))
|
|
thread.start()
|
|
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
|
|
)
|