Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -615,269 +615,217 @@ def create_quiz_interface():
|
|
615 |
)
|
616 |
certificate_display = gr.Image(label="Your Certificate")
|
617 |
|
618 |
-
|
619 |
def on_generate_questions(text, num_questions):
|
620 |
-
|
621 |
-
|
622 |
-
|
623 |
-
|
624 |
-
|
625 |
-
|
626 |
-
|
627 |
-
|
628 |
-
|
629 |
-
|
630 |
-
|
631 |
-
|
632 |
-
|
633 |
-
|
634 |
-
|
635 |
-
|
636 |
-
|
637 |
-
|
638 |
-
|
639 |
-
|
640 |
-
|
641 |
-
|
642 |
-
|
643 |
-
|
644 |
-
|
645 |
-
|
646 |
-
|
647 |
-
|
648 |
-
|
649 |
-
|
650 |
-
|
651 |
-
|
652 |
-
|
653 |
-
|
654 |
-
|
655 |
-
|
656 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
657 |
|
658 |
-
def
|
659 |
-
|
660 |
-
|
661 |
-
|
662 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
663 |
return [
|
664 |
-
|
665 |
-
|
666 |
-
|
667 |
-
|
668 |
-
|
669 |
-
gr.update(
|
670 |
]
|
671 |
|
672 |
-
|
673 |
-
new_answers = list(answers)
|
674 |
-
if current_answer and 0 <= current_idx < len(new_answers):
|
675 |
-
new_answers[current_idx] = current_answer
|
676 |
|
677 |
-
#
|
678 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
679 |
|
680 |
-
|
681 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
682 |
|
683 |
return [
|
684 |
-
|
685 |
-
|
686 |
-
|
687 |
-
|
688 |
-
|
689 |
-
|
690 |
-
gr.update(
|
691 |
-
choices=question.options,
|
692 |
-
value=new_answers[new_idx] if new_idx < len(new_answers) else None,
|
693 |
-
visible=True,
|
694 |
-
label=f"Select your answer for Question {new_idx + 1}:"
|
695 |
-
), # current_options
|
696 |
-
f"Question {new_idx + 1} of {len(questions)}", # question_counter
|
697 |
-
gr.update(visible=True) # question_box
|
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 |
-
return [
|
785 |
-
result[current_question_idx],
|
786 |
-
result[answer_state],
|
787 |
-
result[question_display],
|
788 |
-
result[current_options],
|
789 |
-
result[question_counter],
|
790 |
-
result[question_box]
|
791 |
-
]
|
792 |
-
|
793 |
-
prev_btn.click(
|
794 |
-
fn=handle_prev,
|
795 |
-
inputs=[
|
796 |
-
current_question_idx,
|
797 |
-
current_questions,
|
798 |
-
answer_state,
|
799 |
-
current_options
|
800 |
-
],
|
801 |
-
outputs=[
|
802 |
-
current_question_idx,
|
803 |
-
answer_state,
|
804 |
-
question_display,
|
805 |
-
current_options,
|
806 |
-
question_counter,
|
807 |
-
question_box
|
808 |
-
]
|
809 |
-
)
|
810 |
-
|
811 |
-
next_btn.click(
|
812 |
-
fn=handle_next,
|
813 |
-
inputs=[
|
814 |
-
current_question_idx,
|
815 |
-
current_questions,
|
816 |
-
answer_state,
|
817 |
-
current_options
|
818 |
-
],
|
819 |
-
outputs=[
|
820 |
-
current_question_idx,
|
821 |
-
answer_state,
|
822 |
-
question_display,
|
823 |
-
current_options,
|
824 |
-
question_counter,
|
825 |
-
question_box
|
826 |
-
]
|
827 |
-
)
|
828 |
-
|
829 |
-
# Update answer state when radio button changes
|
830 |
-
def update_answer_state(answer, idx, current_answers):
|
831 |
-
new_answers = list(current_answers)
|
832 |
-
if 0 <= idx < len(new_answers):
|
833 |
-
new_answers[idx] = answer
|
834 |
-
return new_answers
|
835 |
-
|
836 |
-
current_options.change(
|
837 |
-
fn=update_answer_state,
|
838 |
-
inputs=[current_options, current_question_idx, answer_state],
|
839 |
-
outputs=answer_state
|
840 |
-
)
|
841 |
-
|
842 |
-
# Submission handler
|
843 |
-
def on_submit_wrapper(questions, answers, current_idx, current_answer):
|
844 |
-
result = on_submit(questions, answers, current_idx, current_answer)
|
845 |
-
return [
|
846 |
-
result[feedback_box],
|
847 |
-
result[results_group],
|
848 |
-
result[score_display],
|
849 |
-
result[result_message],
|
850 |
-
result[question_box],
|
851 |
-
result[tabs]
|
852 |
-
]
|
853 |
-
|
854 |
-
submit_btn.click(
|
855 |
-
fn=on_submit_wrapper,
|
856 |
-
inputs=[
|
857 |
-
current_questions,
|
858 |
-
answer_state,
|
859 |
-
current_question_idx,
|
860 |
-
current_options
|
861 |
-
],
|
862 |
-
outputs=[
|
863 |
-
feedback_box,
|
864 |
-
results_group,
|
865 |
-
score_display,
|
866 |
-
result_message,
|
867 |
-
question_box,
|
868 |
-
tabs
|
869 |
-
]
|
870 |
-
)
|
871 |
-
|
872 |
-
# Certificate generation
|
873 |
-
score_display.change(
|
874 |
-
fn=quiz_app.certificate_generator.generate,
|
875 |
-
inputs=[score_display, name, course_name, company_logo, participant_photo],
|
876 |
-
outputs=certificate_display
|
877 |
-
)
|
878 |
-
|
879 |
-
return demo
|
880 |
-
|
881 |
-
if __name__ == "__main__":
|
882 |
-
demo = create_quiz_interface()
|
883 |
-
demo.launch()
|
|
|
615 |
)
|
616 |
certificate_display = gr.Image(label="Your Certificate")
|
617 |
|
618 |
+
# Helper Functions
|
619 |
def on_generate_questions(text, num_questions):
|
620 |
+
success, questions = quiz_app.generate_questions(text, num_questions)
|
621 |
+
if not success:
|
622 |
+
return [
|
623 |
+
"",
|
624 |
+
gr.update(choices=[], visible=False),
|
625 |
+
"",
|
626 |
+
gr.update(visible=False),
|
627 |
+
[],
|
628 |
+
0,
|
629 |
+
[None] * 5,
|
630 |
+
gr.update(selected=1),
|
631 |
+
gr.update(visible=False)
|
632 |
+
]
|
633 |
+
|
634 |
+
initial_answers = [None] * len(questions)
|
635 |
+
question = questions[0]
|
636 |
+
|
637 |
+
return [
|
638 |
+
f"## Question 1\n{question.question}\n\nPlease select one answer:",
|
639 |
+
gr.update(
|
640 |
+
choices=question.options,
|
641 |
+
value=None,
|
642 |
+
visible=True,
|
643 |
+
label="Select your answer for Question 1:"
|
644 |
+
),
|
645 |
+
f"Question 1 of {len(questions)}",
|
646 |
+
gr.update(visible=True),
|
647 |
+
questions,
|
648 |
+
0,
|
649 |
+
initial_answers,
|
650 |
+
gr.update(selected=1),
|
651 |
+
gr.update(visible=False)
|
652 |
+
]
|
653 |
+
|
654 |
+
def navigate(direction, current_idx, questions, answers, current_answer):
|
655 |
+
"""Handle navigation between questions"""
|
656 |
+
if not questions:
|
657 |
+
return [
|
658 |
+
0,
|
659 |
+
answers,
|
660 |
+
"",
|
661 |
+
gr.update(choices=[], value=None, visible=False),
|
662 |
+
"",
|
663 |
+
gr.update(visible=False)
|
664 |
+
]
|
665 |
+
|
666 |
+
new_answers = list(answers)
|
667 |
+
if current_answer and 0 <= current_idx < len(new_answers):
|
668 |
+
new_answers[current_idx] = current_answer
|
669 |
+
|
670 |
+
new_idx = max(0, min(len(questions) - 1, current_idx + direction))
|
671 |
+
question = questions[new_idx]
|
672 |
+
|
673 |
+
return [
|
674 |
+
new_idx,
|
675 |
+
new_answers,
|
676 |
+
f"## Question {new_idx + 1}\n{question.question}\n\nPlease select one answer:",
|
677 |
+
gr.update(
|
678 |
+
choices=question.options,
|
679 |
+
value=new_answers[new_idx] if new_idx < len(new_answers) else None,
|
680 |
+
visible=True,
|
681 |
+
label=f"Select your answer for Question {new_idx + 1}:"
|
682 |
+
),
|
683 |
+
f"Question {new_idx + 1} of {len(questions)}",
|
684 |
+
gr.update(visible=True)
|
685 |
+
]
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|