Challenge: Creating a Quiz Bot

Test your skills and learn how to create a quiz bot with generative AI and Gradio.

Buckle up; this is going to be a challenging lesson! You will be tasked to create a neat, generative AI-powered quiz bot that can generate a quiz for us on the fly on a topic of our choosing. This should be similar to the demo we showcased in the first lesson.

Don’t worry; we will still give you hints and solutions.

The game plan

As with any challenge, it will be best to break it down into smaller tasks. Let’s create a list:

  1. We need to be able to take a subject as input from the user and generate a set of multiple-choice questions.

    1. The MCQs should have four answers.

    2. The model must generate them in a format that is readable by the code

    3. The model must also indicate the correct answer

  2. Once we have the MCQs in the desired format, we need to be able to render the question text and the answers as four buttons on the screen.

    1. The questions and answers should be stored appropriately

    2. We would want the buttons to be able to change color based on whether the chosen answer was correct or not.

    3. Creating buttons as a part of a class will help us modify it later

  3. With the basics complete, we would want to be able to keep a score on the quiz.

    1. There must be a next button to progress through the quiz.

    2. The score should be shown on the screen once the quiz ends.

If you were wondering, let’s tell you why this differs from just using prompts with a chatbot to have it take your quiz. Without the Gradio interface, we would be left with just a plain chat interface with the user having to type in their answers each time. Our Gradio interface saves the user from chat fatigue and provides a simple quiz interface with buttons. Not only is this visually appealing, but it also makes the quiz more enjoyable and interactive.

With our plan laid out, let’s get to work. Based on what we have learned, we will provide some skeleton code. We encourage you to fill in the code. The solutions will be available in the solutions.py file.

Generating questions

We will be using Llama with Groq to generate our questions. Try to write the code in the widget below.

Gradio automatically monitors the current directory, where the main.py file resides, for changes. If it detects a modification, it will automatically reload the application, saving us time by not needing to re-run the command. However, keep in mind that the command might exit with an error message if there are errors in the main.py file.

The provided code examples in this lesson might intentionally contain incomplete code to challenge you. If you encounter errors and the command exits, simply fix the code in main.py and run gradio main.py again in the terminal. Alternatively, you can use the gradio solution.py command to directly run the provided solutions.

Get hands-on with 1400+ tech skills courses.