import gradio as gr questions = [ {"question": "Who is the narrator in 'The Road Not Taken'?", "options": ["A young boy", "An old man", "A traveler", "None of the above"], "correct_option": 2}, {"question": "What is the setting of the poem?", "options": ["A city", "A forest", "A beach", "A mountain"], "correct_option": 1}, {"question": "What is the underlying message of the poem?", "options": ["The importance of making choices", "The beauty of nature", "The joy of youth", "The value of time"], "correct_option": 0} ] def get_first_question(): current_question = questions[0] options = "\n".join([f"{idx}. {option}" for idx, option in enumerate(current_question['options'])]) return f"Question 1: {current_question['question']}\n{options}", 0 def interactive_quiz(user_response=None, continue_quiz='yes', question_index=0): output_messages = [] current_question = questions[question_index] if user_response is not None: user_response = int(user_response) if user_response == current_question['correct_option']: output_messages.append("Correct!") else: output_messages.append(f"Incorrect. The correct answer was: {current_question['options'][current_question['correct_option']]}") if continue_quiz.lower() == 'no': output_messages.append("Thank you for participating in the quiz!") question_index = 0 else: question_index = (question_index + 1) % len(questions) next_question = questions[question_index] output_messages.append(f"\nQuestion {question_index + 1}: {next_question['question']}") for i, option in enumerate(next_question['options']): output_messages.append(f"{i}. {option}") return "\n".join(output_messages), question_index iface = gr.Interface( fn=interactive_quiz, inputs=["text", "text", "number"], outputs=["text", "number"], live=True ) iface.launch()