capradeepgujaran commited on
Commit
9fe878e
·
verified ·
1 Parent(s): e21b32f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +103 -37
app.py CHANGED
@@ -615,29 +615,43 @@ def create_quiz_interface():
615
  )
616
  certificate_display = gr.Image(label="Your Certificate")
617
 
618
- def update_question_display(questions, idx, current_answers):
619
- if not questions or idx >= len(questions):
 
620
  return {
621
  question_display: "",
622
- current_options: gr.update(choices=[], value=None, visible=False),
623
  question_counter: "",
624
- question_box: gr.update(visible=False)
 
 
 
 
 
625
  }
626
 
627
- question = questions[idx]
 
 
 
628
  return {
629
- question_display: f"""## Question {idx + 1}
630
  {question.question}
631
 
632
  Please select one answer:""",
633
  current_options: gr.update(
634
  choices=question.options,
635
- value=current_answers[idx] if idx < len(current_answers) else None,
636
  visible=True,
637
- label=f"Select your answer for Question {idx + 1}:"
638
  ),
639
- question_counter: f"Question {idx + 1} of {len(questions)}",
640
- question_box: gr.update(visible=True)
 
 
 
 
 
641
  }
642
 
643
  def navigate(direction, current_idx, questions, answers, current_answer):
@@ -669,9 +683,9 @@ Please select one answer:""",
669
  current_question_idx: new_idx,
670
  answer_state: new_answers,
671
  question_display: f"""## Question {new_idx + 1}
672
- {question.question}
673
-
674
- Please select one answer:""",
675
  current_options: gr.update(
676
  choices=question.options,
677
  value=new_answers[new_idx] if new_idx < len(new_answers) else None,
@@ -681,31 +695,84 @@ Please select one answer:""",
681
  question_counter: f"Question {new_idx + 1} of {len(questions)}",
682
  question_box: gr.update(visible=True)
683
  }
684
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
685
  # Event handlers
686
  generate_btn.click(
687
  fn=on_generate_questions,
688
  inputs=[text_input, num_questions],
689
- outputs=[
690
- question_display,
691
- current_options,
692
- question_counter,
693
- question_box,
694
- current_questions,
695
- current_question_idx,
696
- answer_state,
697
- tabs,
698
- results_group
699
- ]
700
  )
701
-
702
- # Navigation event handlers with dictionary returns
703
  def handle_prev(current_idx, questions, answers, current_answer):
704
  return navigate(-1, current_idx, questions, answers, current_answer)
705
-
706
  def handle_next(current_idx, questions, answers, current_answer):
707
  return navigate(1, current_idx, questions, answers, current_answer)
708
-
709
  prev_btn.click(
710
  fn=handle_prev,
711
  inputs=[
@@ -723,7 +790,7 @@ Please select one answer:""",
723
  question_box: "visible"
724
  }
725
  )
726
-
727
  next_btn.click(
728
  fn=handle_next,
729
  inputs=[
@@ -741,20 +808,20 @@ Please select one answer:""",
741
  question_box: "visible"
742
  }
743
  )
744
-
745
  # Update answer state when radio button changes
746
  def update_answer_state(answer, idx, current_answers):
747
  new_answers = list(current_answers)
748
  if 0 <= idx < len(new_answers):
749
  new_answers[idx] = answer
750
  return new_answers
751
-
752
  current_options.change(
753
  fn=update_answer_state,
754
  inputs=[current_options, current_question_idx, answer_state],
755
  outputs=answer_state
756
  )
757
-
758
  # Submission handler
