Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -537,6 +537,7 @@ def create_quiz_interface():
|
|
| 537 |
# State management
|
| 538 |
current_questions = gr.State([])
|
| 539 |
current_question_idx = gr.State(0)
|
|
|
|
| 540 |
|
| 541 |
# Header
|
| 542 |
gr.Markdown("""
|
|
@@ -573,14 +574,14 @@ def create_quiz_interface():
|
|
| 573 |
|
| 574 |
# Assessment Tab
|
| 575 |
with gr.Tab("📝 Step 2: Take Assessment") as assessment_tab:
|
| 576 |
-
with gr.Box(visible=True):
|
| 577 |
# Question display
|
| 578 |
question_display = gr.Markdown("", label="Current Question")
|
| 579 |
|
| 580 |
# Single radio group for current question
|
| 581 |
current_options = gr.Radio(
|
| 582 |
choices=[],
|
| 583 |
-
label="Select your answer",
|
| 584 |
visible=False
|
| 585 |
)
|
| 586 |
|
|
@@ -590,6 +591,8 @@ def create_quiz_interface():
|
|
| 590 |
question_counter = gr.Markdown("Question 1 of 3")
|
| 591 |
next_btn = gr.Button("Next →", variant="secondary", size="sm")
|
| 592 |
|
|
|
|
|
|
|
| 593 |
submit_btn = gr.Button("Submit Assessment", variant="primary", size="lg")
|
| 594 |
|
| 595 |
# Results section
|
|
@@ -606,27 +609,47 @@ def create_quiz_interface():
|
|
| 606 |
)
|
| 607 |
certificate_display = gr.Image(label="Your Certificate")
|
| 608 |
|
| 609 |
-
|
| 610 |
-
def update_question_display(questions, idx):
|
| 611 |
if not questions or idx >= len(questions):
|
| 612 |
return {
|
| 613 |
question_display: "",
|
| 614 |
current_options: gr.update(choices=[], value=None, visible=False),
|
| 615 |
-
question_counter: ""
|
|
|
|
| 616 |
}
|
| 617 |
|
| 618 |
question = questions[idx]
|
| 619 |
return {
|
| 620 |
-
question_display: f"
|
| 621 |
-
|
| 622 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 623 |
}
|
| 624 |
|
| 625 |
-
def navigate(direction, current_idx, questions):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 626 |
new_idx = max(0, min(len(questions) - 1, current_idx + direction))
|
| 627 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 628 |
|
| 629 |
-
# Event handlers
|
| 630 |
def on_generate_questions(text, num_questions):
|
| 631 |
success, questions = quiz_app.generate_questions(text, num_questions)
|
| 632 |
if not success:
|
|
@@ -634,58 +657,43 @@ def create_quiz_interface():
|
|
| 634 |
question_display: "",
|
| 635 |
current_options: gr.update(choices=[], visible=False),
|
| 636 |
question_counter: "",
|
|
|
|
| 637 |
current_questions: [],
|
| 638 |
current_question_idx: 0,
|
| 639 |
-
|
|
|
|
|
|
|
| 640 |
}
|
| 641 |
|
| 642 |
# Initialize first question
|
|
|
|
| 643 |
return {
|
| 644 |
-
**update_question_display(questions, 0),
|
| 645 |
current_questions: questions,
|
| 646 |
current_question_idx: 0,
|
| 647 |
-
|
|
|
|
|
|
|
| 648 |
}
|
| 649 |
|
| 650 |
-
|
| 651 |
-
|
| 652 |
-
|
| 653 |
-
|
| 654 |
-
|
| 655 |
-
|
| 656 |
-
|
| 657 |
-
|
| 658 |
-
current_question_idx,
|
| 659 |
-
tabs
|
| 660 |
-
]
|
| 661 |
-
)
|
| 662 |
-
|
| 663 |
-
# Navigation event handlers
|
| 664 |
-
prev_btn.click(
|
| 665 |
-
fn=navigate,
|
| 666 |
-
inputs=[-1, current_question_idx, current_questions],
|
| 667 |
-
outputs=[current_question_idx, question_display, current_options, question_counter]
|
| 668 |
-
)
|
| 669 |
-
|
| 670 |
-
next_btn.click(
|
| 671 |
-
fn=navigate,
|
| 672 |
-
inputs=[1, current_question_idx, current_questions],
|
| 673 |
-
outputs=[current_question_idx, question_display, current_options, question_counter]
|
| 674 |
-
)
|
| 675 |
-
|
| 676 |
-
# Submission handler
|
| 677 |
-
def on_submit(questions, current_answer):
|
| 678 |
-
if not current_answer:
|
| 679 |
return {
|
| 680 |
feedback_box: "⚠️ Please answer all questions before submitting.",
|
| 681 |
-
results_group: gr.update(visible=
|
| 682 |
score_display: 0,
|
| 683 |
result_message: "",
|
|
|
|
| 684 |
tabs: gr.update(selected=1)
|
| 685 |
}
|
| 686 |
|
| 687 |
-
|
| 688 |
-
score, passed, feedback = quiz_app.calculate_score(answers)
|
| 689 |
|
| 690 |
# Generate feedback HTML
|
| 691 |
feedback_html = "# Assessment Results\n\n"
|
|
@@ -702,25 +710,113 @@ def create_quiz_interface():
|
|
| 702 |
</div>
|
| 703 |
"""
|
| 704 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 705 |
return {
|
| 706 |
feedback_box: feedback_html,
|
| 707 |
results_group: gr.update(visible=True),
|
| 708 |
score_display: score,
|
| 709 |
-
result_message:
|
|
|
|
| 710 |
tabs: gr.update(selected=2) if passed else gr.update()
|
| 711 |
}
|
| 712 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 713 |
submit_btn.click(
|
| 714 |
fn=on_submit,
|
| 715 |
-
inputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 716 |
outputs=[
|
| 717 |
feedback_box,
|
| 718 |
results_group,
|
| 719 |
score_display,
|
| 720 |
result_message,
|
|
|
|
| 721 |
tabs
|
| 722 |
]
|
| 723 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 724 |
|
| 725 |
return demo
|
| 726 |
|
|
|
|
| 537 |
# State management
|
| 538 |
current_questions = gr.State([])
|
| 539 |
current_question_idx = gr.State(0)
|
| 540 |
+
answer_state = gr.State([None] * 5) # Store all answers
|
| 541 |
|
| 542 |
# Header
|
| 543 |
gr.Markdown("""
|
|
|
|
| 574 |
|
| 575 |
# Assessment Tab
|
| 576 |
with gr.Tab("📝 Step 2: Take Assessment") as assessment_tab:
|
| 577 |
+
with gr.Box(visible=True) as question_box:
|
| 578 |
# Question display
|
| 579 |
question_display = gr.Markdown("", label="Current Question")
|
| 580 |
|
| 581 |
# Single radio group for current question
|
| 582 |
current_options = gr.Radio(
|
| 583 |
choices=[],
|
| 584 |
+
label="Select your answer:",
|
| 585 |
visible=False
|
| 586 |
)
|
| 587 |
|
|
|
|
| 591 |
question_counter = gr.Markdown("Question 1 of 3")
|
| 592 |
next_btn = gr.Button("Next →", variant="secondary", size="sm")
|
| 593 |
|
| 594 |
+
gr.Markdown("---") # Separator
|
| 595 |
+
|
| 596 |
submit_btn = gr.Button("Submit Assessment", variant="primary", size="lg")
|
| 597 |
|
| 598 |
# Results section
|
|
|
|
| 609 |
)
|
| 610 |
certificate_display = gr.Image(label="Your Certificate")
|
| 611 |
|
| 612 |
+
def update_question_display(questions, idx, current_answers):
|
|
|
|
| 613 |
if not questions or idx >= len(questions):
|
| 614 |
return {
|
| 615 |
question_display: "",
|
| 616 |
current_options: gr.update(choices=[], value=None, visible=False),
|
| 617 |
+
question_counter: "",
|
| 618 |
+
question_box: gr.update(visible=False)
|
| 619 |
}
|
| 620 |
|
| 621 |
question = questions[idx]
|
| 622 |
return {
|
| 623 |
+
question_display: f"""## Question {idx + 1}
|
| 624 |
+
{question.question}
|
| 625 |
+
|
| 626 |
+
Please select one answer:""",
|
| 627 |
+
current_options: gr.update(
|
| 628 |
+
choices=question.options,
|
| 629 |
+
value=current_answers[idx] if idx < len(current_answers) else None,
|
| 630 |
+
visible=True,
|
| 631 |
+
label=f"Select your answer for Question {idx + 1}:"
|
| 632 |
+
),
|
| 633 |
+
question_counter: f"Question {idx + 1} of {len(questions)}",
|
| 634 |
+
question_box: gr.update(visible=True)
|
| 635 |
}
|
| 636 |
|
| 637 |
+
def navigate(direction, current_idx, questions, answers, current_answer):
|
| 638 |
+
# Save current answer
|
| 639 |
+
new_answers = list(answers)
|
| 640 |
+
if 0 <= current_idx < len(new_answers):
|
| 641 |
+
new_answers[current_idx] = current_answer
|
| 642 |
+
|
| 643 |
+
# Calculate new index
|
| 644 |
new_idx = max(0, min(len(questions) - 1, current_idx + direction))
|
| 645 |
+
display_updates = update_question_display(questions, new_idx, new_answers)
|
| 646 |
+
|
| 647 |
+
return (
|
| 648 |
+
new_idx,
|
| 649 |
+
new_answers,
|
| 650 |
+
*display_updates.values()
|
| 651 |
+
)
|
| 652 |
|
|
|
|
| 653 |
def on_generate_questions(text, num_questions):
|
| 654 |
success, questions = quiz_app.generate_questions(text, num_questions)
|
| 655 |
if not success:
|
|
|
|
| 657 |
question_display: "",
|
| 658 |
current_options: gr.update(choices=[], visible=False),
|
| 659 |
question_counter: "",
|
| 660 |
+
question_box: gr.update(visible=False),
|
| 661 |
current_questions: [],
|
| 662 |
current_question_idx: 0,
|
| 663 |
+
answer_state: [None] * 5,
|
| 664 |
+
tabs: gr.update(selected=1),
|
| 665 |
+
results_group: gr.update(visible=False)
|
| 666 |
}
|
| 667 |
|
| 668 |
# Initialize first question
|
| 669 |
+
initial_answers = [None] * len(questions)
|
| 670 |
return {
|
| 671 |
+
**update_question_display(questions, 0, initial_answers),
|
| 672 |
current_questions: questions,
|
| 673 |
current_question_idx: 0,
|
| 674 |
+
answer_state: initial_answers,
|
| 675 |
+
tabs: gr.update(selected=1),
|
| 676 |
+
results_group: gr.update(visible=False)
|
| 677 |
}
|
| 678 |
|
| 679 |
+
def on_submit(questions, answers, current_idx, current_answer):
|
| 680 |
+
# Update the current answer in the answers list
|
| 681 |
+
final_answers = list(answers)
|
| 682 |
+
if 0 <= current_idx < len(final_answers):
|
| 683 |
+
final_answers[current_idx] = current_answer
|
| 684 |
+
|
| 685 |
+
# Validate all answers are provided
|
| 686 |
+
if not all(a is not None for a in final_answers[:len(questions)]):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 687 |
return {
|
| 688 |
feedback_box: "⚠️ Please answer all questions before submitting.",
|
| 689 |
+
results_group: gr.update(visible=True),
|
| 690 |
score_display: 0,
|
| 691 |
result_message: "",
|
| 692 |
+
question_box: gr.update(visible=True),
|
| 693 |
tabs: gr.update(selected=1)
|
| 694 |
}
|
| 695 |
|
| 696 |
+
score, passed, feedback = quiz_app.calculate_score(final_answers[:len(questions)])
|
|
|
|
| 697 |
|
| 698 |
# Generate feedback HTML
|
| 699 |
feedback_html = "# Assessment Results\n\n"
|
|
|
|
| 710 |
</div>
|
| 711 |
"""
|
| 712 |
|
| 713 |
+
result_msg = "🎉 Passed!" if passed else "Please try again"
|
| 714 |
+
if not passed:
|
| 715 |
+
feedback_html += f"""
|
| 716 |
+
<div style="background-color: #ffe6e6; padding: 20px; margin-top: 20px; border-radius: 10px;">
|
| 717 |
+
<h3 style="color: #cc0000;">Please Try Again</h3>
|
| 718 |
+
<p>Your score: {score:.1f}%</p>
|
| 719 |
+
<p>You need 80% or higher to pass and receive a certificate.</p>
|
| 720 |
+
</div>
|
| 721 |
+
"""
|
| 722 |
+
|
| 723 |
return {
|
| 724 |
feedback_box: feedback_html,
|
| 725 |
results_group: gr.update(visible=True),
|
| 726 |
score_display: score,
|
| 727 |
+
result_message: result_msg,
|
| 728 |
+
question_box: gr.update(visible=False),
|
| 729 |
tabs: gr.update(selected=2) if passed else gr.update()
|
| 730 |
}
|
| 731 |
|
| 732 |
+
# Event handlers
|
| 733 |
+
generate_btn.click(
|
| 734 |
+
fn=on_generate_questions,
|
| 735 |
+
inputs=[text_input, num_questions],
|
| 736 |
+
outputs=[
|
| 737 |
+
question_display,
|
| 738 |
+
current_options,
|
| 739 |
+
question_counter,
|
| 740 |
+
question_box,
|
| 741 |
+
current_questions,
|
| 742 |
+
current_question_idx,
|
| 743 |
+
answer_state,
|
| 744 |
+
tabs,
|
| 745 |
+
results_group
|
| 746 |
+
]
|
| 747 |
+
)
|
| 748 |
+
|
| 749 |
+
# Navigation event handlers
|
| 750 |
+
prev_btn.click(
|
| 751 |
+
fn=navigate,
|
| 752 |
+
inputs=[
|
| 753 |
+
-1,
|
| 754 |
+
current_question_idx,
|
| 755 |
+
current_questions,
|
| 756 |
+
answer_state,
|
| 757 |
+
current_options
|
| 758 |
+
],
|
| 759 |
+
outputs=[
|
| 760 |
+
current_question_idx,
|
| 761 |
+
answer_state,
|
| 762 |
+
question_display,
|
| 763 |
+
current_options,
|
| 764 |
+
question_counter,
|
| 765 |
+
question_box
|
| 766 |
+
]
|
| 767 |
+
)
|
| 768 |
+
|
| 769 |
+
next_btn.click(
|
| 770 |
+
fn=navigate,
|
| 771 |
+
inputs=[
|
| 772 |
+
1,
|
| 773 |
+
current_question_idx,
|
| 774 |
+
current_questions,
|
| 775 |
+
answer_state,
|
| 776 |
+
current_options
|
| 777 |
+
],
|
| 778 |
+
outputs=[
|
| 779 |
+
current_question_idx,
|
| 780 |
+
answer_state,
|
| 781 |
+
question_display,
|
| 782 |
+
current_options,
|
| 783 |
+
question_counter,
|
| 784 |
+
question_box
|
| 785 |
+
]
|
| 786 |
+
)
|
| 787 |
+
|
| 788 |
+
# Update answer state when radio button changes
|
| 789 |
+
current_options.change(
|
| 790 |
+
lambda a, i, old: [old[j] if j != i else a for j in range(len(old))],
|
| 791 |
+
inputs=[current_options, current_question_idx, answer_state],
|
| 792 |
+
outputs=[answer_state]
|
| 793 |
+
)
|
| 794 |
+
|
| 795 |
+
# Submission handler
|
| 796 |
submit_btn.click(
|
| 797 |
fn=on_submit,
|
| 798 |
+
inputs=[
|
| 799 |
+
current_questions,
|
| 800 |
+
answer_state,
|
| 801 |
+
current_question_idx,
|
| 802 |
+
current_options
|
| 803 |
+
],
|
| 804 |
outputs=[
|
| 805 |
feedback_box,
|
| 806 |
results_group,
|
| 807 |
score_display,
|
| 808 |
result_message,
|
| 809 |
+
question_box,
|
| 810 |
tabs
|
| 811 |
]
|
| 812 |
)
|
| 813 |
+
|
| 814 |
+
# Certificate generation
|
| 815 |
+
score_display.change(
|
| 816 |
+
fn=quiz_app.certificate_generator.generate,
|
| 817 |
+
inputs=[score_display, name, course_name, company_logo, participant_photo],
|
| 818 |
+
outputs=certificate_display
|
| 819 |
+
)
|
| 820 |
|
| 821 |
return demo
|
| 822 |
|