Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -594,7 +594,7 @@ def create_quiz_interface():
|
|
| 594 |
generate_btn = gr.Button("Generate Assessment", variant="primary", size="lg")
|
| 595 |
|
| 596 |
# Assessment Tab
|
| 597 |
-
with gr.Tab(id=2, label="📝 Step 2: Take Assessment")
|
| 598 |
with gr.Column() as main_container:
|
| 599 |
# Questions Section
|
| 600 |
with gr.Column(visible=True) as question_box:
|
|
@@ -610,16 +610,20 @@ def create_quiz_interface():
|
|
| 610 |
question_counter = gr.Markdown("Question 1")
|
| 611 |
next_btn = gr.Button("Next →", variant="secondary", size="sm")
|
| 612 |
|
|
|
|
|
|
|
| 613 |
submit_btn = gr.Button(
|
| 614 |
"Submit Assessment",
|
| 615 |
variant="primary",
|
| 616 |
size="lg"
|
| 617 |
)
|
| 618 |
|
| 619 |
-
# Results Section
|
| 620 |
with gr.Column(visible=False) as results_group:
|
| 621 |
feedback_box = gr.Markdown("")
|
| 622 |
-
|
|
|
|
|
|
|
| 623 |
reset_btn = gr.Button(
|
| 624 |
"Reset Quiz",
|
| 625 |
variant="secondary",
|
|
@@ -635,27 +639,25 @@ def create_quiz_interface():
|
|
| 635 |
|
| 636 |
# Certification Tab
|
| 637 |
with gr.Tab(id=3, label="🎓 Step 3: Get Certified"):
|
| 638 |
-
|
| 639 |
-
|
| 640 |
-
|
| 641 |
-
|
| 642 |
-
|
| 643 |
-
|
| 644 |
-
|
| 645 |
-
|
| 646 |
# Helper Functions
|
| 647 |
def on_generate_questions(text, num_questions):
|
| 648 |
-
"""Generate quiz questions and setup initial state"""
|
| 649 |
if not text.strip():
|
| 650 |
return [
|
| 651 |
-
|
| 652 |
gr.update(visible=False),
|
| 653 |
gr.update(choices=[], visible=False),
|
| 654 |
"",
|
| 655 |
[],
|
| 656 |
0,
|
| 657 |
[None] * 5,
|
| 658 |
-
gr.
|
| 659 |
gr.update(visible=False),
|
| 660 |
gr.update(visible=False)
|
| 661 |
]
|
|
@@ -663,25 +665,24 @@ def create_quiz_interface():
|
|
| 663 |
success, questions = quiz_app.generate_questions(text, num_questions)
|
| 664 |
if not success or not questions:
|
| 665 |
return [
|
| 666 |
-
|
| 667 |
gr.update(visible=False),
|
| 668 |
gr.update(choices=[], visible=False),
|
| 669 |
"",
|
| 670 |
[],
|
| 671 |
0,
|
| 672 |
[None] * 5,
|
| 673 |
-
gr.
|
| 674 |
gr.update(visible=False),
|
| 675 |
gr.update(visible=False)
|
| 676 |
]
|
| 677 |
|
| 678 |
-
# Setup initial question
|
| 679 |
question = questions[0]
|
| 680 |
-
|
| 681 |
-
|
| 682 |
|
| 683 |
return [
|
| 684 |
-
|
| 685 |
gr.update(visible=True),
|
| 686 |
gr.update(
|
| 687 |
choices=question.options,
|
|
@@ -693,47 +694,38 @@ def create_quiz_interface():
|
|
| 693 |
questions,
|
| 694 |
0,
|
| 695 |
[None] * len(questions),
|
| 696 |
-
gr.
|
| 697 |
gr.update(visible=False),
|
| 698 |
gr.update(visible=False)
|
| 699 |
]
|
| 700 |
|
| 701 |
def navigate(direction, current_idx, questions, answers, current_answer):
|
| 702 |
-
"""Handle navigation between questions"""
|
| 703 |
if not questions:
|
| 704 |
return [0, answers, "", gr.update(choices=[], visible=False), "", gr.update(visible=False)]
|
| 705 |
|
| 706 |
-
# Update current answer in state
|
| 707 |
new_answers = list(answers)
|
| 708 |
if current_answer is not None and 0 <= current_idx < len(new_answers):
|
| 709 |
new_answers[current_idx] = current_answer
|
| 710 |
|
| 711 |
-
# Calculate new index
|
| 712 |
new_idx = max(0, min(len(questions) - 1, current_idx + direction))
|
| 713 |
question = questions[new_idx]
|
| 714 |
|
| 715 |
-
|
| 716 |
-
|
| 717 |
-
{question.question}"""
|
| 718 |
|
| 719 |
return [
|
| 720 |
new_idx,
|
| 721 |
new_answers,
|
| 722 |
-
|
| 723 |
gr.update(
|
| 724 |
choices=question.options,
|
| 725 |
value=new_answers[new_idx] if new_idx < len(new_answers) else None,
|
| 726 |
visible=True,
|
| 727 |
-
label=
|
| 728 |
),
|
| 729 |
f"Question {new_idx + 1} of {len(questions)}",
|
| 730 |
gr.update(visible=True)
|
| 731 |
]
|
| 732 |
-
def handle_prev(current_idx, questions, answers, current_answer):
|
| 733 |
-
return navigate(-1, current_idx, questions, answers, current_answer)
|
| 734 |
-
|
| 735 |
-
def handle_next(current_idx, questions, answers, current_answer):
|
| 736 |
-
return navigate(1, current_idx, questions, answers, current_answer)
|
| 737 |
|
| 738 |
def update_answer_state(answer, idx, current_answers):
|
| 739 |
new_answers = list(current_answers)
|
|
@@ -747,7 +739,7 @@ def create_quiz_interface():
|
|
| 747 |
|
| 748 |
def view_certificate():
|
| 749 |
"""Navigate to certificate tab"""
|
| 750 |
-
return gr.
|
| 751 |
|
| 752 |
def on_submit(questions, answers, current_idx, current_answer):
|
| 753 |
"""Handle quiz submission with proper Markdown rendering"""
|
|
@@ -812,6 +804,7 @@ def create_quiz_interface():
|
|
| 812 |
gr.update(visible=True), # back_to_assessment
|
| 813 |
gr.update(visible=False) # profile_tab
|
| 814 |
]
|
|
|
|
| 815 |
# Event Handlers
|
| 816 |
generate_btn.click(
|
| 817 |
fn=on_generate_questions,
|
|
@@ -831,13 +824,13 @@ def create_quiz_interface():
|
|
| 831 |
)
|
| 832 |
|
| 833 |
prev_btn.click(
|
| 834 |
-
fn=
|
| 835 |
inputs=[current_question_idx, current_questions, answer_state, current_options],
|
| 836 |
outputs=[current_question_idx, answer_state, question_display, current_options, question_counter, question_box]
|
| 837 |
)
|
| 838 |
|
| 839 |
next_btn.click(
|
| 840 |
-
fn=
|
| 841 |
inputs=[current_question_idx, current_questions, answer_state, current_options],
|
| 842 |
outputs=[current_question_idx, answer_state, question_display, current_options, question_counter, question_box]
|
| 843 |
)
|
|
@@ -859,7 +852,7 @@ def create_quiz_interface():
|
|
| 859 |
)
|
| 860 |
|
| 861 |
reset_btn.click(
|
| 862 |
-
fn=
|
| 863 |
inputs=[text_input, num_questions],
|
| 864 |
outputs=[
|
| 865 |
question_display,
|
|
@@ -876,16 +869,22 @@ def create_quiz_interface():
|
|
| 876 |
)
|
| 877 |
|
| 878 |
view_cert_btn.click(
|
| 879 |
-
fn=lambda: gr.
|
| 880 |
outputs=tabs
|
| 881 |
)
|
| 882 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 883 |
score_display.change(
|
| 884 |
fn=lambda s, n, c, l, p: quiz_app.certificate_generator.generate(s, n, c, l, p) or gr.update(value=None),
|
| 885 |
inputs=[score_display, name, course_name, company_logo, participant_photo],
|
| 886 |
outputs=certificate_display
|
| 887 |
)
|
| 888 |
-
|
| 889 |
return demo
|
| 890 |
|
| 891 |
if __name__ == "__main__":
|
|
|
|
| 594 |
generate_btn = gr.Button("Generate Assessment", variant="primary", size="lg")
|
| 595 |
|
| 596 |
# Assessment Tab
|
| 597 |
+
with gr.Tab(id=2, label="📝 Step 2: Take Assessment"):
|
| 598 |
with gr.Column() as main_container:
|
| 599 |
# Questions Section
|
| 600 |
with gr.Column(visible=True) as question_box:
|
|
|
|
| 610 |
question_counter = gr.Markdown("Question 1")
|
| 611 |
next_btn = gr.Button("Next →", variant="secondary", size="sm")
|
| 612 |
|
| 613 |
+
gr.Markdown("---") # Separator
|
| 614 |
+
|
| 615 |
submit_btn = gr.Button(
|
| 616 |
"Submit Assessment",
|
| 617 |
variant="primary",
|
| 618 |
size="lg"
|
| 619 |
)
|
| 620 |
|
| 621 |
+
# Results Section in Assessment Tab
|
| 622 |
with gr.Column(visible=False) as results_group:
|
| 623 |
feedback_box = gr.Markdown("")
|
| 624 |
+
gr.Markdown("---") # Separator
|
| 625 |
+
with gr.Row(equal_height=True):
|
| 626 |
+
# Only one of these buttons will be visible based on pass/fail
|
| 627 |
reset_btn = gr.Button(
|
| 628 |
"Reset Quiz",
|
| 629 |
variant="secondary",
|
|
|
|
| 639 |
|
| 640 |
# Certification Tab
|
| 641 |
with gr.Tab(id=3, label="🎓 Step 3: Get Certified"):
|
| 642 |
+
score_display = gr.Number(label="Your Score", visible=False)
|
| 643 |
+
course_name = gr.Textbox(
|
| 644 |
+
label="Certification Title",
|
| 645 |
+
value="Professional Assessment Certification"
|
| 646 |
+
)
|
| 647 |
+
certificate_display = gr.Image(label="Your Certificate")
|
| 648 |
+
|
|
|
|
| 649 |
# Helper Functions
|
| 650 |
def on_generate_questions(text, num_questions):
|
|
|
|
| 651 |
if not text.strip():
|
| 652 |
return [
|
| 653 |
+
"",
|
| 654 |
gr.update(visible=False),
|
| 655 |
gr.update(choices=[], visible=False),
|
| 656 |
"",
|
| 657 |
[],
|
| 658 |
0,
|
| 659 |
[None] * 5,
|
| 660 |
+
gr.update(selected=1),
|
| 661 |
gr.update(visible=False),
|
| 662 |
gr.update(visible=False)
|
| 663 |
]
|
|
|
|
| 665 |
success, questions = quiz_app.generate_questions(text, num_questions)
|
| 666 |
if not success or not questions:
|
| 667 |
return [
|
| 668 |
+
"",
|
| 669 |
gr.update(visible=False),
|
| 670 |
gr.update(choices=[], visible=False),
|
| 671 |
"",
|
| 672 |
[],
|
| 673 |
0,
|
| 674 |
[None] * 5,
|
| 675 |
+
gr.update(selected=1),
|
| 676 |
gr.update(visible=False),
|
| 677 |
gr.update(visible=False)
|
| 678 |
]
|
| 679 |
|
|
|
|
| 680 |
question = questions[0]
|
| 681 |
+
question_md = f"""### Question 1
|
| 682 |
+
{question.question}"""
|
| 683 |
|
| 684 |
return [
|
| 685 |
+
question_md,
|
| 686 |
gr.update(visible=True),
|
| 687 |
gr.update(
|
| 688 |
choices=question.options,
|
|
|
|
| 694 |
questions,
|
| 695 |
0,
|
| 696 |
[None] * len(questions),
|
| 697 |
+
gr.update(selected=2),
|
| 698 |
gr.update(visible=False),
|
| 699 |
gr.update(visible=False)
|
| 700 |
]
|
| 701 |
|
| 702 |
def navigate(direction, current_idx, questions, answers, current_answer):
|
|
|
|
| 703 |
if not questions:
|
| 704 |
return [0, answers, "", gr.update(choices=[], visible=False), "", gr.update(visible=False)]
|
| 705 |
|
|
|
|
| 706 |
new_answers = list(answers)
|
| 707 |
if current_answer is not None and 0 <= current_idx < len(new_answers):
|
| 708 |
new_answers[current_idx] = current_answer
|
| 709 |
|
|
|
|
| 710 |
new_idx = max(0, min(len(questions) - 1, current_idx + direction))
|
| 711 |
question = questions[new_idx]
|
| 712 |
|
| 713 |
+
question_md = f"""### Question {new_idx + 1}
|
| 714 |
+
{question.question}"""
|
|
|
|
| 715 |
|
| 716 |
return [
|
| 717 |
new_idx,
|
| 718 |
new_answers,
|
| 719 |
+
question_md,
|
| 720 |
gr.update(
|
| 721 |
choices=question.options,
|
| 722 |
value=new_answers[new_idx] if new_idx < len(new_answers) else None,
|
| 723 |
visible=True,
|
| 724 |
+
label="Select your answer:"
|
| 725 |
),
|
| 726 |
f"Question {new_idx + 1} of {len(questions)}",
|
| 727 |
gr.update(visible=True)
|
| 728 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 729 |
|
| 730 |
def update_answer_state(answer, idx, current_answers):
|
| 731 |
new_answers = list(current_answers)
|
|
|
|
| 739 |
|
| 740 |
def view_certificate():
|
| 741 |
"""Navigate to certificate tab"""
|
| 742 |
+
return gr.update(selected=3)
|
| 743 |
|
| 744 |
def on_submit(questions, answers, current_idx, current_answer):
|
| 745 |
"""Handle quiz submission with proper Markdown rendering"""
|
|
|
|
| 804 |
gr.update(visible=True), # back_to_assessment
|
| 805 |
gr.update(visible=False) # profile_tab
|
| 806 |
]
|
| 807 |
+
|
| 808 |
# Event Handlers
|
| 809 |
generate_btn.click(
|
| 810 |
fn=on_generate_questions,
|
|
|
|
| 824 |
)
|
| 825 |
|
| 826 |
prev_btn.click(
|
| 827 |
+
fn=handle_prev,
|
| 828 |
inputs=[current_question_idx, current_questions, answer_state, current_options],
|
| 829 |
outputs=[current_question_idx, answer_state, question_display, current_options, question_counter, question_box]
|
| 830 |
)
|
| 831 |
|
| 832 |
next_btn.click(
|
| 833 |
+
fn=handle_next,
|
| 834 |
inputs=[current_question_idx, current_questions, answer_state, current_options],
|
| 835 |
outputs=[current_question_idx, answer_state, question_display, current_options, question_counter, question_box]
|
| 836 |
)
|
|
|
|
| 852 |
)
|
| 853 |
|
| 854 |
reset_btn.click(
|
| 855 |
+
fn=reset_quiz,
|
| 856 |
inputs=[text_input, num_questions],
|
| 857 |
outputs=[
|
| 858 |
question_display,
|
|
|
|
| 869 |
)
|
| 870 |
|
| 871 |
view_cert_btn.click(
|
| 872 |
+
fn=lambda: gr.update(selected=3),
|
| 873 |
outputs=tabs
|
| 874 |
)
|
| 875 |
|
| 876 |
+
current_options.change(
|
| 877 |
+
fn=update_answer_state,
|
| 878 |
+
inputs=[current_options, current_question_idx, answer_state],
|
| 879 |
+
outputs=answer_state
|
| 880 |
+
)
|
| 881 |
+
|
| 882 |
score_display.change(
|
| 883 |
fn=lambda s, n, c, l, p: quiz_app.certificate_generator.generate(s, n, c, l, p) or gr.update(value=None),
|
| 884 |
inputs=[score_display, name, course_name, company_logo, participant_photo],
|
| 885 |
outputs=certificate_display
|
| 886 |
)
|
| 887 |
+
|
| 888 |
return demo
|
| 889 |
|
| 890 |
if __name__ == "__main__":
|