fix model contribution.

This commit is contained in:
dameyer 2022-10-25 13:07:10 +02:00
parent 9802fdd175
commit 40a71ad9dd
3 changed files with 108 additions and 3 deletions

View File

@ -1,4 +1,5 @@
from flask_wtf import FlaskForm from flask_wtf import FlaskForm
from flask_wtf.file import FileField, FileRequired
from wtforms import ( from wtforms import (
BooleanField, BooleanField,
PasswordField, PasswordField,
@ -70,6 +71,12 @@ class ContributionForm(FlaskForm):
validators=[InputRequired()] validators=[InputRequired()]
) )
# file upload
file = FileField(
'File',
validators=[FileRequired()]
)
submit = SubmitField() submit = SubmitField()

View File

@ -1,9 +1,11 @@
from flask import render_template from apifairy import authenticate
from flask_login import login_required from flask import render_template, flash, abort
from flask_login import login_required, current_user
from app.decorators import permission_required from app.decorators import permission_required
from app.models import Permission from app.models import Permission, TesseractOCRModel, JobStatus
from . import bp from . import bp
from .forms import ContributionForm from .forms import ContributionForm
from .. import db
@bp.before_request @bp.before_request
@ -14,8 +16,55 @@ def before_request():
@bp.route('', methods=['GET', 'POST']) @bp.route('', methods=['GET', 'POST'])
@login_required
def contributions(): def contributions():
form = ContributionForm(prefix='contribution-form') form = ContributionForm(prefix='contribution-form')
# Is the form submitted?
if form.is_submitted():
# Are their any false inputs?
if not form.validate():
response = {'errors': form.errors}
return response, 400
# At this point we can be sure that the form data is okay
# example
# compatible_service_versions = ['0.1.0', '0.1.1']
compatible_service_versions=form.compatible_service_versions.data
description=form.description
publisher=form.publisher
publisher_url=form.publisher_url
publishing_url=form.publishing_url
publishing_year=form.publishing_year
shared=False
title=form.title
version=form.version
try:
TesseractOCRModel.create(form.file.data,
compatible_service_versions,
description,
publisher,
publisher_url,
publishing_url,
publishing_year,
shared,
title,
version)
except OSError:
abort(500)
# job.status = JobStatus.SUBMITTED
db.session.commit()
# message = Markup(f'Job "<a href="{job.url}">{job.title}</a>" created')
flash("Model has been added.")
return {}, 201, {'message': 'Model has been created'}
return render_template( return render_template(
'contributions/contribute.html.j2', 'contributions/contribute.html.j2',
form=form, form=form,

View File

@ -602,6 +602,55 @@ class TesseractOCRModel(FileMixin, HashidMixin, db.Model):
_json['user'] = self.user.to_json(backrefs=True) _json['user'] = self.user.to_json(backrefs=True)
return _json return _json
@staticmethod
def create(model_file, **kwargs): # ??? Ist das so richtig? übergeben wir das form? Im anderen Fall ist das nicht so .. aber was soll hier überhaupt rein?
filename = kwargs.get('filename', model_file.filename)
mimetype = kwargs.get('mimetype', model_file.mimetype)
tesseract_ocr_model = TesseractOCRModel(
filename=secure_filename(filename),
mimetype=mimetype,
**kwargs
)
db.session.add(tesseract_ocr_model)
db.session.flush(objects=[tesseract_ocr_model])
db.session.refresh(tesseract_ocr_model)
try:
# ??? Woher soll file kommen? Wir haben der methode das ganze form übergeben.
# filename = form.file.data.filename
model_file.save(tesseract_ocr_model.path)
except OSError as e:
current_app.logger.error(e)
db.session.rollback()
raise e
return tesseract_ocr_model
# Kann man das so committen?
# db.session.add(model)
# db.session.flush(objects=[model])
# db.session.refresh(model)
# model.filename = f'{model.id}.traineddata'
# r = requests.get(m['url'], stream=True)
# pbar = tqdm(
# desc=f'{model.title} ({model.filename})',
# unit="B",
# unit_scale=True,
# unit_divisor=1024,
# total=int(r.headers['Content-Length'])
# )
# pbar.clear()
# with open(model.path, 'wb') as f:
# for chunk in r.iter_content(chunk_size=1024):
# if chunk: # filter out keep-alive new chunks
# pbar.update(len(chunk))
# f.write(chunk)
# pbar.close()
#
# db.session.commit()
class TranskribusHTRModel(HashidMixin, db.Model): class TranskribusHTRModel(HashidMixin, db.Model):
__tablename__ = 'transkribus_htr_models' __tablename__ = 'transkribus_htr_models'