Skip to content

Instantly share code, notes, and snippets.

@sttuartt
Last active September 25, 2023 07:18
Show Gist options
  • Save sttuartt/5eef3e513dad0f23afdfbe2868a450fa to your computer and use it in GitHub Desktop.
Save sttuartt/5eef3e513dad0f23afdfbe2868a450fa to your computer and use it in GitHub Desktop.
Chatgpt prompt for online quiz content creation

Generate online quiz

Overview

Using transcript/subtitles from online course, use Chatgpt to create a multiple choice quiz and output in json format. Save this output in separate file/s. Then use a second script to process these json file/s in order to generate html file/s that present the quiz nicely, and hide the correct answer for each question. Lastly, open all html files in a new browser and simply navigate from tab to tab to test yourself.

I used this to generate a quiz for all content from the A Cloud Guru course for DevOps Engineer - use this tool to download all files (including .srt files, which is what is relevant here). I set the quality to 480 as I didn't care about the videos, just the transcripts.

Notes

This was developed on, and for use with a mac, so some adjustment may be necessary for use on other operating systems.

Assumptions

You do not have a paid Chatgpt account, so cannot use the API - must use the web client like me..

Caveats

This still requires a reasonable number of manual steps - i.e. copying/pasting etc. - depends on how many transcript files there are.

Steps

  1. Obtain transcript/s of course content and save in file/s
  2. Run the prompt script below to generate a prompt for Chatgpt for each transcript file
  3. Copy each prompt, and paste into Chatgpt
  4. Wait for Chatgpt to generate the quiz, then copy the output and paste into separate files on local machine - save as .json
  5. Once all of the json quiz files have been created, create the build_html template (below), and run the build_html script (below) for each file

Specific commands used

note: gls -v allows for numeric sorting - can be installed on mac with brew install coreutils

For each (transcript) file in current directory, copy file contents to clipboard and paste into Chatgpt. Once content is generated move on to next step. When that is complete come back here, hit enter to copy contents of next file into clipboard, then off to Chatgpt again - repeat until loop complete:

for file in $(gls -v); do echo $file && cat $file|pbcopy && read; done

Once json is copied from Chatgpt, paste into new file:

pbpaste > page1.json

Once all json files complete, build html (assumes template has been created):

for file in $(gls -v *json); do echo $file && ./build_html.py $file; done

Open all html files in new window (manually open new windown in Chrome, then immeidately run this):

for file in $(gls -v *html); do open -a "google chrome" $file; done

Prompt script

#!/bin/bash

# Check if a file path is provided as an argument
if [ $# -eq 0 ]; then
  echo "Usage: $0 <input_file>"
  exit 1
fi

input_file="$1"

# Check if the input file exists
if [ ! -f "$input_file" ]; then
  echo "Error: Input file '$input_file' not found."
  exit 1
fi

# Read the input file and store its contents
file_contents=$(<"$input_file")

# Print the prompt with the file contents
echo "Please create a multiple choice quiz, with an appropriate number of questions, based on the following input:"
echo ""
echo "$file_contents"
echo ""
echo "Please format the quiz output in json in the following format:"
echo "["
echo "  {"
echo "    \"Question 1\": \"the question...\","
echo "    \"Answers\": ["
echo "      {\"a\": \"answer\"},"
echo "      {\"b\": \"answer\"},"
echo "      {\"c\": \"answer\"},"
echo "      {\"d\": \"answer\"}"
echo "    ],"
echo "    \"Correct Answer\": {\"<letter>\": \"correct answer\"}"
echo "  },"
echo "  {"
echo "    \"Question 2\": \"the question...\","
echo "    \"Answers\": ["
echo "      {\"a\": \"answer\"},"
echo "      {\"b\": \"answer\"},"
echo "      {\"c\": \"answer\"},"
echo "      {\"d\": \"answer\"}"
echo "    ],"
echo "    \"Correct Answer\": {\"<letter>\": \"correct answer\"}"
echo "  }"
echo "]"

exit 0

build_html template

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Quiz</title>
    <style>
        .card {
            border: 1px solid #ccc;
            margin: 10px auto;
            padding: 10px;
            background-color: #f9f9f9;
            width: 800px;
            border-radius: 5px;
        }

        .question {
            font-weight: bold;
            margin-bottom: 10px;
        }

        .answer {
            color: #333; /* Change the color as per your choice */
            line-height: 30px !important;
        }

        .correct-answer {
            color: #f9f9f9;
            line-height: 30px !important;
            border-radius: 2px;
            transition: color 0.3s ease-in-out, background-color 0.3s ease-in-out;
        }

        .correct-answer:hover {
            color: #4CAF50; /* Green color on hover */
        }
    </style>
</head>
<body>
    <div class="card">
        <div class="question">Question {{ question_number }}: {{ question_text }}</div>
        <div>
            {% for answer in answers %}
                {{ answer }}<br>
            {% endfor %}
        </div>
        <div><span class="correct-answer">Correct Answer: {{ correct_answer }}</span></div>
    </div>
</body>
</html>

build_html script

#!/usr/bin/env python

import json
from jinja2 import Environment, FileSystemLoader
import sys
import os

# Check if the correct number of command-line arguments is provided
if len(sys.argv) != 2:
    print("Usage: python script.py input.json")
    sys.exit(1)

input_file = sys.argv[1]

# Check if the input file exists
if not os.path.isfile(input_file):
    print(f"Input file '{input_file}' does not exist.")
    sys.exit(1)

# Load the JSON data
with open(input_file, 'r') as json_file:
    data = json.load(json_file)

# Define Jinja2 template environment
env = Environment(loader=FileSystemLoader('./'))
template = env.get_template('template.html')

# Define a function to format the answers
def format_answers(answers):
    formatted_answers = []
    for answer in answers:
        for key, value in answer.items():
            formatted_answers.append(f'<span class="answer">{key}: {value}</span>')
    return formatted_answers

# Generate HTML
output_html = ""
for i, question in enumerate(data):
    question_key = next(key for key in question.keys() if key.startswith("Question"))
    question_text = question[question_key]
    answers = question["Answers"]
    correct_answer = list(question["Correct Answer"].values())[0]

    formatted_answers = format_answers(answers)

    output_html += template.render(
        question_number=i + 1,
        question_text=question_text,
        answers=formatted_answers,
        correct_answer=correct_answer
    )

# Define the output file name with .html extension
output_file = os.path.splitext(input_file)[0] + ".html"

# Write the HTML to the output file
with open(output_file, 'w') as output_file:
    output_file.write(output_html)

print(f"HTML file '{output_file}' has been created.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment