capradeepgujaran commited on
Commit
033eb0a
·
verified ·
1 Parent(s): 77a01c5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +145 -33
app.py CHANGED
@@ -570,8 +570,7 @@ def create_quiz_interface():
570
  participant_photo = gr.Image(label="Your Photo (Optional)", type="filepath")
571
 
572
  generate_btn = gr.Button("Generate Assessment", variant="primary", size="lg")
573
-
574
- # Assessment Tab
575
  with gr.Tab("📝 Step 2: Take Assessment") as assessment_tab:
576
  with gr.Column(visible=True) as question_box:
577
  # Question section
@@ -616,6 +615,15 @@ def create_quiz_interface():
616
  size="lg",
617
  visible=False
618
  )
 
 
 
 
 
 
 
 
 
619
 
620
  # Certification Tab
621
  with gr.Tab("🎓 Step 3: Get Certified"):
@@ -627,43 +635,147 @@ def create_quiz_interface():
627
  )
628
  certificate_display = gr.Image(label="Your Certificate")
629
  # Helper Functions
630
- def on_generate_questions(text, num_questions):
631
- success, questions = quiz_app.generate_questions(text, num_questions)
632
- if not success:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
633
  return [
634
- "",
635
- gr.update(choices=[], visible=False),
636
- "",
637
- gr.update(visible=False),
638
- [],
 
 
 
 
 
639
  0,
640
- [None] * 5,
641
- gr.update(selected=1),
642
  gr.update(visible=False),
643
  gr.update(visible=False) # view_cert_btn visibility
644
  ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
645
 
646
- initial_answers = [None] * len(questions)
647
- question = questions[0]
648
-
649
- return [
650
- f"## Question 1\n{question.question}\n\nPlease select one answer:",
651
- gr.update(
652
- choices=question.options,
653
- value=None,
654
- visible=True,
655
- label="Select your answer for Question 1:"
656
- ),
657
- f"Question 1 of {len(questions)}",
658
- gr.update(visible=True),
659
- questions,
660
- 0,
661
- initial_answers,
662
- gr.update(selected=1),
663
- gr.update(visible=False),
664
- gr.update(visible=False) # view_cert_btn visibility
665
- ]
666
-
667
  def navigate(direction, current_idx, questions, answers, current_answer):
668
  """Handle navigation between questions"""
669
  if not questions:
 
570
  participant_photo = gr.Image(label="Your Photo (Optional)", type="filepath")
571
 
572
  generate_btn = gr.Button("Generate Assessment", variant="primary", size="lg")
573
+
 
574
  with gr.Tab("📝 Step 2: Take Assessment") as assessment_tab:
575
  with gr.Column(visible=True) as question_box:
576
  # Question section
 
615
  size="lg",
616
  visible=False
617
  )
618
+ back_to_assessment = gr.Button(
619
+ "Back to Assessment",
620
+ variant="secondary",
621
+ size="lg",
622
+ visible=True
623
+ )
624
+
625
+
626
+
627
 
628
  # Certification Tab
629
  with gr.Tab("🎓 Step 3: Get Certified"):
 
635
  )
636
  certificate_display = gr.Image(label="Your Certificate")
637
  # Helper Functions
638
+
639
+ def on_generate_questions(text, num_questions):
640
+ success, questions = quiz_app.generate_questions(text, num_questions)
641
+ tab_index = 1 # Index for "Take Assessment" tab
642
+
643
+ if not success:
644
+ return [
645
+ "",
646
+ gr.update(choices=[], visible=False),
647
+ "",
648
+ gr.update(visible=False),
649
+ [],
650
+ 0,
651
+ [None] * 5,
652
+ gr.update(selected=tab_index), # Force navigation
653
+ gr.update(visible=False),
654
+ gr.update(visible=False) # view_cert_btn visibility
655
+ ]
656
+
657
+ initial_answers = [None] * len(questions)
658
+ question = questions[0]
659
+
660
  return [
661
+ f"## Question 1\n{question.question}\n\nPlease select one answer:",
662
+ gr.update(
663
+ choices=question.options,
664
+ value=None,
665
+ visible=True,
666
+ label="Select your answer for Question 1:"
667
+ ),
668
+ f"Question 1 of {len(questions)}",
669
+ gr.update(visible=True),
670
+ questions,
671
  0,
672
+ initial_answers,
673
+ gr.update(selected=tab_index), # Force navigation
674
  gr.update(visible=False),
675
  gr.update(visible=False) # view_cert_btn visibility
676
  ]
