2022-11-07 08:15:38 +00:00
|
|
|
from flask import abort, current_app, flash, Markup, render_template, url_for
|
2022-11-03 14:38:35 +00:00
|
|
|
from flask_login import login_required, current_user
|
|
|
|
from threading import Thread
|
2022-10-26 08:23:22 +00:00
|
|
|
from app import db
|
2022-11-03 14:38:35 +00:00
|
|
|
from app.decorators import admin_required, permission_required
|
2022-11-07 08:15:38 +00:00
|
|
|
from app.models import Permission, SpaCyNLPPipelineModel, TesseractOCRPipelineModel
|
2022-02-03 11:39:16 +00:00
|
|
|
from . import bp
|
2022-11-07 08:15:38 +00:00
|
|
|
from .forms import TesseractOCRModelContributionForm, EditForm, SpacyNLPModelContributionForm
|
2022-02-03 11:39:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
@bp.before_request
|
|
|
|
@login_required
|
|
|
|
@permission_required(Permission.CONTRIBUTE)
|
|
|
|
def before_request():
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2022-11-03 14:38:35 +00:00
|
|
|
@bp.route('/')
|
|
|
|
@login_required
|
|
|
|
@admin_required
|
2022-09-02 11:02:04 +00:00
|
|
|
def contributions():
|
2022-11-03 14:38:35 +00:00
|
|
|
tesseract_ocr_user_models = [
|
|
|
|
x for x in current_user.tesseract_ocr_pipeline_models
|
|
|
|
]
|
2022-11-07 08:15:38 +00:00
|
|
|
spacy_nlp_user_models = [
|
|
|
|
x for x in current_user.spacy_nlp_pipeline_models
|
|
|
|
]
|
2022-11-03 14:38:35 +00:00
|
|
|
return render_template(
|
|
|
|
'contributions/contribution_overview.html.j2',
|
2022-11-07 08:15:38 +00:00
|
|
|
tesseract_ocr_user_models=tesseract_ocr_user_models,
|
|
|
|
spacy_nlp_user_models=spacy_nlp_user_models,
|
2022-11-03 14:38:35 +00:00
|
|
|
userId = current_user.hashid,
|
|
|
|
title='Contribution Overview'
|
|
|
|
)
|
|
|
|
|
2022-11-07 08:15:38 +00:00
|
|
|
@bp.route('/edit-tesseract-model/<hashid:tesseract_ocr_pipeline_model_id>', methods=['GET', 'POST'])
|
2022-11-03 14:38:35 +00:00
|
|
|
@login_required
|
|
|
|
def tesseract_ocr_pipeline_model(tesseract_ocr_pipeline_model_id):
|
|
|
|
tesseract_ocr_pipeline_model = TesseractOCRPipelineModel.query.get_or_404(
|
|
|
|
tesseract_ocr_pipeline_model_id
|
|
|
|
)
|
2022-11-07 08:15:38 +00:00
|
|
|
form = EditForm(prefix='tesseract-ocr-model-edit-form')
|
2022-11-03 14:38:35 +00:00
|
|
|
if form.validate_on_submit():
|
|
|
|
if tesseract_ocr_pipeline_model.title != form.title.data:
|
|
|
|
tesseract_ocr_pipeline_model.title = form.title.data
|
|
|
|
if tesseract_ocr_pipeline_model.description != form.description.data:
|
|
|
|
tesseract_ocr_pipeline_model.description = form.description.data
|
|
|
|
if tesseract_ocr_pipeline_model.publisher != form.publisher.data:
|
|
|
|
tesseract_ocr_pipeline_model.publisher = form.publisher.data
|
|
|
|
if tesseract_ocr_pipeline_model.publishing_year != form.publishing_year.data:
|
|
|
|
tesseract_ocr_pipeline_model.publishing_year = form.publishing_year.data
|
|
|
|
if tesseract_ocr_pipeline_model.publisher_url != form.publisher_url.data:
|
|
|
|
tesseract_ocr_pipeline_model.publisher_url = form.publisher_url.data
|
|
|
|
if tesseract_ocr_pipeline_model.publishing_url != form.publishing_url.data:
|
|
|
|
tesseract_ocr_pipeline_model.publishing_url = form.publishing_url.data
|
|
|
|
if tesseract_ocr_pipeline_model.version != form.version.data:
|
|
|
|
tesseract_ocr_pipeline_model.version = form.version.data
|
|
|
|
if tesseract_ocr_pipeline_model.shared != form.shared.data:
|
|
|
|
tesseract_ocr_pipeline_model.shared = form.shared.data
|
|
|
|
db.session.commit()
|
|
|
|
message = Markup(f'Model "<a href="contribute/{tesseract_ocr_pipeline_model.hashid}">{tesseract_ocr_pipeline_model.title}</a>" updated')
|
|
|
|
flash(message, category='corpus')
|
|
|
|
return {}, 201, {'Location': url_for('contributions.contributions')}
|
|
|
|
form.prefill(tesseract_ocr_pipeline_model)
|
|
|
|
return render_template(
|
|
|
|
'contributions/tesseract_ocr_pipeline_model.html.j2',
|
|
|
|
tesseract_ocr_pipeline_model=tesseract_ocr_pipeline_model,
|
|
|
|
form=form,
|
|
|
|
title='Edit your Tesseract OCR model'
|
|
|
|
)
|
2022-10-25 11:07:10 +00:00
|
|
|
|
2022-11-07 08:15:38 +00:00
|
|
|
@bp.route('/edit-tesseract-model/<hashid:tesseract_ocr_pipeline_model_id>', methods=['DELETE'])
|
2022-11-03 14:38:35 +00:00
|
|
|
@login_required
|
|
|
|
def delete_tesseract_model(tesseract_ocr_pipeline_model_id):
|
|
|
|
def _delete_tesseract_model(app, tesseract_ocr_pipeline_model_id):
|
|
|
|
with app.app_context():
|
|
|
|
model = TesseractOCRPipelineModel.query.get(tesseract_ocr_pipeline_model_id)
|
|
|
|
model.delete()
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
model = TesseractOCRPipelineModel.query.get_or_404(tesseract_ocr_pipeline_model_id)
|
|
|
|
if not (model.user == current_user or current_user.is_administrator()):
|
|
|
|
abort(403)
|
|
|
|
thread = Thread(
|
|
|
|
target=_delete_tesseract_model,
|
|
|
|
args=(current_app._get_current_object(), tesseract_ocr_pipeline_model_id)
|
|
|
|
)
|
|
|
|
thread.start()
|
|
|
|
return {}, 202
|
2022-10-26 08:23:22 +00:00
|
|
|
|
2022-11-03 14:38:35 +00:00
|
|
|
@bp.route('/add-tesseract-ocr-pipeline-model', methods=['GET', 'POST'])
|
|
|
|
def add_tesseract_ocr_pipeline_model():
|
2022-10-26 10:02:53 +00:00
|
|
|
form = TesseractOCRModelContributionForm(
|
2022-10-26 08:23:22 +00:00
|
|
|
prefix='contribute-tesseract-ocr-pipeline-model-form'
|
|
|
|
)
|
2022-10-25 11:07:10 +00:00
|
|
|
if form.is_submitted():
|
|
|
|
if not form.validate():
|
|
|
|
response = {'errors': form.errors}
|
|
|
|
return response, 400
|
|
|
|
try:
|
2022-10-26 10:02:53 +00:00
|
|
|
tesseract_ocr_model = TesseractOCRPipelineModel.create(
|
2022-11-03 14:38:35 +00:00
|
|
|
form.tesseract_model_file.data,
|
2022-10-26 08:23:22 +00:00
|
|
|
compatible_service_versions=form.compatible_service_versions.data,
|
|
|
|
description=form.description.data,
|
|
|
|
publisher=form.publisher.data,
|
|
|
|
publisher_url=form.publisher_url.data,
|
|
|
|
publishing_url=form.publishing_url.data,
|
|
|
|
publishing_year=form.publishing_year.data,
|
|
|
|
shared=form.shared.data,
|
|
|
|
title=form.title.data,
|
2022-11-03 14:38:35 +00:00
|
|
|
version=form.version.data,
|
|
|
|
user=current_user
|
2022-10-26 08:23:22 +00:00
|
|
|
)
|
2022-10-25 11:07:10 +00:00
|
|
|
except OSError:
|
|
|
|
abort(500)
|
|
|
|
db.session.commit()
|
2022-10-26 08:23:22 +00:00
|
|
|
message = Markup(f'Model "{tesseract_ocr_model.title}" created')
|
|
|
|
flash(message)
|
|
|
|
return {}, 201, {'Location': url_for('contributions.contributions')}
|
2022-11-03 14:38:35 +00:00
|
|
|
tesseract_ocr_pipeline_models = [
|
|
|
|
x for x in TesseractOCRPipelineModel.query.all()
|
|
|
|
]
|
|
|
|
|
2022-10-02 21:49:14 +00:00
|
|
|
return render_template(
|
2022-11-03 14:38:35 +00:00
|
|
|
'contributions/contribute_tesseract_ocr_models.html.j2',
|
2022-10-02 21:49:14 +00:00
|
|
|
form=form,
|
2022-11-03 14:38:35 +00:00
|
|
|
tesseract_ocr_pipeline_models=tesseract_ocr_pipeline_models,
|
|
|
|
title='Tesseract OCR Model Contribution'
|
2022-10-02 21:49:14 +00:00
|
|
|
)
|
2022-11-07 08:15:38 +00:00
|
|
|
|
|
|
|
@bp.route('/edit-spacy-model//<hashid:spacy_nlp_pipeline_model_id>', methods=['GET', 'POST'])
|
|
|
|
@login_required
|
|
|
|
def spacy_nlp_pipeline_model(spacy_nlp_pipeline_model_id):
|
|
|
|
spacy_nlp_pipeline_model = SpaCyNLPPipelineModel.query.get_or_404(
|
|
|
|
spacy_nlp_pipeline_model_id
|
|
|
|
)
|
|
|
|
form = EditForm(prefix='spacy-nlp-model-edit-form')
|
|
|
|
if form.validate_on_submit():
|
|
|
|
if spacy_nlp_pipeline_model.title != form.title.data:
|
|
|
|
spacy_nlp_pipeline_model.title = form.title.data
|
|
|
|
if spacy_nlp_pipeline_model.description != form.description.data:
|
|
|
|
spacy_nlp_pipeline_model.description = form.description.data
|
|
|
|
if spacy_nlp_pipeline_model.publisher != form.publisher.data:
|
|
|
|
spacy_nlp_pipeline_model.publisher = form.publisher.data
|
|
|
|
if spacy_nlp_pipeline_model.publishing_year != form.publishing_year.data:
|
|
|
|
spacy_nlp_pipeline_model.publishing_year = form.publishing_year.data
|
|
|
|
if spacy_nlp_pipeline_model.publisher_url != form.publisher_url.data:
|
|
|
|
spacy_nlp_pipeline_model.publisher_url = form.publisher_url.data
|
|
|
|
if spacy_nlp_pipeline_model.publishing_url != form.publishing_url.data:
|
|
|
|
spacy_nlp_pipeline_model.publishing_url = form.publishing_url.data
|
|
|
|
if spacy_nlp_pipeline_model.version != form.version.data:
|
|
|
|
spacy_nlp_pipeline_model.version = form.version.data
|
|
|
|
if spacy_nlp_pipeline_model.shared != form.shared.data:
|
|
|
|
spacy_nlp_pipeline_model.shared = form.shared.data
|
|
|
|
db.session.commit()
|
|
|
|
message = Markup(f'Model "<a href="contribute/{spacy_nlp_pipeline_model.hashid}">{spacy_nlp_pipeline_model.title}</a>" updated')
|
|
|
|
flash(message, category='corpus')
|
|
|
|
return {}, 201, {'Location': url_for('contributions.contributions')}
|
|
|
|
form.prefill(spacy_nlp_pipeline_model)
|
|
|
|
return render_template(
|
|
|
|
'contributions/spacy_nlp_pipeline_model.html.j2',
|
|
|
|
spacy_nlp_pipeline_model=spacy_nlp_pipeline_model,
|
|
|
|
form=form,
|
|
|
|
title='Edit your spaCy NLP model'
|
|
|
|
)
|
|
|
|
|
|
|
|
@bp.route('/edit-spacy-model/<hashid:spacy_nlp_pipeline_model_id>', methods=['DELETE'])
|
|
|
|
@login_required
|
|
|
|
def delete_spacy_model(spacy_nlp_pipeline_model_id):
|
|
|
|
def _delete_spacy_model(app, spacy_nlp_pipeline_model_id):
|
|
|
|
with app.app_context():
|
|
|
|
model = SpaCyNLPPipelineModel.query.get(spacy_nlp_pipeline_model_id)
|
|
|
|
model.delete()
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
model = SpaCyNLPPipelineModel.query.get_or_404(spacy_nlp_pipeline_model_id)
|
|
|
|
if not (model.user == current_user or current_user.is_administrator()):
|
|
|
|
abort(403)
|
|
|
|
thread = Thread(
|
|
|
|
target=_delete_spacy_model,
|
|
|
|
args=(current_app._get_current_object(), spacy_nlp_pipeline_model_id)
|
|
|
|
)
|
|
|
|
thread.start()
|
|
|
|
return {}, 202
|
|
|
|
|
|
|
|
@bp.route('/add-spacy-nlp-pipeline-model', methods=['GET', 'POST'])
|
|
|
|
def add_spacy_nlp_pipeline_model():
|
|
|
|
form = SpacyNLPModelContributionForm(prefix='contribute-spacy-nlp-pipeline-model-form')
|
|
|
|
if form.is_submitted():
|
|
|
|
if not form.validate():
|
|
|
|
response = {'errors': form.errors}
|
|
|
|
return response, 400
|
|
|
|
try:
|
|
|
|
spacy_nlp_model = SpaCyNLPPipelineModel.create(
|
|
|
|
form.spacy_model_file.data,
|
|
|
|
compatible_service_versions=form.compatible_service_versions.data,
|
|
|
|
description=form.description.data,
|
|
|
|
publisher=form.publisher.data,
|
|
|
|
publisher_url=form.publisher_url.data,
|
|
|
|
publishing_url=form.publishing_url.data,
|
|
|
|
publishing_year=form.publishing_year.data,
|
|
|
|
shared=form.shared.data,
|
|
|
|
title=form.title.data,
|
|
|
|
version=form.version.data,
|
|
|
|
user=current_user
|
|
|
|
)
|
|
|
|
except OSError:
|
|
|
|
abort(500)
|
|
|
|
db.session.commit()
|
|
|
|
message = Markup(f'Model "{spacy_nlp_model.title}" created')
|
|
|
|
flash(message)
|
|
|
|
return {}, 201, {'Location': url_for('contributions.contributions')}
|
|
|
|
spacy_nlp_pipeline_models = [
|
|
|
|
x for x in SpaCyNLPPipelineModel.query.all()
|
|
|
|
]
|
|
|
|
return render_template(
|
|
|
|
'contributions/contribute_spacy_nlp_models.html.j2',
|
|
|
|
form=form,
|
|
|
|
spacy_nlp_pipeline_models=spacy_nlp_pipeline_models,
|
|
|
|
title='spaCy NLP Model Contribution'
|
|
|
|
)
|