Skip to content

Automated Quiz Generator from Folder Files for Personalized Practice #982

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions llm_quiz_generator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# Automated Quiz Generator from PDF Files

This Python script automates the process of generating Multiple Choice Questions (MCQs) from the content of PDF files stored in a folder. The script extracts the text from the PDFs, generates unique questions with four answer options using a language model (LLM), and saves the quiz as a text file. This is useful for creating quizzes from educational or study material.

## Features

- **Automatic PDF Text Extraction**: Extracts text from all PDFs in a specified folder.
- **MCQ Generation**: Generates unique multiple choice questions with 4 answer options and identifies the correct answer.
- **Quiz Output**: Saves the quiz in a text format for easy review.

## Requirements

The following Python packages are required to run the script:
- PyPDF2
- langchain
- langchain_groq
- langchain_community
- langchain_huggingface
- faiss-cpu

---

## How to Use the Automated Quiz Generator Script

This guide will walk you through the steps to set up and run the Automated Quiz Generator, which converts PDF content into multiple-choice questions (MCQs). You will need to create an account with Groq to get an API key and set up your Python environment before running the script.


### Step 1: Create a Groq Account and Get an API Key (100% free)

1. **Visit Groq's Console**:
Open your web browser and go to [Groq Console](https://console.groq.com/login).

2. **Log In or Create an Account**:
You can log in with your email, GitHub account, or create a new Groq account for free.

3. **Generate an API Key**:
- After logging in, navigate to the "API Keys" section in the Groq console.
- Click the "Create API Key" button.
- Enter a name for your API key (e.g., `quiz_key`).
- **Important**: After you create the key, Groq will display it **only once**. Be sure to copy it correctly at this time.

4. **Save the API Key**:
You will need this key to run the quiz generator script.

---

### **Step 2: Add Your API Key to the Script**

You have two options to use your API key. We recommend using the method 1 since, storing API keys directly in your code is risky because it exposes sensitive information, especially if you share or push your code to platforms like GitHub. Using a `.env` file is a safer approach because it keeps your keys private and separate from the code. It also prevents accidental exposure by ensuring the keys aren't included in version control systems like Git. This method enhances security and protects your application from unauthorized access.



#### **Option 1: Store the API Key in a `.env` File**

1. Create a new file in the same directory as your script and name it `.env`.
2. Open the `.env` file in a text editor.
3. Add the following line to the `.env` file, replacing your_groq_api_key_here with your actual API key:
```
GROQ_API_KEY=your_groq_api_key_here
```
4. Save the `.env` file

#### **Option 2: Paste the API Key Directly into the Script**

1. Open the `main.py` file in your code editor.
2. Find the following line in the script (around line 38):
```python
API_KEY = os.environ["GROQ_API_KEY"]
```
3. Replace the above line with your API key directly, like this:
```python
API_KEY = "your_groq_api_key_here"
```

---

### Step 3: Prepare the PDF files
1. Create a folder (if not present) called `Source` in the same directory as your Python script.
2. Place all the PDF files that you want to generate quizzes from inside the `Source` folder.

---

### Step 4: Install the Dependencies
1. Install all the required modules using this command:
```
pip install -r requirements.txt
```


---

### Step 5: Run the Script
1. To generate the quiz, open a terminal or command prompt in the folder where the script is located and run the following command:
```
python main.py
```
This will extract the text from the PDFs, generate multiple-choice questions (MCQs) using the language model, and save the output the folder named `Source`.

---

## Rate Limits and Troubleshooting
Please note that the free tier of Groq API has rate limits, which may cause errors if too many requests are made in a short period of time. If you encounter a rate limit error, try reducing the number of PDFs in the 'Source' folder or lower the number of questions being generated. This should help avoid hitting the rate limits. For more information on the exact rate limits, please refer to the [Groq API documentation](https://console.groq.com/settings/limits).
123 changes: 123 additions & 0 deletions llm_quiz_generator/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
"""
___________________________________________________________________________________________________________________________________________________
| |
| To use this script, please check the README.md file in the directory. A quick start to get the project running is described here. |
| |
| 1. Create a Groq account and get your API key at https://console.groq.com/login. |
| |
| 2. Either: |
| - Add your API key directly to line 38: API_KEY = "your_groq_api_key_here", or |
| - Create a .env file in the same directory, and add GROQ_API_KEY=your_groq_api_key_here. |
| |
| 3. Place all your PDFs in a folder named 'Source' in the same directory as this script. |
| |
| 4. Run the script: |
| python quiz_generator.py |
| |
| The generated MCQ quiz will be saved in a file called 'generated_mcq_quiz.txt'. |
|_________________________________________________________________________________________________________________________________________________|
"""


# Change this if you want to set the number of MCQ's
num_questions = 5


import os
from PyPDF2 import PdfReader
from datetime import datetime
from langchain_groq import ChatGroq
from langchain.chains import RetrievalQA
from dotenv import load_dotenv, find_dotenv
from langchain_community.vectorstores import FAISS
from langchain_huggingface import HuggingFaceEmbeddings
from langchain.text_splitter import CharacterTextSplitter


load_dotenv(find_dotenv())
API_KEY = os.environ["GROQ_API_KEY"]


def extract_text_from_pdfs():
print(f"Extracting text from PDF files in the folder: 'Source'...")
all_text = []

if not os.path.exists('Source') or not os.listdir('Source'):
print("Folder 'Source' is empty or not found!")
print("Process exiting...")
exit(0)

for file_name in os.listdir('Source'):
if file_name.endswith(".pdf"):
file_path = os.path.join('Source', file_name)
print(f"Processing file: {file_name}")
reader = PdfReader(file_path)
for page in reader.pages:
all_text.append(page.extract_text())
print("Text extraction completed.")
return " ".join(all_text)

def generate_unique_mcq(text, num_questions=5):
print(f"Splitting text into chunks and creating embeddings for LLM processing...")
text_splitter = CharacterTextSplitter(
chunk_size=1000,
chunk_overlap=0
)
docs = text_splitter.create_documents([text])

embeddings = HuggingFaceEmbeddings()
store = FAISS.from_documents(docs, embeddings)

print(f"Connecting to LLM to generate {num_questions} unique MCQs...")
llm = ChatGroq(
temperature=0.2,
model="llama-3.1-70b-versatile",
api_key=API_KEY
)

retrieval_chain = RetrievalQA.from_chain_type(
llm=llm,
chain_type="stuff",
retriever=store.as_retriever()
)

quiz = []
query = f"Generate {num_questions} unique multiple choice questions from the following text: {text} " \
f"Provide 4 answer options and also the correct answer in plaintext."

response = retrieval_chain.invoke(query)
question_and_options = response['result']
quiz.append(question_and_options)

print("MCQ generation completed.")
return quiz

def save_mcq_to_file(quiz, file_name="generated_mcq_quiz.txt"):
output_folder = "Generated_Quizes"

if not os.path.exists(output_folder):
os.makedirs(output_folder)
print(f"Folder '{output_folder}' created.")

current_time = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
file_name = f"generated_mcq_quiz_{current_time}.txt"
file_path = os.path.join(output_folder, file_name)

print(f"Saving the generated MCQs to file: '{file_path}'...")
with open(file_path, "w") as f:
for i, question in enumerate(quiz, 1):
f.write(f"Question {i}:\n{question}\n\n")

print(f"MCQ Quiz saved to {file_path}")

if __name__ == "__main__":
if not os.path.exists('Source'):
print(f"Folder 'Source' not found.")
else:
print(f"Folder 'Source' found. Starting process...")
text = extract_text_from_pdfs()
print("Text extracted from PDFs.")

mcq_quiz = generate_unique_mcq(text, num_questions=num_questions)
save_mcq_to_file(mcq_quiz)
print("Process completed successfully.")
6 changes: 6 additions & 0 deletions llm_quiz_generator/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
PyPDF2
langchain
langchain_groq
langchain_community
langchain_huggingface
faiss-cpu
Loading