Rename ocr job form and add nlp job form

This commit is contained in:
Patrick Jentsch 2019-08-05 08:36:29 +02:00
parent 1c98dc0104
commit a1be95ff68
3 changed files with 54 additions and 21 deletions

View File

@ -3,7 +3,7 @@ from wtforms import MultipleFileField, SelectField, StringField, SubmitField, Va
from wtforms.validators import DataRequired, Length from wtforms.validators import DataRequired, Length
class CreateOCRJobForm(FlaskForm): class NewOCRJobForm(FlaskForm):
description = StringField( description = StringField(
'Description', 'Description',
validators=[DataRequired(), Length(1, 64)] validators=[DataRequired(), Length(1, 64)]
@ -37,3 +37,36 @@ class CreateOCRJobForm(FlaskForm):
'File does not have an approved extension: ' 'File does not have an approved extension: '
'.pdf | .tif | .tiff' '.pdf | .tif | .tiff'
) )
class NewNLPJobForm(FlaskForm):
description = StringField(
'Description',
validators=[DataRequired(), Length(1, 64)]
)
files = MultipleFileField('Files', validators=[DataRequired()])
language = SelectField(
'Language',
choices=[('', 'Choose your option'),
('en', 'English'),
('fr', 'French'),
('de', 'German'),
('it', 'Italian'),
('pt', 'Portuguese'),
('es', 'Spanish')
],
validators=[DataRequired()]
)
submit = SubmitField('Create OCR job')
title = StringField(
'Title',
validators=[DataRequired(), Length(1, 32)]
)
def validate_files(form, field):
for file in field.data:
if not file.filename.lower().endswith('.txt'):
raise ValidationError(
'File does not have an approved extension: '
'.txt'
)

View File

@ -2,7 +2,7 @@ from datetime import datetime
from flask import current_app, flash, redirect, render_template, url_for from flask import current_app, flash, redirect, render_template, url_for
from . import services from . import services
from flask_login import current_user, login_required from flask_login import current_user, login_required
from .forms import CreateOCRJobForm from .forms import NewOCRJobForm
from ..import swarm from ..import swarm
from threading import Thread from threading import Thread
import hashlib import hashlib
@ -12,8 +12,8 @@ import os
@services.route('/ocr', methods=['GET', 'POST']) @services.route('/ocr', methods=['GET', 'POST'])
@login_required @login_required
def ocr(): def ocr():
create_ocr_job_form = CreateOCRJobForm() new_ocr_job_form = NewOCRJobForm()
if create_ocr_job_form.validate_on_submit(): if new_ocr_job_form.validate_on_submit():
app = current_app._get_current_object() app = current_app._get_current_object()
id = hashlib.md5( id = hashlib.md5(
(current_user.username + '_' + datetime.now().isoformat()).encode() (current_user.username + '_' + datetime.now().isoformat()).encode()
@ -27,7 +27,7 @@ def ocr():
'requested_cpus': 2, 'requested_cpus': 2,
'requested_memory': 2048, 'requested_memory': 2048,
'service': 'ocr', 'service': 'ocr',
'service_args': {'lang': create_ocr_job_form.language.data, 'service_args': {'lang': new_ocr_job_form.language.data,
'version': 'latest' 'version': 'latest'
}, },
'status': 'queued' 'status': 'queued'
@ -39,7 +39,7 @@ def ocr():
except OSError: except OSError:
flash('OSError!') flash('OSError!')
else: else:
for file in create_ocr_job_form.files.data: for file in new_ocr_job_form.files.data:
file.save(os.path.join(dir, file.filename)) file.save(os.path.join(dir, file.filename))
''' '''
' TODO: Let the scheduler run this job in the background. ' TODO: Let the scheduler run this job in the background.
@ -55,5 +55,5 @@ def ocr():
return render_template( return render_template(
'services/ocr.html.j2', 'services/ocr.html.j2',
title='Optical Character Recognition', title='Optical Character Recognition',
create_ocr_job_form=create_ocr_job_form new_ocr_job_form=new_ocr_job_form
) )

View File

@ -80,14 +80,14 @@
<div class="card"> <div class="card">
<div class="card-content"> <div class="card-content">
<form method="POST" enctype="multipart/form-data"> <form method="POST" enctype="multipart/form-data">
{{ create_ocr_job_form.hidden_tag() }} {{ new_ocr_job_form.hidden_tag() }}
<div class="row"> <div class="row">
<div class="col s12 m4"> <div class="col s12 m4">
<div class="input-field"> <div class="input-field">
<i class="material-icons prefix">title</i> <i class="material-icons prefix">title</i>
{{ create_ocr_job_form.title() }} {{ new_ocr_job_form.title() }}
{{ create_ocr_job_form.title.label }} {{ new_ocr_job_form.title.label }}
{% for error in create_ocr_job_form.title.errors %} {% for error in new_ocr_job_form.title.errors %}
<span class="helper-text red-text">{{ error }}</span> <span class="helper-text red-text">{{ error }}</span>
{% endfor %} {% endfor %}
</div> </div>
@ -95,9 +95,9 @@
<div class="col s12 m8"> <div class="col s12 m8">
<div class="input-field"> <div class="input-field">
<i class="material-icons prefix">description</i> <i class="material-icons prefix">description</i>
{{ create_ocr_job_form.description() }} {{ new_ocr_job_form.description() }}
{{ create_ocr_job_form.description.label }} {{ new_ocr_job_form.description.label }}
{% for error in create_ocr_job_form.description.errors %} {% for error in new_ocr_job_form.description.errors %}
<span class="helper-text red-text">{{ error }}</span> <span class="helper-text red-text">{{ error }}</span>
{% endfor %} {% endfor %}
</div> </div>
@ -105,13 +105,13 @@
<div class="col s12 m8"> <div class="col s12 m8">
<div class="file-field input-field"> <div class="file-field input-field">
<div class="btn"> <div class="btn">
<span>{{ create_ocr_job_form.files.label.text }}</span> <span>{{ new_ocr_job_form.files.label.text }}</span>
{{ create_ocr_job_form.files(accept='application/pdf, image/tiff') }} {{ new_ocr_job_form.files(accept='application/pdf, image/tiff') }}
</div> </div>
<div class="file-path-wrapper"> <div class="file-path-wrapper">
<input class="file-path validate" type="text"> <input class="file-path validate" type="text">
</div> </div>
{% for error in create_ocr_job_form.files.errors %} {% for error in new_ocr_job_form.files.errors %}
<span class="helper-text red-text">{{ error }}</span> <span class="helper-text red-text">{{ error }}</span>
{% endfor %} {% endfor %}
</div> </div>
@ -119,15 +119,15 @@
<div class="col s12 m4"> <div class="col s12 m4">
<div class="input-field"> <div class="input-field">
<i class="material-icons prefix">language</i> <i class="material-icons prefix">language</i>
{{ create_ocr_job_form.language() }} {{ new_ocr_job_form.language() }}
{{ create_ocr_job_form.language.label }} {{ new_ocr_job_form.language.label }}
{% for error in create_ocr_job_form.language.errors %} {% for error in new_ocr_job_form.language.errors %}
<span class="helper-text red-text">{{ error }}</span> <span class="helper-text red-text">{{ error }}</span>
{% endfor %} {% endfor %}
</div> </div>
</div> </div>
</div> </div>
{{ create_ocr_job_form.submit(class='btn') }} {{ new_ocr_job_form.submit(class='btn') }}
</form> </form>
</div> </div>
</div> </div>