def api_completions(task_id): """ Delete or save new completion to output_dir with the same name as task_id """ project = project_get_or_create() if request.method == 'POST': completion = request.json completion.pop('state', None) # remove editor state completion_id = project.save_completion(task_id, completion) log.info(msg='Completion saved', extra={ 'task_id': task_id, 'output': request.json }) # try to train model with new completions if project.ml_backend: project.ml_backend.update_model(project.get_task(task_id), completion, project.project_obj) project.analytics.send(getframeinfo(currentframe()).function) return make_response(json.dumps({'id': completion_id}), 201) else: project.analytics.send(getframeinfo(currentframe()).function, error=500) return make_response('Incorrect request method', 500)
def api_completions(task_id): """ Delete or save new completion to output_dir with the same name as task_id """ project = project_get_or_create() if request.method == 'POST': completion = request.json completion.pop('state', None) # remove editor state cur_user= User.query.filter_by(email=current_user.email).first() completion_id = project.save_completion(task_id, completion, cur_user.name)[0] completion_user = project.save_completion(task_id, completion, cur_user.name)[1] log.info(msg='Completion saved', extra={'task_id': task_id, 'output': request.json}) # Increase tasks that the user has completed get_db().execute('update num_completed SET num = num + 1 where user = :u', {'u':cur_user.name}) worker_num_tasks = list(get_db().execute('select num as c from num_completed')) get_db().commit() # try to train model with new completions if project.ml_backend: project.ml_backend.update_model(project.get_task(task_id), completion, project.project_obj) project.analytics.send(getframeinfo(currentframe()).function) return make_response(json.dumps({'id': completion_id, 'user': completion_user}), 201) else: project.analytics.send(getframeinfo(currentframe()).function, error=500) return make_response('Incorrect request method', 500)
def api_completion_update(task_id, completion_id): """ Rewrite existing completion with patch. This is technical api call for editor testing only. It's used for Rewrite button in editor. """ project = project_get_or_create() completion = request.json completion.pop('state', None) # remove editor state completion['id'] = int(completion_id) project.save_completion(task_id, completion) log.info(msg='Completion saved', extra={'task_id': task_id, 'output': request.json}) project.analytics.send(getframeinfo(currentframe()).function) return make_response('ok', 201)
def api_generate_next_task(): """ Generate next task to label """ # try to find task is not presented in completions completions = db.get_completions_ids() for task_id, task in db.iter_tasks(): if task_id not in completions: log.info(msg='New task for labeling', extra=task) analytics.send(getframeinfo(currentframe()).function) # try to use ml backend for predictions if ml_backend: task = deepcopy(task) task['predictions'] = ml_backend.make_predictions( task, project) return make_response(jsonify(task), 200) # no tasks found analytics.send(getframeinfo(currentframe()).function, error=404) return make_response('', 404)
def api_generate_next_task(): print("in api generate next task") """ Generate next task to label """ # try to find task is not presented in completions project = project_get_or_create() completions = project.get_completions_ids() #filter tasks num_workers = db.session.execute( 'select count(id) as c from user where role="worker" ').scalar() num_tasks = len(project.tasks) num_each = num_tasks // num_workers remain = num_tasks % num_workers lower_bound = 0 upper_bound = num_tasks task_queue = make_task_queue(num_tasks) if (current_user.role == "worker"): cur_id = current_user.id if (cur_id > len(task_queue)): return flask.render_template('closed.html') if (len(task_queue) != 0): lower_bound = task_queue[cur_id % len(task_queue)][0] upper_bound = task_queue[cur_id % len(task_queue)][ len(task_queue[cur_id % len(task_queue)]) - 1] for task_id, task in project.iter_tasks(): if task_id not in completions and task_id >= lower_bound and task_id <= upper_bound: log.info(msg='New task for labeling', extra=task) project.analytics.send(getframeinfo(currentframe()).function) # try to use ml backend for predictions if project.ml_backend: task = deepcopy(task) task['predictions'] = project.ml_backend.make_predictions( task, project.project_obj) return make_response(jsonify(task), 200) # no tasks found project.analytics.send(getframeinfo(currentframe()).function, error=404) return make_response('', 404)