diff --git a/llm_quiz_generator/README.md b/llm_quiz_generator/README.md new file mode 100644 index 000000000..aeaa7681c --- /dev/null +++ b/llm_quiz_generator/README.md @@ -0,0 +1,96 @@ +# 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. It 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. + +- **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. + +## Setup Instructions + +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 option 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 in a folder named `Generated_Quizes`. + +--- + +## Output + +The generated MCQ quiz will be saved in a text file with a timestamp in the `Generated_Quizes` folder. Each question will have four options, and the correct answer will be indicated. + +## Author(s) + +[Naman Verma](https://github.com/NamanVer02/) + +## Disclaimers + +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). diff --git a/llm_quiz_generator/main.py b/llm_quiz_generator/main.py new file mode 100644 index 000000000..32a155c13 --- /dev/null +++ b/llm_quiz_generator/main.py @@ -0,0 +1,109 @@ +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"] + +# Change this if you want to set the number of MCQs +num_questions = 5 + + +def extract_text_from_pdfs(): + """Extracts text from PDF files in the 'Source' folder.""" + print("Extracting text from PDF files in the folder: 'Source'...") + all_text = [] + + if len(os.listdir('Source')) == 0: + print("Source Folder Empty!") + 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): + """Generates unique multiple choice questions from text.""" + print("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 text: {text}" + "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"): + """Saves generated MCQs to a text file.""" + 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("Folder 'Source' not found.") + else: + print("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.") diff --git a/llm_quiz_generator/requirements.txt b/llm_quiz_generator/requirements.txt new file mode 100644 index 000000000..087e22335 --- /dev/null +++ b/llm_quiz_generator/requirements.txt @@ -0,0 +1,6 @@ +PyPDF2==3.0.1 +langchain==0.3.3 +langchain_groq==0.2.0 +langchain_community==0.3.2 +langchain_huggingface==0.1.0 +faiss-cpu==1.9.0 \ No newline at end of file