mirror of
				https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
				synced 2025-10-31 02:32:45 +00:00 
			
		
		
		
	Add input options for ocr job creation.
This commit is contained in:
		| @@ -1,6 +1,35 @@ | ||||
| from flask_wtf import FlaskForm | ||||
| from wtforms import SubmitField | ||||
| from flask_wtf.file import FileAllowed, FileRequired | ||||
| from wtforms import FileField, SelectField, StringField, SubmitField | ||||
| from wtforms.validators import DataRequired, Length | ||||
|  | ||||
|  | ||||
| class OCRJobForm(FlaskForm): | ||||
|     description = StringField( | ||||
|         'Description', | ||||
|         validators=[DataRequired(), Length(1, 64)] | ||||
|     ) | ||||
|     file = FileField( | ||||
|         'File', | ||||
|         validators=[ | ||||
|             FileAllowed(['pdf', 'tif', 'tiff']), | ||||
|             FileRequired() | ||||
|         ] | ||||
|     ) | ||||
|     language = SelectField( | ||||
|         'Language', | ||||
|         choices=[ | ||||
|             ('', 'Choose your option'), | ||||
|             ('eng', 'English'), | ||||
|             ('enm', 'English, Middle (1100-1500)'), | ||||
|             ('fra', 'French'), | ||||
|             ('frm', 'French, Middle (ca. 1400-1600)'), | ||||
|             ('deu', 'German'), | ||||
|             ('frk', 'German Fraktur'), | ||||
|             ('ita', 'Italian'), | ||||
|             ('por', 'Portuguese'), | ||||
|             ('spa', 'Spanish; Castilian') | ||||
|         ], | ||||
|         validators=[DataRequired()] | ||||
|     ) | ||||
|     submit = SubmitField('Submit') | ||||
|   | ||||
| @@ -1,9 +1,12 @@ | ||||
| from flask import redirect, render_template, url_for | ||||
| from datetime import datetime | ||||
| from flask import current_app, flash, redirect, render_template, url_for, request | ||||
| from . import services | ||||
| from flask_login import current_user, login_required | ||||
| from .forms import OCRJobForm | ||||
| from ..import swarm | ||||
| from threading import Thread | ||||
| import hashlib | ||||
| import os | ||||
|  | ||||
|  | ||||
| @services.route('/ocr', methods=['GET', 'POST']) | ||||
| @@ -11,29 +14,44 @@ from threading import Thread | ||||
| def ocr(): | ||||
|     ocr_job_form = OCRJobForm() | ||||
|     if ocr_job_form.validate_on_submit(): | ||||
|         ''' | ||||
|         ' TODO: Implement a Job class. For now a dictionary representation is | ||||
|         '       enough. | ||||
|         ''' | ||||
|         job = {'worker': None, | ||||
|                'creator': current_user.id, | ||||
|                'id': '5fd40cb0cadef3ab5676c4968fc3d748', | ||||
|                'requested_cpus': 2, | ||||
|                'requested_memory': 2048, | ||||
|                'service': 'ocr', | ||||
|                'service_args': {'lang': 'eng', | ||||
|                                 'version': 'latest' | ||||
|                                 }, | ||||
|                'status': 'queued' | ||||
|                } | ||||
|         ''' | ||||
|         ' TODO: Let the scheduler run this job in the background. | ||||
|         ' | ||||
|         ' NOTE: Using self created threads is just for testing purpose as there | ||||
|         '       is no scheduler available. | ||||
|         ''' | ||||
|         thread = Thread(target=swarm.run, args=(job,)) | ||||
|         thread.start() | ||||
|         app = current_app._get_current_object() | ||||
|         id = hashlib.md5( | ||||
|             (current_user.username + '_' + datetime.now().isoformat()).encode() | ||||
|         ).hexdigest() | ||||
|         dir = os.path.join(app.config['OPAQUE_UPLOAD_DIRECTORY'], id) | ||||
|  | ||||
|         try: | ||||
|             os.mkdir(dir) | ||||
|         except FileExistsError: | ||||
|             # Possible MD5 hash collision occurred. | ||||
|             flash('Internal error occurred, please try again!') | ||||
|         else: | ||||
|             file = ocr_job_form.file.data | ||||
|             file.save(os.path.join(dir, file.filename)) | ||||
|  | ||||
|             ''' | ||||
|             ' TODO: Implement a Job class. For now a dictionary representation | ||||
|             '       is enough. | ||||
|             ''' | ||||
|             job = {'worker': None, | ||||
|                    'creator': current_user.id, | ||||
|                    'id': id, | ||||
|                    'requested_cpus': 2, | ||||
|                    'requested_memory': 2048, | ||||
|                    'service': 'ocr', | ||||
|                    'service_args': {'lang': ocr_job_form.language.data, | ||||
|                                     'version': 'latest' | ||||
|                                     }, | ||||
|                    'status': 'queued' | ||||
|                    } | ||||
|             ''' | ||||
|             ' TODO: Let the scheduler run this job in the background. | ||||
|             ' | ||||
|             ' NOTE: Using self created threads is just for testing purpose as | ||||
|             '       there is no scheduler available. | ||||
|             ''' | ||||
|             thread = Thread(target=swarm.run, args=(job,)) | ||||
|             thread.start() | ||||
|         return redirect(url_for('services.ocr')) | ||||
|  | ||||
|     return render_template( | ||||
|   | ||||
		Reference in New Issue
	
	Block a user