nopaque/app/services/routes.py

228 lines
8.6 KiB
Python
Raw Normal View History

2023-03-13 15:22:42 +00:00
from flask import abort, current_app, flash, Markup, redirect, render_template, request, url_for
from flask_breadcrumbs import register_breadcrumb
2023-04-11 09:46:33 +00:00
from flask_login import current_user
import requests
from app import db, hashids
2022-04-22 13:27:52 +00:00
from app.models import (
Job,
JobInput,
JobStatus,
TesseractOCRPipelineModel,
SpaCyNLPPipelineModel
2022-04-22 13:27:52 +00:00
)
2022-09-02 11:07:30 +00:00
from . import bp, SERVICES
from .forms import (
2022-09-02 11:07:30 +00:00
CreateFileSetupPipelineJobForm,
CreateTesseractOCRPipelineJobForm,
CreateTranskribusHTRPipelineJobForm,
CreateSpacyNLPPipelineJobForm
)
2019-07-19 11:28:17 +00:00
2023-03-13 15:22:42 +00:00
@bp.route('/services')
@register_breadcrumb(bp, '.', 'Services')
def services():
return redirect(url_for('main.dashboard'))
@bp.route('/file-setup-pipeline', methods=['GET', 'POST'])
2023-03-14 10:13:35 +00:00
@register_breadcrumb(bp, '.file_setup_pipeline', '<i class="nopaque-icons service-icons left" data-service="file-setup-pipeline"></i>File Setup')
def file_setup_pipeline():
service = 'file-setup-pipeline'
service_manifest = SERVICES[service]
version = request.args.get('version', service_manifest['latest_version'])
if version not in service_manifest['versions']:
abort(404)
2023-03-31 07:14:21 +00:00
form = CreateFileSetupPipelineJobForm(prefix='create-job-form', version=version)
if form.is_submitted():
if not form.validate():
2022-09-02 11:07:30 +00:00
response = {'errors': form.errors}
return response, 400
2019-08-05 13:35:18 +00:00
try:
2022-09-02 11:07:30 +00:00
job = Job.create(
title=form.title.data,
description=form.description.data,
service=service,
service_args={},
service_version=form.version.data,
user=current_user
)
2022-09-02 11:07:30 +00:00
except OSError:
abort(500)
for input_file in form.images.data:
try:
2022-09-02 11:07:30 +00:00
JobInput.create(input_file, job=job)
2022-10-11 09:32:50 +00:00
except (AttributeError, OSError):
2022-09-02 11:07:30 +00:00
abort(500)
job.status = JobStatus.SUBMITTED
db.session.commit()
2022-09-02 11:07:30 +00:00
message = Markup(f'Job "<a href="{job.url}">{job.title}</a>" created')
flash(message, 'job')
return {}, 201, {'Location': job.url}
2021-12-08 13:45:05 +00:00
return render_template(
2022-04-22 13:27:52 +00:00
'services/file_setup_pipeline.html.j2',
2023-03-28 12:19:37 +00:00
title=service_manifest['name'],
form=form
)
@bp.route('/tesseract-ocr-pipeline', methods=['GET', 'POST'])
2023-03-14 10:13:35 +00:00
@register_breadcrumb(bp, '.tesseract_ocr_pipeline', '<i class="nopaque-icons service-icons left" data-service="tesseract-ocr-pipeline"></i>Tesseract OCR Pipeline')
def tesseract_ocr_pipeline():
2022-09-02 11:07:30 +00:00
service_name = 'tesseract-ocr-pipeline'
service_manifest = SERVICES[service_name]
version = request.args.get('version', service_manifest['latest_version'])
if version not in service_manifest['versions']:
abort(404)
2023-03-31 07:14:21 +00:00
form = CreateTesseractOCRPipelineJobForm(prefix='create-job-form', version=version)
if form.is_submitted():
if not form.validate():
2022-09-02 11:07:30 +00:00
response = {'errors': form.errors}
return response, 400
try:
2022-09-02 11:07:30 +00:00
job = Job.create(
title=form.title.data,
description=form.description.data,
service=service_name,
service_args={
'binarization': form.binarization.data,
2022-11-10 11:14:03 +00:00
'model': hashids.decode(form.model.data),
2022-11-10 15:19:58 +00:00
'ocropus_nlbin_threshold': float(form.ocropus_nlbin_threshold.data)
2022-09-02 11:07:30 +00:00
},
service_version=form.version.data,
user=current_user
)
except OSError:
abort(500)
try:
2022-09-02 11:07:30 +00:00
JobInput.create(form.pdf.data, job=job)
2022-10-11 09:32:50 +00:00
except (AttributeError, OSError):
2022-09-02 11:07:30 +00:00
abort(500)
job.status = JobStatus.SUBMITTED
db.session.commit()
2022-09-02 11:07:30 +00:00
message = Markup(f'Job "<a href="{job.url}">{job.title}</a>" created')
flash(message, 'job')
return {}, 201, {'Location': job.url}
2022-10-12 08:23:05 +00:00
tesseract_ocr_pipeline_models = [
x for x in TesseractOCRPipelineModel.query.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-05-17 14:16:31 +00:00
]
return render_template(
2022-04-22 13:27:52 +00:00
'services/tesseract_ocr_pipeline.html.j2',
2023-03-28 12:19:37 +00:00
title=service_manifest['name'],
form=form,
2023-03-28 12:19:37 +00:00
tesseract_ocr_pipeline_models=tesseract_ocr_pipeline_models
)
@bp.route('/transkribus-htr-pipeline', methods=['GET', 'POST'])
2023-03-14 10:13:35 +00:00
@register_breadcrumb(bp, '.transkribus_htr_pipeline', '<i class="nopaque-icons service-icons left" data-service="transkribus-htr-pipeline"></i>Transkribus HTR Pipeline')
def transkribus_htr_pipeline():
if not current_app.config.get('NOPAQUE_TRANSKRIBUS_ENABLED'):
abort(404)
service = 'transkribus-htr-pipeline'
service_manifest = SERVICES[service]
version = request.args.get('version', service_manifest['latest_version'])
if version not in service_manifest['versions']:
abort(404)
r = requests.get(
'https://transkribus.eu/TrpServer/rest/models/text',
headers={'Accept': 'application/json'}
)
if r.status_code != 200:
abort(500)
transkribus_htr_pipeline_models = r.json()['trpModelMetadata']
transkribus_htr_pipeline_models.append({'modelId': 48513, 'name': 'Caroline Minuscle', 'language': 'lat', 'isoLanguages': ['lat']})
form = CreateTranskribusHTRPipelineJobForm(
2023-03-31 07:14:21 +00:00
prefix='create-job-form',
transkribus_htr_pipeline_models=transkribus_htr_pipeline_models,
version=version
)
if form.is_submitted():
if not form.validate():
2022-09-02 11:07:30 +00:00
response = {'errors': form.errors}
return response, 400
try:
2022-09-02 11:07:30 +00:00
job = Job.create(
title=form.title.data,
description=form.description.data,
service=service,
service_args={
'binarization': form.binarization.data,
'model': form.model.data
2022-09-02 11:07:30 +00:00
},
service_version=form.version.data,
user=current_user
)
except OSError:
abort(500)
try:
2022-09-02 11:07:30 +00:00
JobInput.create(form.pdf.data, job=job)
2022-10-11 09:32:50 +00:00
except (AttributeError, OSError):
2022-09-02 11:07:30 +00:00
abort(500)
job.status = JobStatus.SUBMITTED
db.session.commit()
2022-09-02 11:07:30 +00:00
message = Markup(f'Job "<a href="{job.url}">{job.title}</a>" created')
flash(message, 'job')
return {}, 201, {'Location': job.url}
return render_template(
2022-09-02 11:07:30 +00:00
'services/transkribus_htr_pipeline.html.j2',
2022-04-22 13:27:52 +00:00
title=service_manifest['name'],
2023-03-28 12:19:37 +00:00
form=form,
2022-10-12 08:23:05 +00:00
transkribus_htr_pipeline_models=transkribus_htr_pipeline_models
2021-12-08 13:45:05 +00:00
)
@bp.route('/spacy-nlp-pipeline', methods=['GET', 'POST'])
2023-03-14 10:13:35 +00:00
@register_breadcrumb(bp, '.spacy_nlp_pipeline', '<i class="nopaque-icons service-icons left" data-service="spacy-nlp-pipeline"></i>SpaCy NLP Pipeline')
def spacy_nlp_pipeline():
service = 'spacy-nlp-pipeline'
service_manifest = SERVICES[service]
version = request.args.get('version', SERVICES[service]['latest_version'])
if version not in service_manifest['versions']:
abort(404)
2023-03-31 07:14:21 +00:00
form = CreateSpacyNLPPipelineJobForm(prefix='create-job-form', version=version)
spacy_nlp_pipeline_models = SpaCyNLPPipelineModel.query.all()
if form.is_submitted():
if not form.validate():
2022-09-02 11:07:30 +00:00
response = {'errors': form.errors}
return response, 400
try:
2022-09-02 11:07:30 +00:00
job = Job.create(
title=form.title.data,
description=form.description.data,
service=service,
service_args={
'encoding_detection': form.encoding_detection.data,
'model': form.model.data
},
service_version=form.version.data,
user=current_user
)
except OSError:
abort(500)
try:
2022-09-02 11:07:30 +00:00
JobInput.create(form.txt.data, job=job)
2022-10-11 09:32:50 +00:00
except (AttributeError, OSError):
2022-09-02 11:07:30 +00:00
abort(500)
job.status = JobStatus.SUBMITTED
db.session.commit()
2022-09-02 11:07:30 +00:00
message = Markup(f'Job "<a href="{job.url}">{job.title}</a>" created')
flash(message, 'job')
return {}, 201, {'Location': job.url}
return render_template(
2022-04-22 13:27:52 +00:00
'services/spacy_nlp_pipeline.html.j2',
2023-03-28 12:19:37 +00:00
title=service_manifest['name'],
form=form,
2023-03-28 12:19:37 +00:00
spacy_nlp_pipeline_models=spacy_nlp_pipeline_models
)
@bp.route('/corpus-analysis')
2023-03-14 10:13:35 +00:00
@register_breadcrumb(bp, '.corpus_analysis', '<i class="nopaque-icons service-icons left" data-service="corpus-analysis"></i>Corpus Analysis')
def corpus_analysis():
return render_template(
'services/corpus_analysis.html.j2',
2023-03-13 15:22:42 +00:00
title='Corpus Analysis'
)