Update app.py
Browse files
app.py
CHANGED
@@ -1,181 +1,100 @@
|
|
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 |
-
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>
|
|
|
1 |
+
from flask import Flask, request, jsonify, render_template
|
2 |
+
from huggingface_hub import InferenceClient
|
3 |
+
from requests.exceptions import RequestException
|
4 |
+
|
5 |
+
app = Flask(__name__)
|
6 |
+
|
7 |
+
client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.2")
|
8 |
+
|
9 |
+
def system_instructions(context):
|
10 |
+
return f"""<s> [INST] Your are a great teacher and your task is to create 6 questions with answer and 4 choices based on the following context:\n\n{context}\n\n. Each example should be like this
|
11 |
+
Question: ""
|
12 |
+
Choices:
|
13 |
+
A): ""
|
14 |
+
B): ""
|
15 |
+
C): ""
|
16 |
+
D): ""
|
17 |
+
Answer: "Choose either A, B, C, or D as the correct answer"
|
18 |
+
Explanation: ""
|
19 |
+
\n
|
20 |
+
[/INST]
|
21 |
+
"""
|
22 |
+
|
23 |
+
def generate_quiz(context):
|
24 |
+
formatted_prompt = system_instructions(context)
|
25 |
+
|
26 |
+
generate_kwargs = dict(
|
27 |
+
temperature=0.1,
|
28 |
+
max_new_tokens=2048,
|
29 |
+
top_p=0.95,
|
30 |
+
repetition_penalty=1.0,
|
31 |
+
do_sample=True,
|
32 |
+
seed=42,)
|
33 |
+
|
34 |
+
try:
|
35 |
+
response = client.text_generation(
|
36 |
+
formatted_prompt,
|
37 |
+
**generate_kwargs,
|
38 |
+
stream=False,
|
39 |
+
details=False,
|
40 |
+
return_full_text=False,
|
41 |
+
)
|
42 |
+
return response
|
43 |
+
|
44 |
+
except (RequestException, SystemExit) as e:
|
45 |
+
return {"error": str(e)}
|
46 |
+
|
47 |
+
@app.route("/", methods=["GET", "POST"])
|
48 |
+
def generate_quiz_page():
|
49 |
+
if request.method == "POST":
|
50 |
+
if request.content_type == 'application/json':
|
51 |
+
data = request.get_json()
|
52 |
+
context = data.get("context")
|
53 |
+
if context is None or context.strip() == "":
|
54 |
+
return jsonify({"error": "Missing or empty 'context' parameter"}), 400
|
55 |
+
else:
|
56 |
+
context = request.form.get("context")
|
57 |
+
if context is None or context.strip() == "":
|
58 |
+
return jsonify({"error": "Missing or empty 'context' parameter"}), 400
|
59 |
+
|
60 |
+
response = generate_quiz(context)
|
61 |
+
|
62 |
+
if request.content_type == 'application/json':
|
63 |
+
return jsonify(response)
|
64 |
+
|
65 |
+
# Handle text processing for quiz generation
|
66 |
+
filtered_text, answers_and_explanations = handle_text(response)
|
67 |
+
|
68 |
+
return render_template('quiz.html', filtered_text=filtered_text, answers_and_explanations=answers_and_explanations)
|
69 |
+
|
70 |
+
# Default GET request handling
|
71 |
+
return render_template('index.html')
|
72 |
+
|
73 |
+
def handle_text(text):
|
74 |
+
lines = text.splitlines()
|
75 |
+
filtered_lines = []
|
76 |
+
answers_and_explanations = []
|
77 |
+
|
78 |
+
for line in lines:
|
79 |
+
line = line.strip()
|
80 |
+
|
81 |
+
if line.startswith("Question:"):
|
82 |
+
filtered_lines.append(line)
|
83 |
+
elif line.startswith("Choices:"):
|
84 |
+
filtered_lines.append(line)
|
85 |
+
elif line.startswith("Answer:"):
|
86 |
+
answer = line.split(":")[1].strip()
|
87 |
+
answers_and_explanations.append({"Answer": answer})
|
88 |
+
elif line.startswith("Explanation:"):
|
89 |
+
explanation = line.split(":")[1].strip()
|
90 |
+
answers_and_explanations[-1]["Explanation"] = explanation
|
91 |
+
elif line.startswith(("A)", "B)", "C)", "D)")):
|
92 |
+
filtered_lines.append(line)
|
93 |
+
else:
|
94 |
+
filtered_lines.append(line)
|
95 |
+
|
96 |
+
filtered_text = "\n".join(filtered_lines)
|
97 |
+
return filtered_text, answers_and_explanations
|
98 |
+
|
99 |
+
if __name__ == "__main__":
|
100 |
+
app.run(debug=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|