import gradio as gr import random # Sample content for flashcards and quizzes flashcards = [ {"question": "What is the capital of France?", "answer": "Paris"}, {"question": "What is the largest planet in our solar system?", "answer": "Jupiter"}, {"question": "Who wrote '1984'?", "answer": "George Orwell"} ] quizzes = [ {"question": "What is 5 + 5?", "choices": ["5", "10", "15"], "answer": "10"}, {"question": "What is the square root of 16?", "choices": ["2", "4", "8"], "answer": "4"}, {"question": "Which country is known as the land of the rising sun?", "choices": ["China", "Japan", "India"], "answer": "Japan"} ] # Function to display a random flashcard def get_flashcard(): flashcard = random.choice(flashcards) return flashcard['question'], flashcard['answer'] # Function to generate a random quiz question def get_quiz(): quiz = random.choice(quizzes) return quiz['question'], quiz['choices'], quiz['answer'] # Function to check the answer for a quiz def check_quiz_answer(selected_answer, correct_answer): if selected_answer == correct_answer: return "Correct!" else: return f"Incorrect! The right answer is: {correct_answer}" # Gradio Interface using gr.Interface for Gradio 5.20.1 def create_gradio_interface(): flashcard_question, flashcard_answer = get_flashcard() quiz_question, quiz_choices, correct_answer = get_quiz() def quiz_interface(selected_answer): return check_quiz_answer(selected_answer, correct_answer) with gr.Blocks() as demo: with gr.Tab("Flashcards"): gr.Markdown("## Flashcards Section") gr.Textbox(label="Flashcard Question", value=flashcard_question, interactive=False) gr.Textbox(label="Answer", value=flashcard_answer, interactive=False) with gr.Tab("Quizzes"): gr.Markdown("## Quiz Section") selected_answer = gr.Radio(choices=quiz_choices, label="Choose your answer") submit_button = gr.Button("Submit Answer") result = gr.Textbox(label="Result", interactive=False) # Use the Interface for the quiz submit_button.click(quiz_interface, inputs=selected_answer, outputs=result) return demo # Launch the Gradio interface demo = create_gradio_interface() demo.launch()