|
document.addEventListener('DOMContentLoaded', function() { |
|
|
|
const form = document.getElementById('evaluation-form'); |
|
const evaluateBtn = document.getElementById('evaluate-btn'); |
|
const newEvaluationBtn = document.getElementById('new-evaluation-btn'); |
|
const uploadSection = document.querySelector('.upload-section'); |
|
const resultsSection = document.querySelector('.results-section'); |
|
const loadingOverlay = document.querySelector('.loading-overlay'); |
|
const loadingStep = document.getElementById('loading-step'); |
|
const flowSteps = document.querySelectorAll('.flow-step'); |
|
|
|
|
|
setupFileUpload('question'); |
|
setupFileUpload('student'); |
|
setupFileUpload('reference'); |
|
|
|
|
|
function checkAllUploaded() { |
|
const questionPreview = document.getElementById('question-preview'); |
|
const studentPreview = document.getElementById('student-preview'); |
|
const referencePreview = document.getElementById('reference-preview'); |
|
|
|
if (questionPreview.style.display === 'block' && |
|
studentPreview.style.display === 'block' && |
|
referencePreview.style.display === 'block') { |
|
evaluateBtn.disabled = false; |
|
} else { |
|
evaluateBtn.disabled = true; |
|
} |
|
} |
|
|
|
|
|
function setupFileUpload(type) { |
|
const uploadArea = document.getElementById(`${type}-upload`); |
|
const fileInput = document.getElementById(`${type}-input`); |
|
const preview = document.getElementById(`${type}-preview`); |
|
|
|
|
|
uploadArea.addEventListener('click', () => { |
|
fileInput.click(); |
|
}); |
|
|
|
|
|
uploadArea.addEventListener('dragover', (e) => { |
|
e.preventDefault(); |
|
uploadArea.classList.add('drag-over'); |
|
}); |
|
|
|
uploadArea.addEventListener('dragleave', () => { |
|
uploadArea.classList.remove('drag-over'); |
|
}); |
|
|
|
uploadArea.addEventListener('drop', (e) => { |
|
e.preventDefault(); |
|
uploadArea.classList.remove('drag-over'); |
|
|
|
if (e.dataTransfer.files.length) { |
|
fileInput.files = e.dataTransfer.files; |
|
handleFileSelect(fileInput, preview, uploadArea); |
|
} |
|
}); |
|
|
|
|
|
fileInput.addEventListener('change', () => { |
|
handleFileSelect(fileInput, preview, uploadArea); |
|
}); |
|
} |
|
|
|
|
|
function handleFileSelect(fileInput, preview, uploadArea) { |
|
if (fileInput.files && fileInput.files[0]) { |
|
const file = fileInput.files[0]; |
|
|
|
|
|
const fileType = file.type; |
|
if (fileType !== 'image/jpeg' && fileType !== 'image/png' && fileType !== 'image/jpg') { |
|
alert('Please upload an image file (JPEG, JPG or PNG)'); |
|
return; |
|
} |
|
|
|
|
|
const reader = new FileReader(); |
|
reader.onload = function(e) { |
|
preview.innerHTML = `<img src="${e.target.result}" alt="Preview">`; |
|
preview.style.display = 'block'; |
|
uploadArea.style.display = 'none'; |
|
checkAllUploaded(); |
|
}; |
|
reader.readAsDataURL(file); |
|
} |
|
} |
|
|
|
|
|
form.addEventListener('submit', function(e) { |
|
e.preventDefault(); |
|
|
|
|
|
loadingOverlay.style.display = 'flex'; |
|
updateFlowStep(0); |
|
|
|
const formData = new FormData(form); |
|
|
|
|
|
console.log("Files being sent:"); |
|
console.log("Question file:", formData.get('question')); |
|
console.log("Student answer file:", formData.get('student_answer')); |
|
console.log("Reference answer file:", formData.get('reference_answer')); |
|
|
|
setTimeout(() => { |
|
loadingStep.textContent = 'Preprocessing images...'; |
|
updateFlowStep(1); |
|
|
|
setTimeout(() => { |
|
loadingStep.textContent = 'Evaluating answer...'; |
|
updateFlowStep(2); |
|
|
|
|
|
fetch('/evaluate', { |
|
method: 'POST', |
|
body: formData |
|
}) |
|
.then(response => { |
|
if (!response.ok) { |
|
throw new Error('Network response was not ok'); |
|
} |
|
return response.json(); |
|
}) |
|
.then(data => { |
|
if (data.error) { |
|
throw new Error(data.error); |
|
} |
|
|
|
|
|
|
|
|
|
updateScore(data.score); |
|
|
|
|
|
loadingOverlay.style.display = 'none'; |
|
uploadSection.style.display = 'none'; |
|
resultsSection.style.display = 'block'; |
|
updateFlowStep(3); |
|
}) |
|
.catch(error => { |
|
alert('Error: ' + error.message); |
|
loadingOverlay.style.display = 'none'; |
|
}); |
|
}, 1500); |
|
}, 1500); |
|
}); |
|
|
|
|
|
function updateFlowStep(activeIndex) { |
|
flowSteps.forEach((step, index) => { |
|
if (index < activeIndex) { |
|
step.classList.remove('active'); |
|
step.classList.add('completed'); |
|
} else if (index === activeIndex) { |
|
step.classList.add('active'); |
|
step.classList.remove('completed'); |
|
} else { |
|
step.classList.remove('active', 'completed'); |
|
} |
|
}); |
|
} |
|
|
|
|
|
function updateScore(score) { |
|
const scoreValue = document.getElementById('score-value'); |
|
const scoreProgress = document.querySelector('.score-progress'); |
|
const scoreMessage = document.getElementById('score-message'); |
|
|
|
|
|
const scorePercent = score <= 1 ? Math.round(score * 100) : score; |
|
|
|
|
|
scoreValue.textContent = `${scorePercent}%`; |
|
|
|
|
|
const circumference = 2 * Math.PI * 90; |
|
const offset = circumference - (scorePercent / 100) * circumference; |
|
scoreProgress.style.strokeDashoffset = offset; |
|
|
|
|
|
if (scorePercent >= 80) { |
|
scoreProgress.style.stroke = '#28a745'; |
|
scoreMessage.textContent = 'Excellent! The answer matches the reference very well.'; |
|
} else if (scorePercent >= 60) { |
|
scoreProgress.style.stroke = '#17a2b8'; |
|
scoreMessage.textContent = 'Good job! The answer is quite similar to the reference.'; |
|
} else if (scorePercent >= 40) { |
|
scoreProgress.style.stroke = '#ffc107'; |
|
scoreMessage.textContent = 'Average match. There is room for improvement.'; |
|
} else { |
|
scoreProgress.style.stroke = '#dc3545'; |
|
scoreMessage.textContent = 'The answer differs significantly from the reference.'; |
|
} |
|
} |
|
|
|
|
|
newEvaluationBtn.addEventListener('click', function() { |
|
|
|
form.reset(); |
|
|
|
|
|
document.querySelectorAll('.preview').forEach(preview => { |
|
preview.style.display = 'none'; |
|
preview.innerHTML = ''; |
|
}); |
|
|
|
|
|
document.querySelectorAll('.upload-area').forEach(area => { |
|
area.style.display = 'block'; |
|
}); |
|
|
|
|
|
updateFlowStep(0); |
|
|
|
|
|
uploadSection.style.display = 'block'; |
|
resultsSection.style.display = 'none'; |
|
|
|
evaluateBtn.disabled = true; |
|
}); |
|
}); |