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"""

πŸ† Quiz Results

Score: {score}/{len(questions)}

Participant: {state['name']}

""" for res in results: result_html += f"""

{res[0]}

{res[1]}

{res[2]}

{res[3]}

{res[4]}

""" result_html += """
""" return { results_container: gr.update(value=result_html, visible=True), quiz_container: gr.update(visible=False) } with gr.Blocks(title="Building Construction Quiz", theme=gr.themes.Soft()) as app: # State management quiz_state = gr.State({}) # Start screen with gr.Column(visible=True, elem_id="start-container") as start_container: gr.Markdown("# πŸ—οΈ Building Construction Quiz") gr.Markdown("Test your knowledge of construction techniques and architectural history") name_input = gr.Textbox(label="Enter your name", placeholder="John Doe", value="Construction Pro") start_btn = gr.Button("Start Quiz", variant="primary") # Quiz screen with gr.Column(visible=False) as quiz_container: questions_column = gr.Column() submit_button = gr.Button() # Results screen results_container = gr.HTML(visible=False) # Event handling start_btn.click( start_quiz, inputs=[name_input], outputs=[quiz_state, quiz_container, start_container, questions_column, submit_button] ) submit_button.click( lambda *answers, state: {**state, **{f"answer_{i}": a for i, a in enumerate(answers)}}, inputs=[questions_column, quiz_state], outputs=[quiz_state] ).then( evaluate_answers, inputs=[quiz_state], outputs=[results_container, quiz_container] ) if __name__ == "__main__": app.launch()