import gradio as gr import random # Quiz questions and answers questions = [ { "question": "Who was the chief engineer of the Eiffel Tower?", "options": ["Gustave Eiffel", "Maurice Koechlin", "Γmile Nouguier", "Stephen Sauvestre"], "answer": "Maurice Koechlin", "explanation": "Maurice Koechlin designed the internal structure of the tower." }, { "question": "What material provides tensile strength in concrete?", "options": ["Steel rebar", "Carbon fiber", "Bamboo", "Glass fibers"], "answer": "Steel rebar", "explanation": "Steel reinforcement bars handle tensile forces in concrete." }, { "question": "The Leaning Tower of Pisa tilts due to:", "options": ["Inadequate foundation", "Earthquakes", "Material decay", "Design choice"], "answer": "Inadequate foundation", "explanation": "It was built on unstable subsoil with insufficient foundation depth." } ] def start_quiz(name): """Initialize the quiz""" if not name.strip(): name = "Construction Expert" # Shuffle questions random.shuffle(questions) # Create question interface question_uis = [] for i, q in enumerate(questions): with gr.Row(): gr.Markdown(f"**{i+1}. {q['question']}**") question_uis.append(gr.Radio(choices=q["options"], label="Select answer", show_label=False)) # Add submit button with gr.Row(): submit_btn = gr.Button("Submit Answers", variant="primary") return { quiz_state: {"name": name, "questions": questions}, quiz_container: gr.update(visible=True), start_container: gr.update(visible=False), questions_column: gr.Column(question_uis), submit_button: submit_btn } def evaluate_answers(state): """Calculate and display results""" results = [] score = 0 for i, q in enumerate(state["questions"]): user_answer = state[f"answer_{i}"] is_correct = user_answer == q["answer"] if is_correct: score += 1 results.append(( f"{i+1}. {q['question']}", f"Your answer: {user_answer}", f"Correct answer: {q['answer']}", f"Explanation: {q['explanation']}", "β Correct" if is_correct else "β Incorrect" )) # Create results display result_html = f"""
{res[0]}
{res[1]}
{res[2]}
{res[3]}
{res[4]}