mirror of
https://github.com/karma-riuk/crab-webapp.git
synced 2025-07-05 06:08:13 +02:00
unified the way comment evaluation and refinement
are handled
This commit is contained in:
@ -66,11 +66,6 @@ function populateRefinementTable(results) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleRefinementAnswer(json) {
|
|
||||||
uploadStatusEl.style.color = "green";
|
|
||||||
uploadStatusEl.textContent = json["id"];
|
|
||||||
}
|
|
||||||
|
|
||||||
// Upload logic
|
// Upload logic
|
||||||
document.getElementById("upload-btn").onclick = async () => {
|
document.getElementById("upload-btn").onclick = async () => {
|
||||||
uploadStatusEl.classList.add("hidden");
|
uploadStatusEl.classList.add("hidden");
|
||||||
@ -104,8 +99,9 @@ document.getElementById("upload-btn").onclick = async () => {
|
|||||||
|
|
||||||
commentResultsContainer.classList.add("hidden");
|
commentResultsContainer.classList.add("hidden");
|
||||||
refinementResultsContainer.classList.add("hidden");
|
refinementResultsContainer.classList.add("hidden");
|
||||||
if (type === "comment") populateCommentTable(json);
|
|
||||||
else handleRefinementAnswer(json);
|
uploadStatusEl.style.color = "green";
|
||||||
|
uploadStatusEl.textContent = json["id"];
|
||||||
};
|
};
|
||||||
|
|
||||||
[...document.getElementsByClassName("download-results")].forEach((e) => {
|
[...document.getElementsByClassName("download-results")].forEach((e) => {
|
||||||
@ -136,10 +132,19 @@ socket.on("started-processing", () => {
|
|||||||
setProgress(0);
|
setProgress(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.on("complete", (results) => {
|
socket.on("complete", (data) => {
|
||||||
|
commentResultsContainer.classList.add("hidden");
|
||||||
|
refinementResultsContainer.classList.add("hidden");
|
||||||
progressContainer.classList.add("hidden");
|
progressContainer.classList.add("hidden");
|
||||||
refinementResultsContainer.classList.remove("hidden");
|
if (data.type == "comment") {
|
||||||
populateRefinementTable(results);
|
commentResultsContainer.classList.remove("hidden");
|
||||||
|
populateCommentTable(data.results);
|
||||||
|
} else if (data.type == "refinement") {
|
||||||
|
refinementResultsContainer.classList.remove("hidden");
|
||||||
|
populateRefinementTable(data.results);
|
||||||
|
} else {
|
||||||
|
console.error(`Unknown type ${data.type}`);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.on("successful-upload", () => {
|
socket.on("successful-upload", () => {
|
||||||
@ -167,5 +172,11 @@ document.getElementById("request-status").onclick = async () => {
|
|||||||
statusStatusEl.style.color = "green";
|
statusStatusEl.style.color = "green";
|
||||||
statusStatusEl.textContent = json["status"];
|
statusStatusEl.textContent = json["status"];
|
||||||
|
|
||||||
if (json.status == "complete") populateRefinementTable(json.results);
|
if (json.status == "complete") {
|
||||||
|
commentResultsContainer.classList.add("hidden");
|
||||||
|
refinementResultsContainer.classList.add("hidden");
|
||||||
|
if (json.type == "comment") populateCommentTable(json.results);
|
||||||
|
else if (json.type == "comment") populateRefinementTable(json.results);
|
||||||
|
else console.error(`Unknown type ${data.type}`);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
# routes/answers.py
|
# routes/answers.py
|
||||||
from threading import Thread
|
from typing import Callable
|
||||||
from flask import Blueprint, request, jsonify, current_app, url_for
|
from flask import Blueprint, request, jsonify, current_app, url_for
|
||||||
from utils.errors import InvalidJsonFormatError
|
from utils.errors import InvalidJsonFormatError
|
||||||
from utils.process_data import evaluate_comments, evaluate_refinement
|
from utils.process_data import evaluate_comments, evaluate_refinement
|
||||||
@ -47,40 +47,13 @@ def validate_json_format_for_code_refinement(data: str) -> dict[str, dict[str, s
|
|||||||
raise InvalidJsonFormatError()
|
raise InvalidJsonFormatError()
|
||||||
|
|
||||||
|
|
||||||
@router.route('/submit/comment', methods=['POST'])
|
def handler(type_: str, validate_json: Callable, evaluate_submission: Callable):
|
||||||
def submit_comments():
|
|
||||||
file = request.files.get('file')
|
file = request.files.get('file')
|
||||||
if file is None or file.filename is None or file.filename.split('.')[-1] not in ALLOWED_EXT:
|
if file is None or file.filename is None or file.filename.split('.')[-1] not in ALLOWED_EXT:
|
||||||
return jsonify({'error': 'Only JSON files are allowed'}), 400
|
return jsonify({'error': 'Only JSON files are allowed'}), 400
|
||||||
data = file.read().decode()
|
data = file.read().decode()
|
||||||
try:
|
try:
|
||||||
validated = validate_json_format_for_comment_gen(data)
|
validated = validate_json(data)
|
||||||
except InvalidJsonFormatError as e:
|
|
||||||
return jsonify({'error': 'Invalid JSON format', 'message': str(e)}), 400
|
|
||||||
|
|
||||||
socketio = current_app.extensions['socketio']
|
|
||||||
sid = request.headers.get('X-Socket-Id')
|
|
||||||
if sid:
|
|
||||||
socketio.emit('successful-upload', room=sid)
|
|
||||||
socketio.emit('started-processing', room=sid)
|
|
||||||
|
|
||||||
results = evaluate_comments(
|
|
||||||
validated, lambda p: socketio.emit('progress', {'percent': p}, room=sid)
|
|
||||||
)
|
|
||||||
return jsonify(results)
|
|
||||||
|
|
||||||
|
|
||||||
socket2observer = {}
|
|
||||||
|
|
||||||
|
|
||||||
@router.route('/submit/refinement', methods=['POST'])
|
|
||||||
def submit_refinement():
|
|
||||||
file = request.files.get('file')
|
|
||||||
if file is None or file.filename is None or file.filename.split('.')[-1] not in ALLOWED_EXT:
|
|
||||||
return jsonify({'error': 'Only JSON files are allowed'}), 400
|
|
||||||
data = file.read().decode()
|
|
||||||
try:
|
|
||||||
validated = validate_json_format_for_code_refinement(data)
|
|
||||||
except InvalidJsonFormatError as e:
|
except InvalidJsonFormatError as e:
|
||||||
return jsonify({'error': 'Invalid JSON format', 'message': str(e)}), 400
|
return jsonify({'error': 'Invalid JSON format', 'message': str(e)}), 400
|
||||||
|
|
||||||
@ -89,7 +62,7 @@ def submit_refinement():
|
|||||||
socket_emit = functools.partial(socketio.emit, room=sid)
|
socket_emit = functools.partial(socketio.emit, room=sid)
|
||||||
|
|
||||||
process_id = str(uuid.uuid4())
|
process_id = str(uuid.uuid4())
|
||||||
subject = Subject(process_id, evaluate_refinement)
|
subject = Subject(process_id, type_, evaluate_submission)
|
||||||
request2status[process_id] = subject
|
request2status[process_id] = subject
|
||||||
|
|
||||||
if sid:
|
if sid:
|
||||||
@ -110,6 +83,18 @@ def submit_refinement():
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@router.route('/submit/<any(comment, refinement):task>', methods=['POST'])
|
||||||
|
def submit_comments(task):
|
||||||
|
if task == "comment":
|
||||||
|
validator = validate_json_format_for_comment_gen
|
||||||
|
evaluator = evaluate_comments
|
||||||
|
else:
|
||||||
|
validator = validate_json_format_for_code_refinement
|
||||||
|
evaluator = evaluate_refinement
|
||||||
|
|
||||||
|
return handler(task, validator, evaluator)
|
||||||
|
|
||||||
|
|
||||||
@router.route('/status/<id>')
|
@router.route('/status/<id>')
|
||||||
def status(id):
|
def status(id):
|
||||||
if id not in request2status:
|
if id not in request2status:
|
||||||
|
@ -56,7 +56,7 @@ class Subject:
|
|||||||
def notifyComplete(self, results: dict):
|
def notifyComplete(self, results: dict):
|
||||||
self.status = Status.COMPLETE
|
self.status = Status.COMPLETE
|
||||||
for observer in self.observers:
|
for observer in self.observers:
|
||||||
observer.updateComplete(results)
|
observer.updateComplete({"type": self.type, "results": results})
|
||||||
self.results = results
|
self.results = results
|
||||||
# TODO: maybe save results to disk here?
|
# TODO: maybe save results to disk here?
|
||||||
|
|
||||||
|
@ -12,7 +12,11 @@ REFERENCE_MAP = Dataset.from_json(
|
|||||||
ARCHIVES_ROOT = str(get_project_path('../data/archives'))
|
ARCHIVES_ROOT = str(get_project_path('../data/archives'))
|
||||||
|
|
||||||
|
|
||||||
def evaluate_comments(answers: dict[str, str], percent_cb):
|
def evaluate_comments(
|
||||||
|
answers: dict[str, str],
|
||||||
|
percent_cb: Callable[[float], None] = lambda _: None,
|
||||||
|
complete_cb: Callable[[dict], None] = lambda _: None,
|
||||||
|
):
|
||||||
total = len(answers)
|
total = len(answers)
|
||||||
results = {}
|
results = {}
|
||||||
for i, (id_, gen) in enumerate(answers.items(), 1):
|
for i, (id_, gen) in enumerate(answers.items(), 1):
|
||||||
@ -33,6 +37,8 @@ def evaluate_comments(answers: dict[str, str], percent_cb):
|
|||||||
'proposed_comment': gen,
|
'proposed_comment': gen,
|
||||||
}
|
}
|
||||||
percent_cb(int(i / total * 100))
|
percent_cb(int(i / total * 100))
|
||||||
|
|
||||||
|
complete_cb(results)
|
||||||
return results
|
return results
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user