2022-04-12 14:11:24 +00:00
|
|
|
|
from app.models import TesseractOCRModel
|
2022-04-04 11:31:09 +00:00
|
|
|
|
from flask_login import current_user
|
2021-02-19 12:00:52 +00:00
|
|
|
|
from flask_wtf import FlaskForm
|
2022-04-12 14:11:24 +00:00
|
|
|
|
from flask_wtf.file import FileField, FileRequired
|
2022-02-08 11:26:20 +00:00
|
|
|
|
from wtforms import (
|
|
|
|
|
BooleanField,
|
|
|
|
|
MultipleFileField,
|
|
|
|
|
SelectField,
|
|
|
|
|
StringField,
|
|
|
|
|
SubmitField,
|
|
|
|
|
ValidationError
|
|
|
|
|
)
|
2022-04-04 11:31:09 +00:00
|
|
|
|
from wtforms.validators import DataRequired, InputRequired, Length
|
2021-02-19 12:00:52 +00:00
|
|
|
|
from . import SERVICES
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AddJobForm(FlaskForm):
|
2022-04-12 14:11:24 +00:00
|
|
|
|
description = StringField('Description', validators=[InputRequired(), Length(min=1, max=255)])
|
|
|
|
|
title = StringField('Title', validators=[InputRequired(), Length(min=1, max=32)])
|
2021-03-26 12:10:42 +00:00
|
|
|
|
version = SelectField('Version', validators=[DataRequired()])
|
2022-04-12 14:11:24 +00:00
|
|
|
|
submit = SubmitField()
|
2022-02-03 11:39:16 +00:00
|
|
|
|
|
2021-02-19 12:00:52 +00:00
|
|
|
|
|
2022-04-04 11:31:09 +00:00
|
|
|
|
class AddFileSetupPipelineJobForm(AddJobForm):
|
|
|
|
|
images = MultipleFileField('File(s)', validators=[DataRequired()])
|
|
|
|
|
|
|
|
|
|
def validate_images(form, field):
|
|
|
|
|
valid_mimetypes = ['image/jpeg', 'image/png', 'image/tiff']
|
|
|
|
|
for image in field.data:
|
|
|
|
|
if image.mimetype not in valid_mimetypes:
|
|
|
|
|
raise ValidationError('JPEG, PNG and TIFF files only!')
|
2021-02-19 12:00:52 +00:00
|
|
|
|
|
2021-03-26 12:10:42 +00:00
|
|
|
|
def __init__(self, *args, **kwargs):
|
2022-04-04 11:31:09 +00:00
|
|
|
|
service_manifest = SERVICES['file-setup-pipeline']
|
|
|
|
|
version = kwargs.pop('version', service_manifest['latest_version'])
|
2021-03-26 12:10:42 +00:00
|
|
|
|
super().__init__(*args, **kwargs)
|
2022-04-04 11:31:09 +00:00
|
|
|
|
self.version.choices = [(x, x) for x in service_manifest['versions']]
|
|
|
|
|
self.version.data = version
|
|
|
|
|
self.version.default = service_manifest['latest_version']
|
2021-03-26 12:10:42 +00:00
|
|
|
|
|
2021-02-19 12:00:52 +00:00
|
|
|
|
|
2022-04-04 11:31:09 +00:00
|
|
|
|
class AddTesseractOCRPipelineJobForm(AddJobForm):
|
2022-02-03 11:39:16 +00:00
|
|
|
|
binarization = BooleanField('Binarization')
|
2022-04-04 11:31:09 +00:00
|
|
|
|
pdf = FileField('File', validators=[FileRequired()])
|
|
|
|
|
model = SelectField('Model', validators=[DataRequired()])
|
2021-03-26 12:10:42 +00:00
|
|
|
|
|
|
|
|
|
def validate_binarization(self, field):
|
2022-04-04 11:31:09 +00:00
|
|
|
|
service_info = SERVICES['tesseract-ocr-pipeline']['versions'][self.version.data]
|
2022-02-08 11:26:20 +00:00
|
|
|
|
if field.data and 'binarization' not in service_info['methods']:
|
2022-02-03 11:39:16 +00:00
|
|
|
|
raise ValidationError('Binarization is not available')
|
2021-02-19 12:00:52 +00:00
|
|
|
|
|
2022-04-04 11:31:09 +00:00
|
|
|
|
def validate_pdf(self, field):
|
|
|
|
|
if field.data.mimetype != 'application/pdf':
|
|
|
|
|
raise ValidationError('PDF files only!')
|
2021-02-19 12:00:52 +00:00
|
|
|
|
|
2021-03-26 12:10:42 +00:00
|
|
|
|
def __init__(self, *args, **kwargs):
|
2022-04-04 11:31:09 +00:00
|
|
|
|
service_manifest = SERVICES['tesseract-ocr-pipeline']
|
|
|
|
|
version = kwargs.pop('version', service_manifest['latest_version'])
|
2021-03-26 12:10:42 +00:00
|
|
|
|
super().__init__(*args, **kwargs)
|
2022-04-04 11:31:09 +00:00
|
|
|
|
service_info = service_manifest['versions'][version]
|
2022-02-03 11:39:16 +00:00
|
|
|
|
if 'binarization' not in service_info['methods']:
|
2021-03-26 12:10:42 +00:00
|
|
|
|
self.binarization.render_kw = {'disabled': True}
|
2022-04-04 11:31:09 +00:00
|
|
|
|
compatible_models = [
|
|
|
|
|
x for x in TesseractOCRModel.query.filter_by(shared=True).all()
|
|
|
|
|
if version in x.compatible_service_versions
|
|
|
|
|
]
|
|
|
|
|
compatible_models += [
|
|
|
|
|
x for x in TesseractOCRModel.query.filter_by(shared=False, user=current_user).all()
|
|
|
|
|
if version in x.compatible_service_versions
|
|
|
|
|
]
|
|
|
|
|
self.model.choices = [('', 'Choose your option')]
|
|
|
|
|
self.model.choices += [(x.hashid, x.title) for x in compatible_models]
|
|
|
|
|
self.model.default = ''
|
|
|
|
|
self.version.choices = [(x, x) for x in service_manifest['versions']]
|
2022-02-03 11:39:16 +00:00
|
|
|
|
self.version.data = version
|
2022-04-04 11:31:09 +00:00
|
|
|
|
self.version.default = service_manifest['latest_version']
|
2021-02-19 12:00:52 +00:00
|
|
|
|
|
|
|
|
|
|
2022-04-04 11:31:09 +00:00
|
|
|
|
class AddTranskribusHTRPipelineJobForm(AddJobForm):
|
|
|
|
|
binarization = BooleanField('Binarization')
|
|
|
|
|
pdf = FileField('File', validators=[FileRequired()])
|
|
|
|
|
model = SelectField('Model', validators=[DataRequired()])
|
2021-02-19 12:00:52 +00:00
|
|
|
|
|
2022-04-04 11:31:09 +00:00
|
|
|
|
def validate_binarization(self, field):
|
|
|
|
|
service_info = SERVICES['transkribus-htr-pipeline']['versions'][self.version.data]
|
|
|
|
|
if field.data and 'binarization' not in service_info['methods']:
|
|
|
|
|
raise ValidationError('Binarization is not available')
|
|
|
|
|
|
|
|
|
|
def validate_pdf(self, field):
|
|
|
|
|
if field.data.mimetype != 'application/pdf':
|
|
|
|
|
raise ValidationError('PDF files only!')
|
2021-03-26 12:10:42 +00:00
|
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
2022-04-04 11:31:09 +00:00
|
|
|
|
service_manifest = SERVICES['transkribus-htr-pipeline']
|
|
|
|
|
version = kwargs.pop('version', service_manifest['latest_version'])
|
2021-03-26 12:10:42 +00:00
|
|
|
|
super().__init__(*args, **kwargs)
|
2022-04-04 11:31:09 +00:00
|
|
|
|
service_info = service_manifest['versions'][version]
|
|
|
|
|
if 'binarization' not in service_info['methods']:
|
|
|
|
|
self.binarization.render_kw = {'disabled': True}
|
|
|
|
|
self.model.choices = [('', 'Choose your option')]
|
|
|
|
|
self.model.choices += [
|
|
|
|
|
('37569', 'Tim Model'),
|
|
|
|
|
('29539', 'UCL–University of Toronto #7')
|
|
|
|
|
]
|
|
|
|
|
self.model.default = ''
|
|
|
|
|
self.version.choices = [(x, x) for x in service_manifest['versions']]
|
2022-02-03 11:39:16 +00:00
|
|
|
|
self.version.data = version
|
2022-04-04 11:31:09 +00:00
|
|
|
|
self.version.default = service_manifest['latest_version']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AddSpacyNLPPipelineJobForm(AddJobForm):
|
|
|
|
|
encoding_detection = BooleanField('Encoding detection')
|
|
|
|
|
txt = FileField('File', validators=[FileRequired()])
|
|
|
|
|
model = SelectField('Model', validators=[DataRequired()])
|
|
|
|
|
|
|
|
|
|
def validate_encoding_detection(self, field):
|
|
|
|
|
service_manifest = SERVICES['spacy-nlp-pipeline']
|
|
|
|
|
service_info = service_manifest['versions'][self.version.data]
|
|
|
|
|
if field.data and 'encoding_detection' not in service_info['methods']:
|
|
|
|
|
raise ValidationError('Encoding detection is not available!')
|
2021-08-04 10:26:49 +00:00
|
|
|
|
|
2022-04-04 11:31:09 +00:00
|
|
|
|
def validate_txt(form, field):
|
|
|
|
|
if field.data.mimetype != 'text/plain':
|
|
|
|
|
raise ValidationError('Plain text files only!')
|
2021-08-04 10:26:49 +00:00
|
|
|
|
|
2022-04-04 11:31:09 +00:00
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
service_manifest = SERVICES['spacy-nlp-pipeline']
|
|
|
|
|
version = kwargs.pop('version', service_manifest['latest_version'])
|
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
service_info = service_manifest['versions'][version]
|
|
|
|
|
if 'encoding_detection' not in service_info['methods']:
|
|
|
|
|
self.encoding_detection.render_kw = {'disabled': True}
|
|
|
|
|
self.model.choices = [('', 'Choose your option')]
|
|
|
|
|
self.model.choices += [(x, y) for x, y in service_info['models'].items()] # noqa
|
|
|
|
|
self.model.default = ''
|
|
|
|
|
self.version.choices = [(x, x) for x in service_manifest['versions']]
|
|
|
|
|
self.version.data = version
|
|
|
|
|
self.version.default = version
|