677
+
678
+ def on_submit(questions, answers, current_idx, current_answer):
679
+ final_answers = list(answers)
680
+ if 0 <= current_idx < len(final_answers):
681
+ final_answers[current_idx] = current_answer
682
+
683
+ if not all(a is not None for a in final_answers[:len(questions)]):
684
+ return [
685
+ "⚠️ Please answer all questions before submitting.",
686
+ gr.update(visible=True),
687
+ 0,
688
+ "",
689
+ gr.update(visible=True),
690
+ gr.update(selected=1),
691
+ gr.update(visible=False) # view_cert_btn visibility
692
+ ]
693
+
694
+ score, passed, feedback = quiz_app.calculate_score(final_answers[:len(questions)])
695
+
696
+ feedback_html = "# Assessment Results\n\n"
697
+ for i, (q, f) in enumerate(zip(questions, feedback)):
698
+ color = "green" if f.is_correct else "red"
699
+ symbol = "✅" if f.is_correct else "❌"
700
+ feedback_html += f"""### Question {i+1}
701
+ {q.question}
702
+
703
+ <div style="color: {color}; padding: 10px; margin: 5px 0; border-left: 3px solid {color};">
704
+ {symbol} Your answer: {f.selected or "No answer"}
705
+ {'' if f.is_correct else f'<br>Correct answer: {f.correct_answer}'}
706
+ </div>
707
+ """
708
+
709
+ result_msg = "🎉 Passed!" if passed else "Please try again"
710
+
711
+ # Add a message about viewing certificate if passed
712
+ if passed:
713
+ feedback_html += f"""
714
+ <div style="background-color: #e6ffe6; padding: 20px; margin-top: 20px; border-radius: 10px;">
715
+ <h3 style="color: #008000;">🎉 Congratulations!</h3>
716
+ <p>You passed with a score of {score:.1f}%</p>
717
+ <p>Click the "View Certificate" button below to see your certificate.</p>
718
+ </div>
719
+ """
720
+ else:
721
+ feedback_html += f"""
722
+ <div style="background-color: #ffe6e6; padding: 20px; margin-top: 20px; border-radius: 10px;">
723
+ <h3 style="color: #cc0000;">Please Try Again</h3>
724
+ <p>Your score: {score:.1f}%</p>
725
+ <p>You need 80% or higher to pass and receive a certificate.</p>
726
+ </div>
727
+ """
728
+
729
+ return [
730
+ feedback_html,
731
+ gr.update(visible=True),
732
+ score,
733
+ result_msg,
734
+ gr.update(visible=not passed),
735
+ gr.update(selected=1),
736
+ gr.update(visible=passed) # Show view certificate button only if passed
737
+ ]
738
+
739
+ def goto_take_assessment():
740
+ """Navigate to Take Assessment tab"""
741
+ return gr.update(selected=1)
742
+
743
+ def goto_certificate():
744
+ """Navigate to Get Certified tab"""
745
+ return gr.update(selected=2)
746
+
747
+ # Event handlers
748
+ generate_btn.click(
749
+ fn=on_generate_questions,
750
+ inputs=[text_input, num_questions],
751
+ outputs=[
752
+ question_display,
753
+ current_options,
754
+ question_counter,
755
+ question_box,
756
+ current_questions,
757
+ current_question_idx,
758
+ answer_state,
759
+ tabs,
760
+ results_group,
761
+ view_cert_btn
762
+ ]
763
+ )
764
+
765
+ # Fix View Certificate navigation
766
+ view_cert_btn.click(
767
+ fn=goto_certificate,
768
+ outputs=tabs
769
+ )
770
+
771
+ # Add "Back to Assessment" button in results view
772
+ back_to_assessment = gr.Button("Back to Assessment", visible=False)
773
+ back_to_assessment.click(
774
+ fn=goto_take_assessment,
775
+ outputs=tabs
776
+ )
777
 
778
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
779
  def navigate(direction, current_idx, questions, answers, current_answer):
780
  """Handle navigation between questions"""
781
  if not questions: