Skip to content

Instantly share code, notes, and snippets.

@debakarr
Last active March 5, 2023 15:42
Show Gist options
  • Save debakarr/2c7e4d729fb722f7f7f0cd76e6e87d8a to your computer and use it in GitHub Desktop.
Save debakarr/2c7e4d729fb722f7f7f0cd76e6e87d8a to your computer and use it in GitHub Desktop.
Unlimited Quiz Generator

Simple streamlit application to generate unlimited quiz on any topic.

Installation

pip install streamlit openai

To run

streamlit run main.py
import json
from typing import Dict
import openai
chat_history = [
{
"role": "system",
"content": "You are a REST API server with an endpoint /generate-random-question/:topic, which generates unique random quiz question in json data.",
},
{"role": "user", "content": "GET /generate-random-question/devops"},
{
"role": "assistant",
"content": '\n\n{\n "question": "What is the difference between Docker and Kubernetes?",\n "options": ["Docker is a containerization platform whereas Kubernetes is a container orchestration platform", " Kubernetes is a containerization platform whereas Docker is a container orchestration platform", "Both are containerization platforms", "Neither are containerization platforms"],\n "answer": "Docker is a containerization platform whereas Kubernetes is a container orchestration platform",\n "explanation": "Docker helps you create, deploy, and run applications within containers, while Kubernetes helps you manage collections of containers, automating their deployment, scaling, and more."\n}',
},
{"role": "user", "content": "GET /generate-random-question/jenkins"},
{
"role": "assistant",
"content": '\n\n{\n "question": "What is Jenkins?",\n "options": ["A continuous integration server", "A database management system", "A programming language", "An operating system"],\n "answer": "A continuous integration server",\n "explanation": "Jenkins is an open source automation server that helps to automate parts of the software development process such as building, testing, and deploying code."\n}',
},
]
def get_quiz_from_topic(topic: str, api_key: str) -> Dict[str, str]:
global chat_history
openai.api_key = api_key
current_chat = chat_history[:]
current_user_message = {"role": "user", "content": f"GET /generate-random-question/{topic}"}
current_chat.append(current_user_message)
chat_history.append(current_user_message)
response = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=current_chat)
quiz = response["choices"][0]["message"]["content"]
current_assistent_message = {"role": "assistant", "content": quiz}
chat_history.append(current_assistent_message)
return json.loads(quiz)
import os
import streamlit as st
from get_quiz import get_quiz_from_topic
# Input box to enter the topic of the quiz
topic = st.sidebar.text_input(
"To change topic just enter in below. From next new quiz question the topic entered here will be used.",
value="devops",
)
api_key = st.sidebar.text_input("OpenAI API key", type="password").strip()
# Initialize session state variables if they don't exist yet
if "current_question" not in st.session_state:
st.session_state.answers = {}
st.session_state.current_question = 0
st.session_state.questions = []
st.session_state.right_answers = 0
st.session_state.wrong_answers = 0
# Define a function to display the current question and options
def display_question():
# Handle first case
if len(st.session_state.questions) == 0:
st.session_state.questions.append(get_quiz_from_topic(topic, api_key))
# Disable the submit button if the user has already answered this question
submit_button_disabled = st.session_state.current_question in st.session_state.answers
# Get the current question from the questions list
question = st.session_state.questions[st.session_state.current_question]
# Display the question prompt
st.write(f"{st.session_state.current_question + 1}. {question['question']}")
# Use an empty placeholder to display the radio button options
options = st.empty()
# Display the radio button options and wait for the user to select an answer
user_answer = options.radio("Your answer:", question["options"], key=st.session_state.current_question)
# Display the submit button and disable it if necessary
submit_button = st.button("Submit", disabled=submit_button_disabled)
# If the user has already answered this question, display their previous answer
if st.session_state.current_question in st.session_state.answers:
index = st.session_state.answers[st.session_state.current_question]
options.radio("Your answer:", question["options"], key=float(st.session_state.current_question), index=index)
# If the user clicks the submit button, check their answer and show the explanation
if submit_button:
# Record the user's answer in the session state
st.session_state.answers[st.session_state.current_question] = question["options"].index(user_answer)
# Check if the user's answer is correct and update the score
if user_answer == question["answer"]:
st.write("Correct!")
st.session_state.right_answers += 1
else:
st.write(f"Sorry, the correct answer was {question['answer']}.")
st.session_state.wrong_answers += 1
# Show an expander with the explanation of the correct answer
with st.expander("Explanation"):
st.write(question["explanation"])
# Display the current score
st.write(f"Right answers: {st.session_state.right_answers}")
st.write(f"Wrong answers: {st.session_state.wrong_answers}")
# Define a function to go to the next question
def next_question():
# Move to the next question in the questions list
st.session_state.current_question += 1
# If we've reached the end of the questions list, get a new question
if st.session_state.current_question > len(st.session_state.questions) - 1:
st.session_state.questions.append(get_quiz_from_topic(topic, api_key))
# Define a function to go to the previous question
def prev_question():
# Move to the previous question in the questions list
if st.session_state.current_question > 0:
st.session_state.current_question -= 1
st.session_state.explanation = None
# Create a 3-column layout for the Prev/Next buttons and the question display
col1, col2, col3 = st.columns([1, 6, 1])
# Add a Prev button to the left column that goes to the previous question
with col1:
if col1.button("Prev"):
prev_question()
# Add a Next button to the right column that goes to the next question
with col3:
if col3.button("Next"):
next_question()
# Display the actual quiz question
with col2:
display_question()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment