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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +145 -117
app.py CHANGED
@@ -615,86 +615,87 @@ def 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):
658
- """
659
- Handle navigation between questions
660
- """
661
- if not questions: # No questions available
662
- return {
663
- current_question_idx: 0,
664
- answer_state: answers,
665
- question_display: "",
666
- current_options: gr.update(choices=[], value=None, visible=False),
667
- question_counter: "",
668
- question_box: gr.update(visible=False)
669
- }
670
-
671
- # Save current answer
672
- new_answers = list(answers)
673
- if current_answer and 0 <= current_idx < len(new_answers):
674
- new_answers[current_idx] = current_answer
675
-
676
- # Calculate new index
677
- new_idx = max(0, min(len(questions) - 1, current_idx + direction))
678
-
679
- # Get current question
680
- question = questions[new_idx]
681
-
682
- return {
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,
692
- visible=True,
693
- label=f"Select your answer for Question {new_idx + 1}:"
694
- ),
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
@@ -753,25 +754,41 @@ Please select one answer:""",
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,
@@ -781,14 +798,14 @@ Please select one answer:""",
781
  answer_state,
782
  current_options
783
  ],
784
- outputs={
785
- current_question_idx: "value",
786
- answer_state: "value",
787
- question_display: "value",
788
- current_options: "value",
789
- question_counter: "value",
790
- question_box: "visible"
791
- }
792
  )
793
 
794
  next_btn.click(
@@ -799,14 +816,14 @@ Please select one answer:""",
799
  answer_state,
800
  current_options
801
  ],
802
- outputs={
803
- current_question_idx: "value",
804
- answer_state: "value",
805
- question_display: "value",
806
- current_options: "value",
807
- question_counter: "value",
808
- question_box: "visible"
809
- }
810
  )
811
 
812
  # Update answer state when radio button changes
@@ -823,22 +840,33 @@ Please select one answer:""",
823
  )
824
 
825
  # Submission handler
 
 
 
 
 
 
 
 
 
 
 
826
  submit_btn.click(
827
- fn=on_submit,
828
  inputs=[
829
  current_questions,
830
  answer_state,
831
  current_question_idx,
832
  current_options
833
  ],
834
- outputs={
835
- feedback_box: "value",
836
- results_group: "visible",
837
- score_display: "value",
838
- result_message: "value",
839
- question_box: "visible",
840
- tabs: "selected"
841
- }
842
  )
843
 
844
  # Certificate generation
 
615
  )
616
  certificate_display = gr.Image(label="Your Certificate")
617
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
618
 
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
+ "", # question_display
624
+ gr.update(choices=[], visible=False), # current_options
625
+ "", # question_counter
626
+ gr.update(visible=False), # question_box
627
+ [], # current_questions
628
+ 0, # current_question_idx
629
+ [None] * 5, # answer_state
630
+ gr.update(selected=1), # tabs
631
+ gr.update(visible=False) # results_group
632
+ ]
633
+
634
+ # Initialize first question
635
+ initial_answers = [None] * len(questions)
636
+ question = questions[0]
637
+
638
+ return [
639
+ f"""## Question 1
640
+ {question.question}
641
+
642
+ Please select one answer:""", # question_display
643
+ gr.update(
644
+ choices=question.options,
645
+ value=None,
646
+ visible=True,
647
+ label="Select your answer for Question 1:"
648
+ ), # current_options
649
+ f"Question 1 of {len(questions)}", # question_counter
650
+ gr.update(visible=True), # question_box
651
+ questions, # current_questions
652
+ 0, # current_question_idx
653
+ initial_answers, # answer_state
654
+ gr.update(selected=1), # tabs
655
+ gr.update(visible=False) # results_group
656
+ ]
657
+
658
+ def navigate(direction, current_idx, questions, answers, current_answer):
659
+ """
660
+ Handle navigation between questions
661
+ """
662
+ if not questions: # No questions available
663
+ return [
664
+ 0, # current_question_idx
665
+ answers, # answer_state
666
+ "", # question_display
667
+ gr.update(choices=[], value=None, visible=False), # current_options
668
+ "", # question_counter
669
+ gr.update(visible=False) # question_box
670
+ ]
671
+
672
+ # Save current answer
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
+ # Calculate new index
678
+ new_idx = max(0, min(len(questions) - 1, current_idx + direction))
679
+
680
+ # Get current question
681
+ question = questions[new_idx]
682
+
683
+ return [
684
+ new_idx, # current_question_idx
685
+ new_answers, # answer_state
686
+ f"""## Question {new_idx + 1}
687
+ {question.question}
688
+
689
+ Please select one answer:""", # question_display
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
  def on_submit(questions, answers, current_idx, current_answer):
701
  # Update the current answer in the answers list
 
754
  generate_btn.click(
755
  fn=on_generate_questions,
756
  inputs=[text_input, num_questions],
757
+ outputs=[
758
+ question_display,
759
+ current_options,
760
+ question_counter,
761
+ question_box,
762
+ current_questions,
763
+ current_question_idx,
764
+ answer_state,
765
+ tabs,
766
+ results_group
767
+ ]
768
  )
769
 
770
  # Navigation event handlers
771
  def handle_prev(current_idx, questions, answers, current_answer):
772
+ result = navigate(-1, current_idx, questions, answers, current_answer)
773
+ return [
774
+ result[current_question_idx],
775
+ result[answer_state],
776
+ result[question_display],
777
+ result[current_options],
778
+ result[question_counter],
779
+ result[question_box]
780
+ ]
781
 
782
  def handle_next(current_idx, questions, answers, current_answer):
783
+ result = navigate(1, current_idx, questions, answers, current_answer)
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,
 
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(
 
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
 
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