759
  submit_btn.click(
760
  fn=on_submit,
@@ -773,16 +840,15 @@ Please select one answer:""",
773
  tabs: "selected"
774
  }
775
  )
776
-
777
  # Certificate generation
778
  score_display.change(
779
  fn=quiz_app.certificate_generator.generate,
780
  inputs=[score_display, name, course_name, company_logo, participant_photo],
781
  outputs=certificate_display
782
  )
783
-
784
- return demo
785
-
786
 
787
  if __name__ == "__main__":
788
  demo = create_quiz_interface()
 
615
  )
616
  certificate_display = gr.Image(label="Your Certificate")
617
 
618
+ def on_generate_questions(text, num_questions):
619
+ success, questions = quiz_app.generate_questions(text, num_questions)
620
+ if not success:
621
  return {
622
  question_display: "",
623
+ current_options: gr.update(choices=[], visible=False),
624
  question_counter: "",
625
+ question_box: gr.update(visible=False),
626
+ current_questions: [],
627
+ current_question_idx: 0,
628
+ answer_state: [None] * 5,
629
+ tabs: gr.update(selected=1),
630
+ results_group: gr.update(visible=False)
631
  }
632
 
633
+ # Initialize first question
634
+ initial_answers = [None] * len(questions)
635
+ question = questions[0]
636
+
637
  return {
638
+ question_display: f"""## Question 1
639
  {question.question}
640
 
641
  Please select one answer:""",
642
  current_options: gr.update(
643
  choices=question.options,
644
+ value=None,
645
  visible=True,
646
+ label="Select your answer for Question 1:"
647
  ),
648
+ question_counter: f"Question 1 of {len(questions)}",
649
+ question_box: gr.update(visible=True),
650
+ current_questions: questions,
651
+ current_question_idx: 0,
652
+ answer_state: initial_answers,
653
+ tabs: gr.update(selected=1),
654
+ results_group: gr.update(visible=False)
655
  }
656
 
657
  def navigate(direction, current_idx, questions, answers, current_answer):
 
683
  current_question_idx: new_idx,
684
  answer_state: new_answers,
685
  question_display: f"""## Question {new_idx + 1}
686
+ {question.question}
687
+
688
+ Please select one answer:""",
689
  current_options: gr.update(
690
  choices=question.options,
691
  value=new_answers[new_idx] if new_idx < len(new_answers) else None,
 
695
  question_counter: f"Question {new_idx + 1} of {len(questions)}",
696
  question_box: gr.update(visible=True)
697
  }
698
+
699
+ def on_submit(questions, answers, current_idx, current_answer):
700
+ # Update the current answer in the answers list
701
+ final_answers = list(answers)
702
+ if 0 <= current_idx < len(final_answers):
703
+ final_answers[current_idx] = current_answer
704
+
705
+ # Validate all answers are provided
706
+ if not all(a is not None for a in final_answers[:len(questions)]):
707
+ return {
708
+ feedback_box: "⚠️ Please answer all questions before submitting.",
709
+ results_group: gr.update(visible=True),
710
+ score_display: 0,
711
+ result_message: "",
712
+ question_box: gr.update(visible=True),
713
+ tabs: gr.update(selected=1)
714
+ }
715
+
716
+ score, passed, feedback = quiz_app.calculate_score(final_answers[:len(questions)])
717
+
718
+ # Generate feedback HTML
719
+ feedback_html = "# Assessment Results\n\n"
720
+ for i, (q, f) in enumerate(zip(questions, feedback)):
721
+ color = "green" if f.is_correct else "red"
722
+ symbol = "✅" if f.is_correct else "❌"
723
+ feedback_html += f"""
724
+ ### Question {i+1}
725
+ {q.question}
726
+
727
+ <div style="color: {color}; padding: 10px; margin: 5px 0; border-left: 3px solid {color};">
728
+ {symbol} Your answer: {f.selected or "No answer"}
729
+ {'' if f.is_correct else f'<br>Correct answer: {f.correct_answer}'}
730
+ </div>
731
+ """
732
+
733
+ result_msg = "🎉 Passed!" if passed else "Please try again"
734
+ if not passed:
735
+ feedback_html += f"""
736
+ <div style="background-color: #ffe6e6; padding: 20px; margin-top: 20px; border-radius: 10px;">
737
+ <h3 style="color: #cc0000;">Please Try Again</h3>
738
+ <p>Your score: {score:.1f}%</p>
739
+ <p>You need 80% or higher to pass and receive a certificate.</p>
740
+ </div>
741
+ """
742
+
743
+ return {
744
+ feedback_box: feedback_html,
745
+ results_group: gr.update(visible=True),
746
+ score_display: score,
747
+ result_message: result_msg,
748
+ question_box: gr.update(visible=passed),
749
+ tabs: gr.update(selected=2 if passed else 1)
750
+ }
751
+
752
  # Event handlers
753
  generate_btn.click(
754
  fn=on_generate_questions,
755
  inputs=[text_input, num_questions],
756
+ outputs={
757
+ question_display: "value",
758
+ current_options: "value",
759
+ question_counter: "value",
760
+ question_box: "visible",
761
+ current_questions: "value",
762
+ current_question_idx: "value",
763
+ answer_state: "value",
764
+ tabs: "selected",
765
+ results_group: "visible"
766
+ }
767
  )
768
+
769
+ # Navigation event handlers
770
  def handle_prev(current_idx, questions, answers, current_answer):
771
  return navigate(-1, current_idx, questions, answers, current_answer)
772
+
773
  def handle_next(current_idx, questions, answers, current_answer):
774
  return navigate(1, current_idx, questions, answers, current_answer)
775
+
776
  prev_btn.click(
777
  fn=handle_prev,
778
  inputs=[
 
790
  question_box: "visible"
791
  }
792
  )
793
+
794
  next_btn.click(
795
  fn=handle_next,
796
  inputs=[
 
808
  question_box: "visible"
809
  }
810
  )
811
+
812
  # Update answer state when radio button changes
813
  def update_answer_state(answer, idx, current_answers):
814
  new_answers = list(current_answers)
815
  if 0 <= idx < len(new_answers):
816
  new_answers[idx] = answer
817
  return new_answers
818
+
819
  current_options.change(
820
  fn=update_answer_state,
821
  inputs=[current_options, current_question_idx, answer_state],
822
  outputs=answer_state
823
  )
824
+
825
  # Submission handler
826
  submit_btn.click(
827
  fn=on_submit,
 
840
  tabs: "selected"
841
  }
842
  )
843
+
844
  # Certificate generation
845
  score_display.change(
846
  fn=quiz_app.certificate_generator.generate,
847
  inputs=[score_display, name, course_name, company_logo, participant_photo],
848
  outputs=certificate_display
849
  )
850
+
851
+ return demo
 
852
 
853
  if __name__ == "__main__":
854
  demo = create_quiz_interface()