Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -963,14 +963,12 @@ def create_quiz_interface():
|
|
| 963 |
return navigate(1, current_idx, questions, answers, current_answer)
|
| 964 |
|
| 965 |
def on_submit(questions, answers, current_idx, current_answer):
|
| 966 |
-
"""Handle quiz submission with proper
|
| 967 |
final_answers = list(answers)
|
| 968 |
if 0 <= current_idx < len(final_answers):
|
| 969 |
final_answers[current_idx] = current_answer
|
| 970 |
|
| 971 |
-
# Check if all questions are answered
|
| 972 |
if not all(a is not None for a in final_answers[:len(questions)]):
|
| 973 |
-
# Create list of unanswered question numbers
|
| 974 |
unanswered = [i+1 for i, a in enumerate(final_answers[:len(questions)]) if a is None]
|
| 975 |
message = f"""
|
| 976 |
<div style="background-color: #fff3cd; padding: 20px; border-radius: 10px; border-left: 5px solid #ffa000;">
|
|
@@ -980,57 +978,55 @@ def create_quiz_interface():
|
|
| 980 |
</div>
|
| 981 |
"""
|
| 982 |
return [
|
| 983 |
-
message,
|
| 984 |
-
gr.update(visible=True),
|
| 985 |
-
0,
|
| 986 |
-
"⚠️ Please answer all questions",
|
| 987 |
-
gr.update(visible=True),
|
| 988 |
-
gr.update(visible=False),
|
| 989 |
-
gr.update(visible=False),
|
| 990 |
-
gr.update(selected=2)
|
| 991 |
]
|
| 992 |
|
| 993 |
-
# Rest of the submission logic remains the same
|
| 994 |
score, passed, feedback = quiz_app.calculate_score(final_answers[:len(questions)])
|
| 995 |
|
| 996 |
-
|
|
|
|
| 997 |
|
| 998 |
for i, (q, f) in enumerate(zip(questions, feedback)):
|
| 999 |
-
|
| 1000 |
-
|
|
|
|
|
|
|
| 1001 |
|
| 1002 |
-
|
| 1003 |
-
|
| 1004 |
-
|
| 1005 |
-
|
| 1006 |
-
|
| 1007 |
-
|
| 1008 |
-
"""
|
| 1009 |
|
| 1010 |
-
# Add summary
|
| 1011 |
if passed:
|
| 1012 |
feedback_content += f"""
|
| 1013 |
-
---
|
| 1014 |
## 🎉 Congratulations!
|
| 1015 |
You passed with a score of {score:.1f}%!
|
| 1016 |
"""
|
| 1017 |
else:
|
| 1018 |
feedback_content += f"""
|
| 1019 |
-
---
|
| 1020 |
## Need Improvement
|
| 1021 |
You scored {score:.1f}%. You need 80% or higher to pass.
|
| 1022 |
Please try again.
|
| 1023 |
"""
|
| 1024 |
|
| 1025 |
return [
|
| 1026 |
-
feedback_content,
|
| 1027 |
-
gr.update(visible=True),
|
| 1028 |
-
score,
|
| 1029 |
-
f"Score: {score:.1f}%",
|
| 1030 |
-
gr.update(visible=False),
|
| 1031 |
-
gr.update(visible=not passed),
|
| 1032 |
-
gr.update(visible=passed),
|
| 1033 |
-
gr.update(selected=2)
|
| 1034 |
]
|
| 1035 |
# Event Handlers
|
| 1036 |
generate_btn.click(
|
|
|
|
| 963 |
return navigate(1, current_idx, questions, answers, current_answer)
|
| 964 |
|
| 965 |
def on_submit(questions, answers, current_idx, current_answer):
|
| 966 |
+
"""Handle quiz submission with proper formatting"""
|
| 967 |
final_answers = list(answers)
|
| 968 |
if 0 <= current_idx < len(final_answers):
|
| 969 |
final_answers[current_idx] = current_answer
|
| 970 |
|
|
|
|
| 971 |
if not all(a is not None for a in final_answers[:len(questions)]):
|
|
|
|
| 972 |
unanswered = [i+1 for i, a in enumerate(final_answers[:len(questions)]) if a is None]
|
| 973 |
message = f"""
|
| 974 |
<div style="background-color: #fff3cd; padding: 20px; border-radius: 10px; border-left: 5px solid #ffa000;">
|
|
|
|
| 978 |
</div>
|
| 979 |
"""
|
| 980 |
return [
|
| 981 |
+
message,
|
| 982 |
+
gr.update(visible=True),
|
| 983 |
+
0,
|
| 984 |
+
"⚠️ Please answer all questions",
|
| 985 |
+
gr.update(visible=True),
|
| 986 |
+
gr.update(visible=False),
|
| 987 |
+
gr.update(visible=False),
|
| 988 |
+
gr.update(selected=2)
|
| 989 |
]
|
| 990 |
|
|
|
|
| 991 |
score, passed, feedback = quiz_app.calculate_score(final_answers[:len(questions)])
|
| 992 |
|
| 993 |
+
# Create formatted feedback with consistent style
|
| 994 |
+
feedback_content = ""
|
| 995 |
|
| 996 |
for i, (q, f) in enumerate(zip(questions, feedback)):
|
| 997 |
+
if i == 0:
|
| 998 |
+
feedback_content += f"What is {q.question}\n\n"
|
| 999 |
+
else:
|
| 1000 |
+
feedback_content += f"### Question {i+1}\n{q.question}\n\n"
|
| 1001 |
|
| 1002 |
+
if f.is_correct:
|
| 1003 |
+
feedback_content += f"✓ **Your answer:** {f.selected}\n\n"
|
| 1004 |
+
else:
|
| 1005 |
+
feedback_content += f"X **Your answer:** {f.selected}\n"
|
| 1006 |
+
feedback_content += f"**Correct answer:** {f.correct_answer}\n\n"
|
|
|
|
|
|
|
| 1007 |
|
| 1008 |
+
# Add result summary at the end
|
| 1009 |
if passed:
|
| 1010 |
feedback_content += f"""
|
|
|
|
| 1011 |
## 🎉 Congratulations!
|
| 1012 |
You passed with a score of {score:.1f}%!
|
| 1013 |
"""
|
| 1014 |
else:
|
| 1015 |
feedback_content += f"""
|
|
|
|
| 1016 |
## Need Improvement
|
| 1017 |
You scored {score:.1f}%. You need 80% or higher to pass.
|
| 1018 |
Please try again.
|
| 1019 |
"""
|
| 1020 |
|
| 1021 |
return [
|
| 1022 |
+
feedback_content,
|
| 1023 |
+
gr.update(visible=True),
|
| 1024 |
+
score,
|
| 1025 |
+
f"Score: {score:.1f}%",
|
| 1026 |
+
gr.update(visible=False),
|
| 1027 |
+
gr.update(visible=not passed),
|
| 1028 |
+
gr.update(visible=passed),
|
| 1029 |
+
gr.update(selected=2)
|
| 1030 |
]
|
| 1031 |
# Event Handlers
|
| 1032 |
generate_btn.click(
|