capradeepgujaran commited on
Commit
6ccb6ec
1 Parent(s): a41c315

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +143 -143
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
- 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()
 
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()