Spaces:
Running
Running
capradeepgujaran
commited on
Commit
•
edec330
1
Parent(s):
058bca9
Update app.py
Browse files
app.py
CHANGED
@@ -227,47 +227,131 @@ class CertificateGenerator:
|
|
227 |
certificate.save(temp_file.name, 'PNG', quality=95)
|
228 |
return temp_file.name
|
229 |
|
|
|
230 |
class QuizApp:
|
231 |
def __init__(self, api_key: str):
|
232 |
self.quiz_generator = QuizGenerator(api_key)
|
233 |
self.certificate_generator = CertificateGenerator()
|
234 |
self.current_questions: List[Question] = []
|
235 |
|
236 |
-
def
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
243 |
|
244 |
-
def
|
245 |
-
|
246 |
-
|
|
|
|
|
|
|
|
|
247 |
|
248 |
-
|
249 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
250 |
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
268 |
|
269 |
-
|
270 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
271 |
|
272 |
def create_quiz_interface():
|
273 |
if not os.getenv("GROQ_API_KEY"):
|
@@ -276,6 +360,7 @@ def create_quiz_interface():
|
|
276 |
quiz_app = QuizApp(os.getenv("GROQ_API_KEY"))
|
277 |
|
278 |
with gr.Blocks(title="CertifyMe AI", theme=gr.themes.Soft()) as demo:
|
|
|
279 |
# State management
|
280 |
current_questions = gr.State([])
|
281 |
|
@@ -333,6 +418,7 @@ def create_quiz_interface():
|
|
333 |
certificate_display = gr.Image(label="Your Certificate")
|
334 |
|
335 |
# Event handlers
|
|
|
336 |
generate_btn.click(
|
337 |
fn=quiz_app.update_questions,
|
338 |
inputs=[text_input, num_questions],
|
@@ -367,4 +453,4 @@ def create_quiz_interface():
|
|
367 |
|
368 |
if __name__ == "__main__":
|
369 |
demo = create_quiz_interface()
|
370 |
-
demo.launch()
|
|
|
227 |
certificate.save(temp_file.name, 'PNG', quality=95)
|
228 |
return temp_file.name
|
229 |
|
230 |
+
|
231 |
class QuizApp:
|
232 |
def __init__(self, api_key: str):
|
233 |
self.quiz_generator = QuizGenerator(api_key)
|
234 |
self.certificate_generator = CertificateGenerator()
|
235 |
self.current_questions: List[Question] = []
|
236 |
|
237 |
+
def update_questions(self, text: str, num_questions: int) -> Tuple[gr.update, gr.update, List[gr.update], List[Question], gr.update]:
|
238 |
+
"""
|
239 |
+
Event handler for generating new questions
|
240 |
+
"""
|
241 |
+
if not text.strip():
|
242 |
+
return (
|
243 |
+
gr.update(value=""),
|
244 |
+
gr.update(value="⚠️ Please enter some text content to generate questions."),
|
245 |
+
*[gr.update(visible=False, choices=[]) for _ in range(5)],
|
246 |
+
[],
|
247 |
+
gr.update(selected=1)
|
248 |
+
)
|
249 |
+
|
250 |
+
success, questions = self.generate_questions(text, num_questions)
|
251 |
+
|
252 |
+
if not success or not questions:
|
253 |
+
return (
|
254 |
+
gr.update(value=""),
|
255 |
+
gr.update(value="❌ Failed to generate questions. Please try again."),
|
256 |
+
*[gr.update(visible=False, choices=[]) for _ in range(5)],
|
257 |
+
[],
|
258 |
+
gr.update(selected=1)
|
259 |
+
)
|
260 |
+
|
261 |
+
# Create question display
|
262 |
+
questions_html = "# 📝 Assessment Questions\n\n"
|
263 |
+
questions_html += "> Please select one answer for each question.\n\n"
|
264 |
+
|
265 |
+
# Update radio buttons
|
266 |
+
updates = []
|
267 |
+
for i, q in enumerate(questions):
|
268 |
+
questions_html += f"### Question {i+1}\n{q.question}\n\n"
|
269 |
+
updates.append(gr.update(
|
270 |
+
visible=True,
|
271 |
+
choices=q.options,
|
272 |
+
value=None,
|
273 |
+
label=f"Select your answer:"
|
274 |
+
))
|
275 |
+
|
276 |
+
# Hide unused radio buttons
|
277 |
+
for i in range(len(questions), 5):
|
278 |
+
updates.append(gr.update(visible=False, choices=[]))
|
279 |
+
|
280 |
+
return (
|
281 |
+
gr.update(value=questions_html),
|
282 |
+
gr.update(value=""),
|
283 |
+
*updates,
|
284 |
+
questions,
|
285 |
+
gr.update(selected=1)
|
286 |
+
)
|
287 |
|
288 |
+
def submit_quiz(self, q1: Optional[str], q2: Optional[str], q3: Optional[str],
|
289 |
+
q4: Optional[str], q5: Optional[str], questions: List[Question]
|
290 |
+
) -> Tuple[gr.update, List[gr.update], float, str, gr.update]:
|
291 |
+
"""
|
292 |
+
Event handler for quiz submission
|
293 |
+
"""
|
294 |
+
answers = [q1, q2, q3, q4, q5][:len(questions)]
|
295 |
|
296 |
+
if not all(a is not None for a in answers):
|
297 |
+
return (
|
298 |
+
gr.update(value="⚠️ Please answer all questions before submitting."),
|
299 |
+
*[gr.update() for _ in range(5)],
|
300 |
+
0,
|
301 |
+
"",
|
302 |
+
gr.update(selected=1)
|
303 |
+
)
|
304 |
|
305 |
+
score, passed, feedback = self.calculate_score(answers)
|
306 |
+
|
307 |
+
# Create feedback HTML
|
308 |
+
feedback_html = "# Assessment Results\n\n"
|
309 |
+
for i, (q, f) in enumerate(zip(self.current_questions, feedback)):
|
310 |
+
color = "green" if f.is_correct else "red"
|
311 |
+
symbol = "✅" if f.is_correct else "❌"
|
312 |
+
feedback_html += f"""
|
313 |
+
### Question {i+1}
|
314 |
+
{q.question}
|
315 |
+
|
316 |
+
<div style="color: {color}; padding: 10px; margin: 5px 0; border-left: 3px solid {color};">
|
317 |
+
{symbol} Your answer: {f.selected}
|
318 |
+
{'' if f.is_correct else f'<br>Correct answer: {f.correct_answer}'}
|
319 |
+
</div>
|
320 |
+
"""
|
321 |
+
|
322 |
+
# Add result message
|
323 |
+
if passed:
|
324 |
+
feedback_html += self._create_success_message(score)
|
325 |
+
result_msg = f"🎉 Congratulations! You passed with {score:.1f}%"
|
326 |
+
else:
|
327 |
+
feedback_html += self._create_failure_message(score)
|
328 |
+
result_msg = f"Score: {score:.1f}%. You need 80% to pass."
|
329 |
|
330 |
+
return (
|
331 |
+
gr.update(value=feedback_html),
|
332 |
+
*[gr.update(visible=False) for _ in range(5)],
|
333 |
+
score,
|
334 |
+
result_msg,
|
335 |
+
gr.update(selected=2)
|
336 |
+
)
|
337 |
+
|
338 |
+
def _create_success_message(self, score: float) -> str:
|
339 |
+
return f"""
|
340 |
+
<div style="background-color: #e6ffe6; padding: 20px; margin-top: 20px; border-radius: 10px;">
|
341 |
+
<h3 style="color: #008000;">🎉 Congratulations!</h3>
|
342 |
+
<p>You passed the assessment with a score of {score:.1f}%</p>
|
343 |
+
<p>Your certificate has been generated.</p>
|
344 |
+
</div>
|
345 |
+
"""
|
346 |
+
|
347 |
+
def _create_failure_message(self, score: float) -> str:
|
348 |
+
return f"""
|
349 |
+
<div style="background-color: #ffe6e6; padding: 20px; margin-top: 20px; border-radius: 10px;">
|
350 |
+
<h3 style="color: #cc0000;">Please Try Again</h3>
|
351 |
+
<p>Your score: {score:.1f}%</p>
|
352 |
+
<p>You need 80% or higher to pass and receive a certificate.</p>
|
353 |
+
</div>
|
354 |
+
"""
|
355 |
|
356 |
def create_quiz_interface():
|
357 |
if not os.getenv("GROQ_API_KEY"):
|
|
|
360 |
quiz_app = QuizApp(os.getenv("GROQ_API_KEY"))
|
361 |
|
362 |
with gr.Blocks(title="CertifyMe AI", theme=gr.themes.Soft()) as demo:
|
363 |
+
|
364 |
# State management
|
365 |
current_questions = gr.State([])
|
366 |
|
|
|
418 |
certificate_display = gr.Image(label="Your Certificate")
|
419 |
|
420 |
# Event handlers
|
421 |
+
|
422 |
generate_btn.click(
|
423 |
fn=quiz_app.update_questions,
|
424 |
inputs=[text_input, num_questions],
|
|
|
453 |
|
454 |
if __name__ == "__main__":
|
455 |
demo = create_quiz_interface()
|
456 |
+
demo.launch()
|