def task(process_id: str, text: str) -> Tuple[Any, ...]: """ This is a task that runs in a child process within the pool """ # We do a bit of functools.partial magic to pass the process_id as the first # parameter to the progress_func whenever it is called task_result = check_grammar( text, progress_func=partial(ChildTask.progress_func, process_id), split_paragraphs=True, ) # The result is automatically communicated back to the parent process via IPC return task_result
def correct_api(version=1): """ Correct text manually entered by the user, i.e. not coming from an article. This is a lower level API used by the Greynir web front-end. """ if current_app.config["PRODUCTION"]: return abort(403) # Forbidden if not (1 <= version <= 1): return better_jsonify(valid=False, reason="Unsupported version") try: text = text_from_request(request) except Exception as e: logging.warning("Exception in correct_api(): {0}".format(e)) return better_jsonify(valid=False, reason="Invalid request") pgs, stats = check_grammar(text) # Return the annotated paragraphs/sentences and stats # in a JSON structure to the client return better_jsonify(valid=True, result=pgs, stats=stats)
def correct_task(version=1): """ Correct text provided by the user, i.e. not coming from an article. This can be either an uploaded file or a string. This is a lower level API used by the Greynir web front-end. """ if not (1 <= version <= 1): return better_jsonify(valid=False, reason="Unsupported version") file = request.files.get("file") if file is not None: # Handle uploaded file # file is a proxy object that emulates a Werkzeug FileStorage object mimetype = file.mimetype if mimetype not in SUPPORTED_DOC_MIMETYPES: return better_jsonify(valid=False, reason="File type not supported") # Create document object from an uploaded file and extract its text try: # Instantiate an appropriate class for the MIME type of the file doc_class = MIMETYPE_TO_DOC_CLASS[mimetype] doc = doc_class(file.read()) text = doc.extract_text() except Exception as e: logging.warning("Exception in correct_task(): {0}".format(e)) return better_jsonify(valid=False, reason="Error reading file") else: # Handle POSTed form data or plain text string try: text = text_from_request(request) except Exception as e: logging.warning("Exception in correct_task(): {0}".format(e)) return better_jsonify(valid=False, reason="Invalid request") pgs, stats = check_grammar(text, progress_func=request.progress_func) # Return the annotated paragraphs/sentences and stats # in a JSON structure to the client return better_jsonify(valid=True, result=pgs, stats=stats, text=text)
def correct_api(version=1): """ Correct text provided by the user, i.e. not coming from an article. This can be either an uploaded file or a string. This is a lower level API used by the Greynir web front-end. """ if not (1 <= version <= 1): return better_jsonify(valid=False, reason="Unsupported version") file = request.files.get("file") if file is not None: # file is a Werkzeug FileStorage object mimetype = file.content_type if mimetype not in SUPPORTED_DOC_MIMETYPES: return better_jsonify(valid=False, reason="File type not supported") # Create document object from file and extract text try: # filename = werkzeug.secure_filename(file.filename) # Instantiate an appropriate class for the MIME type of the file doc = Document.for_mimetype(mimetype)(file.read()) text = doc.extract_text() except Exception as e: logging.warning("Exception in correct_api(): {0}".format(e)) return better_jsonify(valid=False, reason="Error reading file") else: try: text = text_from_request(request) except Exception as e: logging.warning("Exception in correct_api(): {0}".format(e)) return better_jsonify(valid=False, reason="Invalid request") pgs, stats = check_grammar(text) # Return the annotated paragraphs/sentences and stats # in a JSON structure to the client return better_jsonify(valid=True, result=pgs, stats=stats, text=text)