Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -686,149 +686,149 @@ def create_quiz_interface():
|
|
| 686 |
|
| 687 |
def handle_prev(current_idx, questions, answers, current_answer):
|
| 688 |
return navigate(-1, current_idx, questions, answers, current_answer)
|
| 689 |
-
|
| 690 |
-
|
| 691 |
-
|
| 692 |
-
|
| 693 |
-
|
| 694 |
-
|
| 695 |
-
|
| 696 |
-
|
| 697 |
-
|
| 698 |
-
|
| 699 |
-
|
| 700 |
-
|
| 701 |
-
|
| 702 |
-
|
| 703 |
-
|
| 704 |
-
|
| 705 |
-
|
| 706 |
-
|
| 707 |
-
|
| 708 |
-
|
| 709 |
-
|
| 710 |
-
|
| 711 |
-
|
| 712 |
-
|
| 713 |
-
|
| 714 |
-
|
| 715 |
-
|
| 716 |
-
|
| 717 |
-
|
| 718 |
-
|
| 719 |
-
|
| 720 |
-
|
| 721 |
-
|
| 722 |
-
|
| 723 |
-
|
| 724 |
-
|
| 725 |
-
|
| 726 |
-
|
| 727 |
-
|
| 728 |
-
|
| 729 |
-
|
| 730 |
-
|
| 731 |
-
|
| 732 |
-
|
| 733 |
-
|
| 734 |
-
|
| 735 |
-
|
| 736 |
-
|
| 737 |
-
|
| 738 |
-
|
| 739 |
-
|
| 740 |
-
|
| 741 |
-
|
| 742 |
-
|
| 743 |
-
|
| 744 |
-
|
| 745 |
-
|
| 746 |
-
|
| 747 |
-
|
| 748 |
-
|
| 749 |
-
|
| 750 |
-
|
| 751 |
-
|
| 752 |
-
|
| 753 |
-
|
| 754 |
-
|
| 755 |
-
|
| 756 |
-
|
| 757 |
-
|
| 758 |
-
|
| 759 |
-
|
| 760 |
-
|
| 761 |
-
|
| 762 |
-
|
| 763 |
-
|
| 764 |
-
|
| 765 |
-
|
| 766 |
-
|
| 767 |
-
|
| 768 |
-
|
| 769 |
-
|
| 770 |
-
|
| 771 |
-
|
| 772 |
-
|
| 773 |
-
|
| 774 |
-
|
| 775 |
-
|
| 776 |
-
|
| 777 |
-
|
| 778 |
-
|
| 779 |
-
|
| 780 |
-
|
| 781 |
-
|
| 782 |
-
|
| 783 |
-
|
| 784 |
-
|
| 785 |
-
|
| 786 |
-
|
| 787 |
-
|
| 788 |
-
|
| 789 |
-
|
| 790 |
-
|
| 791 |
-
|
| 792 |
-
|
| 793 |
-
|
| 794 |
-
|
| 795 |
-
|
| 796 |
-
|
| 797 |
-
|
| 798 |
-
|
| 799 |
-
|
| 800 |
-
|
| 801 |
-
|
| 802 |
-
|
| 803 |
-
|
| 804 |
-
|
| 805 |
-
|
| 806 |
-
|
| 807 |
-
|
| 808 |
-
|
| 809 |
-
|
| 810 |
-
|
| 811 |
-
|
| 812 |
-
|
| 813 |
-
|
| 814 |
-
|
| 815 |
-
|
| 816 |
-
|
| 817 |
-
|
| 818 |
-
|
| 819 |
-
|
| 820 |
-
|
| 821 |
-
|
| 822 |
-
|
| 823 |
-
|
| 824 |
-
|
| 825 |
-
|
| 826 |
-
|
| 827 |
-
|
| 828 |
-
|
| 829 |
-
|
| 830 |
-
|
| 831 |
-
|
| 832 |
|
| 833 |
if __name__ == "__main__":
|
| 834 |
demo = create_quiz_interface()
|
|
|
|
| 686 |
|
| 687 |
def handle_prev(current_idx, questions, answers, current_answer):
|
| 688 |
return navigate(-1, current_idx, questions, answers, current_answer)
|
| 689 |
+
|
| 690 |
+
def handle_next(current_idx, questions, answers, current_answer):
|
| 691 |
+
return navigate(1, current_idx, questions, answers, current_answer)
|
| 692 |
+
|
| 693 |
+
def update_answer_state(answer, idx, current_answers):
|
| 694 |
+
new_answers = list(current_answers)
|
| 695 |
+
if 0 <= idx < len(new_answers):
|
| 696 |
+
new_answers[idx] = answer
|
| 697 |
+
return new_answers
|
| 698 |
+
|
| 699 |
+
def on_submit(questions, answers, current_idx, current_answer):
|
| 700 |
+
final_answers = list(answers)
|
| 701 |
+
if 0 <= current_idx < len(final_answers):
|
| 702 |
+
final_answers[current_idx] = current_answer
|
| 703 |
+
|
| 704 |
+
if not all(a is not None for a in final_answers[:len(questions)]):
|
| 705 |
+
return [
|
| 706 |
+
"⚠️ Please answer all questions before submitting.",
|
| 707 |
+
gr.update(visible=True),
|
| 708 |
+
0,
|
| 709 |
+
"",
|
| 710 |
+
gr.update(visible=True),
|
| 711 |
+
gr.update(selected=1)
|
| 712 |
+
]
|
| 713 |
+
|
| 714 |
+
score, passed, feedback = quiz_app.calculate_score(final_answers[:len(questions)])
|
| 715 |
+
|
| 716 |
+
feedback_html = "# Assessment Results\n\n"
|
| 717 |
+
for i, (q, f) in enumerate(zip(questions, feedback)):
|
| 718 |
+
color = "green" if f.is_correct else "red"
|
| 719 |
+
symbol = "✅" if f.is_correct else "❌"
|
| 720 |
+
feedback_html += f"""### Question {i+1}
|
| 721 |
+
{q.question}
|
| 722 |
+
|
| 723 |
+
<div style="color: {color}; padding: 10px; margin: 5px 0; border-left: 3px solid {color};">
|
| 724 |
+
{symbol} Your answer: {f.selected or "No answer"}
|
| 725 |
+
{'' if f.is_correct else f'<br>Correct answer: {f.correct_answer}'}
|
| 726 |
+
</div>
|
| 727 |
+
"""
|
| 728 |
+
|
| 729 |
+
result_msg = "🎉 Passed!" if passed else "Please try again"
|
| 730 |
+
if not passed:
|
| 731 |
+
feedback_html += f"""
|
| 732 |
+
<div style="background-color: #ffe6e6; padding: 20px; margin-top: 20px; border-radius: 10px;">
|
| 733 |
+
<h3 style="color: #cc0000;">Please Try Again</h3>
|
| 734 |
+
<p>Your score: {score:.1f}%</p>
|
| 735 |
+
<p>You need 80% or higher to pass and receive a certificate.</p>
|
| 736 |
+
</div>
|
| 737 |
+
"""
|
| 738 |
+
|
| 739 |
+
return [
|
| 740 |
+
feedback_html,
|
| 741 |
+
gr.update(visible=True),
|
| 742 |
+
score,
|
| 743 |
+
result_msg,
|
| 744 |
+
gr.update(visible=not passed),
|
| 745 |
+
gr.update(selected=2 if passed else 1)
|
| 746 |
+
]
|
| 747 |
+
|
| 748 |
+
# Event handlers
|
| 749 |
+
generate_btn.click(
|
| 750 |
+
fn=on_generate_questions,
|
| 751 |
+
inputs=[text_input, num_questions],
|
| 752 |
+
outputs=[
|
| 753 |
+
question_display,
|
| 754 |
+
current_options,
|
| 755 |
+
question_counter,
|
| 756 |
+
question_box,
|
| 757 |
+
current_questions,
|
| 758 |
+
current_question_idx,
|
| 759 |
+
answer_state,
|
| 760 |
+
tabs,
|
| 761 |
+
results_group
|
| 762 |
+
]
|
| 763 |
+
)
|
| 764 |
+
|
| 765 |
+
prev_btn.click(
|
| 766 |
+
fn=handle_prev,
|
| 767 |
+
inputs=[
|
| 768 |
+
current_question_idx,
|
| 769 |
+
current_questions,
|
| 770 |
+
answer_state,
|
| 771 |
+
current_options
|
| 772 |
+
],
|
| 773 |
+
outputs=[
|
| 774 |
+
current_question_idx,
|
| 775 |
+
answer_state,
|
| 776 |
+
question_display,
|
| 777 |
+
current_options,
|
| 778 |
+
question_counter,
|
| 779 |
+
question_box
|
| 780 |
+
]
|
| 781 |
+
)
|
| 782 |
+
|
| 783 |
+
next_btn.click(
|
| 784 |
+
fn=handle_next,
|
| 785 |
+
inputs=[
|
| 786 |
+
current_question_idx,
|
| 787 |
+
current_questions,
|
| 788 |
+
answer_state,
|
| 789 |
+
current_options
|
| 790 |
+
],
|
| 791 |
+
outputs=[
|
| 792 |
+
current_question_idx,
|
| 793 |
+
answer_state,
|
| 794 |
+
question_display,
|
| 795 |
+
current_options,
|
| 796 |
+
question_counter,
|
| 797 |
+
question_box
|
| 798 |
+
]
|
| 799 |
+
)
|
| 800 |
+
|
| 801 |
+
current_options.change(
|
| 802 |
+
fn=update_answer_state,
|
| 803 |
+
inputs=[current_options, current_question_idx, answer_state],
|
| 804 |
+
outputs=answer_state
|
| 805 |
+
)
|
| 806 |
+
|
| 807 |
+
submit_btn.click(
|
| 808 |
+
fn=on_submit,
|
| 809 |
+
inputs=[
|
| 810 |
+
current_questions,
|
| 811 |
+
answer_state,
|
| 812 |
+
current_question_idx,
|
| 813 |
+
current_options
|
| 814 |
+
],
|
| 815 |
+
outputs=[
|
| 816 |
+
feedback_box,
|
| 817 |
+
results_group,
|
| 818 |
+
score_display,
|
| 819 |
+
result_message,
|
| 820 |
+
question_box,
|
| 821 |
+
tabs
|
| 822 |
+
]
|
| 823 |
+
)
|
| 824 |
+
|
| 825 |
+
score_display.change(
|
| 826 |
+
fn=quiz_app.certificate_generator.generate,
|
| 827 |
+
inputs=[score_display, name, course_name, company_logo, participant_photo],
|
| 828 |
+
outputs=certificate_display
|
| 829 |
+
)
|
| 830 |
+
|
| 831 |
+
return demo
|
| 832 |
|
| 833 |
if __name__ == "__main__":
|
| 834 |
demo = create_quiz_interface()
|