Update app.py
Browse files
app.py
CHANGED
@@ -1,100 +1,181 @@
|
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<!DOCTYPE html>
|
2 |
+
<html lang="en">
|
3 |
+
<head>
|
4 |
+
<meta charset="UTF-8">
|
5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6 |
+
<title>Generated Quiz</title>
|
7 |
+
<style>
|
8 |
+
body {
|
9 |
+
font-family: Arial, sans-serif;
|
10 |
+
background-color: #f0f0f0;
|
11 |
+
color: #333;
|
12 |
+
margin: 20px;
|
13 |
+
}
|
14 |
+
h2 {
|
15 |
+
background-color: #ffc107;
|
16 |
+
padding: 10px;
|
17 |
+
border-radius: 5px;
|
18 |
+
}
|
19 |
+
.quiz-container {
|
20 |
+
background-color: #fff;
|
21 |
+
padding: 20px;
|
22 |
+
border-radius: 5px;
|
23 |
+
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
|
24 |
+
margin-bottom: 20px;
|
25 |
+
}
|
26 |
+
.quiz-container pre {
|
27 |
+
white-space: pre-wrap;
|
28 |
+
}
|
29 |
+
.generate-link {
|
30 |
+
display: inline-block;
|
31 |
+
margin-top: 10px;
|
32 |
+
border: 2px solid transparent;
|
33 |
+
padding: 5px 10px;
|
34 |
+
text-decoration: none;
|
35 |
+
color: #ffc107;
|
36 |
+
font-weight: bold;
|
37 |
+
border-radius: 4px;
|
38 |
+
background-color: #333;
|
39 |
+
}
|
40 |
+
.generate-link:hover {
|
41 |
+
text-decoration: none;
|
42 |
+
background-color: #555;
|
43 |
+
}
|
44 |
+
a {
|
45 |
+
color: #ffc107;
|
46 |
+
text-decoration: none;
|
47 |
+
}
|
48 |
+
a:hover {
|
49 |
+
text-decoration: underline;
|
50 |
+
}
|
51 |
+
</style>
|
52 |
+
</head>
|
53 |
+
<body>
|
54 |
+
<div class="quiz-container">
|
55 |
+
<h2>Generated Quiz</h2>
|
56 |
+
<pre>{{ filtered_text }}</pre>
|
57 |
+
<a href="/" class="generate-link">Back to generate another quiz</a>
|
58 |
+
</div>
|
59 |
+
<div id="quizSection"></div>
|
60 |
+
<button id="submitQuiz" style="display: none;" onclick="submitQuiz()">Submit Quiz</button>
|
61 |
+
|
62 |
+
<script>
|
63 |
+
document.addEventListener('DOMContentLoaded', function() {
|
64 |
+
generateQuiz("{{ filtered_text }}", {{ answers_and_explanations | tojson }});
|
65 |
+
});
|
66 |
+
|
67 |
+
function generateQuiz(filteredText, answersAndExplanations) {
|
68 |
+
let quizSection = document.getElementById('quizSection');
|
69 |
+
quizSection.innerHTML = '';
|
70 |
+
|
71 |
+
let questions = filteredText.split(/\d+\.\s+/).filter(q => q.trim() !== '');
|
72 |
+
|
73 |
+
questions.forEach((question, index) => {
|
74 |
+
let parts = question.split('Choices:');
|
75 |
+
let questionText = parts[0].replace('Question:', '').trim();
|
76 |
+
let choicesText = parts[1].trim();
|
77 |
+
|
78 |
+
let div = document.createElement('div');
|
79 |
+
div.className = 'quiz-question';
|
80 |
+
div.innerHTML = `<strong>${index + 1}: ${questionText}</strong>`;
|
81 |
+
|
82 |
+
let choices = choicesText.split(/\s*[A-D]\)\s*/).filter(choice => choice.trim() !== '');
|
83 |
+
let labels = choicesText.match(/\s*[A-D]\)\s*/g);
|
84 |
+
choices.forEach((choice, i) => {
|
85 |
+
let label = labels[i].trim().charAt(0);
|
86 |
+
|
87 |
+
let choiceDiv = document.createElement('div');
|
88 |
+
|
89 |
+
let input = document.createElement('input');
|
90 |
+
input.type = 'radio';
|
91 |
+
input.name = `question_${index + 1}`;
|
92 |
+
input.value = label;
|
93 |
+
input.id = `q${index + 1}_${label}`;
|
94 |
+
|
95 |
+
let choiceLabel = document.createElement('label');
|
96 |
+
choiceLabel.setAttribute('for', `q${index + 1}_${label}`);
|
97 |
+
choiceLabel.textContent = `${label}) ${choice}`;
|
98 |
+
|
99 |
+
choiceDiv.appendChild(input);
|
100 |
+
choiceDiv.appendChild(choiceLabel);
|
101 |
+
|
102 |
+
div.appendChild(choiceDiv);
|
103 |
+
});
|
104 |
+
|
105 |
+
let answerDiv = document.createElement('div');
|
106 |
+
answerDiv.className = 'quiz-answer';
|
107 |
+
answerDiv.style.display = 'none';
|
108 |
+
div.appendChild(answerDiv);
|
109 |
+
|
110 |
+
let explanationDiv = document.createElement('div');
|
111 |
+
explanationDiv.className = 'quiz-explanation';
|
112 |
+
explanationDiv.style.display = 'none';
|
113 |
+
div.appendChild(explanationDiv);
|
114 |
+
|
115 |
+
quizSection.appendChild(div);
|
116 |
+
});
|
117 |
+
|
118 |
+
document.getElementById('submitQuiz').style.display = 'block';
|
119 |
+
}
|
120 |
+
|
121 |
+
function validateQuiz() {
|
122 |
+
let quizSection = document.getElementById('quizSection');
|
123 |
+
let questions = quizSection.getElementsByClassName('quiz-question');
|
124 |
+
|
125 |
+
for (let i = 0; i < questions.length; i++) {
|
126 |
+
let inputs = questions[i].querySelectorAll('input[type="radio"]');
|
127 |
+
let answered = false;
|
128 |
+
|
129 |
+
for (let j = 0; j < inputs.length; j++) {
|
130 |
+
if (inputs[j].checked) {
|
131 |
+
answered = true;
|
132 |
+
break;
|
133 |
+
}
|
134 |
+
}
|
135 |
+
|
136 |
+
if (!answered) {
|
137 |
+
return false;
|
138 |
+
}
|
139 |
+
}
|
140 |
+
|
141 |
+
return true;
|
142 |
+
}
|
143 |
+
|
144 |
+
function submitQuiz() {
|
145 |
+
if (!validateQuiz()) {
|
146 |
+
alert('Please answer all questions before submitting.');
|
147 |
+
return;
|
148 |
+
}
|
149 |
+
|
150 |
+
let quizSection = document.getElementById('quizSection');
|
151 |
+
let questions = quizSection.getElementsByClassName('quiz-question');
|
152 |
+
|
153 |
+
fetch('/process_text', {
|
154 |
+
method: 'POST',
|
155 |
+
headers: {
|
156 |
+
'Content-Type': 'application/json'
|
157 |
+
},
|
158 |
+
body: JSON.stringify({ text: "{{ filtered_text }}" })
|
159 |
+
})
|
160 |
+
.then(response => response.json())
|
161 |
+
.then(data => {
|
162 |
+
let answersAndExplanations = data.answers_and_explanations;
|
163 |
+
for (let i = 0; i < questions.length; i++) {
|
164 |
+
let answerDiv = questions[i].querySelector('.quiz-answer');
|
165 |
+
let explanationDiv = questions[i].querySelector('.quiz-explanation');
|
166 |
+
let answer = answersAndExplanations[i]?.Answer || '';
|
167 |
+
let explanation = answersAndExplanations[i]?.Explanation || '';
|
168 |
+
|
169 |
+
answerDiv.style.display = 'block';
|
170 |
+
answerDiv.innerHTML = `Answer: ${answer}`;
|
171 |
+
|
172 |
+
explanationDiv.style.display = 'block';
|
173 |
+
explanationDiv.innerHTML = `Explanation: ${explanation}`;
|
174 |
+
}
|
175 |
+
|
176 |
+
document.getElementById('submitQuiz').style.display = 'none';
|
177 |
+
});
|
178 |
+
}
|
179 |
+
</script>
|
180 |
+
</body>
|
181 |
+
</html>
|