Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -641,100 +641,47 @@ Please select one answer:""",
|
|
| 641 |
}
|
| 642 |
|
| 643 |
def navigate(direction, current_idx, questions, answers, current_answer):
|
| 644 |
-
|
| 645 |
-
|
| 646 |
-
|
| 647 |
-
|
| 648 |
-
|
| 649 |
-
# Calculate new index
|
| 650 |
-
new_idx = max(0, min(len(questions) - 1, current_idx + direction))
|
| 651 |
-
display_updates = update_question_display(questions, new_idx, new_answers)
|
| 652 |
-
|
| 653 |
-
return (
|
| 654 |
-
new_idx,
|
| 655 |
-
new_answers,
|
| 656 |
-
*display_updates.values()
|
| 657 |
-
)
|
| 658 |
-
|
| 659 |
-
def on_generate_questions(text, num_questions):
|
| 660 |
-
success, questions = quiz_app.generate_questions(text, num_questions)
|
| 661 |
-
if not success:
|
| 662 |
return {
|
|
|
|
|
|
|
| 663 |
question_display: "",
|
| 664 |
-
current_options: gr.update(choices=[], visible=False),
|
| 665 |
question_counter: "",
|
| 666 |
-
question_box: gr.update(visible=False)
|
| 667 |
-
current_questions: [],
|
| 668 |
-
current_question_idx: 0,
|
| 669 |
-
answer_state: [None] * 5,
|
| 670 |
-
tabs: gr.update(selected=1),
|
| 671 |
-
results_group: gr.update(visible=False)
|
| 672 |
-
}
|
| 673 |
-
|
| 674 |
-
# Initialize first question
|
| 675 |
-
initial_answers = [None] * len(questions)
|
| 676 |
-
return {
|
| 677 |
-
**update_question_display(questions, 0, initial_answers),
|
| 678 |
-
current_questions: questions,
|
| 679 |
-
current_question_idx: 0,
|
| 680 |
-
answer_state: initial_answers,
|
| 681 |
-
tabs: gr.update(selected=1),
|
| 682 |
-
results_group: gr.update(visible=False)
|
| 683 |
-
}
|
| 684 |
-
|
| 685 |
-
def on_submit(questions, answers, current_idx, current_answer):
|
| 686 |
-
# Update the current answer in the answers list
|
| 687 |
-
final_answers = list(answers)
|
| 688 |
-
if 0 <= current_idx < len(final_answers):
|
| 689 |
-
final_answers[current_idx] = current_answer
|
| 690 |
-
|
| 691 |
-
# Validate all answers are provided
|
| 692 |
-
if not all(a is not None for a in final_answers[:len(questions)]):
|
| 693 |
-
return {
|
| 694 |
-
feedback_box: "⚠️ Please answer all questions before submitting.",
|
| 695 |
-
results_group: gr.update(visible=True),
|
| 696 |
-
score_display: 0,
|
| 697 |
-
result_message: "",
|
| 698 |
-
question_box: gr.update(visible=True),
|
| 699 |
-
tabs: gr.update(selected=1)
|
| 700 |
}
|
| 701 |
|
| 702 |
-
|
|
|
|
|
|
|
|
|
|
| 703 |
|
| 704 |
-
#
|
| 705 |
-
|
| 706 |
-
for i, (q, f) in enumerate(zip(questions, feedback)):
|
| 707 |
-
color = "green" if f.is_correct else "red"
|
| 708 |
-
symbol = "✅" if f.is_correct else "❌"
|
| 709 |
-
feedback_html += f"""
|
| 710 |
-
### Question {i+1}
|
| 711 |
-
{q.question}
|
| 712 |
-
|
| 713 |
-
<div style="color: {color}; padding: 10px; margin: 5px 0; border-left: 3px solid {color};">
|
| 714 |
-
{symbol} Your answer: {f.selected or "No answer"}
|
| 715 |
-
{'' if f.is_correct else f'<br>Correct answer: {f.correct_answer}'}
|
| 716 |
-
</div>
|
| 717 |
-
"""
|
| 718 |
|
| 719 |
-
|
| 720 |
-
|
| 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 |
-
|
| 731 |
-
|
| 732 |
-
|
| 733 |
-
|
| 734 |
-
|
| 735 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 736 |
}
|
| 737 |
-
|
| 738 |
# Event handlers
|
| 739 |
generate_btn.click(
|
| 740 |
fn=on_generate_questions,
|
|
@@ -751,53 +698,63 @@ Please select one answer:""",
|
|
| 751 |
results_group
|
| 752 |
]
|
| 753 |
)
|
| 754 |
-
|
| 755 |
-
# Navigation event handlers
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 756 |
prev_btn.click(
|
| 757 |
-
fn=
|
| 758 |
inputs=[
|
| 759 |
-
-1,
|
| 760 |
current_question_idx,
|
| 761 |
current_questions,
|
| 762 |
answer_state,
|
| 763 |
current_options
|
| 764 |
],
|
| 765 |
-
outputs=
|
| 766 |
-
current_question_idx,
|
| 767 |
-
answer_state,
|
| 768 |
-
question_display,
|
| 769 |
-
current_options,
|
| 770 |
-
question_counter,
|
| 771 |
-
question_box
|
| 772 |
-
|
| 773 |
)
|
| 774 |
-
|
| 775 |
next_btn.click(
|
| 776 |
-
fn=
|
| 777 |
inputs=[
|
| 778 |
-
1,
|
| 779 |
current_question_idx,
|
| 780 |
current_questions,
|
| 781 |
answer_state,
|
| 782 |
current_options
|
| 783 |
],
|
| 784 |
-
outputs=
|
| 785 |
-
current_question_idx,
|
| 786 |
-
answer_state,
|
| 787 |
-
question_display,
|
| 788 |
-
current_options,
|
| 789 |
-
question_counter,
|
| 790 |
-
question_box
|
| 791 |
-
|
| 792 |
)
|
| 793 |
-
|
| 794 |
# Update answer state when radio button changes
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 795 |
current_options.change(
|
| 796 |
-
|
| 797 |
inputs=[current_options, current_question_idx, answer_state],
|
| 798 |
-
outputs=
|
| 799 |
)
|
| 800 |
-
|
| 801 |
# Submission handler
|
| 802 |
submit_btn.click(
|
| 803 |
fn=on_submit,
|
|
@@ -807,16 +764,16 @@ Please select one answer:""",
|
|
| 807 |
current_question_idx,
|
| 808 |
current_options
|
| 809 |
],
|
| 810 |
-
outputs=
|
| 811 |
-
feedback_box,
|
| 812 |
-
results_group,
|
| 813 |
-
score_display,
|
| 814 |
-
result_message,
|
| 815 |
-
question_box,
|
| 816 |
-
tabs
|
| 817 |
-
|
| 818 |
)
|
| 819 |
-
|
| 820 |
# Certificate generation
|
| 821 |
score_display.change(
|
| 822 |
fn=quiz_app.certificate_generator.generate,
|
|
@@ -824,7 +781,8 @@ Please select one answer:""",
|
|
| 824 |
outputs=certificate_display
|
| 825 |
)
|
| 826 |
|
| 827 |
-
|
|
|
|
| 828 |
|
| 829 |
if __name__ == "__main__":
|
| 830 |
demo = create_quiz_interface()
|
|
|
|
| 641 |
}
|
| 642 |
|
| 643 |
def navigate(direction, current_idx, questions, answers, current_answer):
|
| 644 |
+
"""
|
| 645 |
+
Handle navigation between questions
|
| 646 |
+
"""
|
| 647 |
+
if not questions: # No questions available
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 648 |
return {
|
| 649 |
+
current_question_idx: 0,
|
| 650 |
+
answer_state: answers,
|
| 651 |
question_display: "",
|
| 652 |
+
current_options: gr.update(choices=[], value=None, visible=False),
|
| 653 |
question_counter: "",
|
| 654 |
+
question_box: gr.update(visible=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 655 |
}
|
| 656 |
|
| 657 |
+
# Save current answer
|
| 658 |
+
new_answers = list(answers)
|
| 659 |
+
if current_answer and 0 <= current_idx < len(new_answers):
|
| 660 |
+
new_answers[current_idx] = current_answer
|
| 661 |
|
| 662 |
+
# Calculate new index
|
| 663 |
+
new_idx = max(0, min(len(questions) - 1, current_idx + direction))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 664 |
|
| 665 |
+
# Get current question
|
| 666 |
+
question = questions[new_idx]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 667 |
|
| 668 |
return {
|
| 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,
|
| 678 |
+
visible=True,
|
| 679 |
+
label=f"Select your answer for Question {new_idx + 1}:"
|
| 680 |
+
),
|
| 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,
|
|
|
|
| 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=[
|
|
|
|
| 712 |
current_question_idx,
|
| 713 |
current_questions,
|
| 714 |
answer_state,
|
| 715 |
current_options
|
| 716 |
],
|
| 717 |
+
outputs={
|
| 718 |
+
current_question_idx: "value",
|
| 719 |
+
answer_state: "value",
|
| 720 |
+
question_display: "value",
|
| 721 |
+
current_options: "value",
|
| 722 |
+
question_counter: "value",
|
| 723 |
+
question_box: "visible"
|
| 724 |
+
}
|
| 725 |
)
|
| 726 |
+
|
| 727 |
next_btn.click(
|
| 728 |
+
fn=handle_next,
|
| 729 |
inputs=[
|
|
|
|
| 730 |
current_question_idx,
|
| 731 |
current_questions,
|
| 732 |
answer_state,
|
| 733 |
current_options
|
| 734 |
],
|
| 735 |
+
outputs={
|
| 736 |
+
current_question_idx: "value",
|
| 737 |
+
answer_state: "value",
|
| 738 |
+
question_display: "value",
|
| 739 |
+
current_options: "value",
|
| 740 |
+
question_counter: "value",
|
| 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,
|
|
|
|
| 764 |
current_question_idx,
|
| 765 |
current_options
|
| 766 |
],
|
| 767 |
+
outputs={
|
| 768 |
+
feedback_box: "value",
|
| 769 |
+
results_group: "visible",
|
| 770 |
+
score_display: "value",
|
| 771 |
+
result_message: "value",
|
| 772 |
+
question_box: "visible",
|
| 773 |
+
tabs: "selected"
|
| 774 |
+
}
|
| 775 |
)
|
| 776 |
+
|
| 777 |
# Certificate generation
|
| 778 |
score_display.change(
|
| 779 |
fn=quiz_app.certificate_generator.generate,
|
|
|
|
| 781 |
outputs=certificate_display
|
| 782 |
)
|
| 783 |
|
| 784 |
+
return demo
|
| 785 |
+
|
| 786 |
|
| 787 |
if __name__ == "__main__":
|
| 788 |
demo = create_quiz_interface()
|