Spaces:
Sleeping
Sleeping
fatimataba21 commited on
Commit ·
774e27c
1
Parent(s): 87b5c78
message de commit
Browse files
app.py
CHANGED
|
@@ -558,116 +558,101 @@ questions = [
|
|
| 558 |
}
|
| 559 |
]
|
| 560 |
|
| 561 |
-
|
| 562 |
-
|
| 563 |
-
|
| 564 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 565 |
|
| 566 |
-
|
| 567 |
-
|
| 568 |
-
|
| 569 |
|
| 570 |
-
|
| 571 |
-
|
| 572 |
-
|
| 573 |
-
|
| 574 |
-
|
| 575 |
-
|
| 576 |
-
categories[cat]['total'] += 1
|
| 577 |
-
if r['correct']:
|
| 578 |
-
categories[cat]['correct'] += 1
|
| 579 |
|
| 580 |
-
#
|
| 581 |
-
|
| 582 |
-
stats += f"- **Questions répondues:** {total}\n"
|
| 583 |
-
stats += f"- **Questions correctes:** {correct} ({percentage:.1f}%)\n\n"
|
| 584 |
-
stats += "## Par catégorie\n\n"
|
| 585 |
-
for cat, data in categories.items():
|
| 586 |
-
cat_percentage = (data['correct'] / data['total']) * 100 if data['total'] > 0 else 0
|
| 587 |
-
stats += f"- **{cat}:** {data['correct']}/{data['total']} ({cat_percentage:.1f}%)\n"
|
| 588 |
|
| 589 |
-
return
|
| 590 |
|
| 591 |
-
#
|
| 592 |
-
|
| 593 |
-
|
| 594 |
-
|
| 595 |
-
|
| 596 |
-
|
| 597 |
-
self.mode = "all" # "all", "category" ou "wrong"
|
| 598 |
-
self.selected_category = None
|
| 599 |
-
|
| 600 |
-
def reset_with_questions(self, new_questions, mode="all", category=None):
|
| 601 |
-
self.current_questions = new_questions.copy()
|
| 602 |
-
random.shuffle(self.current_questions)
|
| 603 |
-
self.current_index = 0
|
| 604 |
-
self.mode = mode
|
| 605 |
-
self.selected_category = category
|
| 606 |
-
|
| 607 |
-
def get_current_question(self):
|
| 608 |
-
if not self.current_questions or self.current_index >= len(self.current_questions):
|
| 609 |
-
return None
|
| 610 |
-
return self.current_questions[self.current_index]
|
| 611 |
|
| 612 |
-
|
| 613 |
-
|
| 614 |
-
|
| 615 |
-
|
| 616 |
-
return None
|
| 617 |
|
| 618 |
-
|
| 619 |
-
|
| 620 |
-
|
| 621 |
-
return self.current_questions[self.current_index]
|
| 622 |
-
return None
|
| 623 |
|
| 624 |
-
|
| 625 |
-
result = {
|
| 626 |
-
'question': question['question'],
|
| 627 |
-
'selected_option': selected_option,
|
| 628 |
-
'correct': is_correct,
|
| 629 |
-
'category': question['category']
|
| 630 |
-
}
|
| 631 |
-
self.results.append(result)
|
| 632 |
-
return result
|
| 633 |
|
| 634 |
-
|
| 635 |
-
|
| 636 |
-
|
| 637 |
-
|
| 638 |
|
| 639 |
-
|
| 640 |
-
|
| 641 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 642 |
|
| 643 |
-
|
| 644 |
-
|
| 645 |
-
q_box = gr.Markdown(init_display)
|
| 646 |
-
answer_radio = gr.Radio(choices=curr["options"] if curr else [], label="Votre réponse")
|
| 647 |
-
message = gr.Markdown("")
|
| 648 |
-
|
| 649 |
-
def submit_answer(selected_option):
|
| 650 |
-
curr_q = app_state.get_current_question()
|
| 651 |
-
# If no current question, end quiz
|
| 652 |
-
if curr_q is None:
|
| 653 |
-
return q_box.update(value="Quiz terminé."), gr.Radio.update(choices=[], value=None, interactive=False), message.update(value="")
|
| 654 |
-
# Check answer and set result message
|
| 655 |
-
is_correct = (selected_option == curr_q["correct"])
|
| 656 |
-
result_msg = "Bonne réponse !" if is_correct else f"Mauvaise réponse. La réponse correcte était {curr_q['correct']}."
|
| 657 |
-
app_state.add_result(curr_q, selected_option, is_correct)
|
| 658 |
-
nxt = app_state.next_question()
|
| 659 |
-
if nxt:
|
| 660 |
-
return (q_box.update(value=nxt["question"]),
|
| 661 |
-
gr.Radio.update(choices=nxt["options"], value=None),
|
| 662 |
-
message.update(value=result_msg))
|
| 663 |
-
else:
|
| 664 |
-
stats = calculate_stats(app_state.results)
|
| 665 |
-
final_msg = "Quiz terminé.\n\n" + stats + "\n\n" + result_msg
|
| 666 |
-
return (q_box.update(value=final_msg),
|
| 667 |
-
gr.Radio.update(choices=[], value=None, interactive=False),
|
| 668 |
-
message.update(value=""))
|
| 669 |
-
|
| 670 |
-
submit_btn = gr.Button("Submit")
|
| 671 |
-
submit_btn.click(fn=submit_answer, inputs=answer_radio, outputs=[q_box, answer_radio, message])
|
| 672 |
|
| 673 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 558 |
}
|
| 559 |
]
|
| 560 |
|
| 561 |
+
|
| 562 |
+
|
| 563 |
+
# Add correct_answers variable
|
| 564 |
+
correct_answers = [q["correct"] for q in questions]
|
| 565 |
+
|
| 566 |
+
def calculate_score(user_answers):
|
| 567 |
+
# Convertir les réponses utilisateur de A,B,C,D à 0,1,2,3
|
| 568 |
+
idx_answers = []
|
| 569 |
+
for ans in user_answers:
|
| 570 |
+
if ans == "A":
|
| 571 |
+
idx_answers.append(0)
|
| 572 |
+
elif ans == "B":
|
| 573 |
+
idx_answers.append(1)
|
| 574 |
+
elif ans == "C":
|
| 575 |
+
idx_answers.append(2)
|
| 576 |
+
elif ans == "D":
|
| 577 |
+
idx_answers.append(3)
|
| 578 |
+
else:
|
| 579 |
+
idx_answers.append(None) # Pour gérer les réponses vides
|
| 580 |
+
|
| 581 |
+
# Convertir les réponses correctes
|
| 582 |
+
correct_idx = []
|
| 583 |
+
for ans in correct_answers:
|
| 584 |
+
if ans == "A":
|
| 585 |
+
correct_idx.append(0)
|
| 586 |
+
elif ans == "B":
|
| 587 |
+
correct_idx.append(1)
|
| 588 |
+
elif ans == "C":
|
| 589 |
+
correct_idx.append(2)
|
| 590 |
+
elif ans == "D":
|
| 591 |
+
correct_idx.append(3)
|
| 592 |
|
| 593 |
+
# Calculer le score
|
| 594 |
+
score = 0
|
| 595 |
+
user_responses = []
|
| 596 |
|
| 597 |
+
for i in range(len(idx_answers)):
|
| 598 |
+
if idx_answers[i] is not None and idx_answers[i] == correct_idx[i]:
|
| 599 |
+
score += 1
|
| 600 |
+
user_responses.append(f"Question {i+1}: Correct ✓ - Votre réponse: {user_answers[i]}")
|
| 601 |
+
else:
|
| 602 |
+
user_responses.append(f"Question {i+1}: Incorrect ✗ - Votre réponse: {user_answers[i] if idx_answers[i] is not None else 'Non répondue'}, Réponse correcte: {correct_answers[i]}")
|
|
|
|
|
|
|
|
|
|
| 603 |
|
| 604 |
+
# Calculer la note sur 20
|
| 605 |
+
note_sur_20 = (score / len(correct_answers)) * 20
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 606 |
|
| 607 |
+
return score, note_sur_20, user_responses
|
| 608 |
|
| 609 |
+
# Modified qcm_app to handle multiple arguments from Gradio click event
|
| 610 |
+
def qcm_app(*user_responses):
|
| 611 |
+
# Convert tuple of responses to list
|
| 612 |
+
answers = list(user_responses)
|
| 613 |
+
# Calculer le score
|
| 614 |
+
score, note, detailed_responses = calculate_score(answers)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 615 |
|
| 616 |
+
# Formatter la sortie
|
| 617 |
+
output = f"# Résultats du QCM sur la Gouvernance des Données\n\n"
|
| 618 |
+
output += f"## Score: {score}/{len(questions)} questions correctes\n"
|
| 619 |
+
output += f"## Note: {note:.2f}/20\n\n"
|
|
|
|
| 620 |
|
| 621 |
+
output += "## Détail des réponses:\n\n"
|
| 622 |
+
for resp in detailed_responses:
|
| 623 |
+
output += f"- {resp}\n"
|
|
|
|
|
|
|
| 624 |
|
| 625 |
+
return output
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 626 |
|
| 627 |
+
# Créer l'interface Gradio
|
| 628 |
+
with gr.Blocks(title="QCM - Gouvernance des Données") as demo:
|
| 629 |
+
gr.Markdown("# Questionnaire à Choix Multiples - Gouvernance des Données")
|
| 630 |
+
gr.Markdown("Répondez aux questions en sélectionnant la réponse correcte (A, B, C ou D).")
|
| 631 |
|
| 632 |
+
user_choices = []
|
| 633 |
+
with gr.Accordion("Questions", open=True):
|
| 634 |
+
for i in range(len(questions)):
|
| 635 |
+
with gr.Row():
|
| 636 |
+
with gr.Column(scale=10):
|
| 637 |
+
gr.Markdown(f"### {questions[i]['question']}")
|
| 638 |
+
for opt in questions[i]['options']:
|
| 639 |
+
gr.Markdown(f"- {opt}")
|
| 640 |
+
with gr.Column(scale=1):
|
| 641 |
+
# Changed from Dropdown to Radio for clickable answers
|
| 642 |
+
user_choice = gr.Radio(
|
| 643 |
+
choices=["A", "B", "C", "D"],
|
| 644 |
+
label=f"Question {i+1}"
|
| 645 |
+
)
|
| 646 |
+
user_choices.append(user_choice)
|
| 647 |
|
| 648 |
+
submit_btn = gr.Button("Soumettre")
|
| 649 |
+
output = gr.Markdown()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 650 |
|
| 651 |
+
submit_btn.click(
|
| 652 |
+
fn=qcm_app,
|
| 653 |
+
inputs=user_choices,
|
| 654 |
+
outputs=output
|
| 655 |
+
)
|
| 656 |
+
|
| 657 |
+
if __name__ == "__main__":
|
| 658 |
+
demo.launch()
|