| import gradio as gr |
| import json |
| import random |
|
|
| |
| with open('questions.json', encoding='utf-8') as f: |
| questions = json.load(f) |
|
|
| |
| scores = {i: 0 for i in range(len(questions))} |
|
|
| |
| def choisir_question(): |
| sorted_q = sorted(scores.items(), key=lambda x: x[1]) |
| idx = sorted_q[0][0] |
| return questions[idx], idx |
|
|
| |
| def afficher_question(): |
| q, idx = choisir_question() |
| options = q["autres_reponses"] + [q["bonne_reponse"]] |
| random.shuffle(options) |
| return q["question"], options, idx |
|
|
| |
| def verifier_reponse(reponse, idx): |
| correcte = questions[idx]["bonne_reponse"] |
| if reponse == correcte: |
| scores[idx] += 1 |
| return f"✅ Correct ! Bravo 🎉" |
| else: |
| scores[idx] = max(scores[idx] - 1, 0) |
| return f"❌ Oups ! La réponse était : {correcte}" |
|
|
| |
| with gr.Blocks(theme=gr.themes.Soft(primary_hue="purple")) as app: |
| gr.Markdown("# ✨ Quiz Apprentissage ✨") |
| |
| question = gr.Markdown() |
| choix = gr.Radio(label="Choisis ta réponse :") |
| resultat = gr.Markdown() |
| idx = gr.State() |
|
|
| def nouvelle_question(): |
| q, options, q_idx = afficher_question() |
| return q, gr.Radio(choices=options, value=None, label="Choisis ta réponse :"), q_idx, "" |
|
|
| btn_valider = gr.Button("Valider ✅") |
| btn_suivant = gr.Button("Question suivante 🔄") |
|
|
| btn_valider.click(verifier_reponse, inputs=[choix, idx], outputs=resultat) |
| btn_suivant.click(nouvelle_question, outputs=[question, choix, idx, resultat]) |
|
|
| app.load(nouvelle_question, outputs=[question, choix, idx, resultat]) |
|
|
| |
| app.launch() |
|
|
| |
| questions_json = [ |
| {"question": "Quelle est la capitale de la France ?", "bonne_reponse": "Paris", "autres_reponses": ["Lyon", "Marseille", "Nice"]}, |
| {"question": "Combien y a-t-il de continents sur Terre ?", "bonne_reponse": "7", "autres_reponses": ["5", "6", "8"]}, |
| {"question": "Quel gaz respirons-nous principalement ?", "bonne_reponse": "Azote", "autres_reponses": ["Oxygène", "Gaz carbonique", "Argon"]} |
| ] |
|
|
| with open('questions.json', 'w', encoding='utf-8') as f: |
| json.dump(questions_json, f, ensure_ascii=False, indent=4) |
|
|