2023-03-13 08:49:59 +00:00
|
|
|
from flask import abort, flash, redirect, render_template, url_for
|
|
|
|
from flask_login import current_user, login_required
|
2023-03-08 14:22:40 +00:00
|
|
|
from app import db
|
|
|
|
from app.models import SpaCyNLPPipelineModel
|
2023-03-13 08:49:59 +00:00
|
|
|
from . import bp, template_base_dir
|
2023-03-08 14:22:40 +00:00
|
|
|
from .forms import (
|
|
|
|
CreateSpaCyNLPPipelineModelForm,
|
|
|
|
EditSpaCyNLPPipelineModelForm
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@bp.route('')
|
|
|
|
@login_required
|
|
|
|
def spacy_nlp_pipeline_models():
|
|
|
|
return render_template(
|
2023-03-13 08:49:59 +00:00
|
|
|
f'{template_base_dir}/spacy_nlp_pipeline_models.html.j2',
|
2023-03-08 14:22:40 +00:00
|
|
|
title='SpaCy NLP Pipeline Models'
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-03-13 10:24:01 +00:00
|
|
|
@bp.route('/create', methods=['GET', 'POST'])
|
2023-03-08 14:22:40 +00:00
|
|
|
@login_required
|
|
|
|
def create_spacy_nlp_pipeline_model():
|
2023-03-13 08:49:59 +00:00
|
|
|
form_prefix = 'create-spacy-nlp-pipeline-model-form'
|
|
|
|
form = CreateSpaCyNLPPipelineModelForm(prefix=form_prefix)
|
2023-03-08 14:22:40 +00:00
|
|
|
if form.is_submitted():
|
|
|
|
if not form.validate():
|
2023-03-13 08:49:59 +00:00
|
|
|
return {'errors': form.errors}, 400
|
2023-03-08 14:22:40 +00:00
|
|
|
try:
|
2023-03-13 08:49:59 +00:00
|
|
|
snpm = SpaCyNLPPipelineModel.create(
|
2023-03-08 14:22:40 +00:00
|
|
|
form.spacy_model_file.data,
|
|
|
|
compatible_service_versions=form.compatible_service_versions.data,
|
|
|
|
description=form.description.data,
|
|
|
|
pipeline_name=form.pipeline_name.data,
|
|
|
|
publisher=form.publisher.data,
|
|
|
|
publisher_url=form.publisher_url.data,
|
|
|
|
publishing_url=form.publishing_url.data,
|
|
|
|
publishing_year=form.publishing_year.data,
|
|
|
|
is_public=False,
|
|
|
|
title=form.title.data,
|
|
|
|
version=form.version.data,
|
|
|
|
user=current_user
|
|
|
|
)
|
|
|
|
except OSError:
|
|
|
|
abort(500)
|
|
|
|
db.session.commit()
|
2023-03-13 08:49:59 +00:00
|
|
|
flash(f'SpaCy NLP Pipeline model "{snpm.title}" created')
|
|
|
|
return {}, 201, {'Location': url_for('.spacy_nlp_pipeline_models')}
|
2023-03-08 14:22:40 +00:00
|
|
|
return render_template(
|
2023-03-13 08:49:59 +00:00
|
|
|
f'{template_base_dir}/create_spacy_nlp_pipeline_model.html.j2',
|
2023-03-08 14:22:40 +00:00
|
|
|
form=form,
|
|
|
|
title='Create SpaCy NLP Pipeline Model'
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@bp.route('/<hashid:spacy_nlp_pipeline_model_id>', methods=['GET', 'POST'])
|
|
|
|
@login_required
|
|
|
|
def spacy_nlp_pipeline_model(spacy_nlp_pipeline_model_id):
|
2023-03-13 08:49:59 +00:00
|
|
|
snpm = SpaCyNLPPipelineModel.query.get_or_404(spacy_nlp_pipeline_model_id)
|
|
|
|
form_prefix = 'edit-spacy-nlp-pipeline-model-form'
|
2023-03-08 14:22:40 +00:00
|
|
|
form = EditSpaCyNLPPipelineModelForm(
|
2023-03-13 08:49:59 +00:00
|
|
|
data=snpm.to_json_serializeable(),
|
|
|
|
prefix=form_prefix
|
2023-03-08 14:22:40 +00:00
|
|
|
)
|
|
|
|
if form.validate_on_submit():
|
2023-03-13 08:49:59 +00:00
|
|
|
form.populate_obj(snpm)
|
|
|
|
if db.session.is_modified(snpm):
|
|
|
|
flash(f'SpaCy NLP Pipeline model "{snpm.title}" updated')
|
2023-03-08 14:22:40 +00:00
|
|
|
db.session.commit()
|
|
|
|
return redirect(url_for('.spacy_nlp_pipeline_models'))
|
|
|
|
return render_template(
|
2023-03-13 08:49:59 +00:00
|
|
|
f'{template_base_dir}/spacy_nlp_pipeline_model.html.j2',
|
2023-03-08 14:22:40 +00:00
|
|
|
form=form,
|
2023-03-13 08:49:59 +00:00
|
|
|
spacy_nlp_pipeline_model=snpm,
|
|
|
|
title=f'{snpm.title} {snpm.version}'
|
2023-03-08 14:22:40 +00:00
|
|
|
)
|