2023-03-31 07:14:21 +00:00
|
|
|
from flask_wtf import FlaskForm
|
2022-04-04 11:31:09 +00:00
|
|
|
from flask_login import current_user
|
2022-04-12 14:11:24 +00:00
|
|
|
from flask_wtf.file import FileField, FileRequired
|
2023-03-29 07:25:08 +00:00
|
|
|
from wtforms import (
|
|
|
|
BooleanField,
|
|
|
|
DecimalRangeField,
|
|
|
|
MultipleFileField,
|
|
|
|
SelectField,
|
|
|
|
StringField,
|
|
|
|
SubmitField,
|
|
|
|
ValidationError
|
|
|
|
)
|
2022-09-02 11:07:30 +00:00
|
|
|
from wtforms.validators import InputRequired, Length
|
2022-11-14 11:25:26 +00:00
|
|
|
from app.models import SpaCyNLPPipelineModel, TesseractOCRPipelineModel
|
2021-02-19 12:00:52 +00:00
|
|
|
from . import SERVICES
|
|
|
|
|
|
|
|
|
2023-03-31 07:14:21 +00:00
|
|
|
class CreateJobBaseForm(FlaskForm):
|
2022-09-02 11:07:30 +00:00
|
|
|
description = StringField(
|
|
|
|
'Description',
|
|
|
|
validators=[InputRequired(), Length(max=255)]
|
|
|
|
)
|
|
|
|
title = StringField(
|
|
|
|
'Title',
|
|
|
|
validators=[InputRequired(), Length(max=32)]
|
|
|
|
)
|
|
|
|
version = SelectField('Version', validators=[InputRequired()])
|
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-09-02 11:07:30 +00:00
|
|
|
class CreateFileSetupPipelineJobForm(CreateJobBaseForm):
|
|
|
|
images = MultipleFileField('File(s)', validators=[InputRequired()])
|
2022-04-04 11:31:09 +00:00
|
|
|
|
|
|
|
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):
|
2023-03-31 07:14:21 +00:00
|
|
|
if 'prefix' not in kwargs:
|
|
|
|
kwargs['prefix'] = 'create-file-setup-pipeline-job-form'
|
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-09-02 11:07:30 +00:00
|
|
|
class CreateTesseractOCRPipelineJobForm(CreateJobBaseForm):
|
2022-02-03 11:39:16 +00:00
|
|
|
binarization = BooleanField('Binarization')
|
2022-04-04 11:31:09 +00:00
|
|
|
pdf = FileField('File', validators=[FileRequired()])
|
2022-09-02 11:07:30 +00:00
|
|
|
model = SelectField('Model', validators=[InputRequired()])
|
2022-11-10 15:19:58 +00:00
|
|
|
ocropus_nlbin_threshold = DecimalRangeField(
|
2022-11-14 11:25:26 +00:00
|
|
|
render_kw={'min': 0, 'max': 1, 'step': 0.1, 'start': [0.5], 'disabled': True}
|
2022-11-10 11:14:03 +00:00
|
|
|
)
|
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-04-13 14:39:51 +00:00
|
|
|
if field.data:
|
2022-09-02 11:07:30 +00:00
|
|
|
if not('methods' in service_info and 'binarization' in service_info['methods']):
|
2022-04-13 14:39:51 +00:00
|
|
|
raise ValidationError('Binarization is not available')
|
2024-11-07 07:32:20 +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):
|
2023-03-31 07:14:21 +00:00
|
|
|
if 'prefix' not in kwargs:
|
|
|
|
kwargs['prefix'] = 'create-tesseract-ocr-pipeline-job-form'
|
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-04-13 14:39:51 +00:00
|
|
|
if self.binarization.render_kw is None:
|
|
|
|
self.binarization.render_kw = {}
|
|
|
|
self.binarization.render_kw['disabled'] = True
|
2022-11-14 11:25:26 +00:00
|
|
|
if self.ocropus_nlbin_threshold.render_kw is None:
|
|
|
|
self.ocropus_nlbin_threshold.render_kw = {}
|
|
|
|
self.ocropus_nlbin_threshold.render_kw['disabled'] = True
|
2022-04-13 14:39:51 +00:00
|
|
|
if 'methods' in service_info:
|
|
|
|
if 'binarization' in service_info['methods']:
|
2022-11-16 15:05:34 +00:00
|
|
|
del self.binarization.render_kw['disabled']
|
2022-12-13 14:01:04 +00:00
|
|
|
if 'ocropus_nlbin_threshold' in service_info['methods']:
|
|
|
|
del self.ocropus_nlbin_threshold.render_kw['disabled']
|
2023-06-07 08:35:16 +00:00
|
|
|
user_models = [
|
|
|
|
x for x in current_user.tesseract_ocr_pipeline_models.order_by(TesseractOCRPipelineModel.title).all()
|
|
|
|
]
|
2022-05-17 14:16:31 +00:00
|
|
|
models = [
|
2022-11-08 13:11:57 +00:00
|
|
|
x for x in TesseractOCRPipelineModel.query.order_by(TesseractOCRPipelineModel.title).all()
|
2022-12-13 14:01:04 +00:00
|
|
|
if version in x.compatible_service_versions and (x.is_public == True or x.user == current_user)
|
2022-04-04 11:31:09 +00:00
|
|
|
]
|
2023-06-07 08:35:16 +00:00
|
|
|
self.model.choices = {
|
|
|
|
'': [('', 'Choose your option')],
|
|
|
|
'Your models': [(x.hashid, f'{x.title} [{x.version}]') for x in user_models] if user_models else [(0, 'Nothing here yet...')],
|
|
|
|
'Public models': [(x.hashid, f'{x.title} [{x.version}]') for x in models]
|
|
|
|
}
|
2022-04-04 11:31:09 +00:00
|
|
|
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-09-02 11:07:30 +00:00
|
|
|
class CreateTranskribusHTRPipelineJobForm(CreateJobBaseForm):
|
2022-04-04 11:31:09 +00:00
|
|
|
binarization = BooleanField('Binarization')
|
|
|
|
pdf = FileField('File', validators=[FileRequired()])
|
2022-09-02 11:07:30 +00:00
|
|
|
model = SelectField('Model', validators=[InputRequired()])
|
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]
|
2022-04-13 14:39:51 +00:00
|
|
|
if field.data:
|
|
|
|
if(
|
|
|
|
'methods' not in service_info
|
|
|
|
or 'binarization' not in service_info['methods']
|
|
|
|
):
|
|
|
|
raise ValidationError('Binarization is not available')
|
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-03-26 12:10:42 +00:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
2023-03-31 07:14:21 +00:00
|
|
|
if 'prefix' not in kwargs:
|
|
|
|
kwargs['prefix'] = 'create-transkribus-htr-pipeline-job-form'
|
2022-10-12 13:10:55 +00:00
|
|
|
transkribus_htr_pipeline_models = kwargs.pop('transkribus_htr_pipeline_models', [])
|
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]
|
2022-04-13 14:39:51 +00:00
|
|
|
if self.binarization.render_kw is None:
|
|
|
|
self.binarization.render_kw = {}
|
|
|
|
self.binarization.render_kw['disabled'] = True
|
|
|
|
if 'methods' in service_info:
|
|
|
|
if 'binarization' in service_info['methods']:
|
2022-11-16 15:05:34 +00:00
|
|
|
del self.binarization.render_kw['disabled']
|
2022-04-04 11:31:09 +00:00
|
|
|
self.model.choices = [('', 'Choose your option')]
|
2022-10-12 13:10:55 +00:00
|
|
|
self.model.choices += [(x['modelId'], x['name']) for x in transkribus_htr_pipeline_models]
|
2022-04-04 11:31:09 +00:00
|
|
|
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']
|
|
|
|
|
|
|
|
|
2022-09-02 11:07:30 +00:00
|
|
|
class CreateSpacyNLPPipelineJobForm(CreateJobBaseForm):
|
2022-04-13 14:39:51 +00:00
|
|
|
encoding_detection = BooleanField('Encoding detection', render_kw={'disabled': True})
|
2022-04-04 11:31:09 +00:00
|
|
|
txt = FileField('File', validators=[FileRequired()])
|
2022-09-02 11:07:30 +00:00
|
|
|
model = SelectField('Model', validators=[InputRequired()])
|
2024-11-07 07:32:20 +00:00
|
|
|
|
2022-04-04 11:31:09 +00:00
|
|
|
def validate_encoding_detection(self, field):
|
2022-04-13 14:39:51 +00:00
|
|
|
service_info = SERVICES['spacy-nlp-pipeline']['versions'][self.version.data]
|
|
|
|
if field.data:
|
|
|
|
if(
|
|
|
|
'methods' not in service_info
|
|
|
|
or '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):
|
2023-03-31 07:14:21 +00:00
|
|
|
if 'prefix' not in kwargs:
|
|
|
|
kwargs['prefix'] = 'create-spacy-nlp-pipeline-job-form'
|
2022-04-04 11:31:09 +00:00
|
|
|
service_manifest = SERVICES['spacy-nlp-pipeline']
|
|
|
|
version = kwargs.pop('version', service_manifest['latest_version'])
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
service_info = service_manifest['versions'][version]
|
2022-04-13 14:39:51 +00:00
|
|
|
if self.encoding_detection.render_kw is None:
|
|
|
|
self.encoding_detection.render_kw = {}
|
|
|
|
self.encoding_detection.render_kw['disabled'] = True
|
|
|
|
if 'methods' in service_info:
|
|
|
|
if 'encoding_detection' in service_info['methods']:
|
2022-11-16 15:05:34 +00:00
|
|
|
del self.encoding_detection.render_kw['disabled']
|
2023-05-31 14:10:05 +00:00
|
|
|
user_models = [
|
|
|
|
x for x in current_user.spacy_nlp_pipeline_models.order_by(SpaCyNLPPipelineModel.title).all()
|
|
|
|
]
|
2022-11-08 13:11:57 +00:00
|
|
|
models = [
|
2023-05-31 14:10:05 +00:00
|
|
|
x for x in SpaCyNLPPipelineModel.query.filter(SpaCyNLPPipelineModel.user != current_user, SpaCyNLPPipelineModel.is_public == True).order_by(SpaCyNLPPipelineModel.title).all()
|
|
|
|
if version in x.compatible_service_versions
|
2022-11-08 13:11:57 +00:00
|
|
|
]
|
2023-05-31 14:10:05 +00:00
|
|
|
self.model.choices = {
|
|
|
|
'': [('', 'Choose your option')],
|
2023-06-07 08:35:16 +00:00
|
|
|
'Your models': [(x.hashid, f'{x.title} [{x.version}]') for x in user_models] if user_models else [(0, 'Nothing here yet...')],
|
2023-05-31 14:10:05 +00:00
|
|
|
'Public models': [(x.hashid, f'{x.title} [{x.version}]') for x in models]
|
|
|
|
}
|
2022-04-04 11:31:09 +00:00
|
|
|
self.model.default = ''
|
|
|
|
self.version.choices = [(x, x) for x in service_manifest['versions']]
|
|
|
|
self.version.data = version
|
|
|
|
self.version.default = version
|