Spaces:
Runtime error
Runtime error
File size: 8,098 Bytes
5463318 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | document.addEventListener('DOMContentLoaded', () => {
const btnProcessUpload = document.getElementById('btnProcessUpload');
const fileUpload = document.getElementById('fileUpload');
const extractionPreview = document.getElementById('extractionPreview');
const previewText = document.getElementById('previewText');
const configSection = document.getElementById('configSection');
const btnGenerate = document.getElementById('btnGenerate');
const genSpinner = document.getElementById('genSpinner');
const resultArea = document.getElementById('resultArea');
const paperPreview = document.getElementById('paperPreview');
const btnAddSection = document.getElementById('btnAddSection');
const patternLines = document.getElementById('patternLines');
const btnDownloadPdf = document.getElementById('btnDownloadPdf');
const step2Indicator = document.getElementById('step2Indicator');
const step3Indicator = document.getElementById('step3Indicator');
const fileNameDisplay = document.getElementById('fileNameDisplay');
const genIcon = document.getElementById('genIcon');
let extractedFullContent = "";
let lastGeneratedData = null;
// File name display update
fileUpload.addEventListener('change', (e) => {
if (e.target.files.length > 0) {
fileNameDisplay.textContent = e.target.files[0].name;
fileNameDisplay.classList.add('text-primary', 'fw-bold');
}
});
// 1. Handle File Upload and Extraction
btnProcessUpload.addEventListener('click', async () => {
if (!fileUpload.files[0]) {
alert('Please select a file first.');
return;
}
const formData = new FormData();
formData.append('file', fileUpload.files[0]);
btnProcessUpload.disabled = true;
btnProcessUpload.innerHTML = '<i class="fas fa-circle-notch fa-spin me-2"></i>Extracting...';
try {
const response = await fetch('/upload', {
method: 'POST',
body: formData
});
const data = await response.json();
if (data.error) {
alert(data.error);
} else {
extractedFullContent = data.content_preview;
previewText.textContent = data.content_preview + "...";
document.getElementById('extractedFileName').textContent = fileUpload.files[0].name;
extractionPreview.classList.remove('d-none');
configSection.classList.remove('d-none');
step2Indicator.classList.add('step-active');
window.currentFileId = data.file_id;
// Smooth scroll to config
setTimeout(() => configSection.scrollIntoView({ behavior: 'smooth', block: 'start' }), 300);
}
} catch (err) {
console.error(err);
alert('Upload failed.');
} finally {
btnProcessUpload.disabled = false;
btnProcessUpload.innerHTML = '<span>Content Extracted</span> <i class="fas fa-check ms-2"></i>';
btnProcessUpload.classList.replace('btn-primary-custom', 'btn-success');
}
});
// 2. Add More Sections
btnAddSection.addEventListener('click', () => {
const newLine = document.createElement('div');
newLine.className = 'row g-2 mb-2 pattern-line';
newLine.innerHTML = `
<div class="col-md-5">
<input type="text" class="form-control" placeholder="Section Name">
</div>
<div class="col-md-2">
<input type="number" class="form-control" placeholder="Count">
</div>
<div class="col-md-2">
<input type="number" class="form-control" placeholder="Marks">
</div>
<div class="col-md-3">
<select class="form-select">
<option value="MCQ">MCQ</option>
<option value="Short Answer">Short Answer</option>
<option value="Long Answer">Long Answer</option>
</select>
</div>
`;
patternLines.appendChild(newLine);
});
// 3. Generate Questions
btnGenerate.addEventListener('click', async () => {
const blooms = Array.from(document.querySelectorAll('.form-check-input:checked')).map(c => c.value);
const difficulty = document.getElementById('difficultyLevel').value;
const sections = Array.from(document.querySelectorAll('.pattern-line')).map(line => {
const inputs = line.querySelectorAll('input, select');
return {
name: inputs[0].value,
count: parseInt(inputs[1].value),
marks: parseInt(inputs[2].value),
type: inputs[3].value
};
});
const payload = {
content: extractedFullContent,
file_id: window.currentFileId,
blooms_levels: blooms,
difficulty: difficulty,
pattern: sections
};
btnGenerate.disabled = true;
genSpinner.classList.remove('d-none');
genIcon.classList.add('d-none');
try {
const response = await fetch('/generate', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload)
});
const data = await response.json();
if (data.error) {
alert(data.error);
} else {
lastGeneratedData = data;
renderPreview(data);
resultArea.classList.remove('d-none');
step3Indicator.classList.add('step-active');
resultArea.scrollIntoView({ behavior: 'smooth' });
}
} catch (err) {
console.error(err);
alert('Generation failed.');
} finally {
btnGenerate.disabled = false;
genSpinner.classList.add('d-none');
genIcon.classList.remove('d-none');
}
});
// 4. Render Preview
function renderPreview(data) {
let html = `<h2 class="text-center mb-4">${data.title || 'Examination Paper'}</h2>`;
data.sections.forEach(section => {
html += `<h4 class="mt-4" style="border-bottom: 2px solid #333;">${section.section_name}</h4>`;
if (section.instructions) html += `<p class="fst-italic">${section.instructions}</p>`;
section.questions.forEach((q, idx) => {
html += `
<div class="mb-3">
<strong>Q${idx + 1}. ${q.question_text}</strong>
<span class="float-end">[${q.marks} Marks]</span>
<div class="text-muted small">Level: ${q.bloom_level}</div>
`;
if (q.type === 'MCQ' && q.options) {
html += '<div class="ms-4 mt-2">';
q.options.forEach((opt, i) => {
html += `<div>${String.fromCharCode(65 + i)}. ${opt}</div>`;
});
html += '</div>';
}
html += '</div>';
});
});
paperPreview.innerHTML = html;
}
// 5. Download PDF
btnDownloadPdf.addEventListener('click', async () => {
if (!lastGeneratedData) return;
// This is a simplified approach. In a real app, you would hit a /download endpoint.
const response = await fetch('/download', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(lastGeneratedData)
});
const blob = await response.blob();
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `ExamPaper_${Date.now()}.pdf`;
document.body.appendChild(a);
a.click();
a.remove();
});
});
|